@chikx/compiler 0.0.0-stage → 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/LICENSE +21 -0
- package/README.md +40 -2
- package/dist/emitter/index.d.ts +96 -0
- package/dist/emitter/index.d.ts.map +1 -0
- package/dist/emitter/index.js +415 -0
- package/dist/emitter/index.js.map +1 -0
- package/dist/formatter/index.d.ts +53 -0
- package/dist/formatter/index.d.ts.map +1 -0
- package/dist/formatter/index.js +464 -0
- package/dist/formatter/index.js.map +1 -0
- package/dist/host/node.d.ts +34 -0
- package/dist/host/node.d.ts.map +1 -0
- package/dist/host/node.js +80 -0
- package/dist/host/node.js.map +1 -0
- package/dist/index.d.ts +93 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +792 -0
- package/dist/index.js.map +1 -0
- package/dist/ir/factories.d.ts +35 -0
- package/dist/ir/factories.d.ts.map +1 -0
- package/dist/ir/factories.js +47 -0
- package/dist/ir/factories.js.map +1 -0
- package/dist/ir/index.d.ts +13 -0
- package/dist/ir/index.d.ts.map +1 -0
- package/dist/ir/index.js +2 -0
- package/dist/ir/index.js.map +1 -0
- package/dist/ir/types.d.ts +139 -0
- package/dist/ir/types.d.ts.map +1 -0
- package/dist/ir/types.js +2 -0
- package/dist/ir/types.js.map +1 -0
- package/dist/lexer/at-keywords.d.ts +27 -0
- package/dist/lexer/at-keywords.d.ts.map +1 -0
- package/dist/lexer/at-keywords.js +35 -0
- package/dist/lexer/at-keywords.js.map +1 -0
- package/dist/lexer/characters.d.ts +38 -0
- package/dist/lexer/characters.d.ts.map +1 -0
- package/dist/lexer/characters.js +74 -0
- package/dist/lexer/characters.js.map +1 -0
- package/dist/lexer/comments.d.ts +22 -0
- package/dist/lexer/comments.d.ts.map +1 -0
- package/dist/lexer/comments.js +20 -0
- package/dist/lexer/comments.js.map +1 -0
- package/dist/lexer/diagnostics.d.ts +24 -0
- package/dist/lexer/diagnostics.d.ts.map +1 -0
- package/dist/lexer/diagnostics.js +29 -0
- package/dist/lexer/diagnostics.js.map +1 -0
- package/dist/lexer/dollar-refs.d.ts +13 -0
- package/dist/lexer/dollar-refs.d.ts.map +1 -0
- package/dist/lexer/dollar-refs.js +22 -0
- package/dist/lexer/dollar-refs.js.map +1 -0
- package/dist/lexer/index.d.ts +32 -0
- package/dist/lexer/index.d.ts.map +1 -0
- package/dist/lexer/index.js +26 -0
- package/dist/lexer/index.js.map +1 -0
- package/dist/lexer/names.d.ts +23 -0
- package/dist/lexer/names.d.ts.map +1 -0
- package/dist/lexer/names.js +48 -0
- package/dist/lexer/names.js.map +1 -0
- package/dist/lexer/numbers.d.ts +22 -0
- package/dist/lexer/numbers.d.ts.map +1 -0
- package/dist/lexer/numbers.js +60 -0
- package/dist/lexer/numbers.js.map +1 -0
- package/dist/lexer/scanner.d.ts +30 -0
- package/dist/lexer/scanner.d.ts.map +1 -0
- package/dist/lexer/scanner.js +158 -0
- package/dist/lexer/scanner.js.map +1 -0
- package/dist/lexer/strings.d.ts +25 -0
- package/dist/lexer/strings.d.ts.map +1 -0
- package/dist/lexer/strings.js +41 -0
- package/dist/lexer/strings.js.map +1 -0
- package/dist/lexer/urls.d.ts +28 -0
- package/dist/lexer/urls.d.ts.map +1 -0
- package/dist/lexer/urls.js +54 -0
- package/dist/lexer/urls.js.map +1 -0
- package/dist/lowering/expand-groups.d.ts +61 -0
- package/dist/lowering/expand-groups.d.ts.map +1 -0
- package/dist/lowering/expand-groups.js +117 -0
- package/dist/lowering/expand-groups.js.map +1 -0
- package/dist/lowering/group.d.ts +30 -0
- package/dist/lowering/group.d.ts.map +1 -0
- package/dist/lowering/group.js +109 -0
- package/dist/lowering/group.js.map +1 -0
- package/dist/lowering/index.d.ts +26 -0
- package/dist/lowering/index.d.ts.map +1 -0
- package/dist/lowering/index.js +23 -0
- package/dist/lowering/index.js.map +1 -0
- package/dist/lowering/lower.d.ts +21 -0
- package/dist/lowering/lower.d.ts.map +1 -0
- package/dist/lowering/lower.js +296 -0
- package/dist/lowering/lower.js.map +1 -0
- package/dist/lowering/media.d.ts +63 -0
- package/dist/lowering/media.d.ts.map +1 -0
- package/dist/lowering/media.js +237 -0
- package/dist/lowering/media.js.map +1 -0
- package/dist/lowering/merge.d.ts +35 -0
- package/dist/lowering/merge.d.ts.map +1 -0
- package/dist/lowering/merge.js +146 -0
- package/dist/lowering/merge.js.map +1 -0
- package/dist/lowering/property-families.d.ts +50 -0
- package/dist/lowering/property-families.d.ts.map +1 -0
- package/dist/lowering/property-families.js +175 -0
- package/dist/lowering/property-families.js.map +1 -0
- package/dist/lowering/spans.d.ts +36 -0
- package/dist/lowering/spans.d.ts.map +1 -0
- package/dist/lowering/spans.js +100 -0
- package/dist/lowering/spans.js.map +1 -0
- package/dist/naming/index.d.ts +37 -0
- package/dist/naming/index.d.ts.map +1 -0
- package/dist/naming/index.js +34 -0
- package/dist/naming/index.js.map +1 -0
- package/dist/naming/mapgen.d.ts +70 -0
- package/dist/naming/mapgen.d.ts.map +1 -0
- package/dist/naming/mapgen.js +153 -0
- package/dist/naming/mapgen.js.map +1 -0
- package/dist/naming/merge.d.ts +62 -0
- package/dist/naming/merge.d.ts.map +1 -0
- package/dist/naming/merge.js +77 -0
- package/dist/naming/merge.js.map +1 -0
- package/dist/naming/rewrite.d.ts +14 -0
- package/dist/naming/rewrite.d.ts.map +1 -0
- package/dist/naming/rewrite.js +191 -0
- package/dist/naming/rewrite.js.map +1 -0
- package/dist/naming/scan.d.ts +80 -0
- package/dist/naming/scan.d.ts.map +1 -0
- package/dist/naming/scan.js +193 -0
- package/dist/naming/scan.js.map +1 -0
- package/dist/naming/table.d.ts +68 -0
- package/dist/naming/table.d.ts.map +1 -0
- package/dist/naming/table.js +132 -0
- package/dist/naming/table.js.map +1 -0
- package/dist/naming/types.d.ts +94 -0
- package/dist/naming/types.d.ts.map +1 -0
- package/dist/naming/types.js +2 -0
- package/dist/naming/types.js.map +1 -0
- package/dist/output/import-path.d.ts +24 -0
- package/dist/output/import-path.d.ts.map +1 -0
- package/dist/output/import-path.js +30 -0
- package/dist/output/import-path.js.map +1 -0
- package/dist/output/index.d.ts +40 -0
- package/dist/output/index.d.ts.map +1 -0
- package/dist/output/index.js +138 -0
- package/dist/output/index.js.map +1 -0
- package/dist/output/manifest.d.ts +17 -0
- package/dist/output/manifest.d.ts.map +1 -0
- package/dist/output/manifest.js +22 -0
- package/dist/output/manifest.js.map +1 -0
- package/dist/output/paths.d.ts +54 -0
- package/dist/output/paths.d.ts.map +1 -0
- package/dist/output/paths.js +183 -0
- package/dist/output/paths.js.map +1 -0
- package/dist/output/resolve.d.ts +100 -0
- package/dist/output/resolve.d.ts.map +1 -0
- package/dist/output/resolve.js +313 -0
- package/dist/output/resolve.js.map +1 -0
- package/dist/output/validate.d.ts +82 -0
- package/dist/output/validate.d.ts.map +1 -0
- package/dist/output/validate.js +128 -0
- package/dist/output/validate.js.map +1 -0
- package/dist/parser/cursor.d.ts +55 -0
- package/dist/parser/cursor.d.ts.map +1 -0
- package/dist/parser/cursor.js +120 -0
- package/dist/parser/cursor.js.map +1 -0
- package/dist/parser/declaration.d.ts +21 -0
- package/dist/parser/declaration.d.ts.map +1 -0
- package/dist/parser/declaration.js +201 -0
- package/dist/parser/declaration.js.map +1 -0
- package/dist/parser/diagnostics.d.ts +52 -0
- package/dist/parser/diagnostics.d.ts.map +1 -0
- package/dist/parser/diagnostics.js +55 -0
- package/dist/parser/diagnostics.js.map +1 -0
- package/dist/parser/group.d.ts +26 -0
- package/dist/parser/group.d.ts.map +1 -0
- package/dist/parser/group.js +219 -0
- package/dist/parser/group.js.map +1 -0
- package/dist/parser/index.d.ts +30 -0
- package/dist/parser/index.d.ts.map +1 -0
- package/dist/parser/index.js +48 -0
- package/dist/parser/index.js.map +1 -0
- package/dist/parser/modifier.d.ts +25 -0
- package/dist/parser/modifier.d.ts.map +1 -0
- package/dist/parser/modifier.js +41 -0
- package/dist/parser/modifier.js.map +1 -0
- package/dist/parser/recovery.d.ts +23 -0
- package/dist/parser/recovery.d.ts.map +1 -0
- package/dist/parser/recovery.js +80 -0
- package/dist/parser/recovery.js.map +1 -0
- package/dist/parser/rule.d.ts +39 -0
- package/dist/parser/rule.d.ts.map +1 -0
- package/dist/parser/rule.js +337 -0
- package/dist/parser/rule.js.map +1 -0
- package/dist/parser/statement.d.ts +21 -0
- package/dist/parser/statement.d.ts.map +1 -0
- package/dist/parser/statement.js +48 -0
- package/dist/parser/statement.js.map +1 -0
- package/dist/parser/statements.d.ts +33 -0
- package/dist/parser/statements.d.ts.map +1 -0
- package/dist/parser/statements.js +605 -0
- package/dist/parser/statements.js.map +1 -0
- package/dist/parser/value.d.ts +57 -0
- package/dist/parser/value.d.ts.map +1 -0
- package/dist/parser/value.js +226 -0
- package/dist/parser/value.js.map +1 -0
- package/dist/resolver/graph.d.ts +20 -0
- package/dist/resolver/graph.d.ts.map +1 -0
- package/dist/resolver/graph.js +382 -0
- package/dist/resolver/graph.js.map +1 -0
- package/dist/resolver/groups.d.ts +70 -0
- package/dist/resolver/groups.d.ts.map +1 -0
- package/dist/resolver/groups.js +143 -0
- package/dist/resolver/groups.js.map +1 -0
- package/dist/resolver/index.d.ts +46 -0
- package/dist/resolver/index.d.ts.map +1 -0
- package/dist/resolver/index.js +46 -0
- package/dist/resolver/index.js.map +1 -0
- package/dist/resolver/keyframes.d.ts +56 -0
- package/dist/resolver/keyframes.d.ts.map +1 -0
- package/dist/resolver/keyframes.js +102 -0
- package/dist/resolver/keyframes.js.map +1 -0
- package/dist/resolver/paths.d.ts +37 -0
- package/dist/resolver/paths.d.ts.map +1 -0
- package/dist/resolver/paths.js +135 -0
- package/dist/resolver/paths.js.map +1 -0
- package/dist/resolver/rules.d.ts +34 -0
- package/dist/resolver/rules.d.ts.map +1 -0
- package/dist/resolver/rules.js +52 -0
- package/dist/resolver/rules.js.map +1 -0
- package/dist/resolver/scopes.d.ts +44 -0
- package/dist/resolver/scopes.d.ts.map +1 -0
- package/dist/resolver/scopes.js +213 -0
- package/dist/resolver/scopes.js.map +1 -0
- package/dist/resolver/types.d.ts +238 -0
- package/dist/resolver/types.d.ts.map +1 -0
- package/dist/resolver/types.js +11 -0
- package/dist/resolver/types.js.map +1 -0
- package/dist/selectors/flatten.d.ts +95 -0
- package/dist/selectors/flatten.d.ts.map +1 -0
- package/dist/selectors/flatten.js +384 -0
- package/dist/selectors/flatten.js.map +1 -0
- package/dist/selectors/index.d.ts +15 -0
- package/dist/selectors/index.d.ts.map +1 -0
- package/dist/selectors/index.js +14 -0
- package/dist/selectors/index.js.map +1 -0
- package/dist/selectors/scan.d.ts +54 -0
- package/dist/selectors/scan.d.ts.map +1 -0
- package/dist/selectors/scan.js +191 -0
- package/dist/selectors/scan.js.map +1 -0
- package/dist/selectors/specificity.d.ts +28 -0
- package/dist/selectors/specificity.d.ts.map +1 -0
- package/dist/selectors/specificity.js +297 -0
- package/dist/selectors/specificity.js.map +1 -0
- package/dist/setup-list.d.ts +41 -0
- package/dist/setup-list.d.ts.map +1 -0
- package/dist/setup-list.js +122 -0
- package/dist/setup-list.js.map +1 -0
- package/dist/sourcemaps/index.d.ts +14 -0
- package/dist/sourcemaps/index.d.ts.map +1 -0
- package/dist/sourcemaps/index.js +13 -0
- package/dist/sourcemaps/index.js.map +1 -0
- package/dist/sourcemaps/mappings.d.ts +77 -0
- package/dist/sourcemaps/mappings.d.ts.map +1 -0
- package/dist/sourcemaps/mappings.js +124 -0
- package/dist/sourcemaps/mappings.js.map +1 -0
- package/dist/sourcemaps/vlq.d.ts +12 -0
- package/dist/sourcemaps/vlq.d.ts.map +1 -0
- package/dist/sourcemaps/vlq.js +29 -0
- package/dist/sourcemaps/vlq.js.map +1 -0
- package/dist/types/ast.d.ts +291 -0
- package/dist/types/ast.d.ts.map +1 -0
- package/dist/types/ast.js +2 -0
- package/dist/types/ast.js.map +1 -0
- package/dist/types/diagnostics.d.ts +55 -0
- package/dist/types/diagnostics.d.ts.map +1 -0
- package/dist/types/diagnostics.js +61 -0
- package/dist/types/diagnostics.js.map +1 -0
- package/dist/types/options.d.ts +274 -0
- package/dist/types/options.d.ts.map +1 -0
- package/dist/types/options.js +11 -0
- package/dist/types/options.js.map +1 -0
- package/dist/types/result.d.ts +112 -0
- package/dist/types/result.d.ts.map +1 -0
- package/dist/types/result.js +2 -0
- package/dist/types/result.js.map +1 -0
- package/dist/types/span.d.ts +19 -0
- package/dist/types/span.d.ts.map +1 -0
- package/dist/types/span.js +2 -0
- package/dist/types/span.js.map +1 -0
- package/dist/types/token.d.ts +68 -0
- package/dist/types/token.d.ts.map +1 -0
- package/dist/types/token.js +2 -0
- package/dist/types/token.js.map +1 -0
- package/dist/variables/ast.d.ts +56 -0
- package/dist/variables/ast.d.ts.map +1 -0
- package/dist/variables/ast.js +167 -0
- package/dist/variables/ast.js.map +1 -0
- package/dist/variables/expand.d.ts +109 -0
- package/dist/variables/expand.d.ts.map +1 -0
- package/dist/variables/expand.js +213 -0
- package/dist/variables/expand.js.map +1 -0
- package/dist/variables/index.d.ts +36 -0
- package/dist/variables/index.d.ts.map +1 -0
- package/dist/variables/index.js +33 -0
- package/dist/variables/index.js.map +1 -0
- package/dist/variables/naming.d.ts +39 -0
- package/dist/variables/naming.d.ts.map +1 -0
- package/dist/variables/naming.js +14 -0
- package/dist/variables/naming.js.map +1 -0
- package/dist/variables/runtime.d.ts +81 -0
- package/dist/variables/runtime.d.ts.map +1 -0
- package/dist/variables/runtime.js +228 -0
- package/dist/variables/runtime.js.map +1 -0
- package/dist/variables/substitute.d.ts +65 -0
- package/dist/variables/substitute.d.ts.map +1 -0
- package/dist/variables/substitute.js +73 -0
- package/dist/variables/substitute.js.map +1 -0
- package/dist/variables/walk.d.ts +37 -0
- package/dist/variables/walk.d.ts.map +1 -0
- package/dist/variables/walk.js +113 -0
- package/dist/variables/walk.js.map +1 -0
- package/package.json +35 -4
- 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
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import type { Diagnostic, DiagnosticCode } from "../types/diagnostics.js";
|
|
2
|
+
import { error } from "../types/diagnostics.js";
|
|
3
|
+
import type { Span } from "../types/span.js";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Collects parser diagnostics and hands them back in source order.
|
|
7
|
+
*
|
|
8
|
+
* Syntax diagnostics are statement-scoped per SPECIFICATION.md §12: recovery
|
|
9
|
+
* reports once per statement and resumes at the next safe boundary.
|
|
10
|
+
*/
|
|
11
|
+
export class DiagnosticBag {
|
|
12
|
+
private readonly collected: Diagnostic[] = [];
|
|
13
|
+
|
|
14
|
+
add(code: DiagnosticCode, message: string, span: Span): void {
|
|
15
|
+
this.collected.push(error(code, message, span));
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** Diagnostics sorted by start (then end) so ordering is deterministic. */
|
|
19
|
+
finish(): readonly Diagnostic[] {
|
|
20
|
+
return Object.freeze(
|
|
21
|
+
[...this.collected].sort(
|
|
22
|
+
(left, right) => left.span.start - right.span.start || left.span.end - right.span.end,
|
|
23
|
+
),
|
|
24
|
+
);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* User-facing wording for parser diagnostics.
|
|
30
|
+
*
|
|
31
|
+
* Syntax fixtures match on `messageContains` (tests/fixtures/README.md), so each
|
|
32
|
+
* message carries the phrase the fixture pins: "conditional block", "semicolon",
|
|
33
|
+
* "malformed" and "block".
|
|
34
|
+
*/
|
|
35
|
+
export const messages = {
|
|
36
|
+
topLevelConditional:
|
|
37
|
+
"conditional block outside rule: conditional blocks are only valid inside a rule (D3)",
|
|
38
|
+
conditionalNeedsBlock: "conditional block is missing its '{' block",
|
|
39
|
+
nativeInConditional:
|
|
40
|
+
"native at-rule is not allowed inside a conditional block; move it into a rule",
|
|
41
|
+
unclosedBlock: (construct: string) =>
|
|
42
|
+
`unclosed block: expected '}' before end of file for ${construct}`,
|
|
43
|
+
missingBrace: (construct: string) => `syntax error: expected '{' to open ${construct}`,
|
|
44
|
+
unexpectedStatement: "syntax error: expected a declaration or nested rule",
|
|
45
|
+
unexpectedToken: "syntax error: unexpected token",
|
|
46
|
+
unexpectedAtRule: "syntax error: at-rule is not allowed here",
|
|
47
|
+
emptySelector: "syntax error: rule selector is missing before '{'",
|
|
48
|
+
emptyProperty: "syntax error: declaration property is missing before ':'",
|
|
49
|
+
missingColon: (property: string) => `syntax error: expected ':' after property '${property}'`,
|
|
50
|
+
missingSemicolon: (what: string) =>
|
|
51
|
+
`missing semicolon after ${what}; v0.1 requires a semicolon for Chikx declarations (ADR-0011)`,
|
|
52
|
+
malformedValue: "malformed value: unbalanced parentheses or brackets in declaration",
|
|
53
|
+
malformedCondition: "malformed condition: unbalanced parentheses or brackets",
|
|
54
|
+
expectedModuleSpecifier: "syntax error: expected a quoted module specifier after @use",
|
|
55
|
+
expectedImportAlias: "syntax error: expected an identifier after 'as' in @use",
|
|
56
|
+
expectedSettingKey: "syntax error: expected a setup setting key",
|
|
57
|
+
expectedSettingColon: "syntax error: expected ':' after a setup setting key",
|
|
58
|
+
expectedTokenName: "syntax error: expected a token name inside @variables",
|
|
59
|
+
expectedAliasName: "syntax error: expected an alias name inside @media-types",
|
|
60
|
+
groupMissingName:
|
|
61
|
+
'syntax error: expected a group name after "@group."; write @group.<name> contiguously (ADR-0017)',
|
|
62
|
+
groupMalformedName:
|
|
63
|
+
'syntax error: malformed group name; "@group", "." and the name must be contiguous and the name a CSS identifier (ADR-0017)',
|
|
64
|
+
groupNeedsBlock: (group: string) =>
|
|
65
|
+
`syntax error: expected '{' to open the ${group} block; the block is required in v0.1 (ADR-0017)`,
|
|
66
|
+
groupConditionalUseSite: (group: string) =>
|
|
67
|
+
`syntax error: conditional group use site "${group} @…" is not supported; an unconditional use-site block is required in v0.1 (ADR-0017)`,
|
|
68
|
+
groupDeclarationsOnly: (group: string) =>
|
|
69
|
+
`syntax error: ${group} accepts declarations only; nested rules, conditional blocks and at-rules are not allowed (ADR-0017)`,
|
|
70
|
+
groupNotAllowedHere: (group: string, context: string) =>
|
|
71
|
+
`syntax error: ${group} is not allowed inside ${context}; group use sites belong in a rule body (ADR-0017)`,
|
|
72
|
+
unclosedTypeBlock: (construct: string) =>
|
|
73
|
+
`unclosed block: expected '}' before end of file for ${construct}`,
|
|
74
|
+
internalRecovery: "syntax error: parser recovered from an unexpected token",
|
|
75
|
+
} as const;
|
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
import type { Declaration, GroupDefinition, GroupUseSite } from "../types/ast.js";
|
|
2
|
+
import type { Span } from "../types/span.js";
|
|
3
|
+
import type { Token } from "../types/token.js";
|
|
4
|
+
import { skipTriviaAt, tokenAt } from "./cursor.js";
|
|
5
|
+
import { type ParseContext, parseDeclaration } from "./declaration.js";
|
|
6
|
+
import { messages } from "./diagnostics.js";
|
|
7
|
+
import { skipAtRule, skipBalancedBlock, skipStatementBlock } from "./recovery.js";
|
|
8
|
+
import { classifyStatement } from "./statement.js";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* `@group.<name>` definitions and use sites (ADR-0017, SPECIFICATION.md §3.7).
|
|
12
|
+
*
|
|
13
|
+
* The lexer keeps the three parts as an `at-keyword`, a `.` delim and an
|
|
14
|
+
* identifier, so this module recognises the dotted form by requiring the three
|
|
15
|
+
* tokens to be adjacent in the source: `@group.card` is a group construct, while
|
|
16
|
+
* `@group .card`, `@group.` and a bare `@group` are malformed and reported as
|
|
17
|
+
* CHIKX1001. Position decides the node kind: a top-level statement is a
|
|
18
|
+
* {@link GroupDefinition}, the same statement inside a rule body is a
|
|
19
|
+
* {@link GroupUseSite} (§3.2 grammar).
|
|
20
|
+
*
|
|
21
|
+
* Both blocks accept declarations and nothing else. A nested rule, conditional
|
|
22
|
+
* block, native at-rule or further `@group.` statement is one CHIKX1001 per
|
|
23
|
+
* offending statement, the statement is omitted and the rest of the block
|
|
24
|
+
* survives. No expansion, printing or symbol resolution happens here — that is
|
|
25
|
+
* plan G3/G5/G2 work.
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
/** A well-formed `@group.<name>` header. */
|
|
29
|
+
interface GroupHeader {
|
|
30
|
+
readonly kind: "group-header";
|
|
31
|
+
/** Group name exactly as authored, without the `@group.` prefix. */
|
|
32
|
+
readonly name: string;
|
|
33
|
+
/** Covers `@group` through the name. */
|
|
34
|
+
readonly span: Span;
|
|
35
|
+
/** Raw index of the first token after the name. */
|
|
36
|
+
readonly nextIndex: number;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** A `@group` attempt whose dotted form is missing, spaced or not an identifier. */
|
|
40
|
+
interface MalformedGroupHeader {
|
|
41
|
+
readonly kind: "malformed-group-header";
|
|
42
|
+
readonly message: string;
|
|
43
|
+
/** Covers `@group` through whatever dotted parts were authored. */
|
|
44
|
+
readonly span: Span;
|
|
45
|
+
/**
|
|
46
|
+
* `statement` skips the whole construct (including a following block);
|
|
47
|
+
* `token` advances past `@group` only, so a following rule on a later line
|
|
48
|
+
* still parses after a missing dotted form.
|
|
49
|
+
*/
|
|
50
|
+
readonly recover: "statement" | "token";
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Declarations and bounds parsed from one group block. */
|
|
54
|
+
interface GroupBody {
|
|
55
|
+
readonly declarations: readonly Declaration[];
|
|
56
|
+
/** Covers the closing `}`, or end of input when the block is unclosed. */
|
|
57
|
+
readonly end: number;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** True when `token` is the reserved `@group` directive name (case-insensitive). */
|
|
61
|
+
export function isGroupAtKeyword(token: Token): boolean {
|
|
62
|
+
if (token.kind !== "at-keyword") return false;
|
|
63
|
+
const name = token.value.startsWith("@") ? token.value.slice(1) : token.value;
|
|
64
|
+
return name.toLowerCase() === "group";
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** Display form of a group name for diagnostic messages (`@group.card`). */
|
|
68
|
+
function groupText(name: string): string {
|
|
69
|
+
return `@group.${name}`;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Read a `@group.<name>` header at `index`, or describe why it is malformed.
|
|
74
|
+
*
|
|
75
|
+
* Contiguity is token adjacency: the `.` must start where `@group` ends and the
|
|
76
|
+
* name where `.` ends, so any whitespace or comment in the dotted form is
|
|
77
|
+
* rejected. The name must be a CSS identifier, which rules out digit-leading
|
|
78
|
+
* members such as `@group.2xl`.
|
|
79
|
+
*/
|
|
80
|
+
function readGroupHeader(ctx: ParseContext, index: number): GroupHeader | MalformedGroupHeader {
|
|
81
|
+
const { cursor } = ctx;
|
|
82
|
+
const tokens = cursor.tokens;
|
|
83
|
+
const file = cursor.file;
|
|
84
|
+
const at = tokenAt(tokens, index, cursor.endOffset);
|
|
85
|
+
const dot = tokenAt(tokens, index + 1, cursor.endOffset);
|
|
86
|
+
const name = tokenAt(tokens, index + 2, cursor.endOffset);
|
|
87
|
+
|
|
88
|
+
if (
|
|
89
|
+
dot.kind === "delim" &&
|
|
90
|
+
dot.value === "." &&
|
|
91
|
+
dot.start === at.end &&
|
|
92
|
+
name.kind === "ident" &&
|
|
93
|
+
name.start === dot.end
|
|
94
|
+
) {
|
|
95
|
+
return {
|
|
96
|
+
kind: "group-header",
|
|
97
|
+
name: name.value,
|
|
98
|
+
span: { start: at.start, end: name.end, file },
|
|
99
|
+
nextIndex: index + 3,
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// Point the diagnostic at the whole authored attempt: `@group`, an optional
|
|
104
|
+
// (possibly spaced) dot and an optional name-like token after it.
|
|
105
|
+
const afterAtIndex = skipTriviaAt(tokens, index + 1);
|
|
106
|
+
const afterAt = tokenAt(tokens, afterAtIndex, cursor.endOffset);
|
|
107
|
+
const spacedDot = afterAt.kind === "delim" && afterAt.value === ".";
|
|
108
|
+
// `@group.2xl` lexes the dotted tail as one number token, so a name that
|
|
109
|
+
// cannot be an identifier is still recognised as a dotted-form attempt.
|
|
110
|
+
const numericTail = afterAt.kind === "number" && afterAt.value.startsWith(".");
|
|
111
|
+
const startsWithDot = spacedDot || numericTail;
|
|
112
|
+
|
|
113
|
+
let end = at.end;
|
|
114
|
+
let message: string = messages.groupMissingName;
|
|
115
|
+
if (startsWithDot) {
|
|
116
|
+
end = afterAt.end;
|
|
117
|
+
message = messages.groupMalformedName;
|
|
118
|
+
if (spacedDot) {
|
|
119
|
+
const afterDot = tokenAt(tokens, skipTriviaAt(tokens, afterAtIndex + 1), cursor.endOffset);
|
|
120
|
+
if (afterDot.kind === "ident" || afterDot.kind === "number") end = afterDot.end;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
return {
|
|
125
|
+
kind: "malformed-group-header",
|
|
126
|
+
message,
|
|
127
|
+
span: { start: at.start, end, file },
|
|
128
|
+
recover:
|
|
129
|
+
message === messages.groupMissingName && afterAt.kind !== "lbrace" ? "token" : "statement",
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** Report a malformed header and resume at the next statement boundary. */
|
|
134
|
+
function requireGroupHeader(ctx: ParseContext): GroupHeader | undefined {
|
|
135
|
+
const { cursor, bag } = ctx;
|
|
136
|
+
const startIndex = cursor.index;
|
|
137
|
+
const header = readGroupHeader(ctx, startIndex);
|
|
138
|
+
if (header.kind === "group-header") return header;
|
|
139
|
+
|
|
140
|
+
bag.add("CHIKX1001", header.message, header.span);
|
|
141
|
+
if (header.recover === "token") {
|
|
142
|
+
cursor.index = Math.min(startIndex + 1, cursor.tokens.length);
|
|
143
|
+
return undefined;
|
|
144
|
+
}
|
|
145
|
+
cursor.index = Math.min(
|
|
146
|
+
skipStatementBlock(cursor.tokens, startIndex, cursor.endOffset),
|
|
147
|
+
cursor.tokens.length,
|
|
148
|
+
);
|
|
149
|
+
return undefined;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Parse `{ declaration ... }` after a validated header.
|
|
154
|
+
*
|
|
155
|
+
* Returns `undefined` when the required block is missing; a conditional use
|
|
156
|
+
* site (`@group.x @md { ... }`) is reported as unsupported and skipped as one
|
|
157
|
+
* statement so the enclosing rule keeps parsing.
|
|
158
|
+
*/
|
|
159
|
+
function parseGroupBlock(ctx: ParseContext, header: GroupHeader): GroupBody | undefined {
|
|
160
|
+
const { cursor, bag } = ctx;
|
|
161
|
+
const tokens = cursor.tokens;
|
|
162
|
+
const startIndex = cursor.index;
|
|
163
|
+
cursor.index = header.nextIndex;
|
|
164
|
+
cursor.skipTrivia();
|
|
165
|
+
|
|
166
|
+
const open = cursor.current();
|
|
167
|
+
if (open.kind !== "lbrace") {
|
|
168
|
+
const conditional = open.kind === "at-keyword" || open.kind === "modifier";
|
|
169
|
+
const span = {
|
|
170
|
+
start: header.span.start,
|
|
171
|
+
end: open.kind === "eof" ? header.span.end : open.end,
|
|
172
|
+
file: cursor.file,
|
|
173
|
+
};
|
|
174
|
+
bag.add(
|
|
175
|
+
"CHIKX1001",
|
|
176
|
+
conditional
|
|
177
|
+
? messages.groupConditionalUseSite(groupText(header.name))
|
|
178
|
+
: messages.groupNeedsBlock(groupText(header.name)),
|
|
179
|
+
span,
|
|
180
|
+
);
|
|
181
|
+
cursor.index = Math.min(
|
|
182
|
+
skipStatementBlock(tokens, startIndex, cursor.endOffset),
|
|
183
|
+
tokens.length,
|
|
184
|
+
);
|
|
185
|
+
return undefined;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
cursor.index++;
|
|
189
|
+
return parseGroupBody(ctx, header.name, header.span.start);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/** Parse declarations only, reporting and skipping every other statement. */
|
|
193
|
+
function parseGroupBody(ctx: ParseContext, name: string, startOffset: number): GroupBody {
|
|
194
|
+
const { cursor, bag } = ctx;
|
|
195
|
+
const tokens = cursor.tokens;
|
|
196
|
+
const declarations: Declaration[] = [];
|
|
197
|
+
let end = cursor.endOffset;
|
|
198
|
+
|
|
199
|
+
while (true) {
|
|
200
|
+
cursor.skipTrivia();
|
|
201
|
+
const guard = cursor.index;
|
|
202
|
+
const token = cursor.current();
|
|
203
|
+
|
|
204
|
+
if (token.kind === "eof") break;
|
|
205
|
+
if (token.kind === "rbrace") {
|
|
206
|
+
end = token.end;
|
|
207
|
+
cursor.index++;
|
|
208
|
+
return { declarations: Object.freeze(declarations), end };
|
|
209
|
+
}
|
|
210
|
+
if (token.kind === "semicolon") {
|
|
211
|
+
cursor.index++;
|
|
212
|
+
continue;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
if (token.kind === "at-keyword" || token.kind === "modifier") {
|
|
216
|
+
bag.add("CHIKX1001", messages.groupDeclarationsOnly(groupText(name)), cursor.span(token));
|
|
217
|
+
cursor.index = Math.min(skipAtRule(tokens, cursor.index, cursor.endOffset), tokens.length);
|
|
218
|
+
} else if (token.kind === "lbrace") {
|
|
219
|
+
bag.add("CHIKX1001", messages.groupDeclarationsOnly(groupText(name)), cursor.span(token));
|
|
220
|
+
cursor.index = Math.min(
|
|
221
|
+
skipBalancedBlock(tokens, cursor.index, cursor.endOffset),
|
|
222
|
+
tokens.length,
|
|
223
|
+
);
|
|
224
|
+
} else if (classifyStatement(cursor) === "rule") {
|
|
225
|
+
bag.add("CHIKX1001", messages.groupDeclarationsOnly(groupText(name)), cursor.span(token));
|
|
226
|
+
cursor.index = Math.min(
|
|
227
|
+
skipStatementBlock(tokens, cursor.index, cursor.endOffset),
|
|
228
|
+
tokens.length,
|
|
229
|
+
);
|
|
230
|
+
} else {
|
|
231
|
+
const declaration = parseDeclaration(ctx);
|
|
232
|
+
if (declaration !== undefined) declarations.push(declaration);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
if (cursor.index === guard) cursor.index++;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
bag.add("CHIKX1001", messages.unclosedBlock(`group "${groupText(name)}"`), {
|
|
239
|
+
start: startOffset,
|
|
240
|
+
end: cursor.endOffset,
|
|
241
|
+
file: cursor.file,
|
|
242
|
+
});
|
|
243
|
+
return { declarations: Object.freeze(declarations), end };
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* Parse a top-level `@group.<name> { declaration ... }` definition.
|
|
248
|
+
*
|
|
249
|
+
* The definition is inert; its declarations are kept in authored order and the
|
|
250
|
+
* resolver owns duplicate and visibility semantics (plan G2).
|
|
251
|
+
*/
|
|
252
|
+
export function parseGroupDefinition(ctx: ParseContext): GroupDefinition | undefined {
|
|
253
|
+
const { cursor } = ctx;
|
|
254
|
+
const header = requireGroupHeader(ctx);
|
|
255
|
+
if (header === undefined) return undefined;
|
|
256
|
+
const body = parseGroupBlock(ctx, header);
|
|
257
|
+
if (body === undefined) return undefined;
|
|
258
|
+
|
|
259
|
+
return Object.freeze({
|
|
260
|
+
kind: "group-definition",
|
|
261
|
+
name: header.name,
|
|
262
|
+
declarations: body.declarations,
|
|
263
|
+
span: { start: header.span.start, end: body.end, file: cursor.file },
|
|
264
|
+
});
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* Parse a `@group.<name> { declaration ... }` use site inside a rule body.
|
|
269
|
+
*
|
|
270
|
+
* The block is required in v0.1 and holds overrides only; expansion is G3.
|
|
271
|
+
*/
|
|
272
|
+
export function parseGroupUseSite(ctx: ParseContext): GroupUseSite | undefined {
|
|
273
|
+
const { cursor } = ctx;
|
|
274
|
+
const header = requireGroupHeader(ctx);
|
|
275
|
+
if (header === undefined) return undefined;
|
|
276
|
+
const body = parseGroupBlock(ctx, header);
|
|
277
|
+
if (body === undefined) return undefined;
|
|
278
|
+
|
|
279
|
+
return Object.freeze({
|
|
280
|
+
kind: "group-use-site",
|
|
281
|
+
name: header.name,
|
|
282
|
+
declarations: body.declarations,
|
|
283
|
+
span: { start: header.span.start, end: body.end, file: cursor.file },
|
|
284
|
+
});
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* Reject a group statement in a context that cannot hold a use site.
|
|
289
|
+
*
|
|
290
|
+
* Conditional blocks are declaration/rule/conditional containers only, so a
|
|
291
|
+
* group use site there is CHIKX1001 and the whole statement is omitted.
|
|
292
|
+
*/
|
|
293
|
+
export function rejectGroupConstruct(ctx: ParseContext, context: string): void {
|
|
294
|
+
const { cursor, bag } = ctx;
|
|
295
|
+
const startIndex = cursor.index;
|
|
296
|
+
const header = readGroupHeader(ctx, startIndex);
|
|
297
|
+
const label = header.kind === "group-header" ? groupText(header.name) : "@group";
|
|
298
|
+
bag.add("CHIKX1001", messages.groupNotAllowedHere(label, context), header.span);
|
|
299
|
+
cursor.index = Math.min(
|
|
300
|
+
skipStatementBlock(cursor.tokens, startIndex, cursor.endOffset),
|
|
301
|
+
cursor.tokens.length,
|
|
302
|
+
);
|
|
303
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type { Stylesheet } from "../types/ast.js";
|
|
2
|
+
import type { ParseResult } from "../types/result.js";
|
|
3
|
+
import type { Token } from "../types/token.js";
|
|
4
|
+
import { DiagnosticBag, messages } from "./diagnostics.js";
|
|
5
|
+
import { parseStylesheet } from "./statements.js";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Parse a token stream into an immutable stylesheet AST plus syntax
|
|
9
|
+
* diagnostics.
|
|
10
|
+
*
|
|
11
|
+
* The parser consumes a `readonly Token[]` produced by the lexer: it never
|
|
12
|
+
* re-tokenizes source text, never mutates the input and never throws. Unknown
|
|
13
|
+
* or malformed constructs are reported with the frozen CHIKX1001/1002/1003
|
|
14
|
+
* factories and recovery resumes at the next statement boundary, so a
|
|
15
|
+
* `Stylesheet` is always returned (SPECIFICATION.md §3, §12; ADR-0004,
|
|
16
|
+
* ADR-0011).
|
|
17
|
+
*
|
|
18
|
+
* Statement-depth rules worth knowing at the call site:
|
|
19
|
+
*
|
|
20
|
+
* - `@setup`, `@use`, `@variables`, `@runtime`, `@media-types`, `@group`
|
|
21
|
+
* definitions, rules and native at-rules are the only top-level constructs; a
|
|
22
|
+
* top-level conditional block is diagnosed and dropped. `@group.<name>`
|
|
23
|
+
* definitions are inert and declarations-only, and the same construct in a
|
|
24
|
+
* rule body parses as a use site (ADR-0017).
|
|
25
|
+
* - Nested rules versus declarations are decided by balanced lookahead: a `{`
|
|
26
|
+
* before the statement terminator makes a rule (ADR-0002).
|
|
27
|
+
* - `!important` stays raw text per segment and sets only that segment's flag
|
|
28
|
+
* (ADR-0003); a `$` reference additionally yields the parsed token sequence
|
|
29
|
+
* (ADR-0005).
|
|
30
|
+
* - A final declaration without `;` before `}` is accepted only when it carries
|
|
31
|
+
* no modifier and no `$` reference; otherwise it is CHIKX1002 (ADR-0011).
|
|
32
|
+
*/
|
|
33
|
+
export function parseTokens(tokens: readonly Token[], file: string): ParseResult {
|
|
34
|
+
try {
|
|
35
|
+
return parseStylesheet(tokens, file);
|
|
36
|
+
} catch {
|
|
37
|
+
// Defensive guarantee: a parser bug must never crash a host or the editor.
|
|
38
|
+
const last = tokens[tokens.length - 1];
|
|
39
|
+
const offset = last === undefined ? 0 : last.kind === "eof" ? last.start : last.end;
|
|
40
|
+
const bag = new DiagnosticBag();
|
|
41
|
+
bag.add("CHIKX1001", messages.internalRecovery, { start: 0, end: offset, file });
|
|
42
|
+
const ast: Stylesheet = Object.freeze({
|
|
43
|
+
kind: "stylesheet",
|
|
44
|
+
file,
|
|
45
|
+
items: Object.freeze([]),
|
|
46
|
+
span: { start: 0, end: offset, file },
|
|
47
|
+
});
|
|
48
|
+
return { ast, diagnostics: bag.finish() };
|
|
49
|
+
}
|
|
50
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import type { Modifier } from "../types/ast.js";
|
|
2
|
+
import type { Token } from "../types/token.js";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Recognized condition modifiers (`@md`, `@xl+dark`, `@2xl`).
|
|
6
|
+
*
|
|
7
|
+
* The lexer emits `at-keyword` for every `@` name because alias tables are
|
|
8
|
+
* configuration-dependent; the parser promotes an `at-keyword` to a modifier
|
|
9
|
+
* only at top-level value depth or in block-condition position (ADR-0005,
|
|
10
|
+
* token contract). A `modifier` token already present in a hand-built stream is
|
|
11
|
+
* accepted as-is.
|
|
12
|
+
*/
|
|
13
|
+
export interface ModifierRead {
|
|
14
|
+
readonly modifier: Modifier;
|
|
15
|
+
/** Raw index of the first token after the modifier text. */
|
|
16
|
+
readonly nextIndex: number;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Read a modifier starting at a raw index, absorbing `+ alias` joins that are
|
|
21
|
+
* directly adjacent in the source. Surrounding trivia is not consumed.
|
|
22
|
+
*
|
|
23
|
+
* Aliases keep authored order and preserve duplicates until lowering
|
|
24
|
+
* normalizes them (AST `Modifier` JSDoc).
|
|
25
|
+
*/
|
|
26
|
+
export function readModifier(
|
|
27
|
+
tokens: readonly Token[],
|
|
28
|
+
index: number,
|
|
29
|
+
limit: number,
|
|
30
|
+
file: string,
|
|
31
|
+
): ModifierRead | undefined {
|
|
32
|
+
const token = tokens[index];
|
|
33
|
+
if (token === undefined) return undefined;
|
|
34
|
+
if (token.kind !== "at-keyword" && token.kind !== "modifier") return undefined;
|
|
35
|
+
|
|
36
|
+
let endToken = token;
|
|
37
|
+
let text = token.value.startsWith("@") ? token.value.slice(1) : token.value;
|
|
38
|
+
let nextIndex = index + 1;
|
|
39
|
+
|
|
40
|
+
while (nextIndex + 1 < limit) {
|
|
41
|
+
const plus = tokens[nextIndex];
|
|
42
|
+
const alias = tokens[nextIndex + 1];
|
|
43
|
+
if (plus === undefined || alias === undefined) break;
|
|
44
|
+
if (plus.kind !== "delim" || plus.value !== "+" || plus.start !== endToken.end) break;
|
|
45
|
+
if (alias.kind !== "ident" && alias.kind !== "number") break;
|
|
46
|
+
if (alias.start !== plus.end) break;
|
|
47
|
+
text += `+${alias.value}`;
|
|
48
|
+
endToken = alias;
|
|
49
|
+
nextIndex += 2;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const aliases = text.split("+").filter((alias) => alias.length > 0);
|
|
53
|
+
if (aliases.length === 0) return undefined;
|
|
54
|
+
|
|
55
|
+
const modifier: Modifier = Object.freeze({
|
|
56
|
+
aliases: Object.freeze(aliases),
|
|
57
|
+
span: { start: token.start, end: endToken.end, file },
|
|
58
|
+
});
|
|
59
|
+
return { modifier, nextIndex };
|
|
60
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import type { Token } from "../types/token.js";
|
|
2
|
+
import { type TokenCursor, tokenAt } from "./cursor.js";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Statement-boundary recovery helpers (SPECIFICATION.md §12, ADR-0004).
|
|
6
|
+
*
|
|
7
|
+
* Recovery never throws and never rewinds: every helper returns an index at or
|
|
8
|
+
* after its input so callers keep making progress.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/** Move past the next `;`, stopping before a `}` that closes the enclosing block. */
|
|
12
|
+
export function recoverStatement(cursor: TokenCursor): void {
|
|
13
|
+
const tokens = cursor.tokens;
|
|
14
|
+
let index = cursor.index;
|
|
15
|
+
while (true) {
|
|
16
|
+
const token = tokenAt(tokens, index, cursor.endOffset);
|
|
17
|
+
if (token.kind === "eof") {
|
|
18
|
+
cursor.index = Math.min(index, tokens.length);
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
if (token.kind === "rbrace") {
|
|
22
|
+
cursor.index = Math.min(index, tokens.length);
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
if (token.kind === "semicolon") {
|
|
26
|
+
cursor.index = index + 1;
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
index++;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** Skip a brace-balanced block; `openIndex` must point at its `{`. */
|
|
34
|
+
export function skipBalancedBlock(
|
|
35
|
+
tokens: readonly Token[],
|
|
36
|
+
openIndex: number,
|
|
37
|
+
eofOffset: number,
|
|
38
|
+
): number {
|
|
39
|
+
let depth = 0;
|
|
40
|
+
let index = openIndex;
|
|
41
|
+
while (true) {
|
|
42
|
+
const token = tokenAt(tokens, index, eofOffset);
|
|
43
|
+
if (token.kind === "eof") return Math.min(index, tokens.length);
|
|
44
|
+
if (token.kind === "lbrace") depth++;
|
|
45
|
+
else if (token.kind === "rbrace") {
|
|
46
|
+
depth--;
|
|
47
|
+
if (depth <= 0) return index + 1;
|
|
48
|
+
}
|
|
49
|
+
index++;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Skip a whole statement, including a balanced block when it opens one.
|
|
55
|
+
*
|
|
56
|
+
* Used for recovery past a rejected nested rule or native at-rule. The closing
|
|
57
|
+
* `}` of the enclosing block is never consumed, so the caller keeps its own
|
|
58
|
+
* terminator.
|
|
59
|
+
*/
|
|
60
|
+
export function skipStatementBlock(
|
|
61
|
+
tokens: readonly Token[],
|
|
62
|
+
startIndex: number,
|
|
63
|
+
eofOffset: number,
|
|
64
|
+
): number {
|
|
65
|
+
let depth = 0;
|
|
66
|
+
let index = startIndex;
|
|
67
|
+
while (true) {
|
|
68
|
+
const token = tokenAt(tokens, index, eofOffset);
|
|
69
|
+
if (token.kind === "eof") return Math.min(index, tokens.length);
|
|
70
|
+
if (token.kind === "lparen" || token.kind === "lbracket") depth++;
|
|
71
|
+
else if (token.kind === "rparen" || token.kind === "rbracket") depth = Math.max(0, depth - 1);
|
|
72
|
+
else if (depth === 0) {
|
|
73
|
+
if (token.kind === "lbrace") return skipBalancedBlock(tokens, index, eofOffset);
|
|
74
|
+
if (token.kind === "semicolon") return index + 1;
|
|
75
|
+
if (token.kind === "rbrace") return index;
|
|
76
|
+
}
|
|
77
|
+
index++;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** Skip a whole native at-rule: prelude plus a balanced block or `;`. */
|
|
82
|
+
export function skipAtRule(
|
|
83
|
+
tokens: readonly Token[],
|
|
84
|
+
startIndex: number,
|
|
85
|
+
eofOffset: number,
|
|
86
|
+
): number {
|
|
87
|
+
return skipStatementBlock(tokens, startIndex + 1, eofOffset);
|
|
88
|
+
}
|