1 /++
2 This module was automatically generated from the following grammar:
3 
4 ExpressionsGrammar(Variable = identifier):
5 
6 
7 
8         Arithmetic     < Factor (Add / Sub)*
9 
10         Add      < "+" Factor
11 
12         Sub      < "-" Factor
13 
14         Factor   < Primary (Mul / Div)*
15 
16         Mul      < "*" Primary
17 
18         Div      < "/" Primary
19 
20         Primary  < Parens / Terminals.Number / Variable
21 
22         Parens   < :"(" Arithmetic :")"
23 
24 
25 
26 +/
27 module coregrammars.gen.expr;
28 
29 import coregrammars.gen.terms;
30 
31 
32 public import pegged.peg;
33 import std.algorithm: startsWith;
34 import std.functional: toDelegate;
35 
36 struct GenericExpressionsGrammar(TParseTree)
37 {
38     import std.functional : toDelegate;
39     import pegged.dynamic.grammar;
40     static import pegged.peg;
41     struct ExpressionsGrammar(alias Variable = identifier)
42     {
43     enum name = "ExpressionsGrammar";
44     static ParseTree delegate(ParseTree)[string] before;
45     static ParseTree delegate(ParseTree)[string] after;
46     static ParseTree delegate(ParseTree)[string] rules;
47     import std.typecons:Tuple, tuple;
48     static TParseTree[Tuple!(string, size_t)] memo;
49     static this()
50     {
51         rules["Arithmetic"] = toDelegate(&Arithmetic);
52         rules["Add"] = toDelegate(&Add);
53         rules["Sub"] = toDelegate(&Sub);
54         rules["Factor"] = toDelegate(&Factor);
55         rules["Mul"] = toDelegate(&Mul);
56         rules["Div"] = toDelegate(&Div);
57         rules["Primary"] = toDelegate(&Primary);
58         rules["Parens"] = toDelegate(&Parens);
59         rules["Spacing"] = toDelegate(&Spacing);
60     }
61 
62     template hooked(alias r, string name)
63     {
64         static ParseTree hooked(ParseTree p)
65         {
66             ParseTree result;
67 
68             if (name in before)
69             {
70                 result = before[name](p);
71                 if (result.successful)
72                     return result;
73             }
74 
75             result = r(p);
76             if (result.successful || name !in after)
77                 return result;
78 
79             result = after[name](p);
80             return result;
81         }
82 
83         static ParseTree hooked(string input)
84         {
85             return hooked!(r, name)(ParseTree("",false,[],input));
86         }
87     }
88 
89     static void addRuleBefore(string parentRule, string ruleSyntax)
90     {
91         // enum name is the current grammar name
92         DynamicGrammar dg = pegged.dynamic.grammar.grammar(name ~ ": " ~ ruleSyntax, rules);
93         foreach(ruleName,rule; dg.rules)
94             if (ruleName != "Spacing") // Keep the local Spacing rule, do not overwrite it
95                 rules[ruleName] = rule;
96         before[parentRule] = rules[dg.startingRule];
97     }
98 
99     static void addRuleAfter(string parentRule, string ruleSyntax)
100     {
101         // enum name is the current grammar named
102         DynamicGrammar dg = pegged.dynamic.grammar.grammar(name ~ ": " ~ ruleSyntax, rules);
103         foreach(ruleName,rule; dg.rules)
104         {
105             if (ruleName != "Spacing")
106                 rules[ruleName] = rule;
107         }
108         after[parentRule] = rules[dg.startingRule];
109     }
110 
111     static bool isRule(string s)
112     {
113         import std.algorithm : startsWith;
114         return s.startsWith("ExpressionsGrammar.");
115     }
116     mixin decimateTree;
117 
118     alias spacing Spacing;
119 
120     static TParseTree Arithmetic(TParseTree p)
121     {
122         if(__ctfe)
123         {
124             return         pegged.peg.defined!(pegged.peg.and!(pegged.peg.wrapAround!(Spacing, Factor, Spacing), pegged.peg.zeroOrMore!(pegged.peg.wrapAround!(Spacing, pegged.peg.or!(pegged.peg.wrapAround!(Spacing, Add, Spacing), pegged.peg.wrapAround!(Spacing, Sub, Spacing)), Spacing))), "ExpressionsGrammar.Arithmetic")(p);
125         }
126         else
127         {
128             if (auto m = tuple(`Arithmetic`, p.end) in memo)
129                 return *m;
130             else
131             {
132                 TParseTree result = hooked!(pegged.peg.defined!(pegged.peg.and!(pegged.peg.wrapAround!(Spacing, Factor, Spacing), pegged.peg.zeroOrMore!(pegged.peg.wrapAround!(Spacing, pegged.peg.or!(pegged.peg.wrapAround!(Spacing, Add, Spacing), pegged.peg.wrapAround!(Spacing, Sub, Spacing)), Spacing))), "ExpressionsGrammar.Arithmetic"), "Arithmetic")(p);
133                 memo[tuple(`Arithmetic`, p.end)] = result;
134                 return result;
135             }
136         }
137     }
138 
139     static TParseTree Arithmetic(string s)
140     {
141         if(__ctfe)
142         {
143             return         pegged.peg.defined!(pegged.peg.and!(pegged.peg.wrapAround!(Spacing, Factor, Spacing), pegged.peg.zeroOrMore!(pegged.peg.wrapAround!(Spacing, pegged.peg.or!(pegged.peg.wrapAround!(Spacing, Add, Spacing), pegged.peg.wrapAround!(Spacing, Sub, Spacing)), Spacing))), "ExpressionsGrammar.Arithmetic")(TParseTree("", false,[], s));
144         }
145         else
146         {
147             forgetMemo();
148             return hooked!(pegged.peg.defined!(pegged.peg.and!(pegged.peg.wrapAround!(Spacing, Factor, Spacing), pegged.peg.zeroOrMore!(pegged.peg.wrapAround!(Spacing, pegged.peg.or!(pegged.peg.wrapAround!(Spacing, Add, Spacing), pegged.peg.wrapAround!(Spacing, Sub, Spacing)), Spacing))), "ExpressionsGrammar.Arithmetic"), "Arithmetic")(TParseTree("", false,[], s));
149         }
150     }
151     static string Arithmetic(GetName g)
152     {
153         return "ExpressionsGrammar.Arithmetic";
154     }
155 
156     static TParseTree Add(TParseTree p)
157     {
158         if(__ctfe)
159         {
160             return         pegged.peg.defined!(pegged.peg.and!(pegged.peg.wrapAround!(Spacing, pegged.peg.literal!("+"), Spacing), pegged.peg.wrapAround!(Spacing, Factor, Spacing)), "ExpressionsGrammar.Add")(p);
161         }
162         else
163         {
164             if (auto m = tuple(`Add`, p.end) in memo)
165                 return *m;
166             else
167             {
168                 TParseTree result = hooked!(pegged.peg.defined!(pegged.peg.and!(pegged.peg.wrapAround!(Spacing, pegged.peg.literal!("+"), Spacing), pegged.peg.wrapAround!(Spacing, Factor, Spacing)), "ExpressionsGrammar.Add"), "Add")(p);
169                 memo[tuple(`Add`, p.end)] = result;
170                 return result;
171             }
172         }
173     }
174 
175     static TParseTree Add(string s)
176     {
177         if(__ctfe)
178         {
179             return         pegged.peg.defined!(pegged.peg.and!(pegged.peg.wrapAround!(Spacing, pegged.peg.literal!("+"), Spacing), pegged.peg.wrapAround!(Spacing, Factor, Spacing)), "ExpressionsGrammar.Add")(TParseTree("", false,[], s));
180         }
181         else
182         {
183             forgetMemo();
184             return hooked!(pegged.peg.defined!(pegged.peg.and!(pegged.peg.wrapAround!(Spacing, pegged.peg.literal!("+"), Spacing), pegged.peg.wrapAround!(Spacing, Factor, Spacing)), "ExpressionsGrammar.Add"), "Add")(TParseTree("", false,[], s));
185         }
186     }
187     static string Add(GetName g)
188     {
189         return "ExpressionsGrammar.Add";
190     }
191 
192     static TParseTree Sub(TParseTree p)
193     {
194         if(__ctfe)
195         {
196             return         pegged.peg.defined!(pegged.peg.and!(pegged.peg.wrapAround!(Spacing, pegged.peg.literal!("-"), Spacing), pegged.peg.wrapAround!(Spacing, Factor, Spacing)), "ExpressionsGrammar.Sub")(p);
197         }
198         else
199         {
200             if (auto m = tuple(`Sub`, p.end) in memo)
201                 return *m;
202             else
203             {
204                 TParseTree result = hooked!(pegged.peg.defined!(pegged.peg.and!(pegged.peg.wrapAround!(Spacing, pegged.peg.literal!("-"), Spacing), pegged.peg.wrapAround!(Spacing, Factor, Spacing)), "ExpressionsGrammar.Sub"), "Sub")(p);
205                 memo[tuple(`Sub`, p.end)] = result;
206                 return result;
207             }
208         }
209     }
210 
211     static TParseTree Sub(string s)
212     {
213         if(__ctfe)
214         {
215             return         pegged.peg.defined!(pegged.peg.and!(pegged.peg.wrapAround!(Spacing, pegged.peg.literal!("-"), Spacing), pegged.peg.wrapAround!(Spacing, Factor, Spacing)), "ExpressionsGrammar.Sub")(TParseTree("", false,[], s));
216         }
217         else
218         {
219             forgetMemo();
220             return hooked!(pegged.peg.defined!(pegged.peg.and!(pegged.peg.wrapAround!(Spacing, pegged.peg.literal!("-"), Spacing), pegged.peg.wrapAround!(Spacing, Factor, Spacing)), "ExpressionsGrammar.Sub"), "Sub")(TParseTree("", false,[], s));
221         }
222     }
223     static string Sub(GetName g)
224     {
225         return "ExpressionsGrammar.Sub";
226     }
227 
228     static TParseTree Factor(TParseTree p)
229     {
230         if(__ctfe)
231         {
232             return         pegged.peg.defined!(pegged.peg.and!(pegged.peg.wrapAround!(Spacing, Primary, Spacing), pegged.peg.zeroOrMore!(pegged.peg.wrapAround!(Spacing, pegged.peg.or!(pegged.peg.wrapAround!(Spacing, Mul, Spacing), pegged.peg.wrapAround!(Spacing, Div, Spacing)), Spacing))), "ExpressionsGrammar.Factor")(p);
233         }
234         else
235         {
236             if (auto m = tuple(`Factor`, p.end) in memo)
237                 return *m;
238             else
239             {
240                 TParseTree result = hooked!(pegged.peg.defined!(pegged.peg.and!(pegged.peg.wrapAround!(Spacing, Primary, Spacing), pegged.peg.zeroOrMore!(pegged.peg.wrapAround!(Spacing, pegged.peg.or!(pegged.peg.wrapAround!(Spacing, Mul, Spacing), pegged.peg.wrapAround!(Spacing, Div, Spacing)), Spacing))), "ExpressionsGrammar.Factor"), "Factor")(p);
241                 memo[tuple(`Factor`, p.end)] = result;
242                 return result;
243             }
244         }
245     }
246 
247     static TParseTree Factor(string s)
248     {
249         if(__ctfe)
250         {
251             return         pegged.peg.defined!(pegged.peg.and!(pegged.peg.wrapAround!(Spacing, Primary, Spacing), pegged.peg.zeroOrMore!(pegged.peg.wrapAround!(Spacing, pegged.peg.or!(pegged.peg.wrapAround!(Spacing, Mul, Spacing), pegged.peg.wrapAround!(Spacing, Div, Spacing)), Spacing))), "ExpressionsGrammar.Factor")(TParseTree("", false,[], s));
252         }
253         else
254         {
255             forgetMemo();
256             return hooked!(pegged.peg.defined!(pegged.peg.and!(pegged.peg.wrapAround!(Spacing, Primary, Spacing), pegged.peg.zeroOrMore!(pegged.peg.wrapAround!(Spacing, pegged.peg.or!(pegged.peg.wrapAround!(Spacing, Mul, Spacing), pegged.peg.wrapAround!(Spacing, Div, Spacing)), Spacing))), "ExpressionsGrammar.Factor"), "Factor")(TParseTree("", false,[], s));
257         }
258     }
259     static string Factor(GetName g)
260     {
261         return "ExpressionsGrammar.Factor";
262     }
263 
264     static TParseTree Mul(TParseTree p)
265     {
266         if(__ctfe)
267         {
268             return         pegged.peg.defined!(pegged.peg.and!(pegged.peg.wrapAround!(Spacing, pegged.peg.literal!("*"), Spacing), pegged.peg.wrapAround!(Spacing, Primary, Spacing)), "ExpressionsGrammar.Mul")(p);
269         }
270         else
271         {
272             if (auto m = tuple(`Mul`, p.end) in memo)
273                 return *m;
274             else
275             {
276                 TParseTree result = hooked!(pegged.peg.defined!(pegged.peg.and!(pegged.peg.wrapAround!(Spacing, pegged.peg.literal!("*"), Spacing), pegged.peg.wrapAround!(Spacing, Primary, Spacing)), "ExpressionsGrammar.Mul"), "Mul")(p);
277                 memo[tuple(`Mul`, p.end)] = result;
278                 return result;
279             }
280         }
281     }
282 
283     static TParseTree Mul(string s)
284     {
285         if(__ctfe)
286         {
287             return         pegged.peg.defined!(pegged.peg.and!(pegged.peg.wrapAround!(Spacing, pegged.peg.literal!("*"), Spacing), pegged.peg.wrapAround!(Spacing, Primary, Spacing)), "ExpressionsGrammar.Mul")(TParseTree("", false,[], s));
288         }
289         else
290         {
291             forgetMemo();
292             return hooked!(pegged.peg.defined!(pegged.peg.and!(pegged.peg.wrapAround!(Spacing, pegged.peg.literal!("*"), Spacing), pegged.peg.wrapAround!(Spacing, Primary, Spacing)), "ExpressionsGrammar.Mul"), "Mul")(TParseTree("", false,[], s));
293         }
294     }
295     static string Mul(GetName g)
296     {
297         return "ExpressionsGrammar.Mul";
298     }
299 
300     static TParseTree Div(TParseTree p)
301     {
302         if(__ctfe)
303         {
304             return         pegged.peg.defined!(pegged.peg.and!(pegged.peg.wrapAround!(Spacing, pegged.peg.literal!("/"), Spacing), pegged.peg.wrapAround!(Spacing, Primary, Spacing)), "ExpressionsGrammar.Div")(p);
305         }
306         else
307         {
308             if (auto m = tuple(`Div`, p.end) in memo)
309                 return *m;
310             else
311             {
312                 TParseTree result = hooked!(pegged.peg.defined!(pegged.peg.and!(pegged.peg.wrapAround!(Spacing, pegged.peg.literal!("/"), Spacing), pegged.peg.wrapAround!(Spacing, Primary, Spacing)), "ExpressionsGrammar.Div"), "Div")(p);
313                 memo[tuple(`Div`, p.end)] = result;
314                 return result;
315             }
316         }
317     }
318 
319     static TParseTree Div(string s)
320     {
321         if(__ctfe)
322         {
323             return         pegged.peg.defined!(pegged.peg.and!(pegged.peg.wrapAround!(Spacing, pegged.peg.literal!("/"), Spacing), pegged.peg.wrapAround!(Spacing, Primary, Spacing)), "ExpressionsGrammar.Div")(TParseTree("", false,[], s));
324         }
325         else
326         {
327             forgetMemo();
328             return hooked!(pegged.peg.defined!(pegged.peg.and!(pegged.peg.wrapAround!(Spacing, pegged.peg.literal!("/"), Spacing), pegged.peg.wrapAround!(Spacing, Primary, Spacing)), "ExpressionsGrammar.Div"), "Div")(TParseTree("", false,[], s));
329         }
330     }
331     static string Div(GetName g)
332     {
333         return "ExpressionsGrammar.Div";
334     }
335 
336     static TParseTree Primary(TParseTree p)
337     {
338         if(__ctfe)
339         {
340             return         pegged.peg.defined!(pegged.peg.or!(pegged.peg.wrapAround!(Spacing, Parens, Spacing), pegged.peg.wrapAround!(Spacing, Terminals.Number, Spacing), pegged.peg.wrapAround!(Spacing, Variable, Spacing)), "ExpressionsGrammar.Primary")(p);
341         }
342         else
343         {
344             if (auto m = tuple(`Primary`, p.end) in memo)
345                 return *m;
346             else
347             {
348                 TParseTree result = hooked!(pegged.peg.defined!(pegged.peg.or!(pegged.peg.wrapAround!(Spacing, Parens, Spacing), pegged.peg.wrapAround!(Spacing, Terminals.Number, Spacing), pegged.peg.wrapAround!(Spacing, Variable, Spacing)), "ExpressionsGrammar.Primary"), "Primary")(p);
349                 memo[tuple(`Primary`, p.end)] = result;
350                 return result;
351             }
352         }
353     }
354 
355     static TParseTree Primary(string s)
356     {
357         if(__ctfe)
358         {
359             return         pegged.peg.defined!(pegged.peg.or!(pegged.peg.wrapAround!(Spacing, Parens, Spacing), pegged.peg.wrapAround!(Spacing, Terminals.Number, Spacing), pegged.peg.wrapAround!(Spacing, Variable, Spacing)), "ExpressionsGrammar.Primary")(TParseTree("", false,[], s));
360         }
361         else
362         {
363             forgetMemo();
364             return hooked!(pegged.peg.defined!(pegged.peg.or!(pegged.peg.wrapAround!(Spacing, Parens, Spacing), pegged.peg.wrapAround!(Spacing, Terminals.Number, Spacing), pegged.peg.wrapAround!(Spacing, Variable, Spacing)), "ExpressionsGrammar.Primary"), "Primary")(TParseTree("", false,[], s));
365         }
366     }
367     static string Primary(GetName g)
368     {
369         return "ExpressionsGrammar.Primary";
370     }
371 
372     static TParseTree Parens(TParseTree p)
373     {
374         if(__ctfe)
375         {
376             return         pegged.peg.defined!(pegged.peg.and!(pegged.peg.discard!(pegged.peg.wrapAround!(Spacing, pegged.peg.literal!("("), Spacing)), pegged.peg.wrapAround!(Spacing, Arithmetic, Spacing), pegged.peg.discard!(pegged.peg.wrapAround!(Spacing, pegged.peg.literal!(")"), Spacing))), "ExpressionsGrammar.Parens")(p);
377         }
378         else
379         {
380             if (auto m = tuple(`Parens`, p.end) in memo)
381                 return *m;
382             else
383             {
384                 TParseTree result = hooked!(pegged.peg.defined!(pegged.peg.and!(pegged.peg.discard!(pegged.peg.wrapAround!(Spacing, pegged.peg.literal!("("), Spacing)), pegged.peg.wrapAround!(Spacing, Arithmetic, Spacing), pegged.peg.discard!(pegged.peg.wrapAround!(Spacing, pegged.peg.literal!(")"), Spacing))), "ExpressionsGrammar.Parens"), "Parens")(p);
385                 memo[tuple(`Parens`, p.end)] = result;
386                 return result;
387             }
388         }
389     }
390 
391     static TParseTree Parens(string s)
392     {
393         if(__ctfe)
394         {
395             return         pegged.peg.defined!(pegged.peg.and!(pegged.peg.discard!(pegged.peg.wrapAround!(Spacing, pegged.peg.literal!("("), Spacing)), pegged.peg.wrapAround!(Spacing, Arithmetic, Spacing), pegged.peg.discard!(pegged.peg.wrapAround!(Spacing, pegged.peg.literal!(")"), Spacing))), "ExpressionsGrammar.Parens")(TParseTree("", false,[], s));
396         }
397         else
398         {
399             forgetMemo();
400             return hooked!(pegged.peg.defined!(pegged.peg.and!(pegged.peg.discard!(pegged.peg.wrapAround!(Spacing, pegged.peg.literal!("("), Spacing)), pegged.peg.wrapAround!(Spacing, Arithmetic, Spacing), pegged.peg.discard!(pegged.peg.wrapAround!(Spacing, pegged.peg.literal!(")"), Spacing))), "ExpressionsGrammar.Parens"), "Parens")(TParseTree("", false,[], s));
401         }
402     }
403     static string Parens(GetName g)
404     {
405         return "ExpressionsGrammar.Parens";
406     }
407 
408     static TParseTree opCall(TParseTree p)
409     {
410         TParseTree result = decimateTree(Arithmetic(p));
411         result.children = [result];
412         result.name = "ExpressionsGrammar";
413         return result;
414     }
415 
416     static TParseTree opCall(string input)
417     {
418         if(__ctfe)
419         {
420             return ExpressionsGrammar(TParseTree(``, false, [], input, 0, 0));
421         }
422         else
423         {
424             forgetMemo();
425             return ExpressionsGrammar(TParseTree(``, false, [], input, 0, 0));
426         }
427     }
428     static string opCall(GetName g)
429     {
430         return "ExpressionsGrammar";
431     }
432 
433 
434     static void forgetMemo()
435     {
436         memo = null;
437         import std.traits;
438         static if (is(typeof(Terminals.forgetMemo)))
439             Terminals.forgetMemo();
440     }
441     }
442 }
443 
444 alias GenericExpressionsGrammar!(ParseTree).ExpressionsGrammar ExpressionsGrammar;
445