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