sfmc-handlebars-parser 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +7 -3
- package/package.json +3 -2
- package/src/errors.js +3 -3
- package/src/expression.js +92 -9
- package/src/index.js +89 -25
package/README.md
CHANGED
|
@@ -46,8 +46,9 @@ It accepts every construct `@handlebars/parser@2.2.2` accepts:
|
|
|
46
46
|
- Comments: `{{! }}` and `{{!-- --}}`.
|
|
47
47
|
- Blocks: `{{#x}}…{{/x}}`, inverse `{{^x}}…{{/x}}`, `{{else}}`, `{{else if …}}` chaining,
|
|
48
48
|
block params `as |a b|`, and empty block bodies.
|
|
49
|
-
- Paths: `this`, `./`, `../` (parent depth),
|
|
50
|
-
|
|
49
|
+
- Paths: `this`, `.` (current context), `..` (parent), `./`, `../` (parent depth), a
|
|
50
|
+
`{{...}}` mustache (parent path plus a current-context param), `@data` variables,
|
|
51
|
+
segment literals `[…]`, the legacy `/` separator, and identifiers containing `$` / `_`.
|
|
51
52
|
- Literals: strings (`"…"` / `'…'` with escapes), numbers (including negative),
|
|
52
53
|
`true` / `false` / `null` / `undefined`.
|
|
53
54
|
- Subexpressions `( … )` nested arbitrarily, and hash pairs `key=value`.
|
|
@@ -57,7 +58,10 @@ It accepts every construct `@handlebars/parser@2.2.2` accepts:
|
|
|
57
58
|
|
|
58
59
|
On a malformed template, `parse` throws. Parse errors mirror the two upstream error
|
|
59
60
|
classes: a Jison-class error carries `hash.loc` (`{first_line, first_column, last_line,
|
|
60
|
-
last_column}`); an exception-class error carries `lineNumber` / `column
|
|
61
|
+
last_column}`); an exception-class error carries `lineNumber` (1-based) / `column`
|
|
62
|
+
(0-based). The positions follow upstream. The messages are our own, one line each, and say
|
|
63
|
+
what is wrong, for example `Unclosed block {{#if}}: expected {{/if}}.`. Every error has a
|
|
64
|
+
position, including an expression that is still being typed.
|
|
61
65
|
|
|
62
66
|
```js
|
|
63
67
|
import { parse } from 'sfmc-handlebars-parser';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "sfmc-handlebars-parser",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Zero-dependency Handlebars parser producing the @handlebars/parser AST contract for SFMC tooling",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./src/index.js",
|
|
@@ -8,7 +8,8 @@
|
|
|
8
8
|
"exports": {
|
|
9
9
|
".": {
|
|
10
10
|
"types": "./src/index.d.ts",
|
|
11
|
-
"import": "./src/index.js"
|
|
11
|
+
"import": "./src/index.js",
|
|
12
|
+
"default": "./src/index.js"
|
|
12
13
|
}
|
|
13
14
|
},
|
|
14
15
|
"files": [
|
package/src/errors.js
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
* `first_column` and reports the real position.
|
|
12
12
|
* - {@link HandlebarsException} — the **exception-class** logic error (block-name mismatch,
|
|
13
13
|
* `Invalid path:`, lexical "Unrecognized text"). Carries NO `hash`; instead it MAY set
|
|
14
|
-
* `lineNumber` / `column` (1-based line,
|
|
14
|
+
* `lineNumber` / `column` (1-based line, 0-based column, matching upstream's `Exception`).
|
|
15
15
|
* When it carries neither (the lexical case), the wrapper falls back to reporting `1:1`.
|
|
16
16
|
*
|
|
17
17
|
* The golden error projection (`tests/golden/errors.baseline.json`) classifies an error as
|
|
@@ -53,9 +53,9 @@ export class HandlebarsException extends Error {
|
|
|
53
53
|
/**
|
|
54
54
|
* @param {string} message - The diagnostic message (the ` - line:column` suffix, if any, is
|
|
55
55
|
* expected to already be appended by the caller for the logic-error cases).
|
|
56
|
-
* @param {object} [position] - Optional 1-based line /
|
|
56
|
+
* @param {object} [position] - Optional 1-based line / 0-based column of the error.
|
|
57
57
|
* @param {number} [position.lineNumber] - 1-based line number.
|
|
58
|
-
* @param {number} [position.column] -
|
|
58
|
+
* @param {number} [position.column] - 0-based column number.
|
|
59
59
|
*/
|
|
60
60
|
constructor(message, position) {
|
|
61
61
|
super(message);
|
package/src/expression.js
CHANGED
|
@@ -26,7 +26,33 @@
|
|
|
26
26
|
*/
|
|
27
27
|
|
|
28
28
|
import { TokenType } from './tokenizer.js';
|
|
29
|
-
import { HandlebarsException } from './errors.js';
|
|
29
|
+
import { HandlebarsException, HandlebarsParseError } from './errors.js';
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Readable names of the tokens `expect()` can ask for, for error messages.
|
|
33
|
+
*/
|
|
34
|
+
const TOKEN_DESCRIPTIONS = {
|
|
35
|
+
CLOSE: "'}}'",
|
|
36
|
+
CLOSE_PAREN: "')'",
|
|
37
|
+
OPEN: "'{{'",
|
|
38
|
+
OPEN_PAREN: "'('",
|
|
39
|
+
EQUALS: "'='",
|
|
40
|
+
PIPE: "'|'",
|
|
41
|
+
ID: 'a name',
|
|
42
|
+
SIGIL: 'a block sigil',
|
|
43
|
+
RAW_CLOSE: "'}}}}'",
|
|
44
|
+
RAW_OPEN: "'{{{{'",
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Readable name of a token type.
|
|
49
|
+
*
|
|
50
|
+
* @param {string} type - A {@link import('./tokenizer.js').TokenType} value.
|
|
51
|
+
* @returns {string} The description used in error messages.
|
|
52
|
+
*/
|
|
53
|
+
function describeToken(type) {
|
|
54
|
+
return TOKEN_DESCRIPTIONS[type] ?? type;
|
|
55
|
+
}
|
|
30
56
|
import { offsetToPosition } from './loc.js';
|
|
31
57
|
import {
|
|
32
58
|
booleanLiteral,
|
|
@@ -121,8 +147,9 @@ export function createReader(tokens, table, source) {
|
|
|
121
147
|
},
|
|
122
148
|
|
|
123
149
|
/**
|
|
124
|
-
* Consumes the current token, asserting its `type`. Throws a
|
|
125
|
-
*
|
|
150
|
+
* Consumes the current token, asserting its `type`. Throws a {@link HandlebarsParseError}
|
|
151
|
+
* on the unexpected token (or the end of the source) when the type does not match. This
|
|
152
|
+
* position is best effort: it is not part of the upstream parity gate.
|
|
126
153
|
*
|
|
127
154
|
* @param {string} type - The expected {@link import('./tokenizer.js').TokenType}.
|
|
128
155
|
* @returns {object} The consumed token.
|
|
@@ -130,8 +157,22 @@ export function createReader(tokens, table, source) {
|
|
|
130
157
|
expect(type) {
|
|
131
158
|
const token = tokens[reader.index];
|
|
132
159
|
if (!token || token.type !== type) {
|
|
133
|
-
|
|
134
|
-
|
|
160
|
+
const isAtEnd = !token || token.type === TokenType.EOF;
|
|
161
|
+
const startOffset = isAtEnd ? source.length : token.start;
|
|
162
|
+
const endOffset = isAtEnd ? source.length : token.end;
|
|
163
|
+
const found = isAtEnd
|
|
164
|
+
? 'the end of the template'
|
|
165
|
+
: `'${source.slice(startOffset, endOffset)}'`;
|
|
166
|
+
const start = offsetToPosition(table, startOffset);
|
|
167
|
+
const end = offsetToPosition(table, endOffset);
|
|
168
|
+
throw new HandlebarsParseError(
|
|
169
|
+
`Expected ${describeToken(type)} but found ${found}.`,
|
|
170
|
+
{
|
|
171
|
+
first_line: start.line,
|
|
172
|
+
first_column: start.column,
|
|
173
|
+
last_line: end.line,
|
|
174
|
+
last_column: end.column,
|
|
175
|
+
},
|
|
135
176
|
);
|
|
136
177
|
}
|
|
137
178
|
reader.index++;
|
|
@@ -198,10 +239,13 @@ function readContiguousSegment(reader) {
|
|
|
198
239
|
* Parses a single `PathExpression` starting at the cursor (leading whitespace is skipped first).
|
|
199
240
|
*
|
|
200
241
|
* Grammar handled: an optional `@` data prefix; leading `../` parent hops (each `..` increments
|
|
201
|
-
* `depth`); a
|
|
202
|
-
*
|
|
203
|
-
*
|
|
204
|
-
*
|
|
242
|
+
* `depth`); a bare `..` parent-context path; a bare `.` current-context path; a `this` context
|
|
243
|
+
* head; `.`- and legacy `/`-separated segments; bare-word `ID` segments (including `$`/`_`/`-`
|
|
244
|
+
* ids); and `[segment literal]` parts whose inner text is kept verbatim. Adjacent extra `.`/`..`
|
|
245
|
+
* tokens that do not continue this path (e.g. the trailing `.` of `{{...}}`) are left unconsumed
|
|
246
|
+
* so the caller can parse them as the next expression. `original` is the raw path text with
|
|
247
|
+
* segment-literal brackets removed and every separator kept. See {@link pathExpression} for how
|
|
248
|
+
* `head`/`tail`/`this` are derived.
|
|
205
249
|
*
|
|
206
250
|
* @param {object} reader - The shared cursor from {@link createReader}.
|
|
207
251
|
* @returns {object} A `PathExpression` node.
|
|
@@ -220,11 +264,14 @@ export function parsePath(reader) {
|
|
|
220
264
|
const parts = [];
|
|
221
265
|
let isSawThisHead = false;
|
|
222
266
|
let segmentCount = 0;
|
|
267
|
+
let isStarted = false;
|
|
268
|
+
let isLastWasSlash = false;
|
|
223
269
|
|
|
224
270
|
// Optional `@` data prefix.
|
|
225
271
|
if (reader.peekType() === TokenType.DATA) {
|
|
226
272
|
isData = true;
|
|
227
273
|
endOffset = reader.next().end;
|
|
274
|
+
isStarted = true;
|
|
228
275
|
}
|
|
229
276
|
|
|
230
277
|
// Walk segments and separators until the path can no longer continue.
|
|
@@ -249,8 +296,42 @@ export function parsePath(reader) {
|
|
|
249
296
|
},
|
|
250
297
|
);
|
|
251
298
|
}
|
|
299
|
+
// A second adjacent `..` that is not a `../` hop (e.g. `{{....}}`) is a new
|
|
300
|
+
// path, not more depth on this one. Leave it for the next expression.
|
|
301
|
+
if (isStarted && !isLastWasSlash) {
|
|
302
|
+
break;
|
|
303
|
+
}
|
|
252
304
|
depth++;
|
|
305
|
+
isLastWasSlash = false;
|
|
306
|
+
isStarted = true;
|
|
307
|
+
endOffset = token.end;
|
|
308
|
+
reader.next();
|
|
309
|
+
continue;
|
|
310
|
+
}
|
|
311
|
+
if (token.value === '.') {
|
|
312
|
+
const next = reader.peek(1);
|
|
313
|
+
const continues =
|
|
314
|
+
isLastWasSlash ||
|
|
315
|
+
(next && next.type === TokenType.SEP && next.value === '/') ||
|
|
316
|
+
(next && SEGMENT_TOKEN_TYPES.has(next.type) && isStarted);
|
|
317
|
+
if (!continues) {
|
|
318
|
+
// Standalone `.` is current-context (`{{.}}`). A trailing `.` after an
|
|
319
|
+
// already-started path (`{{...}}`, `{{foo.}}`) is a new path — leave it.
|
|
320
|
+
if (!isStarted) {
|
|
321
|
+
endOffset = token.end;
|
|
322
|
+
reader.next();
|
|
323
|
+
}
|
|
324
|
+
break;
|
|
325
|
+
}
|
|
326
|
+
isLastWasSlash = false;
|
|
327
|
+
isStarted = true;
|
|
328
|
+
endOffset = token.end;
|
|
329
|
+
reader.next();
|
|
330
|
+
continue;
|
|
253
331
|
}
|
|
332
|
+
// `/` separator (`./`, `../`, `foo/bar`).
|
|
333
|
+
isLastWasSlash = true;
|
|
334
|
+
isStarted = true;
|
|
254
335
|
endOffset = token.end;
|
|
255
336
|
reader.next();
|
|
256
337
|
continue;
|
|
@@ -261,6 +342,8 @@ export function parsePath(reader) {
|
|
|
261
342
|
// contiguous run as one path segment (`parts: ["0x1"]`).
|
|
262
343
|
const segment = readContiguousSegment(reader);
|
|
263
344
|
endOffset = segment.endOffset;
|
|
345
|
+
isLastWasSlash = false;
|
|
346
|
+
isStarted = true;
|
|
264
347
|
if (segmentCount === 0 && token.type === TokenType.ID && segment.value === 'this') {
|
|
265
348
|
// A `this` head is not a part; it only sets the `this` flag (when a tail follows).
|
|
266
349
|
isSawThisHead = true;
|
package/src/index.js
CHANGED
|
@@ -87,7 +87,12 @@ export function parse(code) {
|
|
|
87
87
|
// A well-formed block-free template consumes every token up to EOF.
|
|
88
88
|
if (!reader.atEnd()) {
|
|
89
89
|
const token = reader.peek();
|
|
90
|
-
throw jisonAt(
|
|
90
|
+
throw jisonAt(
|
|
91
|
+
lineTable,
|
|
92
|
+
token.start,
|
|
93
|
+
token.end,
|
|
94
|
+
`Unexpected '${code.slice(token.start, token.end)}'.`,
|
|
95
|
+
);
|
|
91
96
|
}
|
|
92
97
|
// Upstream spans the Program from its FIRST child's start to its LAST child's end (the
|
|
93
98
|
// per-node `loc`s already account for escaped content trimming a leading `\`). An empty
|
|
@@ -158,7 +163,7 @@ export function parseProgram(reader, isTerminator) {
|
|
|
158
163
|
// an unexpected INVERSE token at statement level. Upstream throws a Jison parse
|
|
159
164
|
// error at `1:0`; reproduce that here (W2b-1 owns bare-caret).
|
|
160
165
|
if (sigil.value === '^' && isBareInverse(reader)) {
|
|
161
|
-
throw jisonAt(reader.table, 0, 0);
|
|
166
|
+
throw jisonAt(reader.table, 0, 0, '{{^}} is only allowed inside a block.');
|
|
162
167
|
}
|
|
163
168
|
body.push(parseBlockLike(reader));
|
|
164
169
|
continue;
|
|
@@ -168,13 +173,23 @@ export function parseProgram(reader, isTerminator) {
|
|
|
168
173
|
// at the opener (`got 'INVERSE'`). Block bodies consume their own `{{else}}` via the
|
|
169
174
|
// terminator, so this only fires for a stray/top-level else.
|
|
170
175
|
if (sigil && sigil.type === TokenType.ID && sigil.value === 'else') {
|
|
171
|
-
throw jisonAt(
|
|
176
|
+
throw jisonAt(
|
|
177
|
+
reader.table,
|
|
178
|
+
token.start,
|
|
179
|
+
token.start,
|
|
180
|
+
'{{else}} is only allowed inside a block.',
|
|
181
|
+
);
|
|
172
182
|
}
|
|
173
183
|
body.push(parseMustache(reader));
|
|
174
184
|
continue;
|
|
175
185
|
}
|
|
176
186
|
// Any other statement-level token here is unexpected (e.g. a stray CLOSE / SIGIL).
|
|
177
|
-
throw jisonAt(
|
|
187
|
+
throw jisonAt(
|
|
188
|
+
reader.table,
|
|
189
|
+
token.start,
|
|
190
|
+
token.end,
|
|
191
|
+
`Unexpected '${reader.source.slice(token.start, token.end)}'.`,
|
|
192
|
+
);
|
|
178
193
|
}
|
|
179
194
|
return body;
|
|
180
195
|
}
|
|
@@ -270,10 +285,25 @@ function parseMustache(reader) {
|
|
|
270
285
|
isEscaped = false;
|
|
271
286
|
reader.next();
|
|
272
287
|
}
|
|
288
|
+
// A closing tag without an open block (`{{/if}}`) reaches here as a plain mustache.
|
|
289
|
+
if (reader.peekType() === TokenType.SIGIL && reader.peek().value === '/') {
|
|
290
|
+
const name = /^\{\{~?\s*\/\s*([^\s}~]*)/.exec(reader.source.slice(open.start))?.[1] ?? '';
|
|
291
|
+
throw jisonAt(
|
|
292
|
+
reader.table,
|
|
293
|
+
open.start,
|
|
294
|
+
open.end,
|
|
295
|
+
`{{/${name}}} has no matching {{#${name}}} before it.`,
|
|
296
|
+
);
|
|
297
|
+
}
|
|
273
298
|
reader.skipWhitespace();
|
|
274
299
|
// Empty body (`{{ }}`) — upstream throws a Jison parse error anchored at the OPEN token.
|
|
275
300
|
if (reader.peekType() === TokenType.CLOSE) {
|
|
276
|
-
throw jisonAt(
|
|
301
|
+
throw jisonAt(
|
|
302
|
+
reader.table,
|
|
303
|
+
open.start,
|
|
304
|
+
open.end,
|
|
305
|
+
'Empty expression: write a helper or a value between the braces.',
|
|
306
|
+
);
|
|
277
307
|
}
|
|
278
308
|
const head = parseCallHead(reader, open);
|
|
279
309
|
const { params, hash } = parseParametersAndHash(
|
|
@@ -281,6 +311,17 @@ function parseMustache(reader) {
|
|
|
281
311
|
(token) => token.type === TokenType.CLOSE,
|
|
282
312
|
);
|
|
283
313
|
reader.skipWhitespace();
|
|
314
|
+
// A missing `}}` (an expression still being typed, `{{upp`) is reported on the opener, where
|
|
315
|
+
// upstream reports the single-word case; other shapes are best effort (not parity-gated).
|
|
316
|
+
if (reader.peekType() !== TokenType.CLOSE) {
|
|
317
|
+
const typed = /^\{\{\{?[^\s{}]*/.exec(reader.source.slice(open.start))?.[0] ?? '{{';
|
|
318
|
+
throw jisonAt(
|
|
319
|
+
reader.table,
|
|
320
|
+
open.start,
|
|
321
|
+
open.end,
|
|
322
|
+
`Unclosed expression ${typed}: expected '}}'.`,
|
|
323
|
+
);
|
|
324
|
+
}
|
|
284
325
|
const close = reader.expect(TokenType.CLOSE);
|
|
285
326
|
const strip = { open: !!open.strip, close: !!close.strip };
|
|
286
327
|
return mustacheStatement(
|
|
@@ -298,8 +339,9 @@ function parseMustache(reader) {
|
|
|
298
339
|
/**
|
|
299
340
|
* Parses the CALL HEAD of a mustache: a literal (`{{"str"}}` / `{{true}}` / `{{5}}` place the
|
|
300
341
|
* literal node in the `path` slot) when the cursor is on a standalone literal, otherwise a
|
|
301
|
-
* `PathExpression`.
|
|
302
|
-
*
|
|
342
|
+
* `PathExpression`. Current-context `.` and parent-context `..` (including `{{...}}`, which is
|
|
343
|
+
* path `..` plus a `.` param) are valid heads. A leading `.` followed by a segment (`{{.foo}}`)
|
|
344
|
+
* is not: upstream throws a Jison parse error anchored at the OPEN token.
|
|
303
345
|
*
|
|
304
346
|
* @param {object} reader - The shared cursor from {@link createReader}.
|
|
305
347
|
* @param {object} open - The consumed OPEN token (for error anchoring).
|
|
@@ -312,14 +354,26 @@ function parseCallHead(reader, open) {
|
|
|
312
354
|
}
|
|
313
355
|
reader.skipWhitespace();
|
|
314
356
|
const token = reader.peek();
|
|
315
|
-
// A path may begin with
|
|
316
|
-
//
|
|
317
|
-
//
|
|
357
|
+
// A path may begin with:
|
|
358
|
+
// - relative/parent context (`./`, `../`) — a `.`/`..` SEP immediately followed by `/`
|
|
359
|
+
// - parent-context `..` (`{{..}}`, and the head of `{{...}}`)
|
|
360
|
+
// - current-context `.` (`{{.}}`) when it is NOT followed by a segment
|
|
361
|
+
// A leading `.` SEP followed by an ID (`{{.foo}}`) is still invalid: upstream throws a
|
|
362
|
+
// Jison "got 'SEP'" error anchored at the OPEN token.
|
|
318
363
|
if (token && token.type === TokenType.SEP) {
|
|
319
364
|
const next = reader.peek(1);
|
|
320
|
-
const
|
|
321
|
-
|
|
322
|
-
|
|
365
|
+
const isSlashContinue = next && next.type === TokenType.SEP && next.value === '/';
|
|
366
|
+
const isParentContext = token.value === '..';
|
|
367
|
+
const nextIsSegment =
|
|
368
|
+
next && [TokenType.ID, TokenType.NUMBER, TokenType.SEGMENT].includes(next.type);
|
|
369
|
+
const isCurrentContext = token.value === '.' && !nextIsSegment;
|
|
370
|
+
if (!isSlashContinue && !isParentContext && !isCurrentContext) {
|
|
371
|
+
throw jisonAt(
|
|
372
|
+
reader.table,
|
|
373
|
+
open.start,
|
|
374
|
+
open.end,
|
|
375
|
+
"Invalid path: a name cannot follow a leading '.'.",
|
|
376
|
+
);
|
|
323
377
|
}
|
|
324
378
|
}
|
|
325
379
|
return parsePath(reader);
|
|
@@ -489,7 +543,7 @@ function consumeCloseTag(reader, openName, openPath) {
|
|
|
489
543
|
if (closePath.original !== openName) {
|
|
490
544
|
const at = openPath.loc.start;
|
|
491
545
|
throw new HandlebarsException(
|
|
492
|
-
|
|
546
|
+
`{{/${closePath.original}}} does not close {{#${openName}}}. Expected {{/${openName}}}.`,
|
|
493
547
|
{ lineNumber: at.line, column: at.column },
|
|
494
548
|
);
|
|
495
549
|
}
|
|
@@ -503,17 +557,21 @@ function consumeCloseTag(reader, openName, openPath) {
|
|
|
503
557
|
* close was expected (the next token, typically `EOF`), e.g. `got 'EOF'`.
|
|
504
558
|
*
|
|
505
559
|
* @param {object} reader - The shared cursor, positioned where a close/inverse was expected.
|
|
560
|
+
* @param {string} [name] - The open block's name, for the message.
|
|
506
561
|
* @returns {never} Always throws.
|
|
507
562
|
* @throws {HandlebarsParseError} The Jison-class unclosed-block error.
|
|
508
563
|
*/
|
|
509
|
-
function throwUnclosed(reader) {
|
|
564
|
+
function throwUnclosed(reader, name) {
|
|
565
|
+
const message = name
|
|
566
|
+
? `Unclosed block {{#${name}}}: expected {{/${name}}}.`
|
|
567
|
+
: 'Unclosed block: expected its closing tag.';
|
|
510
568
|
// Upstream anchors an unclosed block at the last consumed token through the point where the
|
|
511
569
|
// close was expected (typically `EOF`): e.g. `{{#alpha}}x` reports `1:10`-`1:11` (the `x`
|
|
512
570
|
// content start through `EOF`). Span `[previousToken.start, currentToken.end]`.
|
|
513
571
|
const current = reader.peek() || { start: reader.source.length, end: reader.source.length };
|
|
514
572
|
const previous = reader.peek(-1);
|
|
515
573
|
const startOffset = previous ? previous.start : current.start;
|
|
516
|
-
throw jisonAt(reader.table, startOffset, current.end);
|
|
574
|
+
throw jisonAt(reader.table, startOffset, current.end, message);
|
|
517
575
|
}
|
|
518
576
|
|
|
519
577
|
/**
|
|
@@ -542,7 +600,7 @@ function parseBlockStatement(reader, isCaret) {
|
|
|
542
600
|
// Body statements until an inverse split or the `{{/name}}` close.
|
|
543
601
|
const body = parseProgram(reader, () => atBranchEnd(reader));
|
|
544
602
|
if (reader.atEnd()) {
|
|
545
|
-
throwUnclosed(reader);
|
|
603
|
+
throwUnclosed(reader, path.original);
|
|
546
604
|
}
|
|
547
605
|
const bodyEnd = reader.peek().start;
|
|
548
606
|
const bodyProgram = makeSubProgram(reader, body, bodyStart, bodyEnd, {
|
|
@@ -563,7 +621,7 @@ function parseBlockStatement(reader, isCaret) {
|
|
|
563
621
|
|
|
564
622
|
// Consume the matching `{{/name}}` close.
|
|
565
623
|
if (!isCloseTag(reader)) {
|
|
566
|
-
throwUnclosed(reader);
|
|
624
|
+
throwUnclosed(reader, path.original);
|
|
567
625
|
}
|
|
568
626
|
const { closeToken } = consumeCloseTag(reader, path.original, path);
|
|
569
627
|
return blockStatement(reader.table, open.start, closeToken.end, {
|
|
@@ -611,7 +669,7 @@ function parseInverseBranch(reader, blockPath) {
|
|
|
611
669
|
const bodyStart = elseClose.end;
|
|
612
670
|
const body = parseProgram(reader, () => atBranchEnd(reader));
|
|
613
671
|
if (reader.atEnd()) {
|
|
614
|
-
throwUnclosed(reader);
|
|
672
|
+
throwUnclosed(reader, blockPath.original);
|
|
615
673
|
}
|
|
616
674
|
const bodyEnd = reader.peek().start;
|
|
617
675
|
const inverse = makeSubProgram(reader, body, bodyStart, bodyEnd);
|
|
@@ -659,7 +717,7 @@ function parseChainedBlock(reader, blockPath) {
|
|
|
659
717
|
|
|
660
718
|
const body = parseProgram(reader, () => atBranchEnd(reader));
|
|
661
719
|
if (reader.atEnd()) {
|
|
662
|
-
throwUnclosed(reader);
|
|
720
|
+
throwUnclosed(reader, blockPath.original);
|
|
663
721
|
}
|
|
664
722
|
const bodyEnd = reader.peek().start;
|
|
665
723
|
const primaryProgram = makeSubProgram(reader, body, bodyStart, bodyEnd, {
|
|
@@ -670,7 +728,7 @@ function parseChainedBlock(reader, blockPath) {
|
|
|
670
728
|
|
|
671
729
|
// The nested block ends at the shared `{{/name}}` close's OPEN start (not consumed here).
|
|
672
730
|
if (!isCloseTag(reader)) {
|
|
673
|
-
throwUnclosed(reader);
|
|
731
|
+
throwUnclosed(reader, blockPath.original);
|
|
674
732
|
}
|
|
675
733
|
const closeOpenStart = reader.peek().start;
|
|
676
734
|
const block = blockStatement(reader.table, open.start, closeOpenStart, {
|
|
@@ -926,12 +984,13 @@ function parseDecoratorBlock(reader) {
|
|
|
926
984
|
* @param {import('./loc.js').LineTable} table - Line table for the source.
|
|
927
985
|
* @param {number} startOffset - Absolute start offset of the error span.
|
|
928
986
|
* @param {number} endOffset - Absolute end offset of the error span.
|
|
987
|
+
* @param {string} message - What is wrong, in one line (the span is reported separately).
|
|
929
988
|
* @returns {HandlebarsParseError} The Jison-class parse error.
|
|
930
989
|
*/
|
|
931
|
-
function jisonAt(table, startOffset, endOffset) {
|
|
990
|
+
function jisonAt(table, startOffset, endOffset, message) {
|
|
932
991
|
const start = offsetToPosition(table, startOffset);
|
|
933
992
|
const end = offsetToPosition(table, endOffset);
|
|
934
|
-
return new HandlebarsParseError(
|
|
993
|
+
return new HandlebarsParseError(message, {
|
|
935
994
|
first_line: start.line,
|
|
936
995
|
first_column: start.column,
|
|
937
996
|
last_line: end.line,
|
|
@@ -962,11 +1021,16 @@ function convertTokenizerError(error, source) {
|
|
|
962
1021
|
// Jison class. Upstream anchors at the preceding call-head token (the id before the
|
|
963
1022
|
// opening quote). Locate the maximal id run ending just before the quote at `error.offset`.
|
|
964
1023
|
const { start, end } = precedingIdSpan(source, error.offset);
|
|
965
|
-
return jisonAt(
|
|
1024
|
+
return jisonAt(
|
|
1025
|
+
buildLineTable(source),
|
|
1026
|
+
start,
|
|
1027
|
+
end,
|
|
1028
|
+
'Unterminated string: the closing quote is missing.',
|
|
1029
|
+
);
|
|
966
1030
|
}
|
|
967
1031
|
// Other lexical kinds (unterminated raw block / segment) belong to W2b-2's block surface;
|
|
968
1032
|
// surface them as a Jison error at the reported offset for now.
|
|
969
|
-
return jisonAt(buildLineTable(source), error.offset, error.offset);
|
|
1033
|
+
return jisonAt(buildLineTable(source), error.offset, error.offset, error.message);
|
|
970
1034
|
}
|
|
971
1035
|
|
|
972
1036
|
/**
|