sfmc-handlebars-parser 0.1.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 +85 -0
- package/package.json +66 -0
- package/src/errors.js +70 -0
- package/src/expression.js +488 -0
- package/src/index.d.ts +300 -0
- package/src/index.js +996 -0
- package/src/loc.js +109 -0
- package/src/nodes.js +492 -0
- package/src/tokenizer.js +757 -0
package/src/index.js
ADDED
|
@@ -0,0 +1,996 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* sfmc-handlebars-parser — a zero-dependency Handlebars parser for SFMC tooling.
|
|
3
|
+
*
|
|
4
|
+
* Clean-room note: this parser is written from the observed public AST behaviour of
|
|
5
|
+
* `@handlebars/parser` and the published Handlebars language specification. No source
|
|
6
|
+
* was copied from handlebars.js or `@handlebars/parser` (both MIT). The upstream package
|
|
7
|
+
* is kept only as a devDependency, used by the capture/differential tooling to freeze the
|
|
8
|
+
* AST/error goldens the parser is verified against.
|
|
9
|
+
*
|
|
10
|
+
* The public entry point is {@link parse}, which returns the upstream-compatible
|
|
11
|
+
* `AST.Program` node (see `src/index.d.ts`).
|
|
12
|
+
*
|
|
13
|
+
* ── W2b-1 scope (LEAF statements + block seam) ───────────────────────────────────────────
|
|
14
|
+
* This module (worker W2b-1) implements the top-level {@link parse} skeleton plus the
|
|
15
|
+
* BLOCK-FREE statement layer: {@link ContentStatement}, {@link MustacheStatement} (escaped
|
|
16
|
+
* `{{ }}`, unescaped `{{{ }}}` / `{{& }}`, literal-as-head), and {@link CommentStatement}. It
|
|
17
|
+
* drives the shared expression reader (W2a) for the mustache body and reproduces the golden
|
|
18
|
+
* shapes exactly.
|
|
19
|
+
*
|
|
20
|
+
* BLOCKS are NOT implemented here. Every block-opening construct (`{{#…}}` / `{{#>…}}` /
|
|
21
|
+
* `{{#*…}}`, an inverse `{{^…}}`, a partial `{{>…}}`, a decorator `{{*…}}`, or a raw block
|
|
22
|
+
* `{{{{…}}}}`) is routed to {@link parseBlockLike} — the single well-named SEAM that worker
|
|
23
|
+
* W2b-2 replaces with real block / else-chain / blockParams / partial-block / decorator-block /
|
|
24
|
+
* raw-block handling. Today {@link parseBlockLike} throws a not-yet-implemented marker so the
|
|
25
|
+
* block corpus files fail loudly rather than silently mis-parse. See the seam docblock below
|
|
26
|
+
* for the exact hand-off contract.
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
import { tokenizeWithLineTable, TokenType, TokenizerError } from './tokenizer.js';
|
|
30
|
+
import { buildLineTable, offsetToPosition } from './loc.js';
|
|
31
|
+
import {
|
|
32
|
+
createReader,
|
|
33
|
+
parseLiteral,
|
|
34
|
+
parseParametersAndHash,
|
|
35
|
+
parsePath,
|
|
36
|
+
parseSubExpression,
|
|
37
|
+
} from './expression.js';
|
|
38
|
+
import {
|
|
39
|
+
contentStatement,
|
|
40
|
+
mustacheStatement,
|
|
41
|
+
commentStatement,
|
|
42
|
+
program,
|
|
43
|
+
makeLoc,
|
|
44
|
+
blockStatement,
|
|
45
|
+
partialStatement,
|
|
46
|
+
partialBlockStatement,
|
|
47
|
+
decorator,
|
|
48
|
+
decoratorBlock,
|
|
49
|
+
} from './nodes.js';
|
|
50
|
+
import { HandlebarsParseError, HandlebarsException } from './errors.js';
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* The sigil values that open a BLOCK (handled by the W2b-2 seam, not W2b-1). The inverse `^`,
|
|
54
|
+
* partial `>` and decorator `*` sigils are ALSO non-leaf statement openers routed to the seam.
|
|
55
|
+
*/
|
|
56
|
+
const BLOCK_SIGILS = new Set(['#', '#>', '#*', '^', '>', '*']);
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Token types that open a real mustache/comment run immediately after plain content. Used to
|
|
60
|
+
* detect the escaped-backslash-before-a-mustache case (`\\{{`) in {@link parseContent}.
|
|
61
|
+
*/
|
|
62
|
+
const MUSTACHE_OPENER_TYPES = new Set([TokenType.OPEN, TokenType.RAW_OPEN, TokenType.COMMENT]);
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Parses a Handlebars template string into an upstream-compatible AST.
|
|
66
|
+
*
|
|
67
|
+
* Tokenizes the source (capturing a line table for `loc` resolution), wraps the token stream in
|
|
68
|
+
* the shared expression reader, and runs {@link parseProgram} at the top level to assemble the
|
|
69
|
+
* root `Program`. Lexical errors thrown by the tokenizer are converted to the plan's error
|
|
70
|
+
* classes ({@link HandlebarsParseError} / {@link HandlebarsException}) at the reported offset.
|
|
71
|
+
*
|
|
72
|
+
* @param {string} code - The Handlebars (or HTML-with-Handlebars) source to parse.
|
|
73
|
+
* @returns {import('./index.d.ts').AST.Program} The root `Program` node.
|
|
74
|
+
* @throws {HandlebarsParseError} On a Jison-class parse error (unexpected token / empty body).
|
|
75
|
+
* @throws {HandlebarsException} On an exception-class lexical error (unterminated comment).
|
|
76
|
+
*/
|
|
77
|
+
export function parse(code) {
|
|
78
|
+
let tokens;
|
|
79
|
+
let lineTable;
|
|
80
|
+
try {
|
|
81
|
+
({ tokens, lineTable } = tokenizeWithLineTable(code));
|
|
82
|
+
} catch (ex) {
|
|
83
|
+
throw convertTokenizerError(ex, code);
|
|
84
|
+
}
|
|
85
|
+
const reader = createReader(tokens, lineTable, code);
|
|
86
|
+
const body = parseProgram(reader, isTopTerminator);
|
|
87
|
+
// A well-formed block-free template consumes every token up to EOF.
|
|
88
|
+
if (!reader.atEnd()) {
|
|
89
|
+
const token = reader.peek();
|
|
90
|
+
throw jisonAt(lineTable, token.start, token.end);
|
|
91
|
+
}
|
|
92
|
+
// Upstream spans the Program from its FIRST child's start to its LAST child's end (the
|
|
93
|
+
// per-node `loc`s already account for escaped content trimming a leading `\`). An empty
|
|
94
|
+
// top-level body has no children to span, so fall back to the whole-source range.
|
|
95
|
+
const loc =
|
|
96
|
+
body.length > 0
|
|
97
|
+
? { start: body[0].loc.start, end: body.at(-1).loc.end, source: undefined }
|
|
98
|
+
: makeLoc(lineTable, 0, code.length);
|
|
99
|
+
return program(body, loc);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Terminator predicate for the TOP-LEVEL program: nothing but `EOF` ends it. Sub-programs (block
|
|
104
|
+
* bodies / inverse branches — W2b-2) will pass their own terminator (a `{{/…}}` close or an
|
|
105
|
+
* `{{else}}`), so {@link parseProgram} stays reusable for nested bodies.
|
|
106
|
+
*
|
|
107
|
+
* @param {object} token - The next statement-level token.
|
|
108
|
+
* @returns {boolean} True when `token` ends the top-level program (i.e. it is `EOF`).
|
|
109
|
+
*/
|
|
110
|
+
function isTopTerminator(token) {
|
|
111
|
+
return token.type === TokenType.EOF;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Parses a run of statements into a `body` array, dispatching on the statement-level token type
|
|
116
|
+
* until `isTerminator` matches the next token (or `EOF` is reached). This is the reusable program
|
|
117
|
+
* loop: W2b-2 calls it with a block-close terminator to parse a block body or inverse branch.
|
|
118
|
+
*
|
|
119
|
+
* Dispatch:
|
|
120
|
+
* - `CONTENT` → {@link parseContent} (merges contiguous content runs).
|
|
121
|
+
* - `COMMENT` → {@link parseComment}.
|
|
122
|
+
* - `OPEN` + block/partial/decorator/inverse `SIGIL`, or `RAW_OPEN` → {@link parseBlockLike}
|
|
123
|
+
* (the W2b-2 seam). Bare `{{^}}` (an inverse sigil with no path) is a top-level Jison error
|
|
124
|
+
* owned by W2b-1 and thrown here.
|
|
125
|
+
* - `OPEN` (plain, `{{{`, or `{{&`) → {@link parseMustache}.
|
|
126
|
+
*
|
|
127
|
+
* @param {object} reader - The shared cursor from {@link createReader}.
|
|
128
|
+
* @param {(token: object) => boolean} isTerminator - Predicate matching the token that ends this
|
|
129
|
+
* program body (WITHOUT consuming it — the caller decides how to consume the terminator).
|
|
130
|
+
* @returns {object[]} The parsed statement nodes.
|
|
131
|
+
*/
|
|
132
|
+
export function parseProgram(reader, isTerminator) {
|
|
133
|
+
/**
|
|
134
|
+
@type {object[]}
|
|
135
|
+
*/
|
|
136
|
+
const body = [];
|
|
137
|
+
while (true) {
|
|
138
|
+
const token = reader.peek();
|
|
139
|
+
if (!token || token.type === TokenType.EOF || isTerminator(token)) {
|
|
140
|
+
break;
|
|
141
|
+
}
|
|
142
|
+
if (token.type === TokenType.CONTENT) {
|
|
143
|
+
body.push(parseContent(reader));
|
|
144
|
+
continue;
|
|
145
|
+
}
|
|
146
|
+
if (token.type === TokenType.COMMENT) {
|
|
147
|
+
body.push(parseComment(reader));
|
|
148
|
+
continue;
|
|
149
|
+
}
|
|
150
|
+
if (token.type === TokenType.RAW_OPEN) {
|
|
151
|
+
body.push(parseBlockLike(reader));
|
|
152
|
+
continue;
|
|
153
|
+
}
|
|
154
|
+
if (token.type === TokenType.OPEN) {
|
|
155
|
+
const sigil = reader.peek(1);
|
|
156
|
+
if (sigil && sigil.type === TokenType.SIGIL && BLOCK_SIGILS.has(sigil.value)) {
|
|
157
|
+
// Bare `{{^}}` / `{{^ }}` (an inverse sigil with no path) is not a block — it is
|
|
158
|
+
// an unexpected INVERSE token at statement level. Upstream throws a Jison parse
|
|
159
|
+
// error at `1:0`; reproduce that here (W2b-1 owns bare-caret).
|
|
160
|
+
if (sigil.value === '^' && isBareInverse(reader)) {
|
|
161
|
+
throw jisonAt(reader.table, 0, 0);
|
|
162
|
+
}
|
|
163
|
+
body.push(parseBlockLike(reader));
|
|
164
|
+
continue;
|
|
165
|
+
}
|
|
166
|
+
// A bare `{{else}}` / `{{else if}}` (OPEN + ID `else`) that reaches statement-level
|
|
167
|
+
// dispatch is not enclosed by a block — upstream throws a Jison parse error anchored
|
|
168
|
+
// at the opener (`got 'INVERSE'`). Block bodies consume their own `{{else}}` via the
|
|
169
|
+
// terminator, so this only fires for a stray/top-level else.
|
|
170
|
+
if (sigil && sigil.type === TokenType.ID && sigil.value === 'else') {
|
|
171
|
+
throw jisonAt(reader.table, token.start, token.start);
|
|
172
|
+
}
|
|
173
|
+
body.push(parseMustache(reader));
|
|
174
|
+
continue;
|
|
175
|
+
}
|
|
176
|
+
// Any other statement-level token here is unexpected (e.g. a stray CLOSE / SIGIL).
|
|
177
|
+
throw jisonAt(reader.table, token.start, token.end);
|
|
178
|
+
}
|
|
179
|
+
return body;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Whether the OPEN + `^` SIGIL at the cursor is a BARE inverse (`{{^}}` / `{{^ }}`) — i.e. the
|
|
184
|
+
* `^` is followed only by whitespace and then the CLOSE, with no path. A `^` followed by a path
|
|
185
|
+
* (`{{^x}}`) is a real inverse block for W2b-2. Does NOT advance the cursor.
|
|
186
|
+
*
|
|
187
|
+
* @param {object} reader - The shared cursor from {@link createReader}.
|
|
188
|
+
* @returns {boolean} True for a bare inverse mustache.
|
|
189
|
+
*/
|
|
190
|
+
function isBareInverse(reader) {
|
|
191
|
+
// Skip the OPEN (offset 0) and the SIGIL (offset 1); look past any whitespace for a CLOSE.
|
|
192
|
+
let offset = 2;
|
|
193
|
+
let token = reader.peek(offset);
|
|
194
|
+
while (token && token.type === TokenType.WHITESPACE) {
|
|
195
|
+
offset++;
|
|
196
|
+
token = reader.peek(offset);
|
|
197
|
+
}
|
|
198
|
+
return !!token && token.type === TokenType.CLOSE;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Parses a `ContentStatement` from a maximal run of CONTIGUOUS `CONTENT` tokens (each token's
|
|
203
|
+
* `start` equal to the previous token's `end`). Upstream merges an escaped-mustache literal
|
|
204
|
+
* (`\{{…}}`) with the surrounding plain text into a single `ContentStatement`, splitting only at
|
|
205
|
+
* a gap (the consumed backslash of the next `\{{`). A trailing backslash on plain content that
|
|
206
|
+
* directly abuts a real mustache is the escaped-backslash case (`\\{{`): upstream drops ONE
|
|
207
|
+
* trailing backslash from `original`/`value` (`\\{{` → content `\`, `\\\\{{` → `\\\`).
|
|
208
|
+
*
|
|
209
|
+
* @param {object} reader - The shared cursor from {@link createReader}.
|
|
210
|
+
* @returns {object} A `ContentStatement` node.
|
|
211
|
+
*/
|
|
212
|
+
function parseContent(reader) {
|
|
213
|
+
const first = reader.next();
|
|
214
|
+
const startOffset = first.start;
|
|
215
|
+
let endOffset = first.end;
|
|
216
|
+
let value = first.value;
|
|
217
|
+
let lastEscaped = first.escaped;
|
|
218
|
+
// Merge contiguous CONTENT tokens (no gap between them).
|
|
219
|
+
while (true) {
|
|
220
|
+
const token = reader.peek();
|
|
221
|
+
if (!token || token.type !== TokenType.CONTENT || token.start !== endOffset) {
|
|
222
|
+
break;
|
|
223
|
+
}
|
|
224
|
+
value += token.value;
|
|
225
|
+
endOffset = token.end;
|
|
226
|
+
lastEscaped = token.escaped;
|
|
227
|
+
reader.next();
|
|
228
|
+
}
|
|
229
|
+
let original = reader.source.slice(startOffset, endOffset);
|
|
230
|
+
// Escaped-backslash before a real mustache: `\\{{` renders one backslash. The tokenizer keeps
|
|
231
|
+
// the raw `\\`; drop one trailing backslash when this plain run directly abuts a real
|
|
232
|
+
// mustache opener (OPEN / RAW_OPEN / COMMENT).
|
|
233
|
+
if (!lastEscaped && original.endsWith('\\')) {
|
|
234
|
+
const next = reader.peek();
|
|
235
|
+
if (next && next.start === endOffset && MUSTACHE_OPENER_TYPES.has(next.type)) {
|
|
236
|
+
original = original.slice(0, -1);
|
|
237
|
+
value = value.slice(0, -1);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
return contentStatement(reader.table, startOffset, endOffset, value, original);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* Parses a `CommentStatement` from a single `COMMENT` token (`{{! … }}` or `{{!-- … --}}`). The
|
|
245
|
+
* comment's inner text is the token `value`; the token already spans the whole comment.
|
|
246
|
+
*
|
|
247
|
+
* @param {object} reader - The shared cursor from {@link createReader}.
|
|
248
|
+
* @returns {object} A `CommentStatement` node.
|
|
249
|
+
*/
|
|
250
|
+
function parseComment(reader) {
|
|
251
|
+
const token = reader.next();
|
|
252
|
+
const strip = { open: !!token.strip, close: !!token.closeStrip };
|
|
253
|
+
return commentStatement(reader.table, token.start, token.end, token.value, strip);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* Parses a `MustacheStatement` (`{{ … }}`, `{{{ … }}}`, or `{{& … }}`). Consumes the OPEN, an
|
|
258
|
+
* optional `&` SIGIL, the call head (a path, or a literal-as-head for `{{"str"}}` / `{{true}}` /
|
|
259
|
+
* `{{5}}`), positional params + hash pairs (via W2a), then the CLOSE. `escaped` is `true` for the
|
|
260
|
+
* `{{` form and `false` for the unescaped `{{{` / `{{&` forms.
|
|
261
|
+
*
|
|
262
|
+
* @param {object} reader - The shared cursor from {@link createReader}.
|
|
263
|
+
* @returns {object} A `MustacheStatement` node.
|
|
264
|
+
*/
|
|
265
|
+
function parseMustache(reader) {
|
|
266
|
+
const open = reader.expect(TokenType.OPEN);
|
|
267
|
+
// `{{{` (triple) and `{{&` are the unescaped forms.
|
|
268
|
+
let isEscaped = open.open !== '{{{';
|
|
269
|
+
if (reader.peekType() === TokenType.SIGIL && reader.peek().value === '&') {
|
|
270
|
+
isEscaped = false;
|
|
271
|
+
reader.next();
|
|
272
|
+
}
|
|
273
|
+
reader.skipWhitespace();
|
|
274
|
+
// Empty body (`{{ }}`) — upstream throws a Jison parse error anchored at the OPEN token.
|
|
275
|
+
if (reader.peekType() === TokenType.CLOSE) {
|
|
276
|
+
throw jisonAt(reader.table, open.start, open.end);
|
|
277
|
+
}
|
|
278
|
+
const head = parseCallHead(reader, open);
|
|
279
|
+
const { params, hash } = parseParametersAndHash(
|
|
280
|
+
reader,
|
|
281
|
+
(token) => token.type === TokenType.CLOSE,
|
|
282
|
+
);
|
|
283
|
+
reader.skipWhitespace();
|
|
284
|
+
const close = reader.expect(TokenType.CLOSE);
|
|
285
|
+
const strip = { open: !!open.strip, close: !!close.strip };
|
|
286
|
+
return mustacheStatement(
|
|
287
|
+
reader.table,
|
|
288
|
+
open.start,
|
|
289
|
+
close.end,
|
|
290
|
+
head,
|
|
291
|
+
params,
|
|
292
|
+
hash,
|
|
293
|
+
isEscaped,
|
|
294
|
+
strip,
|
|
295
|
+
);
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* Parses the CALL HEAD of a mustache: a literal (`{{"str"}}` / `{{true}}` / `{{5}}` place the
|
|
300
|
+
* literal node in the `path` slot) when the cursor is on a standalone literal, otherwise a
|
|
301
|
+
* `PathExpression`. When neither is possible (e.g. a leading `.` separator), upstream throws a
|
|
302
|
+
* Jison parse error anchored at the OPEN token.
|
|
303
|
+
*
|
|
304
|
+
* @param {object} reader - The shared cursor from {@link createReader}.
|
|
305
|
+
* @param {object} open - The consumed OPEN token (for error anchoring).
|
|
306
|
+
* @returns {object} The head node (a literal or `PathExpression`).
|
|
307
|
+
*/
|
|
308
|
+
function parseCallHead(reader, open) {
|
|
309
|
+
const literal = parseLiteral(reader);
|
|
310
|
+
if (literal) {
|
|
311
|
+
return literal;
|
|
312
|
+
}
|
|
313
|
+
reader.skipWhitespace();
|
|
314
|
+
const token = reader.peek();
|
|
315
|
+
// A path may begin with a relative/parent context (`./`, `../`) — a `.`/`..` SEP immediately
|
|
316
|
+
// followed by a `/` SEP. A leading bare `.` SEP that is NOT part of `./` (e.g. `{{.foo}}`) is
|
|
317
|
+
// an invalid path start: upstream throws a Jison "got 'SEP'" error anchored at the OPEN token.
|
|
318
|
+
if (token && token.type === TokenType.SEP) {
|
|
319
|
+
const next = reader.peek(1);
|
|
320
|
+
const isRelativeStart = next && next.type === TokenType.SEP && next.value === '/';
|
|
321
|
+
if (!isRelativeStart) {
|
|
322
|
+
throw jisonAt(reader.table, open.start, open.end);
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
return parsePath(reader);
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
/**
|
|
329
|
+
* Parses a BLOCK-LIKE construct (worker W2b-2) — invoked by {@link parseProgram} with the reader
|
|
330
|
+
* positioned ON the opener (nothing consumed). Dispatches on the opener:
|
|
331
|
+
* - `RAW_OPEN` → {@link parseRawBlock} (`{{{{name}}}} … {{{{/name}}}}`).
|
|
332
|
+
* - `OPEN` + SIGIL: `#` → {@link parseBlockStatement}; `^` (with a path) → caret-inverse
|
|
333
|
+
* shorthand (also {@link parseBlockStatement}); `>` → {@link parsePartial}; `#>` →
|
|
334
|
+
* {@link parsePartialBlock}; `*` → {@link parseDecorator}; `#*` → {@link parseDecoratorBlock}.
|
|
335
|
+
*
|
|
336
|
+
* Bare `{{^}}` / `{{^ }}` is intercepted by {@link parseProgram} before this hook.
|
|
337
|
+
*
|
|
338
|
+
* @param {object} reader - The shared cursor from {@link createReader}, positioned on the opener.
|
|
339
|
+
* @returns {object} The block-like AST node.
|
|
340
|
+
*/
|
|
341
|
+
export function parseBlockLike(reader) {
|
|
342
|
+
if (reader.peekType() === TokenType.RAW_OPEN) {
|
|
343
|
+
return parseRawBlock(reader);
|
|
344
|
+
}
|
|
345
|
+
const sigil = reader.peek(1).value;
|
|
346
|
+
switch (sigil) {
|
|
347
|
+
case '#': {
|
|
348
|
+
return parseBlockStatement(reader, false);
|
|
349
|
+
}
|
|
350
|
+
case '^': {
|
|
351
|
+
// `{{^x}}…{{/x}}` — inverse-shorthand block (the body lives in `inverse`).
|
|
352
|
+
return parseBlockStatement(reader, true);
|
|
353
|
+
}
|
|
354
|
+
case '>': {
|
|
355
|
+
return parsePartial(reader);
|
|
356
|
+
}
|
|
357
|
+
case '#>': {
|
|
358
|
+
return parsePartialBlock(reader);
|
|
359
|
+
}
|
|
360
|
+
case '*': {
|
|
361
|
+
return parseDecorator(reader);
|
|
362
|
+
}
|
|
363
|
+
default: {
|
|
364
|
+
// `#*`
|
|
365
|
+
return parseDecoratorBlock(reader);
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
/**
|
|
371
|
+
* Whether the token at `reader.peek(offset)` opens a block-close tag `{{/…}}` (an `OPEN` whose
|
|
372
|
+
* next token is a `/` SIGIL). Does not advance the cursor.
|
|
373
|
+
*
|
|
374
|
+
* @param {object} reader - The shared cursor.
|
|
375
|
+
* @param {number} [offset] - Look-ahead distance to the candidate `OPEN` (default 0).
|
|
376
|
+
* @returns {boolean} True when a `{{/…}}` close begins at `offset`.
|
|
377
|
+
*/
|
|
378
|
+
function isCloseTag(reader, offset = 0) {
|
|
379
|
+
const open = reader.peek(offset);
|
|
380
|
+
const sigil = reader.peek(offset + 1);
|
|
381
|
+
return (
|
|
382
|
+
!!open &&
|
|
383
|
+
open.type === TokenType.OPEN &&
|
|
384
|
+
!!sigil &&
|
|
385
|
+
sigil.type === TokenType.SIGIL &&
|
|
386
|
+
sigil.value === '/'
|
|
387
|
+
);
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
/**
|
|
391
|
+
* Whether an inverse split (`{{else}}` / `{{else if …}}` / bare `{{^}}`) begins at the cursor.
|
|
392
|
+
* An `else` split is `OPEN` + `ID('else')`; a bare-caret split is `OPEN` + `SIGIL('^')` followed
|
|
393
|
+
* only by whitespace and a CLOSE. Does not advance the cursor.
|
|
394
|
+
*
|
|
395
|
+
* @param {object} reader - The shared cursor.
|
|
396
|
+
* @returns {boolean} True when an inverse split begins at the cursor.
|
|
397
|
+
*/
|
|
398
|
+
function isInverseSplit(reader) {
|
|
399
|
+
if (reader.peekType() !== TokenType.OPEN) {
|
|
400
|
+
return false;
|
|
401
|
+
}
|
|
402
|
+
const marker = reader.peek(1);
|
|
403
|
+
if (marker && marker.type === TokenType.ID && marker.value === 'else') {
|
|
404
|
+
return true;
|
|
405
|
+
}
|
|
406
|
+
return (
|
|
407
|
+
!!marker && marker.type === TokenType.SIGIL && marker.value === '^' && isBareInverse(reader)
|
|
408
|
+
);
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
/**
|
|
412
|
+
* Terminator predicate for a block BODY / inverse branch: the branch ends at an inverse split
|
|
413
|
+
* (`{{else}}` / `{{^}}`) or a `{{/…}}` close, neither consumed.
|
|
414
|
+
*
|
|
415
|
+
* @param {object} reader - The shared cursor.
|
|
416
|
+
* @returns {boolean} True when the current token ends the current branch.
|
|
417
|
+
*/
|
|
418
|
+
function atBranchEnd(reader) {
|
|
419
|
+
return isCloseTag(reader) || isInverseSplit(reader);
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
/**
|
|
423
|
+
* Parses the `as |a b|` block-params fence when it begins at the cursor (an `ID('as')` followed
|
|
424
|
+
* by a `PIPE`), returning the parameter names. Consumes `as`, the opening `|`, the id run, and
|
|
425
|
+
* the closing `|`. Returns `undefined` when no fence is present (the caller keeps the golden's
|
|
426
|
+
* `blockParams: undefined`).
|
|
427
|
+
*
|
|
428
|
+
* @param {object} reader - The shared cursor, positioned after the params/hash.
|
|
429
|
+
* @returns {string[]|undefined} The block-param names, or `undefined` when absent.
|
|
430
|
+
*/
|
|
431
|
+
function parseBlockParameters(reader) {
|
|
432
|
+
reader.skipWhitespace();
|
|
433
|
+
const asToken = reader.peek();
|
|
434
|
+
if (!asToken || asToken.type !== TokenType.ID || asToken.value !== 'as') {
|
|
435
|
+
return;
|
|
436
|
+
}
|
|
437
|
+
reader.next(); // `as`
|
|
438
|
+
reader.skipWhitespace();
|
|
439
|
+
reader.expect(TokenType.PIPE);
|
|
440
|
+
/**
|
|
441
|
+
* @type {string[]}
|
|
442
|
+
*/
|
|
443
|
+
const names = [];
|
|
444
|
+
while (true) {
|
|
445
|
+
reader.skipWhitespace();
|
|
446
|
+
const token = reader.peek();
|
|
447
|
+
if (!token || token.type !== TokenType.ID) {
|
|
448
|
+
break;
|
|
449
|
+
}
|
|
450
|
+
names.push(token.value);
|
|
451
|
+
reader.next();
|
|
452
|
+
}
|
|
453
|
+
reader.skipWhitespace();
|
|
454
|
+
reader.expect(TokenType.PIPE);
|
|
455
|
+
return names;
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
/**
|
|
459
|
+
* Builds a sub-program `Program` for a block body / inverse branch. An EMPTY body carries
|
|
460
|
+
* `loc: undefined` (per the golden); a non-empty body spans `[bodyStart, bodyEnd)`.
|
|
461
|
+
*
|
|
462
|
+
* @param {object} reader - The shared cursor (for the line table).
|
|
463
|
+
* @param {object[]} body - The parsed statement nodes.
|
|
464
|
+
* @param {number} bodyStart - Offset where the body region begins (just after the opener's close).
|
|
465
|
+
* @param {number} bodyEnd - Offset where the body region ends (start of the terminating tag).
|
|
466
|
+
* @param {object} [options] - Program key-presence options (`hasBlockParams` / `chained`).
|
|
467
|
+
* @returns {object} A `Program` node.
|
|
468
|
+
*/
|
|
469
|
+
function makeSubProgram(reader, body, bodyStart, bodyEnd, options) {
|
|
470
|
+
const loc = body.length > 0 ? makeLoc(reader.table, bodyStart, bodyEnd) : undefined;
|
|
471
|
+
return program(body, loc, options);
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
/**
|
|
475
|
+
* Consumes a `{{/name}}` close tag, asserting it matches `openName`. On a name mismatch upstream
|
|
476
|
+
* throws an exception-class error anchored at the opener path's start position (`name doesn't
|
|
477
|
+
* match closer`). Returns the consumed `OPEN` and `CLOSE` tokens.
|
|
478
|
+
*
|
|
479
|
+
* @param {object} reader - The shared cursor, positioned on the `{{/…}}` `OPEN`.
|
|
480
|
+
* @param {string} openName - The `original` text of the opener's path (for the match check).
|
|
481
|
+
* @param {object} openPath - The opener's path node (for the mismatch error position).
|
|
482
|
+
* @returns {{openToken: object, closeToken: object}} The consumed close-tag delimiters.
|
|
483
|
+
* @throws {HandlebarsException} On a name mismatch.
|
|
484
|
+
*/
|
|
485
|
+
function consumeCloseTag(reader, openName, openPath) {
|
|
486
|
+
const openToken = reader.expect(TokenType.OPEN);
|
|
487
|
+
reader.expect(TokenType.SIGIL); // `/`
|
|
488
|
+
const closePath = parsePath(reader);
|
|
489
|
+
if (closePath.original !== openName) {
|
|
490
|
+
const at = openPath.loc.start;
|
|
491
|
+
throw new HandlebarsException(
|
|
492
|
+
`${openName} doesn't match ${closePath.original} - ${at.line}:${at.column}`,
|
|
493
|
+
{ lineNumber: at.line, column: at.column },
|
|
494
|
+
);
|
|
495
|
+
}
|
|
496
|
+
reader.skipWhitespace();
|
|
497
|
+
const closeToken = reader.expect(TokenType.CLOSE);
|
|
498
|
+
return { openToken, closeToken };
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
/**
|
|
502
|
+
* Reports an unclosed block: upstream throws a Jison parse error anchored at the point where the
|
|
503
|
+
* close was expected (the next token, typically `EOF`), e.g. `got 'EOF'`.
|
|
504
|
+
*
|
|
505
|
+
* @param {object} reader - The shared cursor, positioned where a close/inverse was expected.
|
|
506
|
+
* @returns {never} Always throws.
|
|
507
|
+
* @throws {HandlebarsParseError} The Jison-class unclosed-block error.
|
|
508
|
+
*/
|
|
509
|
+
function throwUnclosed(reader) {
|
|
510
|
+
// Upstream anchors an unclosed block at the last consumed token through the point where the
|
|
511
|
+
// close was expected (typically `EOF`): e.g. `{{#alpha}}x` reports `1:10`-`1:11` (the `x`
|
|
512
|
+
// content start through `EOF`). Span `[previousToken.start, currentToken.end]`.
|
|
513
|
+
const current = reader.peek() || { start: reader.source.length, end: reader.source.length };
|
|
514
|
+
const previous = reader.peek(-1);
|
|
515
|
+
const startOffset = previous ? previous.start : current.start;
|
|
516
|
+
throw jisonAt(reader.table, startOffset, current.end);
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
/**
|
|
520
|
+
* Parses a `BlockStatement` — `{{#name params hash as |bp|}} body {{else}} inverse {{/name}}` —
|
|
521
|
+
* or the caret-inverse shorthand `{{^name}} body {{/name}}` (`isCaret`), consuming through the
|
|
522
|
+
* matching `{{/name}}`.
|
|
523
|
+
*
|
|
524
|
+
* For the `#` form the body is the PRIMARY program (carries `blockParams`) and the `{{else}}` /
|
|
525
|
+
* `{{else if}}` / `{{^}}` branch is the `inverse`. For the caret shorthand the body is placed in
|
|
526
|
+
* `inverse` (which carries `blockParams`) and `program` is `undefined`.
|
|
527
|
+
*
|
|
528
|
+
* @param {object} reader - The shared cursor, positioned on the opener `OPEN`.
|
|
529
|
+
* @param {boolean} isCaret - Whether this is the `{{^name}}` inverse-shorthand form.
|
|
530
|
+
* @returns {object} A `BlockStatement` node.
|
|
531
|
+
*/
|
|
532
|
+
function parseBlockStatement(reader, isCaret) {
|
|
533
|
+
const open = reader.expect(TokenType.OPEN);
|
|
534
|
+
reader.expect(TokenType.SIGIL); // `#` or `^`
|
|
535
|
+
const path = parsePath(reader);
|
|
536
|
+
const { params, hash } = parseParametersAndHash(reader, headTerminator(reader));
|
|
537
|
+
const blockParameters = parseBlockParameters(reader);
|
|
538
|
+
reader.skipWhitespace();
|
|
539
|
+
const openClose = reader.expect(TokenType.CLOSE);
|
|
540
|
+
const bodyStart = openClose.end;
|
|
541
|
+
|
|
542
|
+
// Body statements until an inverse split or the `{{/name}}` close.
|
|
543
|
+
const body = parseProgram(reader, () => atBranchEnd(reader));
|
|
544
|
+
if (reader.atEnd()) {
|
|
545
|
+
throwUnclosed(reader);
|
|
546
|
+
}
|
|
547
|
+
const bodyEnd = reader.peek().start;
|
|
548
|
+
const bodyProgram = makeSubProgram(reader, body, bodyStart, bodyEnd, {
|
|
549
|
+
hasBlockParams: true,
|
|
550
|
+
blockParams: blockParameters,
|
|
551
|
+
});
|
|
552
|
+
|
|
553
|
+
let primaryProgram;
|
|
554
|
+
let inverse;
|
|
555
|
+
if (isCaret) {
|
|
556
|
+
// `{{^x}}` — the body lives in `inverse` (which carries blockParams); `program` is absent.
|
|
557
|
+
primaryProgram = undefined;
|
|
558
|
+
inverse = bodyProgram;
|
|
559
|
+
} else {
|
|
560
|
+
primaryProgram = bodyProgram;
|
|
561
|
+
inverse = parseInverseBranch(reader, path).inverse;
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
// Consume the matching `{{/name}}` close.
|
|
565
|
+
if (!isCloseTag(reader)) {
|
|
566
|
+
throwUnclosed(reader);
|
|
567
|
+
}
|
|
568
|
+
const { closeToken } = consumeCloseTag(reader, path.original, path);
|
|
569
|
+
return blockStatement(reader.table, open.start, closeToken.end, {
|
|
570
|
+
path,
|
|
571
|
+
params,
|
|
572
|
+
hash,
|
|
573
|
+
programNode: primaryProgram,
|
|
574
|
+
hasInverse: true,
|
|
575
|
+
inverse,
|
|
576
|
+
openStrip: { open: !!open.strip, close: !!openClose.strip },
|
|
577
|
+
inverseStrip: { open: false, close: false },
|
|
578
|
+
closeStrip: { open: false, close: false },
|
|
579
|
+
});
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
/**
|
|
583
|
+
* Parses the inverse branch of a `#` block after its primary program: a plain `{{else}}` /
|
|
584
|
+
* bare `{{^}}` inverse, an `{{else if …}}` chain (a nested `BlockStatement` under a
|
|
585
|
+
* `chained: true` Program), or nothing (the cursor is already on `{{/name}}`). Does NOT consume
|
|
586
|
+
* the shared `{{/name}}` close (the enclosing block does).
|
|
587
|
+
*
|
|
588
|
+
* @param {object} reader - The shared cursor, positioned on the terminator after the body.
|
|
589
|
+
* @param {object} blockPath - The enclosing block's path node (for name-match / mismatch errors).
|
|
590
|
+
* @returns {{inverse: (object|undefined)}} The inverse `Program` (or `undefined` when there is no
|
|
591
|
+
* `{{else}}` branch — the cursor is on `{{/name}}`).
|
|
592
|
+
*/
|
|
593
|
+
function parseInverseBranch(reader, blockPath) {
|
|
594
|
+
// No inverse: the body was terminated directly by `{{/name}}`.
|
|
595
|
+
if (isCloseTag(reader)) {
|
|
596
|
+
return { inverse: undefined };
|
|
597
|
+
}
|
|
598
|
+
const marker = reader.peek(1);
|
|
599
|
+
// `{{else if …}}` chain — a nested block sharing the enclosing `{{/name}}` close.
|
|
600
|
+
if (marker.type === TokenType.ID && marker.value === 'else' && isElseIf(reader)) {
|
|
601
|
+
const { block } = parseChainedBlock(reader, blockPath);
|
|
602
|
+
const chainedLoc = block.program === undefined ? block.loc : block.program.loc || block.loc;
|
|
603
|
+
const inverse = program([block], chainedLoc, { chained: true });
|
|
604
|
+
return { inverse };
|
|
605
|
+
}
|
|
606
|
+
// Plain `{{else}}` or bare `{{^}}` inverse.
|
|
607
|
+
reader.expect(TokenType.OPEN);
|
|
608
|
+
reader.next(); // `else` ID or `^` SIGIL
|
|
609
|
+
reader.skipWhitespace();
|
|
610
|
+
const elseClose = reader.expect(TokenType.CLOSE);
|
|
611
|
+
const bodyStart = elseClose.end;
|
|
612
|
+
const body = parseProgram(reader, () => atBranchEnd(reader));
|
|
613
|
+
if (reader.atEnd()) {
|
|
614
|
+
throwUnclosed(reader);
|
|
615
|
+
}
|
|
616
|
+
const bodyEnd = reader.peek().start;
|
|
617
|
+
const inverse = makeSubProgram(reader, body, bodyStart, bodyEnd);
|
|
618
|
+
return { inverse };
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
/**
|
|
622
|
+
* Whether the `{{else …}}` at the cursor is an `{{else if …}}` chain (an `else` ID immediately
|
|
623
|
+
* followed by another `ID` before the CLOSE) rather than a plain `{{else}}`. Does not advance.
|
|
624
|
+
*
|
|
625
|
+
* @param {object} reader - The shared cursor, positioned on the `{{else …}}` `OPEN`.
|
|
626
|
+
* @returns {boolean} True for an `{{else if …}}` chain.
|
|
627
|
+
*/
|
|
628
|
+
function isElseIf(reader) {
|
|
629
|
+
// OPEN(0) ID('else')(1) then, skipping whitespace, another ID before CLOSE.
|
|
630
|
+
let offset = 2;
|
|
631
|
+
let token = reader.peek(offset);
|
|
632
|
+
while (token && token.type === TokenType.WHITESPACE) {
|
|
633
|
+
offset++;
|
|
634
|
+
token = reader.peek(offset);
|
|
635
|
+
}
|
|
636
|
+
return !!token && token.type === TokenType.ID;
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
/**
|
|
640
|
+
* Parses one link of an `{{else if …}}` chain: consumes `{{else`, parses the nested block's head
|
|
641
|
+
* (`if cond` etc.), its primary program, and recurses into its own inverse — all sharing the
|
|
642
|
+
* enclosing block's `{{/name}}` close (NOT consumed here). The nested block's `loc` spans from
|
|
643
|
+
* the `{{else …}}` opener to the shared close's `OPEN` start.
|
|
644
|
+
*
|
|
645
|
+
* @param {object} reader - The shared cursor, positioned on the `{{else if …}}` `OPEN`.
|
|
646
|
+
* @param {object} blockPath - The enclosing block's path (for the shared close-name check).
|
|
647
|
+
* @returns {{block: object}} The nested `BlockStatement`.
|
|
648
|
+
*/
|
|
649
|
+
function parseChainedBlock(reader, blockPath) {
|
|
650
|
+
const open = reader.expect(TokenType.OPEN);
|
|
651
|
+
reader.next(); // `else` ID
|
|
652
|
+
reader.skipWhitespace();
|
|
653
|
+
const path = parsePath(reader); // `if` (or another block helper)
|
|
654
|
+
const { params, hash } = parseParametersAndHash(reader, headTerminator(reader));
|
|
655
|
+
const blockParameters = parseBlockParameters(reader);
|
|
656
|
+
reader.skipWhitespace();
|
|
657
|
+
const openClose = reader.expect(TokenType.CLOSE);
|
|
658
|
+
const bodyStart = openClose.end;
|
|
659
|
+
|
|
660
|
+
const body = parseProgram(reader, () => atBranchEnd(reader));
|
|
661
|
+
if (reader.atEnd()) {
|
|
662
|
+
throwUnclosed(reader);
|
|
663
|
+
}
|
|
664
|
+
const bodyEnd = reader.peek().start;
|
|
665
|
+
const primaryProgram = makeSubProgram(reader, body, bodyStart, bodyEnd, {
|
|
666
|
+
hasBlockParams: true,
|
|
667
|
+
blockParams: blockParameters,
|
|
668
|
+
});
|
|
669
|
+
const branch = parseInverseBranch(reader, blockPath);
|
|
670
|
+
|
|
671
|
+
// The nested block ends at the shared `{{/name}}` close's OPEN start (not consumed here).
|
|
672
|
+
if (!isCloseTag(reader)) {
|
|
673
|
+
throwUnclosed(reader);
|
|
674
|
+
}
|
|
675
|
+
const closeOpenStart = reader.peek().start;
|
|
676
|
+
const block = blockStatement(reader.table, open.start, closeOpenStart, {
|
|
677
|
+
path,
|
|
678
|
+
params,
|
|
679
|
+
hash,
|
|
680
|
+
programNode: primaryProgram,
|
|
681
|
+
hasInverse: true,
|
|
682
|
+
inverse: branch.inverse,
|
|
683
|
+
openStrip: { open: !!open.strip, close: !!openClose.strip },
|
|
684
|
+
inverseStrip: { open: false, close: false },
|
|
685
|
+
closeStrip: { open: false, close: false },
|
|
686
|
+
});
|
|
687
|
+
return { block };
|
|
688
|
+
}
|
|
689
|
+
|
|
690
|
+
/**
|
|
691
|
+
* Builds the params/hash terminator for a call HEAD (block / partial / decorator open): the head
|
|
692
|
+
* ends at the CLOSE, a `PIPE` (block-params fence), or the `as` keyword that begins an
|
|
693
|
+
* `as |a b|` fence. Bound to the reader so it can look past `as` for the `|`.
|
|
694
|
+
*
|
|
695
|
+
* @param {object} reader - The shared cursor.
|
|
696
|
+
* @returns {(token: object) => boolean} The terminator predicate.
|
|
697
|
+
*/
|
|
698
|
+
function headTerminator(reader) {
|
|
699
|
+
return (token) => {
|
|
700
|
+
if (token.type === TokenType.CLOSE || token.type === TokenType.PIPE) {
|
|
701
|
+
return true;
|
|
702
|
+
}
|
|
703
|
+
if (token.type === TokenType.ID && token.value === 'as') {
|
|
704
|
+
// `as` followed (after whitespace) by `|` starts the blockParams fence — stop here so
|
|
705
|
+
// `as` is not mis-parsed as a positional param.
|
|
706
|
+
let offset = 1;
|
|
707
|
+
let next = reader.peek(offset);
|
|
708
|
+
while (next && next.type === TokenType.WHITESPACE) {
|
|
709
|
+
offset++;
|
|
710
|
+
next = reader.peek(offset);
|
|
711
|
+
}
|
|
712
|
+
return !!next && next.type === TokenType.PIPE;
|
|
713
|
+
}
|
|
714
|
+
return false;
|
|
715
|
+
};
|
|
716
|
+
}
|
|
717
|
+
|
|
718
|
+
/**
|
|
719
|
+
* Parses a raw block `{{{{name}}}} body {{{{/name}}}}` into a `BlockStatement` whose `program`
|
|
720
|
+
* body is a single verbatim `ContentStatement` (inner `{{ }}` are NOT parsed). Raw blocks omit
|
|
721
|
+
* the `inverse` key entirely and their program carries no `blockParams`.
|
|
722
|
+
*
|
|
723
|
+
* @param {object} reader - The shared cursor, positioned on the `RAW_OPEN`.
|
|
724
|
+
* @returns {object} A `BlockStatement` node.
|
|
725
|
+
*/
|
|
726
|
+
function parseRawBlock(reader) {
|
|
727
|
+
const rawOpen = reader.expect(TokenType.RAW_OPEN);
|
|
728
|
+
// The block name path spans the `name` inside `{{{{name}}}}` (starts after the four braces).
|
|
729
|
+
const nameStart = rawOpen.start + 4;
|
|
730
|
+
const nameEnd = nameStart + rawOpen.name.length;
|
|
731
|
+
const path = pathFromRawName(reader, rawOpen.name, nameStart, nameEnd);
|
|
732
|
+
|
|
733
|
+
/**
|
|
734
|
+
* @type {object[]}
|
|
735
|
+
*/
|
|
736
|
+
const bodyStatements = [];
|
|
737
|
+
if (reader.peekType() === TokenType.RAW_CONTENT) {
|
|
738
|
+
const content = reader.next();
|
|
739
|
+
bodyStatements.push(
|
|
740
|
+
contentStatement(
|
|
741
|
+
reader.table,
|
|
742
|
+
content.start,
|
|
743
|
+
content.end,
|
|
744
|
+
content.value,
|
|
745
|
+
content.value,
|
|
746
|
+
),
|
|
747
|
+
);
|
|
748
|
+
}
|
|
749
|
+
const rawClose = reader.expect(TokenType.RAW_CLOSE);
|
|
750
|
+
// Program + block span the whole construct `[RAW_OPEN.start, RAW_CLOSE.end)`.
|
|
751
|
+
const programNode = program(bodyStatements, makeLoc(reader.table, rawOpen.start, rawClose.end));
|
|
752
|
+
return blockStatement(reader.table, rawOpen.start, rawClose.end, {
|
|
753
|
+
path,
|
|
754
|
+
params: [],
|
|
755
|
+
hash: undefined,
|
|
756
|
+
programNode,
|
|
757
|
+
hasInverse: false,
|
|
758
|
+
openStrip: { open: false, close: false },
|
|
759
|
+
inverseStrip: { open: false, close: false },
|
|
760
|
+
closeStrip: { open: false, close: false },
|
|
761
|
+
});
|
|
762
|
+
}
|
|
763
|
+
|
|
764
|
+
/**
|
|
765
|
+
* Builds a bare `PathExpression` for a raw-block name (a single unbracketed segment).
|
|
766
|
+
*
|
|
767
|
+
* @param {object} reader - The shared cursor (for the line table).
|
|
768
|
+
* @param {string} name - The raw-block name.
|
|
769
|
+
* @param {number} startOffset - Offset of the name's first code unit.
|
|
770
|
+
* @param {number} endOffset - Offset just past the name.
|
|
771
|
+
* @returns {object} A `PathExpression` node.
|
|
772
|
+
*/
|
|
773
|
+
function pathFromRawName(reader, name, startOffset, endOffset) {
|
|
774
|
+
const subReader = createReader(
|
|
775
|
+
[
|
|
776
|
+
{ type: TokenType.ID, start: startOffset, end: endOffset, value: name },
|
|
777
|
+
{ type: TokenType.EOF, start: endOffset, end: endOffset },
|
|
778
|
+
],
|
|
779
|
+
reader.table,
|
|
780
|
+
reader.source,
|
|
781
|
+
);
|
|
782
|
+
return parsePath(subReader);
|
|
783
|
+
}
|
|
784
|
+
|
|
785
|
+
/**
|
|
786
|
+
* Parses a `{{> name params hash}}` partial (or the `PartialStatement` head of any partial). The
|
|
787
|
+
* `name` is a `PathExpression`, a `SubExpression` (`(…)`), or a `StringLiteral`.
|
|
788
|
+
*
|
|
789
|
+
* @param {object} reader - The shared cursor, positioned on the opener `OPEN`.
|
|
790
|
+
* @returns {object} A `PartialStatement` node.
|
|
791
|
+
*/
|
|
792
|
+
function parsePartial(reader) {
|
|
793
|
+
const open = reader.expect(TokenType.OPEN);
|
|
794
|
+
reader.expect(TokenType.SIGIL); // `>`
|
|
795
|
+
const name = parsePartialName(reader);
|
|
796
|
+
const { params, hash } = parseParametersAndHash(
|
|
797
|
+
reader,
|
|
798
|
+
(token) => token.type === TokenType.CLOSE,
|
|
799
|
+
);
|
|
800
|
+
reader.skipWhitespace();
|
|
801
|
+
const close = reader.expect(TokenType.CLOSE);
|
|
802
|
+
return partialStatement(reader.table, open.start, close.end, {
|
|
803
|
+
name,
|
|
804
|
+
params,
|
|
805
|
+
hash,
|
|
806
|
+
indent: '',
|
|
807
|
+
strip: { open: !!open.strip, close: !!close.strip },
|
|
808
|
+
});
|
|
809
|
+
}
|
|
810
|
+
|
|
811
|
+
/**
|
|
812
|
+
* Parses a `{{#> name}} body {{/name}}` partial block into a `PartialBlockStatement`.
|
|
813
|
+
*
|
|
814
|
+
* @param {object} reader - The shared cursor, positioned on the opener `OPEN`.
|
|
815
|
+
* @returns {object} A `PartialBlockStatement` node.
|
|
816
|
+
*/
|
|
817
|
+
function parsePartialBlock(reader) {
|
|
818
|
+
const open = reader.expect(TokenType.OPEN);
|
|
819
|
+
reader.expect(TokenType.SIGIL); // `#>`
|
|
820
|
+
const name = parsePartialName(reader);
|
|
821
|
+
const { params, hash } = parseParametersAndHash(reader, headTerminator(reader));
|
|
822
|
+
reader.skipWhitespace();
|
|
823
|
+
const openClose = reader.expect(TokenType.CLOSE);
|
|
824
|
+
const bodyStart = openClose.end;
|
|
825
|
+
|
|
826
|
+
const body = parseProgram(reader, () => isCloseTag(reader));
|
|
827
|
+
if (reader.atEnd()) {
|
|
828
|
+
throwUnclosed(reader);
|
|
829
|
+
}
|
|
830
|
+
const bodyEnd = reader.peek().start;
|
|
831
|
+
const programNode = makeSubProgram(reader, body, bodyStart, bodyEnd);
|
|
832
|
+
const matchName = name.type === 'PathExpression' ? name.original : undefined;
|
|
833
|
+
const { closeToken } = consumeCloseTag(reader, matchName, name);
|
|
834
|
+
return partialBlockStatement(reader.table, open.start, closeToken.end, {
|
|
835
|
+
name,
|
|
836
|
+
params,
|
|
837
|
+
hash,
|
|
838
|
+
programNode,
|
|
839
|
+
openStrip: { open: !!open.strip, close: !!openClose.strip },
|
|
840
|
+
closeStrip: { open: false, close: false },
|
|
841
|
+
});
|
|
842
|
+
}
|
|
843
|
+
|
|
844
|
+
/**
|
|
845
|
+
* Parses a partial NAME: a `SubExpression` (`(…)`), a `StringLiteral`, or a `PathExpression`.
|
|
846
|
+
*
|
|
847
|
+
* @param {object} reader - The shared cursor, positioned after the partial sigil.
|
|
848
|
+
* @returns {object} The partial-name node.
|
|
849
|
+
*/
|
|
850
|
+
function parsePartialName(reader) {
|
|
851
|
+
reader.skipWhitespace();
|
|
852
|
+
if (reader.peekType() === TokenType.OPEN_PAREN) {
|
|
853
|
+
return parseSubExpression(reader);
|
|
854
|
+
}
|
|
855
|
+
const literal = parseLiteral(reader);
|
|
856
|
+
if (literal) {
|
|
857
|
+
return literal;
|
|
858
|
+
}
|
|
859
|
+
return parsePath(reader);
|
|
860
|
+
}
|
|
861
|
+
|
|
862
|
+
/**
|
|
863
|
+
* Parses a `{{* name params hash}}` inline decorator into a `Decorator` node.
|
|
864
|
+
*
|
|
865
|
+
* @param {object} reader - The shared cursor, positioned on the opener `OPEN`.
|
|
866
|
+
* @returns {object} A `Decorator` node.
|
|
867
|
+
*/
|
|
868
|
+
function parseDecorator(reader) {
|
|
869
|
+
const open = reader.expect(TokenType.OPEN);
|
|
870
|
+
reader.expect(TokenType.SIGIL); // `*`
|
|
871
|
+
const path = parsePath(reader);
|
|
872
|
+
const { params, hash } = parseParametersAndHash(
|
|
873
|
+
reader,
|
|
874
|
+
(token) => token.type === TokenType.CLOSE,
|
|
875
|
+
);
|
|
876
|
+
reader.skipWhitespace();
|
|
877
|
+
const close = reader.expect(TokenType.CLOSE);
|
|
878
|
+
return decorator(reader.table, open.start, close.end, path, params, hash, {
|
|
879
|
+
open: !!open.strip,
|
|
880
|
+
close: !!close.strip,
|
|
881
|
+
});
|
|
882
|
+
}
|
|
883
|
+
|
|
884
|
+
/**
|
|
885
|
+
* Parses a `{{#* name}} body {{/name}}` decorator block into a `DecoratorBlock` node. Its program
|
|
886
|
+
* carries `blockParams` (undefined in practice) and its `inverse` is `undefined`.
|
|
887
|
+
*
|
|
888
|
+
* @param {object} reader - The shared cursor, positioned on the opener `OPEN`.
|
|
889
|
+
* @returns {object} A `DecoratorBlock` node.
|
|
890
|
+
*/
|
|
891
|
+
function parseDecoratorBlock(reader) {
|
|
892
|
+
const open = reader.expect(TokenType.OPEN);
|
|
893
|
+
reader.expect(TokenType.SIGIL); // `#*`
|
|
894
|
+
const path = parsePath(reader);
|
|
895
|
+
const { params, hash } = parseParametersAndHash(reader, headTerminator(reader));
|
|
896
|
+
const blockParameters = parseBlockParameters(reader);
|
|
897
|
+
reader.skipWhitespace();
|
|
898
|
+
const openClose = reader.expect(TokenType.CLOSE);
|
|
899
|
+
const bodyStart = openClose.end;
|
|
900
|
+
|
|
901
|
+
const body = parseProgram(reader, () => isCloseTag(reader));
|
|
902
|
+
if (reader.atEnd()) {
|
|
903
|
+
throwUnclosed(reader);
|
|
904
|
+
}
|
|
905
|
+
const bodyEnd = reader.peek().start;
|
|
906
|
+
const programNode = makeSubProgram(reader, body, bodyStart, bodyEnd, {
|
|
907
|
+
hasBlockParams: true,
|
|
908
|
+
blockParams: blockParameters,
|
|
909
|
+
});
|
|
910
|
+
const { closeToken } = consumeCloseTag(reader, path.original, path);
|
|
911
|
+
return decoratorBlock(reader.table, open.start, closeToken.end, {
|
|
912
|
+
path,
|
|
913
|
+
params,
|
|
914
|
+
hash,
|
|
915
|
+
programNode,
|
|
916
|
+
inverse: undefined,
|
|
917
|
+
openStrip: { open: !!open.strip, close: !!openClose.strip },
|
|
918
|
+
closeStrip: { open: false, close: false },
|
|
919
|
+
});
|
|
920
|
+
}
|
|
921
|
+
|
|
922
|
+
/**
|
|
923
|
+
* Builds a {@link HandlebarsParseError} (Jison class) whose `hash.loc` spans `[startOffset,
|
|
924
|
+
* endOffset)` — 1-based line, 0-based column, matching upstream's Jison `SourceLocation`.
|
|
925
|
+
*
|
|
926
|
+
* @param {import('./loc.js').LineTable} table - Line table for the source.
|
|
927
|
+
* @param {number} startOffset - Absolute start offset of the error span.
|
|
928
|
+
* @param {number} endOffset - Absolute end offset of the error span.
|
|
929
|
+
* @returns {HandlebarsParseError} The Jison-class parse error.
|
|
930
|
+
*/
|
|
931
|
+
function jisonAt(table, startOffset, endOffset) {
|
|
932
|
+
const start = offsetToPosition(table, startOffset);
|
|
933
|
+
const end = offsetToPosition(table, endOffset);
|
|
934
|
+
return new HandlebarsParseError('Parse error', {
|
|
935
|
+
first_line: start.line,
|
|
936
|
+
first_column: start.column,
|
|
937
|
+
last_line: end.line,
|
|
938
|
+
last_column: end.column,
|
|
939
|
+
});
|
|
940
|
+
}
|
|
941
|
+
|
|
942
|
+
/**
|
|
943
|
+
* Converts a {@link TokenizerError} into the plan's error class for the corresponding lexical
|
|
944
|
+
* case. An unterminated `{{!-- --}}` comment is the exception-class "Unrecognized text" error
|
|
945
|
+
* (NO position — the wrapper reports `1:1`). An unterminated string is the Jison class; upstream
|
|
946
|
+
* anchors it at the preceding call-head token, so the span of the token just before the opening
|
|
947
|
+
* quote is used.
|
|
948
|
+
*
|
|
949
|
+
* @param {TokenizerError} error - The lexical error thrown by the tokenizer.
|
|
950
|
+
* @param {string} source - The original source (for locating the preceding token).
|
|
951
|
+
* @returns {HandlebarsParseError|HandlebarsException|TokenizerError} The converted error.
|
|
952
|
+
*/
|
|
953
|
+
function convertTokenizerError(error, source) {
|
|
954
|
+
if (!(error instanceof TokenizerError)) {
|
|
955
|
+
return error;
|
|
956
|
+
}
|
|
957
|
+
if (error.kind === 'unterminated-comment') {
|
|
958
|
+
// Exception-class lexical error; upstream exposes no position (wrapper falls back to 1:1).
|
|
959
|
+
return new HandlebarsException('Lexical error. Unrecognized text.');
|
|
960
|
+
}
|
|
961
|
+
if (error.kind === 'unterminated-string') {
|
|
962
|
+
// Jison class. Upstream anchors at the preceding call-head token (the id before the
|
|
963
|
+
// opening quote). Locate the maximal id run ending just before the quote at `error.offset`.
|
|
964
|
+
const { start, end } = precedingIdSpan(source, error.offset);
|
|
965
|
+
return jisonAt(buildLineTable(source), start, end);
|
|
966
|
+
}
|
|
967
|
+
// Other lexical kinds (unterminated raw block / segment) belong to W2b-2's block surface;
|
|
968
|
+
// surface them as a Jison error at the reported offset for now.
|
|
969
|
+
return jisonAt(buildLineTable(source), error.offset, error.offset);
|
|
970
|
+
}
|
|
971
|
+
|
|
972
|
+
/**
|
|
973
|
+
* Finds the span `[start, end)` of the bare-word token immediately preceding the code unit at
|
|
974
|
+
* `offset` (skipping a single space between them), for anchoring an unterminated-string error at
|
|
975
|
+
* the call-head token as upstream does.
|
|
976
|
+
*
|
|
977
|
+
* @param {string} source - The source string.
|
|
978
|
+
* @param {number} offset - Offset of the opening quote of the unterminated string.
|
|
979
|
+
* @returns {{start: number, end: number}} The preceding token's span (or a zero-width span at
|
|
980
|
+
* `offset` when none is found).
|
|
981
|
+
*/
|
|
982
|
+
function precedingIdSpan(source, offset) {
|
|
983
|
+
let end = offset;
|
|
984
|
+
// Skip a run of insignificant whitespace before the quote.
|
|
985
|
+
while (end > 0 && /\s/.test(source[end - 1])) {
|
|
986
|
+
end--;
|
|
987
|
+
}
|
|
988
|
+
let start = end;
|
|
989
|
+
while (start > 0 && /[^\s{}"'()[\]=~|@./]/.test(source[start - 1])) {
|
|
990
|
+
start--;
|
|
991
|
+
}
|
|
992
|
+
if (start === end) {
|
|
993
|
+
return { start: offset, end: offset };
|
|
994
|
+
}
|
|
995
|
+
return { start, end };
|
|
996
|
+
}
|