@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,85 @@
|
|
|
1
|
+
import type { Span } from "../types/span.js";
|
|
2
|
+
import type {
|
|
3
|
+
AtRuleContextIr,
|
|
4
|
+
DeclarationIr,
|
|
5
|
+
MediaConditionIr,
|
|
6
|
+
NativeBlockChild,
|
|
7
|
+
NativeBlockIr,
|
|
8
|
+
RuleIr,
|
|
9
|
+
StylesheetIr,
|
|
10
|
+
} from "./types.js";
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Frozen IR constructors.
|
|
14
|
+
*
|
|
15
|
+
* IR nodes are immutable by contract, so every factory freezes the node (and
|
|
16
|
+
* its arrays) before returning. Factories preserve the exact field values they
|
|
17
|
+
* receive; they never sort, deduplicate or normalize.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
/** Create a frozen {@link DeclarationIr}. */
|
|
21
|
+
export function declarationIr(
|
|
22
|
+
property: string,
|
|
23
|
+
value: string,
|
|
24
|
+
span: Span,
|
|
25
|
+
important?: boolean,
|
|
26
|
+
): DeclarationIr {
|
|
27
|
+
return Object.freeze(
|
|
28
|
+
important === undefined
|
|
29
|
+
? { property, value, span: Object.freeze(span) }
|
|
30
|
+
: { property, value, important, span: Object.freeze(span) },
|
|
31
|
+
);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** Create a frozen {@link MediaConditionIr} from already-merged alias keys. */
|
|
35
|
+
export function mediaConditionIr(query: string, keys: readonly string[]): MediaConditionIr {
|
|
36
|
+
return Object.freeze({ query, keys: Object.freeze([...keys]) });
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** Create a frozen {@link AtRuleContextIr}. */
|
|
40
|
+
export function atRuleContextIr(name: string, prelude: string, span: Span): AtRuleContextIr {
|
|
41
|
+
return Object.freeze({ name, prelude, span: Object.freeze(span) });
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Create a frozen {@link RuleIr}; `ancestors` and `context` are copied. */
|
|
45
|
+
export function ruleIr(fields: {
|
|
46
|
+
readonly selector: string;
|
|
47
|
+
readonly ancestors: readonly string[];
|
|
48
|
+
readonly declarations: readonly DeclarationIr[];
|
|
49
|
+
readonly condition: MediaConditionIr | null;
|
|
50
|
+
readonly context: readonly AtRuleContextIr[];
|
|
51
|
+
readonly span: Span;
|
|
52
|
+
}): RuleIr {
|
|
53
|
+
return Object.freeze({
|
|
54
|
+
selector: fields.selector,
|
|
55
|
+
ancestors: Object.freeze([...fields.ancestors]),
|
|
56
|
+
declarations: Object.freeze([...fields.declarations]),
|
|
57
|
+
condition: fields.condition,
|
|
58
|
+
context: Object.freeze([...fields.context]),
|
|
59
|
+
span: Object.freeze(fields.span),
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** Create a frozen {@link NativeBlockIr}; `children` is copied. */
|
|
64
|
+
export function nativeBlockIr(fields: {
|
|
65
|
+
readonly name: string;
|
|
66
|
+
readonly prelude: string;
|
|
67
|
+
readonly statement?: boolean;
|
|
68
|
+
readonly children: readonly NativeBlockChild[];
|
|
69
|
+
readonly span: Span;
|
|
70
|
+
}): NativeBlockIr {
|
|
71
|
+
const base = {
|
|
72
|
+
name: fields.name,
|
|
73
|
+
prelude: fields.prelude,
|
|
74
|
+
children: Object.freeze([...fields.children]),
|
|
75
|
+
span: Object.freeze(fields.span),
|
|
76
|
+
};
|
|
77
|
+
return Object.freeze(
|
|
78
|
+
fields.statement === true ? { ...base, statement: true } : base,
|
|
79
|
+
) as NativeBlockIr;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** Create a frozen {@link StylesheetIr}; `rules` is copied. */
|
|
83
|
+
export function stylesheetIr(rules: readonly (RuleIr | NativeBlockIr)[]): StylesheetIr {
|
|
84
|
+
return Object.freeze({ rules: Object.freeze([...rules]) });
|
|
85
|
+
}
|
package/src/ir/index.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Immutable CSS IR produced by lowering. The IR records ordered selectors,
|
|
3
|
+
* declarations, at-rule stack, origin spans and condition keys.
|
|
4
|
+
*
|
|
5
|
+
* The IR sits between the parser AST and generation. It is a flat, frozen,
|
|
6
|
+
* source-ordered view: declarations are already split per value segment, Chikx
|
|
7
|
+
* conditions are resolved to a single media query, and native at-rule wrappers
|
|
8
|
+
* are recorded as a stack. Selector flattening (A06), class naming (A09),
|
|
9
|
+
* variable substitution (A08) and CSS text emission (A06) all happen later.
|
|
10
|
+
*/
|
|
11
|
+
export type {
|
|
12
|
+
AtRuleContextIr,
|
|
13
|
+
DeclarationIr,
|
|
14
|
+
MediaConditionIr,
|
|
15
|
+
NativeBlockChild,
|
|
16
|
+
NativeBlockIr,
|
|
17
|
+
RuleIr,
|
|
18
|
+
StylesheetIr,
|
|
19
|
+
} from "./types.js";
|
|
20
|
+
|
|
21
|
+
export {
|
|
22
|
+
atRuleContextIr,
|
|
23
|
+
declarationIr,
|
|
24
|
+
mediaConditionIr,
|
|
25
|
+
nativeBlockIr,
|
|
26
|
+
ruleIr,
|
|
27
|
+
stylesheetIr,
|
|
28
|
+
} from "./factories.js";
|
package/src/ir/types.ts
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import type { Span } from "../types/span.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* One lowered CSS declaration: a single property/value pair plus its origin.
|
|
5
|
+
*
|
|
6
|
+
* Lowering emits one node per authored value segment (base and every inline
|
|
7
|
+
* variant), in exactly the authored order. It never deduplicates, reorders or
|
|
8
|
+
* merges declarations: a later shorthand, a later base or an early conditional
|
|
9
|
+
* segment keeps its position so the native CSS cascade stays authoritative
|
|
10
|
+
* (SPECIFICATION.md §5).
|
|
11
|
+
*
|
|
12
|
+
* `value` is the raw authored slice. Variable substitution (`$token`), class
|
|
13
|
+
* naming and selector prefixing deliberately do not run here: A08 and A09 own
|
|
14
|
+
* them, so this node can be compared byte-for-byte against the AST.
|
|
15
|
+
*/
|
|
16
|
+
export interface DeclarationIr {
|
|
17
|
+
/** Property exactly as authored, including custom properties (`--brand`). */
|
|
18
|
+
readonly property: string;
|
|
19
|
+
/**
|
|
20
|
+
* Raw value slice exactly as authored, including a trailing `!important`
|
|
21
|
+
* when the segment carried one (ADR-0003).
|
|
22
|
+
*/
|
|
23
|
+
readonly value: string;
|
|
24
|
+
/** Whether this segment alone carried `!important`; never propagated (D2). */
|
|
25
|
+
readonly important?: boolean;
|
|
26
|
+
/**
|
|
27
|
+
* Origin of the segment. The base segment covers the declaration from the
|
|
28
|
+
* property through the first modifier; a variant segment covers its modifier
|
|
29
|
+
* through the next modifier or the end of the declaration. Segments partition
|
|
30
|
+
* the authored declaration span with no gaps or overlaps.
|
|
31
|
+
*/
|
|
32
|
+
readonly span: Span;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* The resolved Chikx condition governing one contiguous declaration run.
|
|
37
|
+
*
|
|
38
|
+
* `query` is a single normalized media-query text ready for `@media` emission:
|
|
39
|
+
* combined aliases are joined with `and` and comma lists are rejected, so the
|
|
40
|
+
* value is never a media query list (SPECIFICATION.md §4.3). `keys` are the
|
|
41
|
+
* merged alias names in authored order, after duplicate normalization, which
|
|
42
|
+
* lets later stages and the editor map a query back to its aliases.
|
|
43
|
+
*
|
|
44
|
+
* Native at-rule wrappers are not folded in here: they stay on
|
|
45
|
+
* {@link RuleIr.context} and are ANDed at emission time by A06.
|
|
46
|
+
*/
|
|
47
|
+
export interface MediaConditionIr {
|
|
48
|
+
/** Normalized single media query, for example `(min-width: 768px) and (orientation: landscape)`. */
|
|
49
|
+
readonly query: string;
|
|
50
|
+
/** Alias names in authored order with per-modifier duplicates removed. */
|
|
51
|
+
readonly keys: readonly string[];
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* One native at-rule wrapper enclosing a rule, for example an outer
|
|
56
|
+
* `@media (min-width: 900px)`, `@supports` or `@layer`.
|
|
57
|
+
*
|
|
58
|
+
* Lowering copies the authored name and prelude verbatim and never rewrites
|
|
59
|
+
* native media lists, so authored meaning is preserved. The stack on
|
|
60
|
+
* {@link RuleIr.context} is ordered outermost-first.
|
|
61
|
+
*/
|
|
62
|
+
export interface AtRuleContextIr {
|
|
63
|
+
/** At-rule name without the sigil, exactly as authored. */
|
|
64
|
+
readonly name: string;
|
|
65
|
+
/** Raw prelude text exactly as authored; empty when absent. */
|
|
66
|
+
readonly prelude: string;
|
|
67
|
+
/** Covers the at-rule from `@` through its block or terminating `;`. */
|
|
68
|
+
readonly span: Span;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* A contiguous run of declarations for one selector in one condition and one
|
|
73
|
+
* native at-rule context.
|
|
74
|
+
*
|
|
75
|
+
* Lowering guarantees authored order: rules appear in source order, and each
|
|
76
|
+
* rule's declarations appear in the order the segments were written, with no
|
|
77
|
+
* deduplication. A run is flushed when a nested rule, a conditional block or a
|
|
78
|
+
* native at-rule is encountered (SPECIFICATION.md §5.1), so a later run is a
|
|
79
|
+
* different node rather than a reordering of an earlier one.
|
|
80
|
+
*
|
|
81
|
+
* `selector` stays the raw, still-nested selector text from the AST. Nesting
|
|
82
|
+
* flattening happens in A06 and class prefixing in P4/A09; `ancestors` carries
|
|
83
|
+
* the raw enclosing selectors, outermost-first, so A06 can flatten without
|
|
84
|
+
* re-reading the AST.
|
|
85
|
+
*/
|
|
86
|
+
export interface RuleIr {
|
|
87
|
+
/** Raw selector text exactly as authored, with `&` and descendant nesting untouched. */
|
|
88
|
+
readonly selector: string;
|
|
89
|
+
/**
|
|
90
|
+
* Raw selector texts of the enclosing rules, outermost-first, excluding this
|
|
91
|
+
* rule's own selector. Empty for top-level rules. Additive IR field required
|
|
92
|
+
* for A06 nesting flattening; see the A05 report.
|
|
93
|
+
*/
|
|
94
|
+
readonly ancestors: readonly string[];
|
|
95
|
+
/** Declarations of this run in authored order; never empty, never deduplicated. */
|
|
96
|
+
readonly declarations: readonly DeclarationIr[];
|
|
97
|
+
/** Chikx condition for this run, or `null` for an unconditional run. */
|
|
98
|
+
readonly condition: MediaConditionIr | null;
|
|
99
|
+
/** Native at-rule stack outermost-first; empty outside native at-rules. */
|
|
100
|
+
readonly context: readonly AtRuleContextIr[];
|
|
101
|
+
/** Origin of the run's first declaration segment. */
|
|
102
|
+
readonly span: Span;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** Any child a native at-rule block may hold, in authored order. */
|
|
106
|
+
export type NativeBlockChild = RuleIr | NativeBlockIr | DeclarationIr;
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* A native CSS at-rule preserved with its authored meaning.
|
|
110
|
+
*
|
|
111
|
+
* Lowering never reorders or rewrites children and never merges a native block
|
|
112
|
+
* with its neighbours. Descriptor at-rules (`@font-face`, `@property`) hold
|
|
113
|
+
* {@link DeclarationIr} children directly because their declarations are not
|
|
114
|
+
* selector-bound; keyframe steps stay {@link RuleIr} nodes with their authored
|
|
115
|
+
* keyframe selector text.
|
|
116
|
+
*/
|
|
117
|
+
export interface NativeBlockIr {
|
|
118
|
+
/** At-rule name without the sigil, exactly as authored. */
|
|
119
|
+
readonly name: string;
|
|
120
|
+
/** Raw prelude text exactly as authored; empty when absent. */
|
|
121
|
+
readonly prelude: string;
|
|
122
|
+
/**
|
|
123
|
+
* `true` for a statement at-rule (`@import`, `@charset`, `@layer a, b;`) that
|
|
124
|
+
* has no block. Absent for block at-rules. Additive IR field: without it the
|
|
125
|
+
* emitter cannot distinguish `@import "a.css";` from `@layer a {}`.
|
|
126
|
+
*/
|
|
127
|
+
readonly statement?: boolean;
|
|
128
|
+
/** Ordered children: nested rules, nested native blocks and descriptor declarations. */
|
|
129
|
+
readonly children: readonly NativeBlockChild[];
|
|
130
|
+
/** Covers the at-rule from `@` through its block or terminating `;`. */
|
|
131
|
+
readonly span: Span;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* The lowered stylesheet: top-level rules and native blocks in authored order.
|
|
136
|
+
*
|
|
137
|
+
* Lowering is a pure, deterministic projection of the AST. Every node is frozen
|
|
138
|
+
* and every span points back at the authored source, so downstream stages
|
|
139
|
+
* (class rewrite, safe grouping, emission, source maps) can trust order and
|
|
140
|
+
* origin without re-reading the AST.
|
|
141
|
+
*/
|
|
142
|
+
export interface StylesheetIr {
|
|
143
|
+
/** Top-level rules and native blocks in authored order. */
|
|
144
|
+
readonly rules: readonly (RuleIr | NativeBlockIr)[];
|
|
145
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@` token scanning.
|
|
3
|
+
*
|
|
4
|
+
* This module deliberately classifies nothing. The frozen `modifier` JSDoc in
|
|
5
|
+
* `types/token.ts` says the tokenizer "never emits this kind on its own: the
|
|
6
|
+
* parser reclassifies `at-keyword` to `modifier` only at top-level value depth
|
|
7
|
+
* (inline variants) or in block-condition position". Alias tables live in
|
|
8
|
+
* configuration, so the lexer only recognises `@` followed by a name and emits
|
|
9
|
+
* `at-keyword` including the `@`. A `@` with no name is a `delim`.
|
|
10
|
+
*
|
|
11
|
+
* Digits may start an alias (`@2xl`) and `-` is a name character, so the scan
|
|
12
|
+
* uses the name grammar rather than the stricter identifier-start rule.
|
|
13
|
+
*
|
|
14
|
+
* `@group.<name>` is deliberately not joined into one token: the frozen
|
|
15
|
+
* at-keyword contract covers an `@` name, so `@group` stays an at-keyword and
|
|
16
|
+
* `.name` stays a delim plus an identifier. The parser recognises the
|
|
17
|
+
* contiguous dotted form from adjacent token offsets and rejects whitespace
|
|
18
|
+
* between the parts (ADR-0017, SPECIFICATION.md §3.7).
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
import { escapeEnd, isNameCode } from "./characters.js";
|
|
22
|
+
import { nameEnd } from "./names.js";
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* End offset of the `@` token starting at `start`.
|
|
26
|
+
*
|
|
27
|
+
* Returns `start + 1` when no name follows, which the caller emits as a `delim`
|
|
28
|
+
* so round-tripping never loses the character.
|
|
29
|
+
*/
|
|
30
|
+
export function scanAtKeyword(source: string, start: number): number {
|
|
31
|
+
const nameStart = start + 1;
|
|
32
|
+
const code = source.charCodeAt(nameStart);
|
|
33
|
+
if (!isNameCode(code) && escapeEnd(source, nameStart) <= nameStart) return start + 1;
|
|
34
|
+
return nameEnd(source, nameStart);
|
|
35
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Character classification shared by every lexer scanner.
|
|
3
|
+
*
|
|
4
|
+
* Helpers are pure and take either a UTF-16 code unit or an offset, so no two
|
|
5
|
+
* scanners share a mutable cursor and the facade stays a plain loop. The
|
|
6
|
+
* families follow CSS syntax (whitespace, digits, name characters, escapes)
|
|
7
|
+
* without decoding anything: the frozen `Token` contract keeps authored text
|
|
8
|
+
* verbatim, including escaped sequences.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/** `\t`, `\n`, `\f`, `\r` and the space are CSS whitespace. */
|
|
12
|
+
export function isWhitespaceCode(code: number): boolean {
|
|
13
|
+
return code === 0x09 || code === 0x0a || code === 0x0c || code === 0x0d || code === 0x20;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** Line terminators that end an unescaped quoted string. */
|
|
17
|
+
export function isNewlineCode(code: number): boolean {
|
|
18
|
+
return code === 0x0a || code === 0x0c || code === 0x0d;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** ASCII decimal digit (`0`–`9`). */
|
|
22
|
+
export function isDigitCode(code: number): boolean {
|
|
23
|
+
return code >= 0x30 && code <= 0x39;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** ASCII hexadecimal digit, used by numeric escapes such as `\31 `. */
|
|
27
|
+
export function isHexDigitCode(code: number): boolean {
|
|
28
|
+
return isDigitCode(code) || (code >= 0x41 && code <= 0x46) || (code >= 0x61 && code <= 0x66);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** First character of a CSS name: ASCII letter, `_` or any non-ASCII code unit. */
|
|
32
|
+
export function isNameStartCode(code: number): boolean {
|
|
33
|
+
return (
|
|
34
|
+
(code >= 0x41 && code <= 0x5a) ||
|
|
35
|
+
(code >= 0x61 && code <= 0x7a) ||
|
|
36
|
+
code === 0x5f ||
|
|
37
|
+
code >= 0x80
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Any CSS name character: name-start, digit or hyphen. */
|
|
42
|
+
export function isNameCode(code: number): boolean {
|
|
43
|
+
return isNameStartCode(code) || isDigitCode(code) || code === 0x2d;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* NUL and C0 control codes other than CSS whitespace are illegal in source.
|
|
48
|
+
* The scanner reports each one once and keeps it as a `delim` token so the
|
|
49
|
+
* stream still reproduces the input exactly.
|
|
50
|
+
*/
|
|
51
|
+
export function isIllegalCode(code: number): boolean {
|
|
52
|
+
return code === 0x00 || (code < 0x20 && !isWhitespaceCode(code));
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* End offset of the backslash escape that starts at `index`, or `index` when
|
|
57
|
+
* the character there is not a valid escape (not a backslash, a trailing
|
|
58
|
+
* backslash, or a backslash before a line terminator).
|
|
59
|
+
*
|
|
60
|
+
* A hex escape consumes at most six hex digits plus one optional whitespace
|
|
61
|
+
* terminator, with CRLF counted as a single terminator, so `\31 23` remains one
|
|
62
|
+
* name. Escapes are measured, never decoded.
|
|
63
|
+
*/
|
|
64
|
+
export function escapeEnd(source: string, index: number): number {
|
|
65
|
+
if (source.charCodeAt(index) !== 0x5c) return index;
|
|
66
|
+
const next = source.charCodeAt(index + 1);
|
|
67
|
+
if (Number.isNaN(next) || isNewlineCode(next)) return index;
|
|
68
|
+
if (!isHexDigitCode(next)) return index + 2;
|
|
69
|
+
|
|
70
|
+
let probe = index + 1;
|
|
71
|
+
let digits = 0;
|
|
72
|
+
while (digits < 6 && isHexDigitCode(source.charCodeAt(probe))) {
|
|
73
|
+
probe += 1;
|
|
74
|
+
digits += 1;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const terminator = source.charCodeAt(probe);
|
|
78
|
+
if (isWhitespaceCode(terminator)) {
|
|
79
|
+
return terminator === 0x0d && source.charCodeAt(probe + 1) === 0x0a ? probe + 2 : probe + 1;
|
|
80
|
+
}
|
|
81
|
+
return probe;
|
|
82
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Block comment scanning.
|
|
3
|
+
*
|
|
4
|
+
* Comments are emitted as their own tokens for fidelity, so any `@`, `$`,
|
|
5
|
+
* quote or brace inside one can never become a Chikx token. An unterminated
|
|
6
|
+
* comment runs to end of file and is reported by the caller.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/** Result of scanning one block comment. */
|
|
10
|
+
export interface CommentScan {
|
|
11
|
+
/** Offset just past the closing star-slash sequence, or end of file. */
|
|
12
|
+
readonly end: number;
|
|
13
|
+
/** False when the closing star-slash sequence never appears. */
|
|
14
|
+
readonly closed: boolean;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Scan the block comment that opens at `start` with a slash-star sequence.
|
|
19
|
+
*
|
|
20
|
+
* `source.indexOf` keeps this linear for the whole file: each comment scans its
|
|
21
|
+
* own body once.
|
|
22
|
+
*/
|
|
23
|
+
export function scanComment(source: string, start: number): CommentScan {
|
|
24
|
+
const close = source.indexOf("*/", start + 2);
|
|
25
|
+
if (close === -1) return { end: source.length, closed: false };
|
|
26
|
+
return { end: close + 2, closed: true };
|
|
27
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Lexical diagnostics.
|
|
3
|
+
*
|
|
4
|
+
* Every lexical failure uses the `CHIKX1001` syntax anchor with `error`
|
|
5
|
+
* severity, which the frozen registry defines as covering "a lexer-level
|
|
6
|
+
* recovery". Spans cover the offending construct itself (the unterminated
|
|
7
|
+
* string, comment or URL, or the single illegal character) so an editor can
|
|
8
|
+
* point at its start and end. The tokenizer never throws: callers always
|
|
9
|
+
* receive tokens plus these diagnostics.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import type { Diagnostic } from "../types/diagnostics.js";
|
|
13
|
+
import { error } from "../types/diagnostics.js";
|
|
14
|
+
|
|
15
|
+
const LEXICAL_CODE = "CHIKX1001" as const;
|
|
16
|
+
|
|
17
|
+
/** User-facing wording for each lexical failure the tokenizer can report. */
|
|
18
|
+
export const LEXICAL_MESSAGES = {
|
|
19
|
+
unterminatedString:
|
|
20
|
+
"Unterminated string literal; the closing quote is missing before the end of the line.",
|
|
21
|
+
unterminatedComment: "Unterminated comment; the closing star-slash sequence is missing.",
|
|
22
|
+
unterminatedUrl: "Unterminated url() construct; the closing parenthesis is missing.",
|
|
23
|
+
malformedUrl:
|
|
24
|
+
"Malformed url() construct; only whitespace and a closing parenthesis may follow a quoted path.",
|
|
25
|
+
invalidEscape:
|
|
26
|
+
"Invalid escape sequence; a backslash must be followed by a character other than a line break.",
|
|
27
|
+
} as const;
|
|
28
|
+
|
|
29
|
+
/** Build one error diagnostic with a span in `file`. */
|
|
30
|
+
export function lexicalError(
|
|
31
|
+
message: string,
|
|
32
|
+
start: number,
|
|
33
|
+
end: number,
|
|
34
|
+
file: string,
|
|
35
|
+
): Diagnostic {
|
|
36
|
+
return error(LEXICAL_CODE, message, { start, end, file });
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** Message for an illegal control character, naming its code point. */
|
|
40
|
+
export function illegalCharacterMessage(code: number): string {
|
|
41
|
+
return `Illegal control character U+${code.toString(16).toUpperCase().padStart(4, "0")} in source.`;
|
|
42
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `$` reference scanning.
|
|
3
|
+
*
|
|
4
|
+
* The frozen `dollar-ref` JSDoc lists exactly two forms, `$name` and
|
|
5
|
+
* `$name.member`, so at most one `.` member joins a reference. A malformed or
|
|
6
|
+
* bare `$` stays a `delim` and the parser reports it; the lexer never guesses.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { isIdentStartAt, nameEnd } from "./names.js";
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* End offset of the `dollar-ref` starting at `start`, or `null` when `$` is not
|
|
13
|
+
* followed by a name.
|
|
14
|
+
*/
|
|
15
|
+
export function scanDollarRef(source: string, start: number): number | null {
|
|
16
|
+
if (!isIdentStartAt(source, start + 1)) return null;
|
|
17
|
+
|
|
18
|
+
let index = nameEnd(source, start + 1);
|
|
19
|
+
if (source.charCodeAt(index) === 0x2e && isIdentStartAt(source, index + 1)) {
|
|
20
|
+
index = nameEnd(source, index + 1);
|
|
21
|
+
}
|
|
22
|
+
return index;
|
|
23
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Lexer facade for `@chikx/compiler`.
|
|
3
|
+
*
|
|
4
|
+
* `tokenize` is the only entry point the parser needs: it turns source text into
|
|
5
|
+
* a lossless token stream that always ends with exactly one `eof` token, plus
|
|
6
|
+
* the lexical diagnostics collected on the way. It is a pure function — no
|
|
7
|
+
* filesystem, no ambient state, no dependencies — and it never throws.
|
|
8
|
+
*
|
|
9
|
+
* `modifier` is deliberately absent from the output: the frozen `Token` JSDoc
|
|
10
|
+
* makes the parser promote a recognised `at-keyword` to `modifier` only at
|
|
11
|
+
* top-level value depth or in block-condition position, so alias tables stay in
|
|
12
|
+
* configuration where they belong.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import type { Diagnostic } from "../types/diagnostics.js";
|
|
16
|
+
import type { Token } from "../types/token.js";
|
|
17
|
+
import { scan } from "./scanner.js";
|
|
18
|
+
|
|
19
|
+
/** Result of tokenizing one source string. */
|
|
20
|
+
export interface TokenizeResult {
|
|
21
|
+
/** Tokens in source order, always ending with exactly one zero-width `eof`. */
|
|
22
|
+
readonly tokens: readonly Token[];
|
|
23
|
+
/** Lexical errors in source order; empty when the source tokenizes cleanly. */
|
|
24
|
+
readonly diagnostics: readonly Diagnostic[];
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Tokenize Chikx source.
|
|
29
|
+
*
|
|
30
|
+
* `file` is copied into every diagnostic {@link Span}; token spans are half-open
|
|
31
|
+
* UTF-16 offsets into `source`, and concatenating `source.slice(start, end)` for
|
|
32
|
+
* every token reproduces `source` exactly (the single `function` token's `value`
|
|
33
|
+
* omits its `(`, but its span still covers only the name).
|
|
34
|
+
*/
|
|
35
|
+
export function tokenize(source: string, file: string): TokenizeResult {
|
|
36
|
+
return scan(source, file);
|
|
37
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Identifier and name scanning shared by idents, function names, dimension
|
|
3
|
+
* units, hashes and `@`/`$` references.
|
|
4
|
+
*
|
|
5
|
+
* `nameEnd` consumes CSS name characters and valid escapes; `identEnd` only
|
|
6
|
+
* starts a name where the identifier grammar allows one. Escapes stay exactly
|
|
7
|
+
* as authored, matching the frozen `ident` JSDoc examples (`\31 23`,
|
|
8
|
+
* `status\:active`).
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { escapeEnd, isNameCode, isNameStartCode } from "./characters.js";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Offset just past the run of name characters and valid escapes starting at
|
|
15
|
+
* `from`, or `from` itself when nothing name-like starts there.
|
|
16
|
+
*/
|
|
17
|
+
export function nameEnd(source: string, from: number): number {
|
|
18
|
+
let index = from;
|
|
19
|
+
while (index < source.length) {
|
|
20
|
+
if (isNameCode(source.charCodeAt(index))) {
|
|
21
|
+
index += 1;
|
|
22
|
+
continue;
|
|
23
|
+
}
|
|
24
|
+
const escaped = escapeEnd(source, index);
|
|
25
|
+
if (escaped <= index) break;
|
|
26
|
+
index = escaped;
|
|
27
|
+
}
|
|
28
|
+
return index;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Whether a CSS identifier can start at `index`: a name-start character, a
|
|
33
|
+
* hyphen followed by a name-start/hyphen/escape (`--label`, `-webkit-calc`), or
|
|
34
|
+
* a valid escape.
|
|
35
|
+
*/
|
|
36
|
+
export function isIdentStartAt(source: string, index: number): boolean {
|
|
37
|
+
const code = source.charCodeAt(index);
|
|
38
|
+
if (isNameStartCode(code)) return true;
|
|
39
|
+
if (code === 0x2d) {
|
|
40
|
+
const next = source.charCodeAt(index + 1);
|
|
41
|
+
return isNameStartCode(next) || next === 0x2d || escapeEnd(source, index + 1) > index + 1;
|
|
42
|
+
}
|
|
43
|
+
return escapeEnd(source, index) > index;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** End offset of the identifier starting at `from`, or `from` when none starts. */
|
|
47
|
+
export function identEnd(source: string, from: number): number {
|
|
48
|
+
return isIdentStartAt(source, from) ? nameEnd(source, from) : from;
|
|
49
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Numeric token scanning.
|
|
3
|
+
*
|
|
4
|
+
* The frozen `number` JSDoc keeps a numeric token "as authored" (`12`, `0.5`,
|
|
5
|
+
* `10px`), so a number, its dimension unit and a trailing `%` all stay in one
|
|
6
|
+
* token with the unit text preserved. There is no separate percentage or
|
|
7
|
+
* dimension kind.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { isDigitCode } from "./characters.js";
|
|
11
|
+
import { nameEnd } from "./names.js";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Whether a numeric token can start at `index`: a digit, `.` followed by a
|
|
15
|
+
* digit, or a sign followed by either.
|
|
16
|
+
*/
|
|
17
|
+
export function isNumberStartAt(source: string, index: number): boolean {
|
|
18
|
+
const code = source.charCodeAt(index);
|
|
19
|
+
if (isDigitCode(code)) return true;
|
|
20
|
+
if (code === 0x2d || code === 0x2b) {
|
|
21
|
+
const next = source.charCodeAt(index + 1);
|
|
22
|
+
return isDigitCode(next) || (next === 0x2e && isDigitCode(source.charCodeAt(index + 2)));
|
|
23
|
+
}
|
|
24
|
+
return code === 0x2e && isDigitCode(source.charCodeAt(index + 1));
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* End offset of the numeric token starting at `from`.
|
|
29
|
+
*
|
|
30
|
+
* Grammar: optional sign, integer and/or fraction, optional exponent, then a
|
|
31
|
+
* `%` suffix or an identifier unit. A `%` wins over a unit, matching CSS
|
|
32
|
+
* tokenization; anything that is not a name character ends the token.
|
|
33
|
+
*/
|
|
34
|
+
export function numberEnd(source: string, from: number): number {
|
|
35
|
+
let index = from;
|
|
36
|
+
const first = source.charCodeAt(index);
|
|
37
|
+
if (first === 0x2d || first === 0x2b) index += 1;
|
|
38
|
+
|
|
39
|
+
while (isDigitCode(source.charCodeAt(index))) index += 1;
|
|
40
|
+
|
|
41
|
+
if (source.charCodeAt(index) === 0x2e && isDigitCode(source.charCodeAt(index + 1))) {
|
|
42
|
+
index += 1;
|
|
43
|
+
while (isDigitCode(source.charCodeAt(index))) index += 1;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const exponent = source.charCodeAt(index);
|
|
47
|
+
if (exponent === 0x65 || exponent === 0x45) {
|
|
48
|
+
let probe = index + 1;
|
|
49
|
+
const sign = source.charCodeAt(probe);
|
|
50
|
+
if (sign === 0x2d || sign === 0x2b) probe += 1;
|
|
51
|
+
if (isDigitCode(source.charCodeAt(probe))) {
|
|
52
|
+
index = probe;
|
|
53
|
+
while (isDigitCode(source.charCodeAt(index))) index += 1;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (source.charCodeAt(index) === 0x25) return index + 1;
|
|
58
|
+
return nameEnd(source, index);
|
|
59
|
+
}
|