-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgrammar.pegjs
195 lines (160 loc) · 4.02 KB
/
grammar.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
193
194
195
// Adapted from
// https://github.com/slightlyoff/cassowary.js/blob/master/src/parser/api.js
start
= __ statements:(Statement*) __ { return statements; }
Statement
= __ expression:LinearExpression WhiteSpace* strength:Strength* WhiteSpace* EOS {
const s = (strength.length) ? strength[0].toUpperCase() : 'STRONG';
expression.strength = s; return expression;
}
/ __ Editable WhiteSpace+ name:Identifier (WhiteSpace+ strength:Strength)? WhiteSpace* EOS {
const s = (typeof strength !== 'undefined') ? strength.toUpperCase() : 'STRONG';
return { type: "EditableVariable", name: name, strength: s };
}
SourceCharacter
= .
IdentifierStart
= [a-zA-Z.]
/ "$"
/ "_"
IdentifierPart
= IdentifierStart
/ [0-9]
WhiteSpace "whitespace"
= [\t\v\f \u00A0\uFEFF]
LineTerminator
= [\n\r\u2028\u2029]
LineTerminatorSequence "end of line"
= "\n"
/ "\r\n"
/ "\r"
/ "\u2028" // line separator
/ "\u2029" // paragraph separator
EOS
= __ ";"
/ _ LineTerminatorSequence
/ __ EOF
EOF
= !.
Comment "comment"
= MultiLineComment
/ SingleLineComment
MultiLineComment
= "/*" (!"*/" SourceCharacter)* "*/"
MultiLineCommentNoLineTerminator
= "/*" (!("*/" / LineTerminator) SourceCharacter)* "*/"
SingleLineComment
= "//" (!LineTerminator SourceCharacter)* (LineTerminator / EOF)
_
= (WhiteSpace / MultiLineCommentNoLineTerminator / SingleLineComment)*
__
= (WhiteSpace / LineTerminatorSequence / Comment)*
Literal
= val:(Real / Integer) {
return {
type: "NumericLiteral",
value: val
}
}
Integer
= digits:[0-9]+ {
return parseInt(digits.join(""));
}
Real
= digits:(Integer ".") subs:[0-9]+ {
return parseFloat(digits.join("") + subs.join(""));
}
SignedInteger
= [-+]? [0-9]+
Identifier "identifier"
= name:IdentifierName { return name; }
IdentifierName "identifier"
= start:IdentifierStart parts:IdentifierPart* {
return start + parts.join("");
}
PrimaryExpression
= name:Identifier { return { type: "Variable", name: name }; }
/ Literal
/ "(" __ expression:LinearExpression __ ")" { return expression; }
UnaryExpression
= PrimaryExpression
/ operator:UnaryOperator __ expression:UnaryExpression {
return {
type: "UnaryExpression",
operator: operator,
expression: expression
};
}
UnaryOperator
= "+"
/ "-"
/ "!"
MultiplicativeExpression
= head:UnaryExpression
tail:(__ MultiplicativeOperator __ UnaryExpression)* {
var result = head;
for (var i = 0; i < tail.length; i++) {
result = {
type: "MultiplicativeExpression",
operator: tail[i][1],
left: result,
right: tail[i][3]
};
}
return result;
}
MultiplicativeOperator
= "*" / "/"
AdditiveExpression
= head:MultiplicativeExpression
tail:(__ AdditiveOperator __ MultiplicativeExpression)* {
var result = head;
for (var i = 0; i < tail.length; i++) {
result = {
type: "AdditiveExpression",
operator: tail[i][1],
left: result,
right: tail[i][3]
};
}
return result;
}
AdditiveOperator
= "+" / "-"
InequalityExpression
= head:AdditiveExpression
tail:(__ InequalityOperator __ AdditiveExpression)* {
var result = head;
for (var i = 0; i < tail.length; i++) {
result = {
type: "Inequality",
operator: tail[i][1],
left: result,
right: tail[i][3]
};
}
return result;
}
InequalityOperator
= "<="
/ ">="
/ "<"
/ ">"
Editable
= "EDITABLE"i
Strength
= "REQUIRED"i / "STRONG"i / "MEDIUM"i / "WEAK"i
LinearExpression
= head:InequalityExpression
tail:(__ "==" __ InequalityExpression)* {
var result = head;
for (var i = 0; i < tail.length; i++) {
result = {
type: "Equality",
operator: tail[i][1],
left: result,
right: tail[i][3]
};
}
return result;
}