1 /++
2 This module was automatically generated from the following grammar:
3 
4 JSONGrammar:
5     JSONObject <  :'{' (Pair (:',' Pair)*)? :'}'
6     Pair       <  ^Terminals.String :':' Value
7     Array      <  :'[' (Value (:',' Value)* )? :']'
8     Value      < Terminals.Literal / JSONObject / Array
9 
10 
11 +/
12 module coregrammars.gen.json;
13 
14 import coregrammars.gen.terms;
15 
16 
17 public import pegged.peg;
18 import std.algorithm: startsWith;
19 import std.functional: toDelegate;
20 
21 struct GenericJSONGrammar(TParseTree)
22 {
23     import std.functional : toDelegate;
24     import pegged.dynamic.grammar;
25     static import pegged.peg;
26     struct JSONGrammar
27     {
28     enum name = "JSONGrammar";
29     static ParseTree delegate(ParseTree)[string] before;
30     static ParseTree delegate(ParseTree)[string] after;
31     static ParseTree delegate(ParseTree)[string] rules;
32     import std.typecons:Tuple, tuple;
33     static TParseTree[Tuple!(string, size_t)] memo;
34     static this()
35     {
36         rules["JSONObject"] = toDelegate(&JSONObject);
37         rules["Pair"] = toDelegate(&Pair);
38         rules["Array"] = toDelegate(&Array);
39         rules["Value"] = toDelegate(&Value);
40         rules["Spacing"] = toDelegate(&Spacing);
41     }
42 
43     template hooked(alias r, string name)
44     {
45         static ParseTree hooked(ParseTree p)
46         {
47             ParseTree result;
48 
49             if (name in before)
50             {
51                 result = before[name](p);
52                 if (result.successful)
53                     return result;
54             }
55 
56             result = r(p);
57             if (result.successful || name !in after)
58                 return result;
59 
60             result = after[name](p);
61             return result;
62         }
63 
64         static ParseTree hooked(string input)
65         {
66             return hooked!(r, name)(ParseTree("",false,[],input));
67         }
68     }
69 
70     static void addRuleBefore(string parentRule, string ruleSyntax)
71     {
72         // enum name is the current grammar name
73         DynamicGrammar dg = pegged.dynamic.grammar.grammar(name ~ ": " ~ ruleSyntax, rules);
74         foreach(ruleName,rule; dg.rules)
75             if (ruleName != "Spacing") // Keep the local Spacing rule, do not overwrite it
76                 rules[ruleName] = rule;
77         before[parentRule] = rules[dg.startingRule];
78     }
79 
80     static void addRuleAfter(string parentRule, string ruleSyntax)
81     {
82         // enum name is the current grammar named
83         DynamicGrammar dg = pegged.dynamic.grammar.grammar(name ~ ": " ~ ruleSyntax, rules);
84         foreach(ruleName,rule; dg.rules)
85         {
86             if (ruleName != "Spacing")
87                 rules[ruleName] = rule;
88         }
89         after[parentRule] = rules[dg.startingRule];
90     }
91 
92     static bool isRule(string s)
93     {
94         import std.algorithm : startsWith;
95         return s.startsWith("JSONGrammar.");
96     }
97     mixin decimateTree;
98 
99     alias spacing Spacing;
100 
101     static TParseTree JSONObject(TParseTree p)
102     {
103         if(__ctfe)
104         {
105             return         pegged.peg.defined!(pegged.peg.and!(pegged.peg.discard!(pegged.peg.wrapAround!(Spacing, pegged.peg.literal!("{"), Spacing)), pegged.peg.option!(pegged.peg.wrapAround!(Spacing, pegged.peg.and!(pegged.peg.wrapAround!(Spacing, Pair, Spacing), pegged.peg.zeroOrMore!(pegged.peg.wrapAround!(Spacing, pegged.peg.and!(pegged.peg.discard!(pegged.peg.wrapAround!(Spacing, pegged.peg.literal!(","), Spacing)), pegged.peg.wrapAround!(Spacing, Pair, Spacing)), Spacing))), Spacing)), pegged.peg.discard!(pegged.peg.wrapAround!(Spacing, pegged.peg.literal!("}"), Spacing))), "JSONGrammar.JSONObject")(p);
106         }
107         else
108         {
109             if (auto m = tuple(`JSONObject`, p.end) in memo)
110                 return *m;
111             else
112             {
113                 TParseTree result = hooked!(pegged.peg.defined!(pegged.peg.and!(pegged.peg.discard!(pegged.peg.wrapAround!(Spacing, pegged.peg.literal!("{"), Spacing)), pegged.peg.option!(pegged.peg.wrapAround!(Spacing, pegged.peg.and!(pegged.peg.wrapAround!(Spacing, Pair, Spacing), pegged.peg.zeroOrMore!(pegged.peg.wrapAround!(Spacing, pegged.peg.and!(pegged.peg.discard!(pegged.peg.wrapAround!(Spacing, pegged.peg.literal!(","), Spacing)), pegged.peg.wrapAround!(Spacing, Pair, Spacing)), Spacing))), Spacing)), pegged.peg.discard!(pegged.peg.wrapAround!(Spacing, pegged.peg.literal!("}"), Spacing))), "JSONGrammar.JSONObject"), "JSONObject")(p);
114                 memo[tuple(`JSONObject`, p.end)] = result;
115                 return result;
116             }
117         }
118     }
119 
120     static TParseTree JSONObject(string s)
121     {
122         if(__ctfe)
123         {
124             return         pegged.peg.defined!(pegged.peg.and!(pegged.peg.discard!(pegged.peg.wrapAround!(Spacing, pegged.peg.literal!("{"), Spacing)), pegged.peg.option!(pegged.peg.wrapAround!(Spacing, pegged.peg.and!(pegged.peg.wrapAround!(Spacing, Pair, Spacing), pegged.peg.zeroOrMore!(pegged.peg.wrapAround!(Spacing, pegged.peg.and!(pegged.peg.discard!(pegged.peg.wrapAround!(Spacing, pegged.peg.literal!(","), Spacing)), pegged.peg.wrapAround!(Spacing, Pair, Spacing)), Spacing))), Spacing)), pegged.peg.discard!(pegged.peg.wrapAround!(Spacing, pegged.peg.literal!("}"), Spacing))), "JSONGrammar.JSONObject")(TParseTree("", false,[], s));
125         }
126         else
127         {
128             forgetMemo();
129             return hooked!(pegged.peg.defined!(pegged.peg.and!(pegged.peg.discard!(pegged.peg.wrapAround!(Spacing, pegged.peg.literal!("{"), Spacing)), pegged.peg.option!(pegged.peg.wrapAround!(Spacing, pegged.peg.and!(pegged.peg.wrapAround!(Spacing, Pair, Spacing), pegged.peg.zeroOrMore!(pegged.peg.wrapAround!(Spacing, pegged.peg.and!(pegged.peg.discard!(pegged.peg.wrapAround!(Spacing, pegged.peg.literal!(","), Spacing)), pegged.peg.wrapAround!(Spacing, Pair, Spacing)), Spacing))), Spacing)), pegged.peg.discard!(pegged.peg.wrapAround!(Spacing, pegged.peg.literal!("}"), Spacing))), "JSONGrammar.JSONObject"), "JSONObject")(TParseTree("", false,[], s));
130         }
131     }
132     static string JSONObject(GetName g)
133     {
134         return "JSONGrammar.JSONObject";
135     }
136 
137     static TParseTree Pair(TParseTree p)
138     {
139         if(__ctfe)
140         {
141             return         pegged.peg.defined!(pegged.peg.and!(pegged.peg.keep!(pegged.peg.wrapAround!(Spacing, Terminals.String, Spacing)), pegged.peg.discard!(pegged.peg.wrapAround!(Spacing, pegged.peg.literal!(":"), Spacing)), pegged.peg.wrapAround!(Spacing, Value, Spacing)), "JSONGrammar.Pair")(p);
142         }
143         else
144         {
145             if (auto m = tuple(`Pair`, p.end) in memo)
146                 return *m;
147             else
148             {
149                 TParseTree result = hooked!(pegged.peg.defined!(pegged.peg.and!(pegged.peg.keep!(pegged.peg.wrapAround!(Spacing, Terminals.String, Spacing)), pegged.peg.discard!(pegged.peg.wrapAround!(Spacing, pegged.peg.literal!(":"), Spacing)), pegged.peg.wrapAround!(Spacing, Value, Spacing)), "JSONGrammar.Pair"), "Pair")(p);
150                 memo[tuple(`Pair`, p.end)] = result;
151                 return result;
152             }
153         }
154     }
155 
156     static TParseTree Pair(string s)
157     {
158         if(__ctfe)
159         {
160             return         pegged.peg.defined!(pegged.peg.and!(pegged.peg.keep!(pegged.peg.wrapAround!(Spacing, Terminals.String, Spacing)), pegged.peg.discard!(pegged.peg.wrapAround!(Spacing, pegged.peg.literal!(":"), Spacing)), pegged.peg.wrapAround!(Spacing, Value, Spacing)), "JSONGrammar.Pair")(TParseTree("", false,[], s));
161         }
162         else
163         {
164             forgetMemo();
165             return hooked!(pegged.peg.defined!(pegged.peg.and!(pegged.peg.keep!(pegged.peg.wrapAround!(Spacing, Terminals.String, Spacing)), pegged.peg.discard!(pegged.peg.wrapAround!(Spacing, pegged.peg.literal!(":"), Spacing)), pegged.peg.wrapAround!(Spacing, Value, Spacing)), "JSONGrammar.Pair"), "Pair")(TParseTree("", false,[], s));
166         }
167     }
168     static string Pair(GetName g)
169     {
170         return "JSONGrammar.Pair";
171     }
172 
173     static TParseTree Array(TParseTree p)
174     {
175         if(__ctfe)
176         {
177             return         pegged.peg.defined!(pegged.peg.and!(pegged.peg.discard!(pegged.peg.wrapAround!(Spacing, pegged.peg.literal!("["), Spacing)), pegged.peg.option!(pegged.peg.wrapAround!(Spacing, pegged.peg.and!(pegged.peg.wrapAround!(Spacing, Value, Spacing), pegged.peg.zeroOrMore!(pegged.peg.wrapAround!(Spacing, pegged.peg.and!(pegged.peg.discard!(pegged.peg.wrapAround!(Spacing, pegged.peg.literal!(","), Spacing)), pegged.peg.wrapAround!(Spacing, Value, Spacing)), Spacing))), Spacing)), pegged.peg.discard!(pegged.peg.wrapAround!(Spacing, pegged.peg.literal!("]"), Spacing))), "JSONGrammar.Array")(p);
178         }
179         else
180         {
181             if (auto m = tuple(`Array`, p.end) in memo)
182                 return *m;
183             else
184             {
185                 TParseTree result = hooked!(pegged.peg.defined!(pegged.peg.and!(pegged.peg.discard!(pegged.peg.wrapAround!(Spacing, pegged.peg.literal!("["), Spacing)), pegged.peg.option!(pegged.peg.wrapAround!(Spacing, pegged.peg.and!(pegged.peg.wrapAround!(Spacing, Value, Spacing), pegged.peg.zeroOrMore!(pegged.peg.wrapAround!(Spacing, pegged.peg.and!(pegged.peg.discard!(pegged.peg.wrapAround!(Spacing, pegged.peg.literal!(","), Spacing)), pegged.peg.wrapAround!(Spacing, Value, Spacing)), Spacing))), Spacing)), pegged.peg.discard!(pegged.peg.wrapAround!(Spacing, pegged.peg.literal!("]"), Spacing))), "JSONGrammar.Array"), "Array")(p);
186                 memo[tuple(`Array`, p.end)] = result;
187                 return result;
188             }
189         }
190     }
191 
192     static TParseTree Array(string s)
193     {
194         if(__ctfe)
195         {
196             return         pegged.peg.defined!(pegged.peg.and!(pegged.peg.discard!(pegged.peg.wrapAround!(Spacing, pegged.peg.literal!("["), Spacing)), pegged.peg.option!(pegged.peg.wrapAround!(Spacing, pegged.peg.and!(pegged.peg.wrapAround!(Spacing, Value, Spacing), pegged.peg.zeroOrMore!(pegged.peg.wrapAround!(Spacing, pegged.peg.and!(pegged.peg.discard!(pegged.peg.wrapAround!(Spacing, pegged.peg.literal!(","), Spacing)), pegged.peg.wrapAround!(Spacing, Value, Spacing)), Spacing))), Spacing)), pegged.peg.discard!(pegged.peg.wrapAround!(Spacing, pegged.peg.literal!("]"), Spacing))), "JSONGrammar.Array")(TParseTree("", false,[], s));
197         }
198         else
199         {
200             forgetMemo();
201             return hooked!(pegged.peg.defined!(pegged.peg.and!(pegged.peg.discard!(pegged.peg.wrapAround!(Spacing, pegged.peg.literal!("["), Spacing)), pegged.peg.option!(pegged.peg.wrapAround!(Spacing, pegged.peg.and!(pegged.peg.wrapAround!(Spacing, Value, Spacing), pegged.peg.zeroOrMore!(pegged.peg.wrapAround!(Spacing, pegged.peg.and!(pegged.peg.discard!(pegged.peg.wrapAround!(Spacing, pegged.peg.literal!(","), Spacing)), pegged.peg.wrapAround!(Spacing, Value, Spacing)), Spacing))), Spacing)), pegged.peg.discard!(pegged.peg.wrapAround!(Spacing, pegged.peg.literal!("]"), Spacing))), "JSONGrammar.Array"), "Array")(TParseTree("", false,[], s));
202         }
203     }
204     static string Array(GetName g)
205     {
206         return "JSONGrammar.Array";
207     }
208 
209     static TParseTree Value(TParseTree p)
210     {
211         if(__ctfe)
212         {
213             return         pegged.peg.defined!(pegged.peg.or!(pegged.peg.wrapAround!(Spacing, Terminals.Literal, Spacing), pegged.peg.wrapAround!(Spacing, JSONObject, Spacing), pegged.peg.wrapAround!(Spacing, Array, Spacing)), "JSONGrammar.Value")(p);
214         }
215         else
216         {
217             if (auto m = tuple(`Value`, p.end) in memo)
218                 return *m;
219             else
220             {
221                 TParseTree result = hooked!(pegged.peg.defined!(pegged.peg.or!(pegged.peg.wrapAround!(Spacing, Terminals.Literal, Spacing), pegged.peg.wrapAround!(Spacing, JSONObject, Spacing), pegged.peg.wrapAround!(Spacing, Array, Spacing)), "JSONGrammar.Value"), "Value")(p);
222                 memo[tuple(`Value`, p.end)] = result;
223                 return result;
224             }
225         }
226     }
227 
228     static TParseTree Value(string s)
229     {
230         if(__ctfe)
231         {
232             return         pegged.peg.defined!(pegged.peg.or!(pegged.peg.wrapAround!(Spacing, Terminals.Literal, Spacing), pegged.peg.wrapAround!(Spacing, JSONObject, Spacing), pegged.peg.wrapAround!(Spacing, Array, Spacing)), "JSONGrammar.Value")(TParseTree("", false,[], s));
233         }
234         else
235         {
236             forgetMemo();
237             return hooked!(pegged.peg.defined!(pegged.peg.or!(pegged.peg.wrapAround!(Spacing, Terminals.Literal, Spacing), pegged.peg.wrapAround!(Spacing, JSONObject, Spacing), pegged.peg.wrapAround!(Spacing, Array, Spacing)), "JSONGrammar.Value"), "Value")(TParseTree("", false,[], s));
238         }
239     }
240     static string Value(GetName g)
241     {
242         return "JSONGrammar.Value";
243     }
244 
245     static TParseTree opCall(TParseTree p)
246     {
247         TParseTree result = decimateTree(JSONObject(p));
248         result.children = [result];
249         result.name = "JSONGrammar";
250         return result;
251     }
252 
253     static TParseTree opCall(string input)
254     {
255         if(__ctfe)
256         {
257             return JSONGrammar(TParseTree(``, false, [], input, 0, 0));
258         }
259         else
260         {
261             forgetMemo();
262             return JSONGrammar(TParseTree(``, false, [], input, 0, 0));
263         }
264     }
265     static string opCall(GetName g)
266     {
267         return "JSONGrammar";
268     }
269 
270 
271     static void forgetMemo()
272     {
273         memo = null;
274         import std.traits;
275         static if (is(typeof(Terminals.forgetMemo)))
276             Terminals.forgetMemo();
277     }
278     }
279 }
280 
281 alias GenericJSONGrammar!(ParseTree).JSONGrammar JSONGrammar;
282