@chikx/compiler 0.1.0 → 0.1.1
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 +5 -5
- package/package.json +3 -3
- package/src/emitter/index.ts +658 -0
- package/src/formatter/index.ts +544 -0
- package/src/host/node.ts +85 -0
- package/src/index.ts +1021 -0
- package/src/ir/factories.ts +85 -0
- package/src/ir/index.ts +28 -0
- package/src/ir/types.ts +145 -0
- package/src/lexer/at-keywords.ts +35 -0
- package/src/lexer/characters.ts +82 -0
- package/src/lexer/comments.ts +27 -0
- package/src/lexer/diagnostics.ts +42 -0
- package/src/lexer/dollar-refs.ts +23 -0
- package/src/lexer/index.ts +37 -0
- package/src/lexer/names.ts +49 -0
- package/src/lexer/numbers.ts +59 -0
- package/src/lexer/scanner.ts +199 -0
- package/src/lexer/strings.ts +52 -0
- package/src/lexer/urls.ts +63 -0
- package/src/lowering/expand-groups.ts +206 -0
- package/src/lowering/group.ts +119 -0
- package/src/lowering/index.ts +43 -0
- package/src/lowering/lower.ts +441 -0
- package/src/lowering/media.ts +314 -0
- package/src/lowering/merge.ts +163 -0
- package/src/lowering/property-families.ts +194 -0
- package/src/lowering/spans.ts +118 -0
- package/src/naming/index.ts +44 -0
- package/src/naming/mapgen.ts +252 -0
- package/src/naming/merge.ts +114 -0
- package/src/naming/rewrite.ts +333 -0
- package/src/naming/scan.ts +267 -0
- package/src/naming/table.ts +153 -0
- package/src/naming/types.ts +100 -0
- package/src/output/import-path.ts +30 -0
- package/src/output/index.ts +213 -0
- package/src/output/manifest.ts +28 -0
- package/src/output/paths.ts +200 -0
- package/src/output/resolve.ts +401 -0
- package/src/output/validate.ts +251 -0
- package/src/parser/cursor.ts +138 -0
- package/src/parser/declaration.ts +247 -0
- package/src/parser/diagnostics.ts +75 -0
- package/src/parser/group.ts +303 -0
- package/src/parser/index.ts +50 -0
- package/src/parser/modifier.ts +60 -0
- package/src/parser/recovery.ts +88 -0
- package/src/parser/rule.ts +370 -0
- package/src/parser/statement.ts +47 -0
- package/src/parser/statements.ts +667 -0
- package/src/parser/value.ts +265 -0
- package/src/resolver/graph.ts +467 -0
- package/src/resolver/groups.ts +177 -0
- package/src/resolver/index.ts +68 -0
- package/src/resolver/keyframes.ts +131 -0
- package/src/resolver/paths.ts +142 -0
- package/src/resolver/rules.ts +54 -0
- package/src/resolver/scopes.ts +342 -0
- package/src/resolver/types.ts +259 -0
- package/src/selectors/flatten.ts +448 -0
- package/src/selectors/index.ts +14 -0
- package/src/selectors/scan.ts +227 -0
- package/src/selectors/specificity.ts +319 -0
- package/src/setup-list.ts +172 -0
- package/src/sourcemaps/index.ts +25 -0
- package/src/sourcemaps/mappings.ts +190 -0
- package/src/sourcemaps/vlq.ts +29 -0
- package/src/types/ast.ts +318 -0
- package/src/types/diagnostics.ts +124 -0
- package/src/types/options.ts +286 -0
- package/src/types/result.ts +121 -0
- package/src/types/span.ts +18 -0
- package/src/types/token.ts +77 -0
- package/src/variables/ast.ts +182 -0
- package/src/variables/expand.ts +319 -0
- package/src/variables/index.ts +38 -0
- package/src/variables/naming.ts +46 -0
- package/src/variables/runtime.ts +336 -0
- package/src/variables/substitute.ts +148 -0
- package/src/variables/walk.ts +147 -0
- package/dist/output/importPath.d.ts +0 -27
- package/dist/output/importPath.d.ts.map +0 -1
- package/dist/output/importPath.js +0 -84
- package/dist/output/importPath.js.map +0 -1
|
@@ -0,0 +1,667 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
AliasDeclaration,
|
|
3
|
+
Declaration,
|
|
4
|
+
MediaTypesBlock,
|
|
5
|
+
RuntimeBlock,
|
|
6
|
+
Setting,
|
|
7
|
+
SetupBlock,
|
|
8
|
+
Stylesheet,
|
|
9
|
+
StylesheetItem,
|
|
10
|
+
TokenDeclaration,
|
|
11
|
+
UseImport,
|
|
12
|
+
VariablesBlock,
|
|
13
|
+
} from "../types/ast.js";
|
|
14
|
+
import type { ParseResult } from "../types/result.js";
|
|
15
|
+
import type { Token } from "../types/token.js";
|
|
16
|
+
import {
|
|
17
|
+
TokenCursor,
|
|
18
|
+
lastContentIndex,
|
|
19
|
+
skipTriviaAt,
|
|
20
|
+
textOf,
|
|
21
|
+
tokenAt,
|
|
22
|
+
trimWhitespace,
|
|
23
|
+
} from "./cursor.js";
|
|
24
|
+
import { type ParseContext, parseDeclaration } from "./declaration.js";
|
|
25
|
+
import { DiagnosticBag, messages } from "./diagnostics.js";
|
|
26
|
+
import { isGroupAtKeyword, parseGroupDefinition } from "./group.js";
|
|
27
|
+
import { readModifier } from "./modifier.js";
|
|
28
|
+
import { recoverStatement, skipAtRule, skipBalancedBlock, skipStatementBlock } from "./recovery.js";
|
|
29
|
+
import {
|
|
30
|
+
atRuleName,
|
|
31
|
+
classifyStatement,
|
|
32
|
+
isNativeAtRuleName,
|
|
33
|
+
parseNativeAtRule,
|
|
34
|
+
parseRule,
|
|
35
|
+
} from "./rule.js";
|
|
36
|
+
import { containsDollarRef } from "./value.js";
|
|
37
|
+
|
|
38
|
+
/** How a scalar `key: value` line ended. */
|
|
39
|
+
type SimpleTerminator = "semicolon" | "rbrace" | "eof" | "lbrace";
|
|
40
|
+
|
|
41
|
+
interface SimpleValue {
|
|
42
|
+
readonly terminator: SimpleTerminator;
|
|
43
|
+
readonly boundaryIndex: number;
|
|
44
|
+
readonly malformed: boolean;
|
|
45
|
+
readonly bounds: { readonly start: number; readonly end: number };
|
|
46
|
+
readonly valueEnd: number;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Scan a scalar value up to `;`, the closing `}`, or end of input while
|
|
51
|
+
* balancing parentheses and brackets (used by `@setup`, `@variables` and
|
|
52
|
+
* `@media-types`).
|
|
53
|
+
*/
|
|
54
|
+
function scanSimpleValue(ctx: ParseContext, startIndex: number): SimpleValue {
|
|
55
|
+
const { cursor } = ctx;
|
|
56
|
+
const tokens = cursor.tokens;
|
|
57
|
+
const closers: Array<"rparen" | "rbracket"> = [];
|
|
58
|
+
let malformed = false;
|
|
59
|
+
let index = startIndex;
|
|
60
|
+
let terminator: SimpleTerminator = "eof";
|
|
61
|
+
|
|
62
|
+
while (true) {
|
|
63
|
+
const token = tokenAt(tokens, index, cursor.endOffset);
|
|
64
|
+
if (token.kind === "eof") {
|
|
65
|
+
terminator = "eof";
|
|
66
|
+
break;
|
|
67
|
+
}
|
|
68
|
+
if (token.kind === "lparen") closers.push("rparen");
|
|
69
|
+
else if (token.kind === "lbracket") closers.push("rbracket");
|
|
70
|
+
else if (token.kind === "rparen" || token.kind === "rbracket") {
|
|
71
|
+
if (closers.pop() !== token.kind) malformed = true;
|
|
72
|
+
} else if (closers.length === 0) {
|
|
73
|
+
if (token.kind === "semicolon") {
|
|
74
|
+
terminator = "semicolon";
|
|
75
|
+
break;
|
|
76
|
+
}
|
|
77
|
+
if (token.kind === "rbrace") {
|
|
78
|
+
terminator = "rbrace";
|
|
79
|
+
break;
|
|
80
|
+
}
|
|
81
|
+
if (token.kind === "lbrace") {
|
|
82
|
+
terminator = "lbrace";
|
|
83
|
+
break;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
index++;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const bounds = trimWhitespace(tokens, startIndex, index);
|
|
90
|
+
const last = lastContentIndex(tokens, bounds.start, bounds.end);
|
|
91
|
+
return {
|
|
92
|
+
terminator,
|
|
93
|
+
boundaryIndex: index,
|
|
94
|
+
malformed: malformed || closers.length > 0,
|
|
95
|
+
bounds,
|
|
96
|
+
valueEnd: last >= bounds.start ? (tokens[last]?.end ?? 0) : 0,
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** Consume a statement terminator, reporting a missing `;` when one is absent. */
|
|
101
|
+
function finishSimple(
|
|
102
|
+
ctx: ParseContext,
|
|
103
|
+
scan: SimpleValue,
|
|
104
|
+
startTokenStart: number,
|
|
105
|
+
fallbackEnd: number,
|
|
106
|
+
what: string,
|
|
107
|
+
): number {
|
|
108
|
+
const { cursor, bag } = ctx;
|
|
109
|
+
const tokens = cursor.tokens;
|
|
110
|
+
if (scan.terminator === "semicolon") {
|
|
111
|
+
const semicolon = tokenAt(tokens, scan.boundaryIndex, cursor.endOffset);
|
|
112
|
+
cursor.index = scan.boundaryIndex + 1;
|
|
113
|
+
return semicolon.end;
|
|
114
|
+
}
|
|
115
|
+
const end = scan.valueEnd > 0 ? scan.valueEnd : fallbackEnd;
|
|
116
|
+
bag.add("CHIKX1001", messages.missingSemicolon(what), {
|
|
117
|
+
start: startTokenStart,
|
|
118
|
+
end,
|
|
119
|
+
file: cursor.file,
|
|
120
|
+
});
|
|
121
|
+
cursor.index = Math.min(scan.boundaryIndex, tokens.length);
|
|
122
|
+
return end;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function expectBlockOpen(ctx: ParseContext, construct: string): boolean {
|
|
126
|
+
const { cursor, bag } = ctx;
|
|
127
|
+
cursor.skipTrivia();
|
|
128
|
+
const token = cursor.current();
|
|
129
|
+
if (token.kind === "lbrace") {
|
|
130
|
+
cursor.index++;
|
|
131
|
+
return true;
|
|
132
|
+
}
|
|
133
|
+
bag.add("CHIKX1001", messages.missingBrace(construct), cursor.span(token));
|
|
134
|
+
recoverStatement(cursor);
|
|
135
|
+
return false;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function reportUnclosed(ctx: ParseContext, startOffset: number, construct: string): void {
|
|
139
|
+
ctx.bag.add("CHIKX1001", messages.unclosedBlock(construct), {
|
|
140
|
+
start: startOffset,
|
|
141
|
+
end: ctx.cursor.endOffset,
|
|
142
|
+
file: ctx.cursor.file,
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Parse `@setup { key: scalar; ... }`.
|
|
148
|
+
*
|
|
149
|
+
* Settings keep authored order and duplicates; the resolver merges blocks and
|
|
150
|
+
* rejects duplicate or unknown keys (AST `SetupBlock` JSDoc). Values stay raw,
|
|
151
|
+
* quotes included.
|
|
152
|
+
*/
|
|
153
|
+
export function parseSetupBlock(ctx: ParseContext): SetupBlock | undefined {
|
|
154
|
+
const { cursor, bag } = ctx;
|
|
155
|
+
const file = cursor.file;
|
|
156
|
+
const startToken = cursor.current();
|
|
157
|
+
cursor.index++;
|
|
158
|
+
if (!expectBlockOpen(ctx, "@setup")) return undefined;
|
|
159
|
+
|
|
160
|
+
const settings: Setting[] = [];
|
|
161
|
+
let end = cursor.endOffset;
|
|
162
|
+
let closed = false;
|
|
163
|
+
|
|
164
|
+
while (true) {
|
|
165
|
+
cursor.skipTrivia();
|
|
166
|
+
const guard = cursor.index;
|
|
167
|
+
const token = cursor.current();
|
|
168
|
+
|
|
169
|
+
if (token.kind === "eof") break;
|
|
170
|
+
if (token.kind === "rbrace") {
|
|
171
|
+
end = token.end;
|
|
172
|
+
cursor.index++;
|
|
173
|
+
closed = true;
|
|
174
|
+
break;
|
|
175
|
+
}
|
|
176
|
+
if (token.kind === "semicolon") {
|
|
177
|
+
cursor.index++;
|
|
178
|
+
continue;
|
|
179
|
+
}
|
|
180
|
+
if (token.kind !== "ident") {
|
|
181
|
+
bag.add("CHIKX1001", messages.expectedSettingKey, cursor.span(token));
|
|
182
|
+
recoverStatement(cursor);
|
|
183
|
+
} else {
|
|
184
|
+
const setting = parseSetting(ctx, token);
|
|
185
|
+
if (setting !== undefined) settings.push(setting);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
if (cursor.index === guard) cursor.index++;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
if (!closed) reportUnclosed(ctx, startToken.start, "@setup");
|
|
192
|
+
return Object.freeze({
|
|
193
|
+
kind: "setup",
|
|
194
|
+
settings: Object.freeze(settings),
|
|
195
|
+
span: { start: startToken.start, end, file },
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
function parseSetting(ctx: ParseContext, keyToken: Token): Setting | undefined {
|
|
200
|
+
const { cursor, bag } = ctx;
|
|
201
|
+
const file = cursor.file;
|
|
202
|
+
cursor.index++;
|
|
203
|
+
cursor.skipTrivia();
|
|
204
|
+
|
|
205
|
+
const colon = cursor.current();
|
|
206
|
+
if (colon.kind !== "colon") {
|
|
207
|
+
bag.add("CHIKX1001", messages.expectedSettingColon, cursor.span(colon));
|
|
208
|
+
recoverStatement(cursor);
|
|
209
|
+
return undefined;
|
|
210
|
+
}
|
|
211
|
+
cursor.index++;
|
|
212
|
+
|
|
213
|
+
const scan = scanSimpleValue(ctx, cursor.index);
|
|
214
|
+
if (scan.malformed) {
|
|
215
|
+
bag.add("CHIKX1003", messages.malformedValue, {
|
|
216
|
+
start: keyToken.start,
|
|
217
|
+
end: scan.valueEnd > 0 ? scan.valueEnd : keyToken.end,
|
|
218
|
+
file,
|
|
219
|
+
});
|
|
220
|
+
if (scan.terminator === "lbrace") {
|
|
221
|
+
cursor.index = skipBalancedBlock(cursor.tokens, scan.boundaryIndex, cursor.endOffset);
|
|
222
|
+
} else if (scan.terminator === "semicolon") {
|
|
223
|
+
cursor.index = scan.boundaryIndex + 1;
|
|
224
|
+
} else {
|
|
225
|
+
cursor.index = Math.min(scan.boundaryIndex, cursor.tokens.length);
|
|
226
|
+
}
|
|
227
|
+
return undefined;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
const value = textOf(cursor.tokens, scan.bounds.start, scan.bounds.end);
|
|
231
|
+
const valueSpan =
|
|
232
|
+
scan.bounds.start < scan.bounds.end
|
|
233
|
+
? {
|
|
234
|
+
start: cursor.tokens[scan.bounds.start]?.start ?? colon.end,
|
|
235
|
+
end: cursor.tokens[scan.bounds.end - 1]?.end ?? colon.end,
|
|
236
|
+
file,
|
|
237
|
+
}
|
|
238
|
+
: { start: colon.end, end: colon.end, file };
|
|
239
|
+
const end = finishSimple(ctx, scan, keyToken.start, keyToken.end, "a setup setting");
|
|
240
|
+
return Object.freeze({
|
|
241
|
+
kind: "setting",
|
|
242
|
+
key: keyToken.value,
|
|
243
|
+
value,
|
|
244
|
+
valueSpan,
|
|
245
|
+
span: { start: keyToken.start, end, file },
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* Parse `@use "..." [as alias] [styles];` (SPECIFICATION.md §3.2, ADR-0014).
|
|
251
|
+
*
|
|
252
|
+
* The optional `styles` clause merges the target's rules into the importing
|
|
253
|
+
* build; it is recorded as `styles: true` and is absent for token-only imports.
|
|
254
|
+
*/
|
|
255
|
+
export function parseUseImport(ctx: ParseContext): UseImport | undefined {
|
|
256
|
+
const { cursor, bag } = ctx;
|
|
257
|
+
const tokens = cursor.tokens;
|
|
258
|
+
const file = cursor.file;
|
|
259
|
+
const startToken = cursor.current();
|
|
260
|
+
cursor.index++;
|
|
261
|
+
cursor.skipTrivia();
|
|
262
|
+
|
|
263
|
+
const specifier = cursor.current();
|
|
264
|
+
if (specifier.kind !== "string") {
|
|
265
|
+
bag.add("CHIKX1001", messages.expectedModuleSpecifier, cursor.span(specifier));
|
|
266
|
+
recoverStatement(cursor);
|
|
267
|
+
return undefined;
|
|
268
|
+
}
|
|
269
|
+
const path = unquote(specifier.value);
|
|
270
|
+
cursor.index++;
|
|
271
|
+
cursor.skipTrivia();
|
|
272
|
+
|
|
273
|
+
let alias: string | undefined;
|
|
274
|
+
const maybeAs = cursor.current();
|
|
275
|
+
if (maybeAs.kind === "ident" && maybeAs.value.toLowerCase() === "as") {
|
|
276
|
+
cursor.index++;
|
|
277
|
+
cursor.skipTrivia();
|
|
278
|
+
const aliasToken = cursor.current();
|
|
279
|
+
if (aliasToken.kind !== "ident" && aliasToken.kind !== "number") {
|
|
280
|
+
bag.add("CHIKX1001", messages.expectedImportAlias, cursor.span(aliasToken));
|
|
281
|
+
recoverStatement(cursor);
|
|
282
|
+
return undefined;
|
|
283
|
+
}
|
|
284
|
+
alias = aliasToken.value;
|
|
285
|
+
cursor.index++;
|
|
286
|
+
cursor.skipTrivia();
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
let styles: boolean | undefined;
|
|
290
|
+
const maybeStyles = cursor.current();
|
|
291
|
+
if (maybeStyles.kind === "ident" && maybeStyles.value === "styles") {
|
|
292
|
+
styles = true;
|
|
293
|
+
cursor.index++;
|
|
294
|
+
cursor.skipTrivia();
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
const terminator = cursor.current();
|
|
298
|
+
let end: number;
|
|
299
|
+
if (terminator.kind === "semicolon") {
|
|
300
|
+
end = terminator.end;
|
|
301
|
+
cursor.index++;
|
|
302
|
+
} else {
|
|
303
|
+
end = cursor.span(specifier).end;
|
|
304
|
+
bag.add("CHIKX1001", messages.missingSemicolon("an @use import"), {
|
|
305
|
+
start: startToken.start,
|
|
306
|
+
end,
|
|
307
|
+
file,
|
|
308
|
+
});
|
|
309
|
+
cursor.index = Math.min(cursor.index, tokens.length);
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
return Object.freeze({
|
|
313
|
+
kind: "use",
|
|
314
|
+
path,
|
|
315
|
+
...(alias === undefined ? {} : { alias }),
|
|
316
|
+
...(styles === undefined ? {} : { styles }),
|
|
317
|
+
span: { start: startToken.start, end, file },
|
|
318
|
+
});
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
/** Strip one matching pair of quotes without decoding escapes. */
|
|
322
|
+
function unquote(value: string): string {
|
|
323
|
+
if (value.length >= 2) {
|
|
324
|
+
const first = value[0];
|
|
325
|
+
const last = value[value.length - 1];
|
|
326
|
+
if ((first === '"' || first === "'") && last === first) return value.slice(1, -1);
|
|
327
|
+
}
|
|
328
|
+
return value;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
/** Parse `@variables { name: value; ... }` with raw values and optional tokens. */
|
|
332
|
+
export function parseVariablesBlock(ctx: ParseContext): VariablesBlock | undefined {
|
|
333
|
+
const { cursor, bag } = ctx;
|
|
334
|
+
const file = cursor.file;
|
|
335
|
+
const startToken = cursor.current();
|
|
336
|
+
cursor.index++;
|
|
337
|
+
if (!expectBlockOpen(ctx, "@variables")) return undefined;
|
|
338
|
+
|
|
339
|
+
const tokens: TokenDeclaration[] = [];
|
|
340
|
+
let end = cursor.endOffset;
|
|
341
|
+
let closed = false;
|
|
342
|
+
|
|
343
|
+
while (true) {
|
|
344
|
+
cursor.skipTrivia();
|
|
345
|
+
const guard = cursor.index;
|
|
346
|
+
const token = cursor.current();
|
|
347
|
+
|
|
348
|
+
if (token.kind === "eof") break;
|
|
349
|
+
if (token.kind === "rbrace") {
|
|
350
|
+
end = token.end;
|
|
351
|
+
cursor.index++;
|
|
352
|
+
closed = true;
|
|
353
|
+
break;
|
|
354
|
+
}
|
|
355
|
+
if (token.kind === "semicolon") {
|
|
356
|
+
cursor.index++;
|
|
357
|
+
continue;
|
|
358
|
+
}
|
|
359
|
+
if (token.kind !== "ident" && token.kind !== "dollar-ref") {
|
|
360
|
+
bag.add("CHIKX1001", messages.expectedTokenName, cursor.span(token));
|
|
361
|
+
recoverStatement(cursor);
|
|
362
|
+
} else {
|
|
363
|
+
const declaration = parseTokenDeclaration(ctx, token);
|
|
364
|
+
if (declaration !== undefined) tokens.push(declaration);
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
if (cursor.index === guard) cursor.index++;
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
if (!closed) reportUnclosed(ctx, startToken.start, "@variables");
|
|
371
|
+
return Object.freeze({
|
|
372
|
+
kind: "variables",
|
|
373
|
+
tokens: Object.freeze(tokens),
|
|
374
|
+
span: { start: startToken.start, end, file },
|
|
375
|
+
});
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
function parseTokenDeclaration(ctx: ParseContext, nameToken: Token): TokenDeclaration | undefined {
|
|
379
|
+
const { cursor, bag } = ctx;
|
|
380
|
+
const file = cursor.file;
|
|
381
|
+
const name = nameToken.value.startsWith("$") ? nameToken.value.slice(1) : nameToken.value;
|
|
382
|
+
cursor.index++;
|
|
383
|
+
cursor.skipTrivia();
|
|
384
|
+
|
|
385
|
+
const colon = cursor.current();
|
|
386
|
+
if (colon.kind !== "colon") {
|
|
387
|
+
bag.add("CHIKX1001", messages.missingColon(name), cursor.span(colon));
|
|
388
|
+
recoverStatement(cursor);
|
|
389
|
+
return undefined;
|
|
390
|
+
}
|
|
391
|
+
cursor.index++;
|
|
392
|
+
|
|
393
|
+
const scan = scanSimpleValue(ctx, cursor.index);
|
|
394
|
+
if (scan.malformed) {
|
|
395
|
+
bag.add("CHIKX1003", messages.malformedValue, {
|
|
396
|
+
start: nameToken.start,
|
|
397
|
+
end: scan.valueEnd > 0 ? scan.valueEnd : nameToken.end,
|
|
398
|
+
file,
|
|
399
|
+
});
|
|
400
|
+
if (scan.terminator === "lbrace") {
|
|
401
|
+
cursor.index = skipBalancedBlock(cursor.tokens, scan.boundaryIndex, cursor.endOffset);
|
|
402
|
+
} else if (scan.terminator === "semicolon") {
|
|
403
|
+
cursor.index = scan.boundaryIndex + 1;
|
|
404
|
+
} else {
|
|
405
|
+
cursor.index = Math.min(scan.boundaryIndex, cursor.tokens.length);
|
|
406
|
+
}
|
|
407
|
+
return undefined;
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
const value = textOf(cursor.tokens, scan.bounds.start, scan.bounds.end);
|
|
411
|
+
const valueTokens = containsDollarRef(cursor.tokens, scan.bounds.start, scan.bounds.end)
|
|
412
|
+
? Object.freeze(cursor.tokens.slice(scan.bounds.start, scan.bounds.end))
|
|
413
|
+
: undefined;
|
|
414
|
+
const end = finishSimple(ctx, scan, nameToken.start, nameToken.end, "a token declaration");
|
|
415
|
+
|
|
416
|
+
return Object.freeze({
|
|
417
|
+
kind: "token-declaration",
|
|
418
|
+
name,
|
|
419
|
+
value,
|
|
420
|
+
...(valueTokens === undefined ? {} : { tokens: valueTokens }),
|
|
421
|
+
span: { start: nameToken.start, end, file },
|
|
422
|
+
});
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
/** Parse `@runtime { declaration ... }`, including inline variant segments. */
|
|
426
|
+
export function parseRuntimeBlock(ctx: ParseContext): RuntimeBlock | undefined {
|
|
427
|
+
const { cursor, bag } = ctx;
|
|
428
|
+
const file = cursor.file;
|
|
429
|
+
const startToken = cursor.current();
|
|
430
|
+
cursor.index++;
|
|
431
|
+
if (!expectBlockOpen(ctx, "@runtime")) return undefined;
|
|
432
|
+
|
|
433
|
+
const declarations: Declaration[] = [];
|
|
434
|
+
let end = cursor.endOffset;
|
|
435
|
+
let closed = false;
|
|
436
|
+
|
|
437
|
+
while (true) {
|
|
438
|
+
cursor.skipTrivia();
|
|
439
|
+
const guard = cursor.index;
|
|
440
|
+
const token = cursor.current();
|
|
441
|
+
|
|
442
|
+
if (token.kind === "eof") break;
|
|
443
|
+
if (token.kind === "rbrace") {
|
|
444
|
+
end = token.end;
|
|
445
|
+
cursor.index++;
|
|
446
|
+
closed = true;
|
|
447
|
+
break;
|
|
448
|
+
}
|
|
449
|
+
if (token.kind === "semicolon") {
|
|
450
|
+
cursor.index++;
|
|
451
|
+
continue;
|
|
452
|
+
}
|
|
453
|
+
if (token.kind === "at-keyword" || token.kind === "modifier") {
|
|
454
|
+
bag.add("CHIKX1001", messages.unexpectedAtRule, cursor.span(token));
|
|
455
|
+
cursor.index = skipAtRule(cursor.tokens, cursor.index, cursor.endOffset);
|
|
456
|
+
} else if (token.kind === "lbrace") {
|
|
457
|
+
bag.add("CHIKX1001", messages.unexpectedToken, cursor.span(token));
|
|
458
|
+
cursor.index = skipBalancedBlock(cursor.tokens, cursor.index, cursor.endOffset);
|
|
459
|
+
} else if (classifyStatement(cursor) === "rule") {
|
|
460
|
+
bag.add("CHIKX1001", messages.unexpectedStatement, cursor.span(token));
|
|
461
|
+
cursor.index = skipStatementBlock(cursor.tokens, cursor.index, cursor.endOffset);
|
|
462
|
+
} else {
|
|
463
|
+
const declaration = parseDeclaration(ctx);
|
|
464
|
+
if (declaration !== undefined) declarations.push(declaration);
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
if (cursor.index === guard) cursor.index++;
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
if (!closed) reportUnclosed(ctx, startToken.start, "@runtime");
|
|
471
|
+
return Object.freeze({
|
|
472
|
+
kind: "runtime",
|
|
473
|
+
declarations: Object.freeze(declarations),
|
|
474
|
+
span: { start: startToken.start, end, file },
|
|
475
|
+
});
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
/** Parse `@media-types { alias: condition; ... }` with raw trimmed conditions. */
|
|
479
|
+
export function parseMediaTypesBlock(ctx: ParseContext): MediaTypesBlock | undefined {
|
|
480
|
+
const { cursor, bag } = ctx;
|
|
481
|
+
const file = cursor.file;
|
|
482
|
+
const startToken = cursor.current();
|
|
483
|
+
cursor.index++;
|
|
484
|
+
if (!expectBlockOpen(ctx, "@media-types")) return undefined;
|
|
485
|
+
|
|
486
|
+
const aliases: AliasDeclaration[] = [];
|
|
487
|
+
let end = cursor.endOffset;
|
|
488
|
+
let closed = false;
|
|
489
|
+
|
|
490
|
+
while (true) {
|
|
491
|
+
cursor.skipTrivia();
|
|
492
|
+
const guard = cursor.index;
|
|
493
|
+
const token = cursor.current();
|
|
494
|
+
|
|
495
|
+
if (token.kind === "eof") break;
|
|
496
|
+
if (token.kind === "rbrace") {
|
|
497
|
+
end = token.end;
|
|
498
|
+
cursor.index++;
|
|
499
|
+
closed = true;
|
|
500
|
+
break;
|
|
501
|
+
}
|
|
502
|
+
if (token.kind === "semicolon") {
|
|
503
|
+
cursor.index++;
|
|
504
|
+
continue;
|
|
505
|
+
}
|
|
506
|
+
if (token.kind !== "ident" && token.kind !== "number") {
|
|
507
|
+
bag.add("CHIKX1001", messages.expectedAliasName, cursor.span(token));
|
|
508
|
+
recoverStatement(cursor);
|
|
509
|
+
} else {
|
|
510
|
+
const alias = parseAliasDeclaration(ctx, token);
|
|
511
|
+
if (alias !== undefined) aliases.push(alias);
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
if (cursor.index === guard) cursor.index++;
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
if (!closed) reportUnclosed(ctx, startToken.start, "@media-types");
|
|
518
|
+
return Object.freeze({
|
|
519
|
+
kind: "media-types",
|
|
520
|
+
aliases: Object.freeze(aliases),
|
|
521
|
+
span: { start: startToken.start, end, file },
|
|
522
|
+
});
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
function parseAliasDeclaration(ctx: ParseContext, nameToken: Token): AliasDeclaration | undefined {
|
|
526
|
+
const { cursor, bag } = ctx;
|
|
527
|
+
const file = cursor.file;
|
|
528
|
+
const name = nameToken.value.startsWith("@") ? nameToken.value.slice(1) : nameToken.value;
|
|
529
|
+
cursor.index++;
|
|
530
|
+
cursor.skipTrivia();
|
|
531
|
+
|
|
532
|
+
const colon = cursor.current();
|
|
533
|
+
if (colon.kind !== "colon") {
|
|
534
|
+
bag.add("CHIKX1001", messages.missingColon(name), cursor.span(colon));
|
|
535
|
+
recoverStatement(cursor);
|
|
536
|
+
return undefined;
|
|
537
|
+
}
|
|
538
|
+
cursor.index++;
|
|
539
|
+
|
|
540
|
+
const scan = scanSimpleValue(ctx, cursor.index);
|
|
541
|
+
if (scan.malformed) {
|
|
542
|
+
bag.add("CHIKX1003", messages.malformedCondition, {
|
|
543
|
+
start: nameToken.start,
|
|
544
|
+
end: scan.valueEnd > 0 ? scan.valueEnd : nameToken.end,
|
|
545
|
+
file,
|
|
546
|
+
});
|
|
547
|
+
if (scan.terminator === "lbrace") {
|
|
548
|
+
cursor.index = skipBalancedBlock(cursor.tokens, scan.boundaryIndex, cursor.endOffset);
|
|
549
|
+
} else if (scan.terminator === "semicolon") {
|
|
550
|
+
cursor.index = scan.boundaryIndex + 1;
|
|
551
|
+
} else {
|
|
552
|
+
cursor.index = Math.min(scan.boundaryIndex, cursor.tokens.length);
|
|
553
|
+
}
|
|
554
|
+
return undefined;
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
const media = textOf(cursor.tokens, scan.bounds.start, scan.bounds.end);
|
|
558
|
+
const end = finishSimple(ctx, scan, nameToken.start, nameToken.end, "an alias declaration");
|
|
559
|
+
return Object.freeze({
|
|
560
|
+
kind: "alias-declaration",
|
|
561
|
+
name,
|
|
562
|
+
media,
|
|
563
|
+
span: { start: nameToken.start, end, file },
|
|
564
|
+
});
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
/**
|
|
568
|
+
* Top-level dispatch: Chikx directives, rules and native at-rules.
|
|
569
|
+
*
|
|
570
|
+
* A top-level conditional block (an unknown `@alias {`) is a CHIKX1001 error and
|
|
571
|
+
* produces no node; recovery skips the balanced block (D3/ADR-0004).
|
|
572
|
+
*/
|
|
573
|
+
function parseTopLevelAtRule(ctx: ParseContext): StylesheetItem | undefined {
|
|
574
|
+
const { cursor, bag } = ctx;
|
|
575
|
+
const tokens = cursor.tokens;
|
|
576
|
+
const startToken = cursor.current();
|
|
577
|
+
const name = atRuleName(startToken).toLowerCase();
|
|
578
|
+
|
|
579
|
+
if (isGroupAtKeyword(startToken)) return parseGroupDefinition(ctx);
|
|
580
|
+
|
|
581
|
+
switch (name) {
|
|
582
|
+
case "setup":
|
|
583
|
+
return parseSetupBlock(ctx);
|
|
584
|
+
case "use":
|
|
585
|
+
return parseUseImport(ctx);
|
|
586
|
+
case "variables":
|
|
587
|
+
return parseVariablesBlock(ctx);
|
|
588
|
+
case "runtime":
|
|
589
|
+
return parseRuntimeBlock(ctx);
|
|
590
|
+
case "media-types":
|
|
591
|
+
return parseMediaTypesBlock(ctx);
|
|
592
|
+
default:
|
|
593
|
+
break;
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
if (isNativeAtRuleName(name)) return parseNativeAtRule(ctx);
|
|
597
|
+
|
|
598
|
+
const read = readModifier(tokens, cursor.index, tokens.length, cursor.file);
|
|
599
|
+
const afterModifier = read === undefined ? cursor.index + 1 : read.nextIndex;
|
|
600
|
+
const openIndex = skipTriviaAt(tokens, afterModifier);
|
|
601
|
+
const open = tokenAt(tokens, openIndex, cursor.endOffset);
|
|
602
|
+
|
|
603
|
+
if (open.kind === "lbrace") {
|
|
604
|
+
bag.add("CHIKX1001", messages.topLevelConditional, cursor.span(startToken));
|
|
605
|
+
cursor.index = skipBalancedBlock(tokens, openIndex, cursor.endOffset);
|
|
606
|
+
return undefined;
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
return parseNativeAtRule(ctx);
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
/**
|
|
613
|
+
* Tolerant recursive-descent parse of a top-level stylesheet.
|
|
614
|
+
*
|
|
615
|
+
* Never throws: statement-level recovery keeps prior items, every iteration
|
|
616
|
+
* advances, and a recovered AST is always returned (SPECIFICATION.md §12).
|
|
617
|
+
*/
|
|
618
|
+
export function parseStylesheet(tokens: readonly Token[], file: string): ParseResult {
|
|
619
|
+
const cursor = new TokenCursor(tokens, file);
|
|
620
|
+
const bag = new DiagnosticBag();
|
|
621
|
+
const ctx: ParseContext = { cursor, bag };
|
|
622
|
+
const items: StylesheetItem[] = [];
|
|
623
|
+
|
|
624
|
+
while (true) {
|
|
625
|
+
cursor.skipTrivia();
|
|
626
|
+
const guard = cursor.index;
|
|
627
|
+
const token = cursor.current();
|
|
628
|
+
|
|
629
|
+
if (token.kind === "eof") break;
|
|
630
|
+
if (token.kind === "semicolon") {
|
|
631
|
+
cursor.index++;
|
|
632
|
+
continue;
|
|
633
|
+
}
|
|
634
|
+
if (token.kind === "rbrace") {
|
|
635
|
+
bag.add("CHIKX1001", messages.unexpectedToken, cursor.span(token));
|
|
636
|
+
cursor.index++;
|
|
637
|
+
continue;
|
|
638
|
+
}
|
|
639
|
+
if (token.kind === "lbrace") {
|
|
640
|
+
bag.add("CHIKX1001", messages.unexpectedToken, cursor.span(token));
|
|
641
|
+
cursor.index = skipBalancedBlock(tokens, cursor.index, cursor.endOffset);
|
|
642
|
+
} else if (token.kind === "at-keyword" || token.kind === "modifier") {
|
|
643
|
+
const item = parseTopLevelAtRule(ctx);
|
|
644
|
+
if (item !== undefined) items.push(item);
|
|
645
|
+
} else if (classifyStatement(cursor) === "rule") {
|
|
646
|
+
const rule = parseRule(ctx);
|
|
647
|
+
if (rule !== undefined) items.push(rule);
|
|
648
|
+
} else {
|
|
649
|
+
bag.add("CHIKX1001", messages.unexpectedStatement, cursor.span(token));
|
|
650
|
+
recoverStatement(cursor);
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
if (cursor.index === guard) {
|
|
654
|
+
bag.add("CHIKX1001", messages.internalRecovery, cursor.span(token));
|
|
655
|
+
cursor.index++;
|
|
656
|
+
}
|
|
657
|
+
}
|
|
658
|
+
|
|
659
|
+
const ast: Stylesheet = Object.freeze({
|
|
660
|
+
kind: "stylesheet",
|
|
661
|
+
file,
|
|
662
|
+
items: Object.freeze(items),
|
|
663
|
+
span: { start: 0, end: cursor.endOffset, file },
|
|
664
|
+
});
|
|
665
|
+
|
|
666
|
+
return { ast, diagnostics: bag.finish() };
|
|
667
|
+
}
|