UNPKG

19 kBJavaScriptView Raw
1// Generated automatically by nearley, version 2.16.0
2// http://github.com/Hardmath123/nearley
3(function () {
4function id(x) { return x[0]; }
5
6 const flatten = d => {
7 return d.reduce(
8 (a, b) => {
9 if(b) {
10 return a.concat(b);
11 } else {
12 return a;
13 }
14 },
15 []
16 );
17 };
18
19const moo = require("moo");
20
21const escapeNearley = (x) => {
22 return x.replace(/\t/g, '\\t') // Replace tab due to Nearley bug #nearley/issues/413
23 .replace(/\f/g, '\\f')
24 .replace(/\r/g, '\\r');
25}
26
27// we use lexer states to distinguish between the tokens
28// in the text and the tokens inside the variables
29const lexer = moo.states({
30 main: {
31 // a chunk is everything up until '{[', even across newlines. We then trim off the '{['
32 // we also push the lexer into the 'var' state
33 Chunk: {
34 match: /[^]*?{{/,
35 lineBreaks: true,
36 push: 'markup',
37 value: x => escapeNearley(x.slice(0, -2))
38 },
39 // we now need to consume everything up until the end of the buffer.
40 // note that the order of these two rules is important!
41 LastChunk : {
42 match: /[^]+/,
43 lineBreaks: true,
44 value: x => escapeNearley(x)
45 }
46 },
47 markup: { // This is a dispatch to the correct lexical state (using moo's "next")
48 markupstartblock: {
49 match: '#',
50 next: 'startblock',
51 },
52 markupstartref: {
53 match: '>',
54 next: 'startref',
55 },
56 markupendblock: {
57 match: '/',
58 next: 'endblock',
59 },
60 markupexpr: {
61 match: '%',
62 next: 'expr',
63 },
64 varend: {
65 match: '}}',
66 pop: true
67 }, // pop back to main state
68 varelseid: 'else',
69 varid: {
70 match: /[a-zA-Z_][_a-zA-Z0-9]*/,
71 type: moo.keywords({varas: 'as'})
72 },
73 varstring: /".*?"/,
74 varspace: ' ',
75 },
76 expr: {
77 exprend: {
78 match: /[^]*?[%]}}/,
79 lineBreaks: true,
80 pop: true
81 },
82 },
83 startblock: {
84 startblockend: {
85 match: '}}',
86 pop: true,
87 }, // pop back to main state
88 startblockspace: / +/,
89 startclauseid: 'clause',
90 startwithid: 'with',
91 startulistid: 'ulist',
92 startolistid: 'olist',
93 startjoinid: 'join',
94 startifid: 'if',
95 startblockstring: /".*?"/,
96 startblockid: /[a-zA-Z_][_a-zA-Z0-9]*/,
97 },
98 startref: {
99 startrefend: {
100 match: '}}',
101 pop: true,
102 }, // pop back to main state
103 startrefspace: / +/,
104 startrefid: /[a-zA-Z_][_a-zA-Z0-9]*/,
105 },
106 endblock: {
107 endblockend: {
108 match: '}}',
109 pop: true
110 }, // pop back to main state
111 endclauseid: 'clause',
112 endwithid: 'with',
113 endulistid: 'ulist',
114 endolistid: 'olist',
115 endjoinid: 'join',
116 endifid: 'if',
117 },
118});
119var grammar = {
120 Lexer: lexer,
121 ParserRules: [
122 {"name": "_$ebnf$1", "symbols": []},
123 {"name": "_$ebnf$1", "symbols": ["_$ebnf$1", "wschar"], "postprocess": function arrpush(d) {return d[0].concat([d[1]]);}},
124 {"name": "_", "symbols": ["_$ebnf$1"], "postprocess": function(d) {return null;}},
125 {"name": "__$ebnf$1", "symbols": ["wschar"]},
126 {"name": "__$ebnf$1", "symbols": ["__$ebnf$1", "wschar"], "postprocess": function arrpush(d) {return d[0].concat([d[1]]);}},
127 {"name": "__", "symbols": ["__$ebnf$1"], "postprocess": function(d) {return null;}},
128 {"name": "wschar", "symbols": [/[ \t\n\v\f]/], "postprocess": id},
129 {"name": "dqstring$ebnf$1", "symbols": []},
130 {"name": "dqstring$ebnf$1", "symbols": ["dqstring$ebnf$1", "dstrchar"], "postprocess": function arrpush(d) {return d[0].concat([d[1]]);}},
131 {"name": "dqstring", "symbols": [{"literal":"\""}, "dqstring$ebnf$1", {"literal":"\""}], "postprocess": function(d) {return d[1].join(""); }},
132 {"name": "sqstring$ebnf$1", "symbols": []},
133 {"name": "sqstring$ebnf$1", "symbols": ["sqstring$ebnf$1", "sstrchar"], "postprocess": function arrpush(d) {return d[0].concat([d[1]]);}},
134 {"name": "sqstring", "symbols": [{"literal":"'"}, "sqstring$ebnf$1", {"literal":"'"}], "postprocess": function(d) {return d[1].join(""); }},
135 {"name": "btstring$ebnf$1", "symbols": []},
136 {"name": "btstring$ebnf$1", "symbols": ["btstring$ebnf$1", /[^`]/], "postprocess": function arrpush(d) {return d[0].concat([d[1]]);}},
137 {"name": "btstring", "symbols": [{"literal":"`"}, "btstring$ebnf$1", {"literal":"`"}], "postprocess": function(d) {return d[1].join(""); }},
138 {"name": "dstrchar", "symbols": [/[^\\"\n]/], "postprocess": id},
139 {"name": "dstrchar", "symbols": [{"literal":"\\"}, "strescape"], "postprocess":
140 function(d) {
141 return JSON.parse("\""+d.join("")+"\"");
142 }
143 },
144 {"name": "sstrchar", "symbols": [/[^\\'\n]/], "postprocess": id},
145 {"name": "sstrchar", "symbols": [{"literal":"\\"}, "strescape"], "postprocess": function(d) { return JSON.parse("\""+d.join("")+"\""); }},
146 {"name": "sstrchar$string$1", "symbols": [{"literal":"\\"}, {"literal":"'"}], "postprocess": function joiner(d) {return d.join('');}},
147 {"name": "sstrchar", "symbols": ["sstrchar$string$1"], "postprocess": function(d) {return "'"; }},
148 {"name": "strescape", "symbols": [/["\\\/bfnrt]/], "postprocess": id},
149 {"name": "strescape", "symbols": [{"literal":"u"}, /[a-fA-F0-9]/, /[a-fA-F0-9]/, /[a-fA-F0-9]/, /[a-fA-F0-9]/], "postprocess":
150 function(d) {
151 return d.join("");
152 }
153 },
154 {"name": "TEMPLATE", "symbols": ["CONTRACT_TEMPLATE"], "postprocess": id},
155 {"name": "CONTRACT_TEMPLATE$ebnf$1", "symbols": []},
156 {"name": "CONTRACT_TEMPLATE$ebnf$1", "symbols": ["CONTRACT_TEMPLATE$ebnf$1", "CONTRACT_ITEM"], "postprocess": function arrpush(d) {return d[0].concat([d[1]]);}},
157 {"name": "CONTRACT_TEMPLATE$ebnf$2", "symbols": [(lexer.has("LastChunk") ? {type: "LastChunk"} : LastChunk)], "postprocess": id},
158 {"name": "CONTRACT_TEMPLATE$ebnf$2", "symbols": [], "postprocess": function(d) {return null;}},
159 {"name": "CONTRACT_TEMPLATE", "symbols": ["CONTRACT_TEMPLATE$ebnf$1", "CONTRACT_TEMPLATE$ebnf$2"], "postprocess": (data) => {
160 return {
161 type: 'ContractTemplate',
162 data: flatten(data)
163 };
164 }
165 },
166 {"name": "CONTRACT_ITEM", "symbols": [(lexer.has("Chunk") ? {type: "Chunk"} : Chunk)], "postprocess": id},
167 {"name": "CONTRACT_ITEM", "symbols": [(lexer.has("markupstartblock") ? {type: "markupstartblock"} : markupstartblock), "ULIST_BLOCK_INLINE"], "postprocess": (data) => { return data[1]; }},
168 {"name": "CONTRACT_ITEM", "symbols": [(lexer.has("markupstartblock") ? {type: "markupstartblock"} : markupstartblock), "OLIST_BLOCK_INLINE"], "postprocess": (data) => { return data[1]; }},
169 {"name": "CONTRACT_ITEM", "symbols": [(lexer.has("markupstartblock") ? {type: "markupstartblock"} : markupstartblock), "JOIN_BLOCK_INLINE"], "postprocess": (data) => { return data[1]; }},
170 {"name": "CONTRACT_ITEM", "symbols": [(lexer.has("markupstartblock") ? {type: "markupstartblock"} : markupstartblock), "IF_BLOCK_INLINE"], "postprocess": (data) => { return data[1]; }},
171 {"name": "CONTRACT_ITEM", "symbols": [(lexer.has("markupstartblock") ? {type: "markupstartblock"} : markupstartblock), "CLAUSE_BLOCK_INLINE"], "postprocess": (data) => { return data[1]; }},
172 {"name": "CONTRACT_ITEM", "symbols": [(lexer.has("markupstartblock") ? {type: "markupstartblock"} : markupstartblock), "WITH_BLOCK_INLINE"], "postprocess": (data) => { return data[1]; }},
173 {"name": "CONTRACT_ITEM", "symbols": [(lexer.has("markupstartref") ? {type: "markupstartref"} : markupstartref), "CLAUSE_BLOCK_EXTERNAL"], "postprocess": (data) => { return data[1]; }},
174 {"name": "CONTRACT_ITEM", "symbols": [(lexer.has("markupexpr") ? {type: "markupexpr"} : markupexpr), "CLAUSE_EXPR"], "postprocess": (data) => { return data[1]; }},
175 {"name": "CONTRACT_ITEM", "symbols": ["VARIABLE"], "postprocess": id},
176 {"name": "CLAUSE_TEMPLATE$ebnf$1", "symbols": []},
177 {"name": "CLAUSE_TEMPLATE$ebnf$1", "symbols": ["CLAUSE_TEMPLATE$ebnf$1", "CLAUSE_ITEM"], "postprocess": function arrpush(d) {return d[0].concat([d[1]]);}},
178 {"name": "CLAUSE_TEMPLATE$ebnf$2", "symbols": [(lexer.has("LastChunk") ? {type: "LastChunk"} : LastChunk)], "postprocess": id},
179 {"name": "CLAUSE_TEMPLATE$ebnf$2", "symbols": [], "postprocess": function(d) {return null;}},
180 {"name": "CLAUSE_TEMPLATE", "symbols": ["CLAUSE_TEMPLATE$ebnf$1", "CLAUSE_TEMPLATE$ebnf$2"], "postprocess": (data) => {
181 return {
182 type: 'ClauseTemplate',
183 data: flatten(data)
184 };
185 }
186 },
187 {"name": "CLAUSE_ITEM", "symbols": [(lexer.has("Chunk") ? {type: "Chunk"} : Chunk)], "postprocess": id},
188 {"name": "CLAUSE_ITEM", "symbols": [(lexer.has("markupstartblock") ? {type: "markupstartblock"} : markupstartblock), "ULIST_BLOCK_INLINE"], "postprocess": (data) => { return data[1]; }},
189 {"name": "CLAUSE_ITEM", "symbols": [(lexer.has("markupstartblock") ? {type: "markupstartblock"} : markupstartblock), "OLIST_BLOCK_INLINE"], "postprocess": (data) => { return data[1]; }},
190 {"name": "CLAUSE_ITEM", "symbols": [(lexer.has("markupstartblock") ? {type: "markupstartblock"} : markupstartblock), "JOIN_BLOCK_INLINE"], "postprocess": (data) => { return data[1]; }},
191 {"name": "CLAUSE_ITEM", "symbols": [(lexer.has("markupstartblock") ? {type: "markupstartblock"} : markupstartblock), "IF_BLOCK_INLINE"], "postprocess": (data) => { return data[1]; }},
192 {"name": "CLAUSE_ITEM", "symbols": [(lexer.has("markupstartblock") ? {type: "markupstartblock"} : markupstartblock), "WITH_BLOCK_INLINE"], "postprocess": (data) => { return data[1]; }},
193 {"name": "CLAUSE_ITEM", "symbols": [(lexer.has("markupexpr") ? {type: "markupexpr"} : markupexpr), "CLAUSE_EXPR"], "postprocess": (data) => { return data[1]; }},
194 {"name": "CLAUSE_ITEM", "symbols": ["VARIABLE"], "postprocess": id},
195 {"name": "CLAUSE_EXPR", "symbols": [(lexer.has("exprend") ? {type: "exprend"} : exprend)], "postprocess": (data) => {
196 return {
197 type: 'Expr'
198 }
199 }
200 },
201 {"name": "CLAUSE_BLOCK_INLINE", "symbols": [(lexer.has("startclauseid") ? {type: "startclauseid"} : startclauseid), (lexer.has("startblockspace") ? {type: "startblockspace"} : startblockspace), (lexer.has("startblockid") ? {type: "startblockid"} : startblockid), (lexer.has("startblockend") ? {type: "startblockend"} : startblockend), "CLAUSE_TEMPLATE", (lexer.has("markupendblock") ? {type: "markupendblock"} : markupendblock), (lexer.has("endclauseid") ? {type: "endclauseid"} : endclauseid), (lexer.has("endblockend") ? {type: "endblockend"} : endblockend)], "postprocess": (data,l,reject) => {
202 // Check that opening and closing clause tags match
203 // Note: this line makes the parser non-context-free
204 return {
205 type: 'ClauseBinding',
206 template: data[4],
207 fieldName: data[2]
208 }
209 }
210 },
211 {"name": "WITH_BLOCK_INLINE", "symbols": [(lexer.has("startwithid") ? {type: "startwithid"} : startwithid), (lexer.has("startblockspace") ? {type: "startblockspace"} : startblockspace), (lexer.has("startblockid") ? {type: "startblockid"} : startblockid), (lexer.has("startblockend") ? {type: "startblockend"} : startblockend), "CLAUSE_TEMPLATE", (lexer.has("markupendblock") ? {type: "markupendblock"} : markupendblock), (lexer.has("endwithid") ? {type: "endwithid"} : endwithid), (lexer.has("endblockend") ? {type: "endblockend"} : endblockend)], "postprocess": (data,l,reject) => {
212 // Check that opening and closing clause tags match
213 // Note: this line makes the parser non-context-free
214 return {
215 type: 'WithBinding',
216 template: data[4],
217 fieldName: data[2]
218 }
219 }
220 },
221 {"name": "CLAUSE_BLOCK_EXTERNAL$ebnf$1", "symbols": [(lexer.has("startrefspace") ? {type: "startrefspace"} : startrefspace)], "postprocess": id},
222 {"name": "CLAUSE_BLOCK_EXTERNAL$ebnf$1", "symbols": [], "postprocess": function(d) {return null;}},
223 {"name": "CLAUSE_BLOCK_EXTERNAL", "symbols": ["CLAUSE_BLOCK_EXTERNAL$ebnf$1", (lexer.has("startrefid") ? {type: "startrefid"} : startrefid), (lexer.has("startrefend") ? {type: "startrefend"} : startrefend)], "postprocess": (data) => {
224 return {
225 type: 'ClauseExternalBinding',
226 fieldName: data[1]
227 }
228 }
229 },
230 {"name": "ULIST_BLOCK_INLINE", "symbols": [(lexer.has("startulistid") ? {type: "startulistid"} : startulistid), (lexer.has("startblockspace") ? {type: "startblockspace"} : startblockspace), (lexer.has("startblockid") ? {type: "startblockid"} : startblockid), (lexer.has("startblockend") ? {type: "startblockend"} : startblockend), "CLAUSE_TEMPLATE", (lexer.has("markupendblock") ? {type: "markupendblock"} : markupendblock), (lexer.has("endulistid") ? {type: "endulistid"} : endulistid), (lexer.has("endblockend") ? {type: "endblockend"} : endblockend)], "postprocess": (data,l,reject) => {
231 // Check that opening and closing clause tags match
232 // Note: this line makes the parser non-context-free
233 return {
234 type: 'UListBinding',
235 template: data[4],
236 fieldName: data[2]
237 }
238 }
239 },
240 {"name": "OLIST_BLOCK_INLINE", "symbols": [(lexer.has("startolistid") ? {type: "startolistid"} : startolistid), (lexer.has("startblockspace") ? {type: "startblockspace"} : startblockspace), (lexer.has("startblockid") ? {type: "startblockid"} : startblockid), (lexer.has("startblockend") ? {type: "startblockend"} : startblockend), "CLAUSE_TEMPLATE", (lexer.has("markupendblock") ? {type: "markupendblock"} : markupendblock), (lexer.has("endolistid") ? {type: "endolistid"} : endolistid), (lexer.has("endblockend") ? {type: "endblockend"} : endblockend)], "postprocess": (data,l,reject) => {
241 // Check that opening and closing clause tags match
242 // Note: this line makes the parser non-context-free
243 return {
244 type: 'OListBinding',
245 template: data[4],
246 fieldName: data[2]
247 }
248 }
249 },
250 {"name": "JOIN_BLOCK_INLINE", "symbols": [(lexer.has("startjoinid") ? {type: "startjoinid"} : startjoinid), (lexer.has("startblockspace") ? {type: "startblockspace"} : startblockspace), (lexer.has("startblockid") ? {type: "startblockid"} : startblockid), (lexer.has("startblockspace") ? {type: "startblockspace"} : startblockspace), (lexer.has("startblockstring") ? {type: "startblockstring"} : startblockstring), (lexer.has("startblockend") ? {type: "startblockend"} : startblockend), "CLAUSE_TEMPLATE", (lexer.has("markupendblock") ? {type: "markupendblock"} : markupendblock), (lexer.has("endjoinid") ? {type: "endjoinid"} : endjoinid), (lexer.has("endblockend") ? {type: "endblockend"} : endblockend)], "postprocess": (data,l,reject) => {
251 // Check that opening and closing clause tags match
252 // Note: this line makes the parser non-context-free
253 return {
254 type: 'JoinBinding',
255 template: data[6],
256 separator: JSON.parse(data[4].value),
257 fieldName: data[2]
258 }
259 }
260 },
261 {"name": "IF_BLOCK_INLINE", "symbols": [(lexer.has("startifid") ? {type: "startifid"} : startifid), (lexer.has("startblockspace") ? {type: "startblockspace"} : startblockspace), (lexer.has("startblockid") ? {type: "startblockid"} : startblockid), (lexer.has("startblockend") ? {type: "startblockend"} : startblockend), (lexer.has("Chunk") ? {type: "Chunk"} : Chunk), (lexer.has("markupendblock") ? {type: "markupendblock"} : markupendblock), (lexer.has("endifid") ? {type: "endifid"} : endifid), (lexer.has("endblockend") ? {type: "endblockend"} : endblockend)], "postprocess": (data,l,reject) => {
262 // Check that opening and closing clause tags match
263 // Note: this line makes the parser non-context-free
264 return {
265 type: 'IfBinding',
266 stringIf: data[4],
267 fieldName: data[2]
268 }
269 }
270 },
271 {"name": "IF_BLOCK_INLINE", "symbols": [(lexer.has("startifid") ? {type: "startifid"} : startifid), (lexer.has("startblockspace") ? {type: "startblockspace"} : startblockspace), (lexer.has("startblockid") ? {type: "startblockid"} : startblockid), (lexer.has("startblockend") ? {type: "startblockend"} : startblockend), (lexer.has("Chunk") ? {type: "Chunk"} : Chunk), (lexer.has("varelseid") ? {type: "varelseid"} : varelseid), (lexer.has("varend") ? {type: "varend"} : varend), (lexer.has("Chunk") ? {type: "Chunk"} : Chunk), (lexer.has("markupendblock") ? {type: "markupendblock"} : markupendblock), (lexer.has("endifid") ? {type: "endifid"} : endifid), (lexer.has("endblockend") ? {type: "endblockend"} : endblockend)], "postprocess": (data,l,reject) => {
272 // Check that opening and closing clause tags match
273 // Note: this line makes the parser non-context-free
274 return {
275 type: 'IfElseBinding',
276 stringIf: data[4],
277 stringElse: data[7],
278 fieldName: data[2]
279 }
280 }
281 },
282 {"name": "VARIABLE", "symbols": ["FORMATTED_BINDING"], "postprocess": id},
283 {"name": "VARIABLE", "symbols": ["BINDING"], "postprocess": id},
284 {"name": "FORMATTED_BINDING", "symbols": [(lexer.has("varid") ? {type: "varid"} : varid), (lexer.has("varspace") ? {type: "varspace"} : varspace), (lexer.has("varas") ? {type: "varas"} : varas), (lexer.has("varspace") ? {type: "varspace"} : varspace), (lexer.has("varstring") ? {type: "varstring"} : varstring), (lexer.has("varend") ? {type: "varend"} : varend)], "postprocess": (data) => {
285 return {
286 type: 'FormattedBinding',
287 fieldName: data[0],
288 format: data[4],
289 };
290 }
291 },
292 {"name": "BINDING", "symbols": [(lexer.has("varid") ? {type: "varid"} : varid), (lexer.has("varend") ? {type: "varend"} : varend)], "postprocess": (data) => {
293 return {
294 type: 'Binding',
295 fieldName: data[0]
296 };
297 }
298 }
299]
300 , ParserStart: "TEMPLATE"
301}
302if (typeof module !== 'undefined'&& typeof module.exports !== 'undefined') {
303 module.exports = grammar;
304} else {
305 window.grammar = grammar;
306}
307})();
308
\No newline at end of file