|
1 | 1 | import { parseNameValue } from './names.js'; |
2 | | -import { parseExpression, parseReservedBody } from './expression.js'; |
| 2 | +import { parseExpression } from './expression.js'; |
3 | 3 | import type { ParseContext } from './parse-cst.js'; |
4 | 4 | import type * as CST from './types.js'; |
5 | 5 | import { whitespaces } from './util.js'; |
@@ -29,7 +29,7 @@ export function parseDeclarations( |
29 | 29 | decl = parseLocalDeclaration(ctx, pos); |
30 | 30 | break; |
31 | 31 | default: |
32 | | - decl = parseReservedStatement(ctx, pos, '.' + keyword); |
| 32 | + decl = parseDeclarationJunk(ctx, pos); |
33 | 33 | } |
34 | 34 | declarations.push(decl); |
35 | 35 | pos = decl.end; |
@@ -110,47 +110,19 @@ function parseLocalDeclaration( |
110 | 110 | }; |
111 | 111 | } |
112 | 112 |
|
113 | | -function parseReservedStatement( |
114 | | - ctx: ParseContext, |
115 | | - start: number, |
116 | | - keyword: string |
117 | | -): CST.ReservedStatement { |
118 | | - let pos = start + keyword.length; |
119 | | - pos += whitespaces(ctx.source, pos); |
120 | | - |
121 | | - const body = parseReservedBody(ctx, pos); |
122 | | - let end = body.end; |
123 | | - pos = end + whitespaces(ctx.source, end); |
124 | | - |
125 | | - const values: CST.Expression[] = []; |
126 | | - while (ctx.source[pos] === '{') { |
127 | | - if (ctx.source.startsWith('{{', pos)) break; |
128 | | - const value = parseExpression(ctx, pos); |
129 | | - values.push(value); |
130 | | - end = value.end; |
131 | | - pos = end + whitespaces(ctx.source, end); |
132 | | - } |
133 | | - if (values.length === 0) ctx.onError('missing-syntax', end, '{'); |
134 | | - |
135 | | - return { |
136 | | - type: 'reserved-statement', |
137 | | - start, |
138 | | - end, |
139 | | - keyword: { start, end: keyword.length, value: keyword }, |
140 | | - body, |
141 | | - values |
142 | | - }; |
143 | | -} |
144 | | - |
145 | 113 | function parseDeclarationValue( |
146 | 114 | ctx: ParseContext, |
147 | 115 | start: number |
148 | 116 | ): CST.Expression | CST.Junk { |
149 | | - const { source } = ctx; |
150 | | - if (source[start] === '{') return parseExpression(ctx, start); |
| 117 | + return ctx.source[start] === '{' |
| 118 | + ? parseExpression(ctx, start) |
| 119 | + : parseDeclarationJunk(ctx, start); |
| 120 | +} |
151 | 121 |
|
152 | | - const junkEndOffset = source.substring(start).search(/\.[a-z]|{{/); |
153 | | - const end = junkEndOffset === -1 ? source.length : start + junkEndOffset; |
| 122 | +function parseDeclarationJunk(ctx: ParseContext, start: number): CST.Junk { |
| 123 | + const { source } = ctx; |
| 124 | + const junkEndOffset = source.substring(start + 1).search(/\s*(\.[a-z]|{{)/); |
| 125 | + const end = junkEndOffset === -1 ? source.length : start + 1 + junkEndOffset; |
154 | 126 | ctx.onError('missing-syntax', start, '{'); |
155 | 127 | return { type: 'junk', start, end, source: source.substring(start, end) }; |
156 | 128 | } |
0 commit comments