-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathcson.pegjs
192 lines (164 loc) · 4.65 KB
/
cson.pegjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
{
// ported from https://goo.gl/md9zUA
var HEREDOC_INDENT = /\n+([^\n\S]*)(?=\S)/g;
var LEADING_BLANK_LINE = /^[^\n\S]*\n/;
var TRAILING_BLANK_LINE = /\n[^\n\S]*$/;
function processMultiLine(doc) {
var attempt, indent = null, indentRegex, match;
while (match = HEREDOC_INDENT.exec(doc)) {
attempt = match[1];
if (indent === null ||
(attempt.length && attempt.length < indent.length))
indent = attempt;
}
if (indent)
indentRegex = new RegExp('\n' + indent, 'g');
if (indentRegex) doc = doc.replace(indentRegex, '\n');
return doc.replace(LEADING_BLANK_LINE, '')
.replace(TRAILING_BLANK_LINE, '');
}
function SubObj(kvs) { this.kvs = kvs; }
SubObj.prototype.toObject = function () {
return this.kvs.reduce(function (obj, kv) {
obj[kv.key] = (kv.value instanceof SubObj)
? kv.value.toObject() : kv.value;
return obj;
}, {});
};
function fixObjNesting(tree, error, parents) {
if (!parents) {
parents = [{
key: 'ROOT', value: tree, loc: { start: { column: 0 } }
}];
}
var kv, col;
var kvs = tree.kvs.slice(0);
for (var i = 0; i < kvs.length; i++) {
kv = kvs[i];
col = kv.loc.start.column;
if (col <= parents[0].loc.start.column) {
for (var j = 0;
parents[j] && col <= parents[j].loc.start.column;
j++) { }
if (j > parents.length) {
return error(
new Error('assertion failure: excessive exdent!?'),
kv.loc
);
}
var pkvs = parents[j].value.kvs;
for (j = 0;
j < pkvs.length
&& pkvs[j].loc.start.line < kv.loc.start.line;
j++) { }
pkvs.splice(j, 0, kv);
tree.kvs =
tree.kvs.filter(function (x) { return x !== kv; });
}
if (kv.value instanceof SubObj)
fixObjNesting(kv.value, error, [kv].concat(parents));
}
}
}
Document
= MultiLineComment? _ v:Value _ { return v }
Value
= ( NonObjValue / Object )
NonObjValue
= ( Array / Bool / Null / String / MathExpr )
Comment
= '\n' MultiLineComment
/ SingleLineComment
MultiLineComment "multi-line comment"
= [ \t\r]* '###'
( [^#] / '#' !'##' )*
'###' [ \t\r]* ( !. / &'\n' )
SingleLineComment "line comment"
= '#' !'##' [^\n]*
SubObj
= ExplicitObj
/ SubImplicitObj
Object "object"
= ExplicitObj
/ ImplicitObj
ExplicitObj "explicit object"
= '{' _ o:ImplicitObj? _ '}' { return o || {} }
ImplicitObj "implicit object"
= so:SubImplicitObj
{
fixObjNesting(so, error);
return so.toObject();
}
SubImplicitObj "nested implicit object"
= first:KeyVal
rest:(
( _ ',' _ / newLine [ \t]* )
kv:KeyVal
{ return kv }
)*
{ return new SubObj([first].concat(rest)) }
KeyVal
= k:( String / Key )
_ ':' _ v:( NonObjValue / SubObj )
{ return { key: k, value: v, loc: location() }; }
Key "key" = $ [a-zA-Z0-9_]+
Array "array"
= '[' _ a:(
first:Value
rest:(( _ ',' / newLine ) _ v:Value { return v })*
{ return [first].concat(rest) }
)? _ ']'
{ return a || [] }
Bool "bool"
= ( 'true' / 'false' / 'yes' / 'no' / 'on' / 'off' )
{ var t = text(); return (t === 'true' || t === 'yes' || t === 'on') }
Number "number"
= neg:( '-' _ )?
int:( '0' / [1-9] [0-9]* )
dec:( '.' [0-9]+ )?
exp:( 'e'i [+-]? [0-9]+ )?
{ return Number(text()) }
Null "null"
= 'null' { return null; }
String "string"
= MultiLineString
/ ( '"' c:( [^"\\] / EscapedChar )* '"' { return c.join('') } )
/ ( "'" c:( [^'\\] / EscapedChar )* "'" { return c.join('') } )
MultiLineString
= s:( "'''" ( [^'\\] / EscapedChar / $("'" !"''") )* "'''"
/ '"""' ( [^"\\] / EscapedChar / $('"' !'""') )* '"""'
) { return processMultiLine(s[1].join('')) }
EscapedChar
= '\\' c:( 'u' HexDigit HexDigit HexDigit HexDigit / . )
{
if (typeof c === 'string') {
return {
b: '\b', f: '\f', r: '\r', t: '\t', n: '\n'
}[c] || c;
}
return String.fromCharCode(
parseInt(c.slice(1).join(''), 16)
);
}
_ "whitespace"
= ( Comment / [ \t\n\r] )*
newLine "newline"
= ( ( Comment / [ \t\r] )* '\n' )+
HexDigit = [0-9a-f]i
MathExpr
= head:MathTerm tail:( _ [+-] _ MathTerm )*
{
return tail.reduce(function (res, n) {
return n[1] === '+' ? res + n[3] : res - n[3]
}, head)
}
MathTerm
= head:MathFactor tail:( _ [*/] _ MathFactor )*
{
return tail.reduce(function (res, n) {
return n[1] === '*' ? res * n[3] : res / n[3]
}, head)
}
MathFactor
= '(' _ x:MathExpr _ ')' { return x }
/ Number