UNPKG

9.46 kBJavaScriptView Raw
1// Generated automatically by nearley, version 2.15.1
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: 'var',
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 var: {
48varend: {
49 match: '}]',
50 pop: true
51 }, // pop back to main state
52 varas: 'as',
53 varid: /[a-zA-Z_][_a-zA-Z0-9]*/,
54 varstring: /".*?"/,
55 varcond: ':?',
56 varspace: ' ',
57 clauseidstart: {
58 match: /#[a-zA-Z_][_a-zA-Z0-9]*/,
59 value: x => x.slice(1)
60 },
61 clauseidend: {
62 match: /\/[a-zA-Z_][_a-zA-Z0-9]*/,
63 value: x => x.slice(1)
64 },
65 clauseclose: /\//
66 },
67});
68var grammar = {
69 Lexer: lexer,
70 ParserRules: [
71 {"name": "_$ebnf$1", "symbols": []},
72 {"name": "_$ebnf$1", "symbols": ["_$ebnf$1", "wschar"], "postprocess": function arrpush(d) {return d[0].concat([d[1]]);}},
73 {"name": "_", "symbols": ["_$ebnf$1"], "postprocess": function(d) {return null;}},
74 {"name": "__$ebnf$1", "symbols": ["wschar"]},
75 {"name": "__$ebnf$1", "symbols": ["__$ebnf$1", "wschar"], "postprocess": function arrpush(d) {return d[0].concat([d[1]]);}},
76 {"name": "__", "symbols": ["__$ebnf$1"], "postprocess": function(d) {return null;}},
77 {"name": "wschar", "symbols": [/[ \t\n\v\f]/], "postprocess": id},
78 {"name": "dqstring$ebnf$1", "symbols": []},
79 {"name": "dqstring$ebnf$1", "symbols": ["dqstring$ebnf$1", "dstrchar"], "postprocess": function arrpush(d) {return d[0].concat([d[1]]);}},
80 {"name": "dqstring", "symbols": [{"literal":"\""}, "dqstring$ebnf$1", {"literal":"\""}], "postprocess": function(d) {return d[1].join(""); }},
81 {"name": "sqstring$ebnf$1", "symbols": []},
82 {"name": "sqstring$ebnf$1", "symbols": ["sqstring$ebnf$1", "sstrchar"], "postprocess": function arrpush(d) {return d[0].concat([d[1]]);}},
83 {"name": "sqstring", "symbols": [{"literal":"'"}, "sqstring$ebnf$1", {"literal":"'"}], "postprocess": function(d) {return d[1].join(""); }},
84 {"name": "btstring$ebnf$1", "symbols": []},
85 {"name": "btstring$ebnf$1", "symbols": ["btstring$ebnf$1", /[^`]/], "postprocess": function arrpush(d) {return d[0].concat([d[1]]);}},
86 {"name": "btstring", "symbols": [{"literal":"`"}, "btstring$ebnf$1", {"literal":"`"}], "postprocess": function(d) {return d[1].join(""); }},
87 {"name": "dstrchar", "symbols": [/[^\\"\n]/], "postprocess": id},
88 {"name": "dstrchar", "symbols": [{"literal":"\\"}, "strescape"], "postprocess":
89 function(d) {
90 return JSON.parse("\""+d.join("")+"\"");
91 }
92 },
93 {"name": "sstrchar", "symbols": [/[^\\'\n]/], "postprocess": id},
94 {"name": "sstrchar", "symbols": [{"literal":"\\"}, "strescape"], "postprocess": function(d) { return JSON.parse("\""+d.join("")+"\""); }},
95 {"name": "sstrchar$string$1", "symbols": [{"literal":"\\"}, {"literal":"'"}], "postprocess": function joiner(d) {return d.join('');}},
96 {"name": "sstrchar", "symbols": ["sstrchar$string$1"], "postprocess": function(d) {return "'"; }},
97 {"name": "strescape", "symbols": [/["\\\/bfnrt]/], "postprocess": id},
98 {"name": "strescape", "symbols": [{"literal":"u"}, /[a-fA-F0-9]/, /[a-fA-F0-9]/, /[a-fA-F0-9]/, /[a-fA-F0-9]/], "postprocess":
99 function(d) {
100 return d.join("");
101 }
102 },
103 {"name": "TEMPLATE", "symbols": ["CONTRACT_TEMPLATE"], "postprocess": id},
104 {"name": "CONTRACT_TEMPLATE$ebnf$1", "symbols": []},
105 {"name": "CONTRACT_TEMPLATE$ebnf$1", "symbols": ["CONTRACT_TEMPLATE$ebnf$1", "CONTRACT_ITEM"], "postprocess": function arrpush(d) {return d[0].concat([d[1]]);}},
106 {"name": "CONTRACT_TEMPLATE$ebnf$2", "symbols": [(lexer.has("LastChunk") ? {type: "LastChunk"} : LastChunk)], "postprocess": id},
107 {"name": "CONTRACT_TEMPLATE$ebnf$2", "symbols": [], "postprocess": function(d) {return null;}},
108 {"name": "CONTRACT_TEMPLATE", "symbols": ["CONTRACT_TEMPLATE$ebnf$1", "CONTRACT_TEMPLATE$ebnf$2"], "postprocess": (data) => {
109 return {
110 type: 'ContractTemplate',
111 data: flatten(data)
112 };
113 }
114 },
115 {"name": "CLAUSE_TEMPLATE$ebnf$1", "symbols": []},
116 {"name": "CLAUSE_TEMPLATE$ebnf$1", "symbols": ["CLAUSE_TEMPLATE$ebnf$1", "CLAUSE_ITEM"], "postprocess": function arrpush(d) {return d[0].concat([d[1]]);}},
117 {"name": "CLAUSE_TEMPLATE$ebnf$2", "symbols": [(lexer.has("LastChunk") ? {type: "LastChunk"} : LastChunk)], "postprocess": id},
118 {"name": "CLAUSE_TEMPLATE$ebnf$2", "symbols": [], "postprocess": function(d) {return null;}},
119 {"name": "CLAUSE_TEMPLATE", "symbols": ["CLAUSE_TEMPLATE$ebnf$1", "CLAUSE_TEMPLATE$ebnf$2"], "postprocess": (data) => {
120 return {
121 type: 'ClauseTemplate',
122 data: flatten(data)
123 };
124 }
125 },
126 {"name": "CONTRACT_ITEM", "symbols": [(lexer.has("Chunk") ? {type: "Chunk"} : Chunk)], "postprocess": id},
127 {"name": "CONTRACT_ITEM", "symbols": ["VARIABLE"], "postprocess": id},
128 {"name": "CONTRACT_ITEM", "symbols": ["CLAUSE_VARIABLE_INLINE"], "postprocess": id},
129 {"name": "CONTRACT_ITEM", "symbols": ["CLAUSE_VARIABLE_EXTERNAL"], "postprocess": id},
130 {"name": "CLAUSE_ITEM", "symbols": [(lexer.has("Chunk") ? {type: "Chunk"} : Chunk)], "postprocess": id},
131 {"name": "CLAUSE_ITEM", "symbols": ["VARIABLE"], "postprocess": id},
132 {"name": "CLAUSE_VARIABLE_INLINE", "symbols": [(lexer.has("clauseidstart") ? {type: "clauseidstart"} : clauseidstart), (lexer.has("varend") ? {type: "varend"} : varend), "CLAUSE_TEMPLATE", (lexer.has("clauseidend") ? {type: "clauseidend"} : clauseidend), (lexer.has("varend") ? {type: "varend"} : varend)], "postprocess": (data,l,reject) => {
133 // Check that opening and closing clause tags match
134 // Note: this line makes the parser non-context-free
135 if(data[0].value !== data[3].value) {
136 return reject;
137 } else {
138 return {
139 type: 'ClauseBinding',
140 template: data[2],
141 fieldName: data[0]
142 }
143 }
144 }
145 },
146 {"name": "CLAUSE_VARIABLE_EXTERNAL", "symbols": [(lexer.has("clauseidstart") ? {type: "clauseidstart"} : clauseidstart), (lexer.has("clauseclose") ? {type: "clauseclose"} : clauseclose), (lexer.has("varend") ? {type: "varend"} : varend)], "postprocess": (data) => {
147 return {
148 type: 'ClauseExternalBinding',
149 fieldName: data[0]
150 }
151 }
152 },
153 {"name": "VARIABLE", "symbols": ["FORMATTED_BINDING"], "postprocess": id},
154 {"name": "VARIABLE", "symbols": ["BOOLEAN_BINDING"], "postprocess": id},
155 {"name": "VARIABLE", "symbols": ["BINDING"], "postprocess": id},
156 {"name": "BOOLEAN_BINDING", "symbols": [(lexer.has("varstring") ? {type: "varstring"} : varstring), (lexer.has("varcond") ? {type: "varcond"} : varcond), (lexer.has("varspace") ? {type: "varspace"} : varspace), (lexer.has("varid") ? {type: "varid"} : varid), (lexer.has("varend") ? {type: "varend"} : varend)], "postprocess": (data) => {
157 return {
158 type: 'BooleanBinding',
159 string: data[0],
160 fieldName: data[3]
161 };
162 }
163 },
164 {"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) => {
165 return {
166 type: 'FormattedBinding',
167 fieldName: data[0],
168 format: data[4],
169 };
170 }
171 },
172 {"name": "BINDING", "symbols": [(lexer.has("varid") ? {type: "varid"} : varid), (lexer.has("varend") ? {type: "varend"} : varend)], "postprocess": (data) => {
173 return {
174 type: 'Binding',
175 fieldName: data[0]
176 };
177 }
178 }
179]
180 , ParserStart: "TEMPLATE"
181}
182if (typeof module !== 'undefined'&& typeof module.exports !== 'undefined') {
183 module.exports = grammar;
184} else {
185 window.grammar = grammar;
186}
187})();
188
\No newline at end of file