1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | 'use strict';
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 | exports.makeLexer = makeLexer;
|
44 |
|
45 |
|
46 | const TOP_LEVEL_DELIMITER = new RegExp(
|
47 |
|
48 | '--' +
|
49 |
|
50 | '|/[*]' +
|
51 |
|
52 |
|
53 |
|
54 | '|[$](?:[a-zA-Z_][a-zA-Z_0-9]*)?[$]' +
|
55 |
|
56 | '|(?:[Uu]&)?"' +
|
57 |
|
58 | '|(?:[Uu]&|[EeBbXx])?\'');
|
59 |
|
60 | const LINE_COMMENT_BODY = /^[^\r\n]*/;
|
61 |
|
62 | const BLOCK_COMMENT_TOKEN = /[*][/]|[/][*]/;
|
63 |
|
64 | const ESC_DQ_STRING_BODY = /^(?:[^"\\]|""|\\.)*(")?/;
|
65 | const ESC_SQ_STRING_BODY = /^(?:[^'\\]|''|\\.)*(')?/;
|
66 |
|
67 | const SIMPLE_DQ_STRING_BODY = /^(?:[^"]|"")*(")?/;
|
68 | const SIMPLE_SQ_STRING_BODY = /^(?:[^']|'')*(')?/;
|
69 |
|
70 | const ESC_STRING_CONTINUATION = /^[\t\n\r ]*([/][*]|--|')?/;
|
71 |
|
72 | const STRING_BODIES = {
|
73 | __proto__: null,
|
74 | '"': SIMPLE_DQ_STRING_BODY,
|
75 | 'u&"': ESC_DQ_STRING_BODY,
|
76 | '\'': SIMPLE_SQ_STRING_BODY,
|
77 | 'b\'': SIMPLE_SQ_STRING_BODY,
|
78 | 'e\'': ESC_SQ_STRING_BODY,
|
79 | 'u&\'': ESC_SQ_STRING_BODY,
|
80 | 'x\'': SIMPLE_SQ_STRING_BODY,
|
81 | };
|
82 |
|
83 | const LAST_DELIMITER_CHARACTER_TO_HANDLER = {
|
84 | '-': (delimiter, chunk) => {
|
85 |
|
86 | const match = LINE_COMMENT_BODY.exec(chunk);
|
87 | const remainder = chunk.substring(match[0].length);
|
88 | if (remainder) {
|
89 | return [ null, remainder ];
|
90 | }
|
91 | throw new Error(`Unterminated line comment: --${ chunk }`);
|
92 | },
|
93 | '*': (delimiter, chunk) => {
|
94 |
|
95 | let depth = delimiter.length / 2;
|
96 | let remainder = chunk;
|
97 | while (remainder) {
|
98 | const match = BLOCK_COMMENT_TOKEN.exec(remainder);
|
99 | if (!match) {
|
100 | break;
|
101 | }
|
102 | remainder = remainder.substring(match.index + 2);
|
103 | if (match[0] === '/*') {
|
104 | ++depth;
|
105 | } else {
|
106 |
|
107 | --depth;
|
108 | if (!depth) {
|
109 | break;
|
110 | }
|
111 | }
|
112 | }
|
113 | if (depth) {
|
114 | throw new Error(`Unterminated block comment: /*${ chunk }`);
|
115 | }
|
116 | return [ null, remainder ];
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 | },
|
123 | '"': (delimiter, chunk) => {
|
124 | const match = STRING_BODIES[delimiter].exec(chunk);
|
125 | const remainder = chunk.substring(match[0].length);
|
126 | if (match[1]) {
|
127 | return [ null, remainder ];
|
128 | }
|
129 | if (match[0]) {
|
130 | return [ delimiter, remainder ];
|
131 | }
|
132 | throw new Error(`Incomplete escape sequence in ${ delimiter } delimited string at \`${ chunk }\``);
|
133 | },
|
134 | '\'': (delimiter, chunk) => {
|
135 | const match = STRING_BODIES[delimiter].exec(chunk);
|
136 | const remainder = chunk.substring(match[0].length);
|
137 | if (match[1]) {
|
138 | return [
|
139 |
|
140 |
|
141 |
|
142 | (delimiter === 'e\'' || delimiter === 'E\'') ? 'e' : null,
|
143 | remainder,
|
144 | ];
|
145 | }
|
146 | if (match[0]) {
|
147 | return [ delimiter, remainder ];
|
148 | }
|
149 | throw new Error(`Incomplete escape sequence in ${ delimiter } delimited string at \`${ chunk }\``);
|
150 | },
|
151 | '$': (delimiter, chunk) => {
|
152 |
|
153 | const i = chunk.indexOf(delimiter);
|
154 | if (i >= 0) {
|
155 | return [ null, chunk.substring(i + delimiter.length) ];
|
156 | }
|
157 | const lastDollar = chunk.lastIndexOf('$');
|
158 | if (lastDollar >= 0) {
|
159 | const suffix = chunk.substring(lastDollar);
|
160 | if (delimiter.indexOf(suffix) === 0) {
|
161 |
|
162 | throw new Error(`merge hazard '${ suffix }' at end of ${ delimiter } delimited string`);
|
163 | }
|
164 | }
|
165 | return [ delimiter, '' ];
|
166 | },
|
167 |
|
168 | 'e': (delimiter, chunk) => {
|
169 | let remainder = chunk;
|
170 | while (remainder) {
|
171 | const match = ESC_STRING_CONTINUATION.exec(remainder);
|
172 | let [ consumed, subdelim ] = match;
|
173 | if (!consumed) {
|
174 | return [ null, remainder ];
|
175 | }
|
176 | remainder = remainder.substring(consumed.length);
|
177 | if (subdelim) {
|
178 | if (subdelim === '\'') {
|
179 | return [ 'e\'', remainder ];
|
180 | }
|
181 | while (remainder && subdelim) {
|
182 | const handler = LAST_DELIMITER_CHARACTER_TO_HANDLER[subdelim[subdelim.length - 1]];
|
183 | [ subdelim, remainder ] = handler(subdelim, remainder);
|
184 | }
|
185 | }
|
186 | }
|
187 | return [ delimiter, remainder ];
|
188 | },
|
189 | };
|
190 |
|
191 | function replayError(fun) {
|
192 | let message = null;
|
193 | return (...args) => {
|
194 | if (message !== null) {
|
195 | throw new Error(message);
|
196 | }
|
197 | try {
|
198 | return fun(...args);
|
199 | } catch (exc) {
|
200 | message = `${ exc.message }`;
|
201 | throw exc;
|
202 | }
|
203 | };
|
204 | }
|
205 |
|
206 | function makeLexer() {
|
207 | let delimiter = null;
|
208 | let continuationAmbiguity = false;
|
209 | let chunkIndex = -1;
|
210 |
|
211 | function consumeFromLeft(remainder) {
|
212 | if (delimiter) {
|
213 | const lastChar = delimiter[delimiter.length - 1];
|
214 | if (lastChar !== '*' && lastChar !== '-') {
|
215 | continuationAmbiguity = false;
|
216 | }
|
217 | const handler = LAST_DELIMITER_CHARACTER_TO_HANDLER[lastChar];
|
218 | ([ delimiter, remainder ] = handler(delimiter, remainder));
|
219 | } else {
|
220 | const match = TOP_LEVEL_DELIMITER.exec(remainder);
|
221 | if (continuationAmbiguity) {
|
222 | const end = match ? match.index : remainder.length;
|
223 | if (/[^\t\n\r ]/.test(remainder.substring(0, end))) {
|
224 | continuationAmbiguity = false;
|
225 | }
|
226 | }
|
227 | if (!match) {
|
228 | return '';
|
229 | }
|
230 | [ delimiter ] = match;
|
231 | if (delimiter[0] !== '$') {
|
232 |
|
233 |
|
234 |
|
235 |
|
236 |
|
237 |
|
238 | delimiter = delimiter.toLowerCase();
|
239 | }
|
240 | remainder = remainder.substring(match.index + delimiter.length);
|
241 | }
|
242 | return remainder;
|
243 | }
|
244 |
|
245 | function lexer(chunk) {
|
246 | if (chunk === null) {
|
247 | if (delimiter && delimiter !== 'e') {
|
248 | throw new Error(`Unclosed quoted string: ${ delimiter }`);
|
249 | }
|
250 | return delimiter;
|
251 | }
|
252 |
|
253 | ++chunkIndex;
|
254 |
|
255 | if (continuationAmbiguity && chunkIndex > 1) {
|
256 |
|
257 |
|
258 |
|
259 |
|
260 |
|
261 |
|
262 |
|
263 |
|
264 |
|
265 |
|
266 |
|
267 |
|
268 |
|
269 |
|
270 |
|
271 |
|
272 |
|
273 |
|
274 |
|
275 |
|
276 |
|
277 | throw new Error(
|
278 |
|
279 | 'Potential for ambiguous string continuation at `${ chunk }`.' +
|
280 | ' If you need string continuation start with an e\'...\' string.');
|
281 | }
|
282 |
|
283 | let remainder = `${ chunk }`;
|
284 | continuationAmbiguity = /[\n\r]/.test(chunk);
|
285 | while (remainder) {
|
286 | remainder = consumeFromLeft(remainder);
|
287 | }
|
288 | return delimiter;
|
289 | }
|
290 |
|
291 | return replayError(lexer);
|
292 | }
|
293 |
|
294 | module.exports.makeLexer = makeLexer;
|