-
Notifications
You must be signed in to change notification settings - Fork 0
/
StringGrammar.peg
260 lines (205 loc) · 12.8 KB
/
StringGrammar.peg
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
start = HtmlInput
HtmlInput = i:(HtmlChars AnyExpr HtmlInput) {
/** <?php $return = array();
array_walk_recursive($i, function($a) use (&$return) { $return[] = $a; });
return $return;
?> **/
return [i[0],i[1]].concat(i[2]).filter(v => v.type !== 'text' || v.body !== ''); }
/ HtmlChars
CodeInput = i:(CodeChars ComplexExpr CodeInput) {
/** <?php $return = array();
array_walk_recursive($i, function($a) use (&$return) { $return[] = $a; });
return $return;
?> **/
return [i[0],i[1]].concat(i[2]).filter(v => v.type !== 'text' || v.body !== ''); }
/ CodeChars
ControlInput = ComplexExpr / ExprBody / StringLiteral
AnyExpr = ComplexExpr / SimpleVar
HtmlChars = text: HtmlChar* { /** <?php return new \Seufert\Hamle\TextNode\StringLit(join('',$text)); ?> **/
return {type:'string', body: text.join('')}; }
CodeChars = text: CodeChar* { /** <?php return new \Seufert\Hamle\TextNode\StringLit(join('',$text)); ?> **/
return {type:'string', body: text.join('')}; }
ComplexExpr = "{" body:ExprBody "}" { /** <?php return $body; ?> **/
return {type:'expr', body};}
SimpleVar = "$" name:Identifier { /** <?php
return new \Seufert\Hamle\TextNode\ScopeId(null, null, new \Seufert\Hamle\TextNode\ModelParam($name)); ?> **/
return {type:'scopeThis', name};}
ExprBody = expr:VarExprPart chain:VarParamFilter* {
/** <?php if(!$chain) return $expr;
$top = array_pop($chain);
while($chain) { $top = array_pop($chain)->withChain($top); } return $expr->withChain($top); ?> **/
return {...expr, filter};}
/ GetVarExpr
GetVarExpr = expr:VarExprPart ExprParamArrow param:VarExprSub
/ VarExprPart
VarParamFilter = ExprParamArrow sub: VarExprSubName {
/** <?php return $sub; ?> **/
return { ...expr, param};}
/ "|" filter:filter {
/** <?php return $filter; ?> **/
}
VarExprSubName = name: Identifier {
/** <?php return new \Seufert\Hamle\TextNode\ModelParam($name, null); ?> **/
return {type:"sub", name};
}
VarExprPart = VarScopeId / VarScopeName
/ "$" "(" " "* query: QueryFilter " "* ")" { /** <?php return $query; ?> **/
return {type:"query", ...query};}
/ SimpleVar
VarScopeId = "$" "[" id:SignedInteger "]" {
/** <?php return new \Seufert\Hamle\TextNode\ScopeId($id); ?> **/
return {type:"scopeId", id: parseInt(id, 10)};
}
VarScopeName = "$" "[" name: Identifier "]" {
/** <?php return new \Seufert\Hamle\TextNode\ScopeName($name); ?> **/
return {type:"scopeName", name};
}
QueryFilter = query:QueryFilterStage chain:QueryRelation* { /** <?php return (new \Seufert\Hamle\TextNode\Query($query))->withImmStack($chain); ?> **/
return {query, sub, rel:rel === '>'?'child':'parent'}}
/ scope: (VarScopeId / VarScopeName) chain:QueryRelation* { /** <?php return $scope->withImmStack($chain); ?> **/
return {query:null, sub, rel:rel === '>'?'child':'parent'}}
/ chain:QueryRelation+ { /** <?php return (new \Seufert\Hamle\TextNode\ScopeId(null))->withImmStack($chain); ?> **/
return {query:null, sub, rel:rel === '>'?'child':'parent'}}
/ query: QueryFilterStage { /** <?php return new \Seufert\Hamle\TextNode\Query($query); ?> **/
return {query}}
QueryFilterStage = id: IdentifierDash query: QueryFilterModifier* { /** <?php return array_merge([['q'=>'type', 'id'=> $id]], $query); ?> **/
return [{q:'type', id}, ...query];}
/ "*" query: QueryFilterModifier* { /** <?php return array_merge([['q'=>'type', 'id'=> '*']], $query); ?> **/
return [{q:'type', id:'*'}, ...query];}
/ query:QueryFilterModifier+ { /** <?php return array_merge([['q'=>'type', 'id'=> '*']], $query); ?> **/
return [{q:'type', id:'*'}, ...query];}
QueryRelation = " "* rel:QueryRelationType " "+ sub:QueryRelationStage {
/** <?php return \Seufert\Hamle\TextNode\RelQuery::for($rel, $sub); ?> **/
return {query, sub, rel:rel === '>'?'child':'parent'}
}
QueryRelationType = ">" / "<" / "&" "g" "t" ";" { /** <?php return '<'; ?> **/}
/ "&" "l" "t" ";" { /** <?php return '>'; ?> **/}
QueryRelationStage = id: AlphaNum query: ChildQueryFilterModifier* { /** <?php return array_merge([['q'=>'type', 'id'=> $id]], $query); ?> **/
return [{q:'type', id}, ...query];}
/ "*" query: ChildQueryFilterModifier* { /** <?php return array_merge([['q'=>'type', 'id'=> '*']], $query); ?> **/
return [{q:'type', id:'*'}, ...query];}
/ query:ChildQueryFilterModifier+ { /** <?php return array_merge([['q'=>'type', 'id'=> '*']], $query); ?> **/
return [{q:'type', id:'*'}, ...query];}
QueryFilterModifier = "#" id:AlphaNum { /** <?php return ['q'=>'id', 'id'=> $id]; ?> **/
return {q:'id', id};}
/ ChildQueryFilterModifier
ChildQueryFilterModifier = "," id:AlphaNum { /** <?php return ['q'=>'type', 'id'=> $id]; ?> **/
return {q:'type', id};}
/ "." id:TagIdentifier { /** <?php return ['q'=>'tag', 'id'=> $id]; ?> **/
return {q:'tag', id};}
/ "^" id:IdentifierDash? { /** <?php return ['q'=>'sort', 'id'=> $id]; ?> **/
return {q:'id', id};}
/ "@" id:Integer { /** <?php return ['q'=>'group', 'id'=> $id]; ?> **/
return {q:'group', id};}
/ ":" offset:Integer "-" limit:Integer { /** <?php return ['q'=>'range', 'limit'=> $limit, 'offset' => $offset]; ?> **/
return {q:'range', limit, offset};}
/ ":" limit:Integer { /** <?php return ['q'=>'range', 'limit'=> $limit, 'offset' => 0]; ?> **/
return {q:'range', limit, offset:0};}
VarExprSub = name: Identifier ExprParamArrow param:VarExprSub {
/** <?php return new \Seufert\Hamle\TextNode\ModelParam($name, $param); ?> **/
return {type:"param", name, param};}
/ name: Identifier {
/** <?php return new \Seufert\Hamle\TextNode\ModelParam($name, null); ?> **/
return {type:"sub", name};
}
ExprParamArrow = "-" ">" / "-" "!" / "-" "&" "g" "t" ";"
filterexpr = "|" f:filter chain:filterexpr {
/** <?php return $f->withChain($chain); ?> **/
return {...f, chain};
}
/ "|" f: filter { /** <?php return $f; ?> **/ return f;}
filter = func:Identifier "(" args:arglist* ")" {
/** <?php return new \Seufert\Hamle\TextNode\FilterFunc($func, null, $args); ?> **/
return {type:'filter', func, args};
}
/ func: Identifier {
/** <?php return new \Seufert\Hamle\TextNode\FilterFunc($func); ?> **/
return {type:'filter', func, args:[]};
}
arglist = arg: argument " "* "," " "* { /** <?php return $arg; ?> **/ return arg;}
/ argument
argument = SignedFloat / SignedInteger / SimpleVar / ComplexExpr / StringLiteralExpr
SignedFloat = s:"-"? n:[0-9]+ "." d:[0-9]+ {
/** <?php return \Seufert\Hamle\TextNode\FloatLit((float)"$s$n.$d"); ?> **/
return parseFloat((s||'')+n.join("")+"."+d.join(""))
}
StringLiteralExpr = parts:('"' DoubleStringExpr? '"' / "'" SingleStringExpr? "'") {
/** <?php return $parts[1]; ?> **/
return parts[1];
}
DoubleStringExpr = s:DoubleStringExprChar* e:ComplexExpr post:DoubleStringExpr {
/** <?php
return \Seufert\Hamle\TextNode\StringConcat::fromParser($s, $e, $post);
?> **/
return [s, e].concat(post);
} / s:DoubleStringExprChar* { /** <?php return new \Seufert\Hamle\TextNode\StringLit(join('',$s)); ?> **/ return s.join; }
SingleStringExpr = s:SingleStringExprChar* e:ComplexExpr post:SingleStringExpr {
/** <?php
return \Seufert\Hamle\TextNode\StringConcat::fromParser($s, $e, $post);
?> **/
return [s, e].concat(post);
} / s:SingleStringExprChar* { /** <?php return new \Seufert\Hamle\TextNode\StringLit(join('',$s)); ?> **/ return s.join; }
StringLiteral "string"
= parts:('"' DoubleStringCharacters? '"' / "'" SingleStringCharacters? "'") {
/** <?php return new \Seufert\Hamle\TextNode\StringLit($parts[1]); ?> **/
return parts[1];
}
DoubleStringCharacters
= chars:DoubleStringCharacter+ {
/** <?php return join('', $chars); ?> **/
return chars.join("");
}
SingleStringCharacters
= chars:SingleStringCharacter+ {
/** <?php return join('', $chars); ?> **/
return chars.join("");
}
DoubleStringCharacter
= !('"' / "\\") c:SourceCharacter { /** <?php return $c; ?> **/ return c; }
/ "\\" s:CharacterEscapeSequence { /** <?php return $s; ?> **/ return s; }
DoubleStringExprChar
= !('"' / "\\" / "{") c:SourceCharacter { /** <?php return $c; ?> **/ return c; }
/ c:"{" !"$" { /** <?php return $c; ?> **/ return $c; }
/ "\\" s:CharacterEscapeSequence { /** <?php return $s; ?> **/ return s; }
SingleStringExprChar
= !("'" / "\\" / "{") c:SourceCharacter { /** <?php return $c; ?> **/ return c; }
/ c:"{" !"$" { /** <?php return $c; ?> **/ return $c; }
/ "\\" s:CharacterEscapeSequence { /** <?php return $s; ?> **/ return s; }
SingleStringCharacter
= !("'" / "\\") char:SourceCharacter { /** <?php return $char; ?> **/ return char; }
/ "\\" sequence:CharacterEscapeSequence { /** <?php return $sequence; ?> **/ return sequence; }
SourceCharacter = .
CharacterEscapeSequence = SingleEscapeCharacter / NonEscapeCharacter
EscapeCharacter = SingleEscapeCharacter
Integer = n:[0-9]+ { /** <?php return (int)join('', $n); ?> **/ return parseInt(n.join(''), 10);}
SignedInteger = sign:"-"? n:[0-9]+ {
/** <?php return new \Seufert\Hamle\TextNode\IntLit((int)($sign.join('', $n))); ?> **/
return parseInt((sign || '')+n.join(''), 10);
}
NonEscapeCharacter
= (!EscapeCharacter / LineTerminator) char:SourceCharacter { /** <?php return $char; ?> **/ return char; }
Identifier = p:[a-zA-Z_] ex:[a-zA-Z0-9_]* {
/** <?php return $p.join('',$ex); ?> **/
return p + ex.join('')}
IdentifierDash = p:[a-zA-Z_-] s:[0-9a-zA-Z_-]+ { /** <?php return $p.join('', $s); ?> **/ return [p].concat(s).join('')}
AlphaNum = s:[0-9a-zA-Z_-]+ { /** <?php return join('', $s); ?> **/ return s.join('')}
TagIdentifier = name:[0-9a-zA-Z_-]+ { /** <?php return join('',$name); ?> **/ return name.join('');}
// Character in Html Mode
HtmlChar = "\\" c: "$" { /** <?php return $c; ?> **/ return c;}
/ c:"{" !"$" { /** <?php return $c; ?> **/ return $c;}
/ [^{$]
/ c: "$" ![a-z] { /** <?php return $c; ?> **/ return c;}
DoubleStringChar = "\\" c: "$" { /** <?php return $c; ?> **/ return c;}
/ c:"{" !"$" { /** <?php return $c; ?> **/ return c;}
/ c: [^{] { /** <?php return $c; ?> **/ return c;}
// Character in Code Mode
CodeChar = "\\" c: "$" { /** <?php return $c; ?> **/ return c;}
/ c:"{" !"$" { /** <?php return $c; ?> **/ return c;}
/ c: [^{] { /** <?php return $c; ?> **/ return c;}
LineTerminator
= [\n\r\u2028\u2029]
SingleEscapeCharacter
= char_:['"\\fnrt] {
/** <?php return str_replace(['f', 'n', 'r', 't'], ["\f","\n","\r","\t"], $char_); ?> **/
return char_.replace(/n/,'\\n',).replace(/r/,'\\r').replace(/t/,'\\t');
}