@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.
Files changed (85) hide show
  1. package/README.md +5 -5
  2. package/package.json +3 -3
  3. package/src/emitter/index.ts +658 -0
  4. package/src/formatter/index.ts +544 -0
  5. package/src/host/node.ts +85 -0
  6. package/src/index.ts +1021 -0
  7. package/src/ir/factories.ts +85 -0
  8. package/src/ir/index.ts +28 -0
  9. package/src/ir/types.ts +145 -0
  10. package/src/lexer/at-keywords.ts +35 -0
  11. package/src/lexer/characters.ts +82 -0
  12. package/src/lexer/comments.ts +27 -0
  13. package/src/lexer/diagnostics.ts +42 -0
  14. package/src/lexer/dollar-refs.ts +23 -0
  15. package/src/lexer/index.ts +37 -0
  16. package/src/lexer/names.ts +49 -0
  17. package/src/lexer/numbers.ts +59 -0
  18. package/src/lexer/scanner.ts +199 -0
  19. package/src/lexer/strings.ts +52 -0
  20. package/src/lexer/urls.ts +63 -0
  21. package/src/lowering/expand-groups.ts +206 -0
  22. package/src/lowering/group.ts +119 -0
  23. package/src/lowering/index.ts +43 -0
  24. package/src/lowering/lower.ts +441 -0
  25. package/src/lowering/media.ts +314 -0
  26. package/src/lowering/merge.ts +163 -0
  27. package/src/lowering/property-families.ts +194 -0
  28. package/src/lowering/spans.ts +118 -0
  29. package/src/naming/index.ts +44 -0
  30. package/src/naming/mapgen.ts +252 -0
  31. package/src/naming/merge.ts +114 -0
  32. package/src/naming/rewrite.ts +333 -0
  33. package/src/naming/scan.ts +267 -0
  34. package/src/naming/table.ts +153 -0
  35. package/src/naming/types.ts +100 -0
  36. package/src/output/import-path.ts +30 -0
  37. package/src/output/index.ts +213 -0
  38. package/src/output/manifest.ts +28 -0
  39. package/src/output/paths.ts +200 -0
  40. package/src/output/resolve.ts +401 -0
  41. package/src/output/validate.ts +251 -0
  42. package/src/parser/cursor.ts +138 -0
  43. package/src/parser/declaration.ts +247 -0
  44. package/src/parser/diagnostics.ts +75 -0
  45. package/src/parser/group.ts +303 -0
  46. package/src/parser/index.ts +50 -0
  47. package/src/parser/modifier.ts +60 -0
  48. package/src/parser/recovery.ts +88 -0
  49. package/src/parser/rule.ts +370 -0
  50. package/src/parser/statement.ts +47 -0
  51. package/src/parser/statements.ts +667 -0
  52. package/src/parser/value.ts +265 -0
  53. package/src/resolver/graph.ts +467 -0
  54. package/src/resolver/groups.ts +177 -0
  55. package/src/resolver/index.ts +68 -0
  56. package/src/resolver/keyframes.ts +131 -0
  57. package/src/resolver/paths.ts +142 -0
  58. package/src/resolver/rules.ts +54 -0
  59. package/src/resolver/scopes.ts +342 -0
  60. package/src/resolver/types.ts +259 -0
  61. package/src/selectors/flatten.ts +448 -0
  62. package/src/selectors/index.ts +14 -0
  63. package/src/selectors/scan.ts +227 -0
  64. package/src/selectors/specificity.ts +319 -0
  65. package/src/setup-list.ts +172 -0
  66. package/src/sourcemaps/index.ts +25 -0
  67. package/src/sourcemaps/mappings.ts +190 -0
  68. package/src/sourcemaps/vlq.ts +29 -0
  69. package/src/types/ast.ts +318 -0
  70. package/src/types/diagnostics.ts +124 -0
  71. package/src/types/options.ts +286 -0
  72. package/src/types/result.ts +121 -0
  73. package/src/types/span.ts +18 -0
  74. package/src/types/token.ts +77 -0
  75. package/src/variables/ast.ts +182 -0
  76. package/src/variables/expand.ts +319 -0
  77. package/src/variables/index.ts +38 -0
  78. package/src/variables/naming.ts +46 -0
  79. package/src/variables/runtime.ts +336 -0
  80. package/src/variables/substitute.ts +148 -0
  81. package/src/variables/walk.ts +147 -0
  82. package/dist/output/importPath.d.ts +0 -27
  83. package/dist/output/importPath.d.ts.map +0 -1
  84. package/dist/output/importPath.js +0 -84
  85. package/dist/output/importPath.js.map +0 -1
@@ -0,0 +1,370 @@
1
+ import type { BlockItem, ConditionalBlock, Modifier, NativeAtRule, Rule } from "../types/ast.js";
2
+ import type { Token } from "../types/token.js";
3
+ import { lastContentIndex, skipTriviaAt, tokenAt, trimWhitespace } from "./cursor.js";
4
+ import { type ParseContext, parseDeclaration } from "./declaration.js";
5
+ import { messages } from "./diagnostics.js";
6
+ import { isGroupAtKeyword, parseGroupUseSite, rejectGroupConstruct } from "./group.js";
7
+ import { readModifier } from "./modifier.js";
8
+ import { recoverStatement, skipAtRule, skipBalancedBlock, skipStatementBlock } from "./recovery.js";
9
+ import { classifyStatement } from "./statement.js";
10
+
11
+ export type { StatementKind } from "./statement.js";
12
+ export { classifyStatement };
13
+
14
+ /**
15
+ * Native CSS at-rules keep their authored meaning (SPECIFICATION.md §3.5) and
16
+ * are never Chikx conditional blocks (ADR-0004). Names are matched
17
+ * case-insensitively.
18
+ */
19
+ const NATIVE_AT_RULES = new Set([
20
+ "charset",
21
+ "container",
22
+ "counter-style",
23
+ "document",
24
+ "font-face",
25
+ "font-feature-values",
26
+ "font-palette-values",
27
+ "import",
28
+ "keyframes",
29
+ "layer",
30
+ "media",
31
+ "namespace",
32
+ "nest",
33
+ "page",
34
+ "position-try",
35
+ "property",
36
+ "scope",
37
+ "starting-style",
38
+ "supports",
39
+ "view-transition",
40
+ ]);
41
+
42
+ /** At-rule name without the leading `@`. */
43
+ export function atRuleName(token: Token): string {
44
+ return token.value.startsWith("@") ? token.value.slice(1) : token.value;
45
+ }
46
+
47
+ export function isNativeAtRuleName(name: string): boolean {
48
+ return NATIVE_AT_RULES.has(name.toLowerCase());
49
+ }
50
+
51
+ interface BlockOptions {
52
+ /** Native at-rules may appear here (rule bodies and native blocks). */
53
+ readonly allowNative: boolean;
54
+ /** Conditional blocks may appear here (rule and conditional bodies). */
55
+ readonly allowConditional: boolean;
56
+ /** `@group.<name>` use sites may appear here (rule bodies and native blocks). */
57
+ readonly allowGroup: boolean;
58
+ }
59
+
60
+ /**
61
+ * Parse members of a `{ ... }` block, stopping before its `}` (the caller
62
+ * consumes it) or at end of input. Every iteration advances at least one token.
63
+ */
64
+ export function parseBlockItems(ctx: ParseContext, options: BlockOptions): readonly BlockItem[] {
65
+ const { cursor, bag } = ctx;
66
+ const items: BlockItem[] = [];
67
+
68
+ while (true) {
69
+ cursor.skipTrivia();
70
+ const guard = cursor.index;
71
+ const token = cursor.current();
72
+
73
+ if (token.kind === "eof") break;
74
+ if (token.kind === "rbrace") break;
75
+ if (token.kind === "semicolon") {
76
+ cursor.index++;
77
+ continue;
78
+ }
79
+
80
+ if (token.kind === "lbrace") {
81
+ bag.add("CHIKX1001", messages.unexpectedToken, cursor.span(token));
82
+ cursor.index = skipBalancedBlock(cursor.tokens, cursor.index, cursor.endOffset);
83
+ } else if (token.kind === "at-keyword" || token.kind === "modifier") {
84
+ const item = parseAtRuleInBlock(ctx, options);
85
+ if (item !== undefined) items.push(item);
86
+ } else if (classifyStatement(cursor) === "rule") {
87
+ const rule = parseRule(ctx);
88
+ if (rule !== undefined) items.push(rule);
89
+ } else {
90
+ const declaration = parseDeclaration(ctx);
91
+ if (declaration !== undefined) items.push(declaration);
92
+ }
93
+
94
+ if (cursor.index === guard) {
95
+ bag.add("CHIKX1001", messages.internalRecovery, cursor.span(token));
96
+ cursor.index++;
97
+ }
98
+ }
99
+
100
+ return Object.freeze(items);
101
+ }
102
+
103
+ /**
104
+ * Parse `selector { ... }`. The selector stays a raw slice with its own span
105
+ * (D1/ADR-0002: bare nested selectors are ordinary rules here; descendant and
106
+ * `&` semantics are lowering concerns per ADR-0012).
107
+ */
108
+ export function parseRule(ctx: ParseContext): Rule | undefined {
109
+ const { cursor, bag } = ctx;
110
+ const tokens = cursor.tokens;
111
+ const file = cursor.file;
112
+ const startIndex = cursor.index;
113
+ const startToken = cursor.current();
114
+
115
+ const closers: Array<"rparen" | "rbracket"> = [];
116
+ let index = cursor.index;
117
+ let openIndex = -1;
118
+
119
+ while (true) {
120
+ const token = tokenAt(tokens, index, cursor.endOffset);
121
+ if (token.kind === "eof") break;
122
+ if (token.kind === "lparen") closers.push("rparen");
123
+ else if (token.kind === "lbracket") closers.push("rbracket");
124
+ else if (token.kind === "rparen" || token.kind === "rbracket") {
125
+ if (closers.pop() !== token.kind) break;
126
+ } else if (closers.length === 0) {
127
+ if (token.kind === "lbrace") {
128
+ openIndex = index;
129
+ break;
130
+ }
131
+ if (token.kind === "semicolon" || token.kind === "rbrace") break;
132
+ }
133
+ index++;
134
+ }
135
+
136
+ if (openIndex === -1) {
137
+ bag.add("CHIKX1001", messages.unexpectedStatement, cursor.span(startToken));
138
+ recoverStatement(cursor);
139
+ return undefined;
140
+ }
141
+
142
+ const selectorBounds = trimWhitespace(tokens, startIndex, openIndex);
143
+ if (selectorBounds.start >= selectorBounds.end) {
144
+ bag.add("CHIKX1001", messages.emptySelector, cursor.spanOfRange(startIndex, openIndex));
145
+ cursor.index = skipBalancedBlock(tokens, openIndex, cursor.endOffset);
146
+ return undefined;
147
+ }
148
+
149
+ const selector = cursor.text(selectorBounds.start, selectorBounds.end);
150
+ const selectorSpan = cursor.spanOfRange(selectorBounds.start, selectorBounds.end);
151
+
152
+ cursor.index = openIndex + 1;
153
+ const body = parseBlockItems(ctx, {
154
+ allowNative: true,
155
+ allowConditional: true,
156
+ allowGroup: true,
157
+ });
158
+
159
+ let end = cursor.endOffset;
160
+ const closing = cursor.current();
161
+ if (closing.kind === "rbrace") {
162
+ end = closing.end;
163
+ cursor.index++;
164
+ } else {
165
+ bag.add("CHIKX1001", messages.unclosedBlock(`rule "${selector}"`), {
166
+ start: startToken.start,
167
+ end: cursor.endOffset,
168
+ file,
169
+ });
170
+ }
171
+
172
+ return Object.freeze({
173
+ kind: "rule",
174
+ selector,
175
+ selectorSpan,
176
+ body,
177
+ span: { start: startToken.start, end, file },
178
+ });
179
+ }
180
+
181
+ /** Parse `@alias { ... }` (rule-scoped conditional block, D3/ADR-0004). */
182
+ export function parseConditionalBlock(
183
+ ctx: ParseContext,
184
+ variant: Modifier,
185
+ openIndex: number,
186
+ ): ConditionalBlock {
187
+ const { cursor, bag } = ctx;
188
+ const tokens = cursor.tokens;
189
+
190
+ cursor.index = openIndex + 1;
191
+ const parsedBody = parseBlockItems(ctx, {
192
+ allowNative: false,
193
+ allowConditional: true,
194
+ allowGroup: false,
195
+ });
196
+ const body: ConditionalBlock["body"] = parsedBody.filter(
197
+ (item): item is ConditionalBlock["body"][number] => item.kind !== "native-at-rule",
198
+ );
199
+
200
+ let end = cursor.endOffset;
201
+ const closing = cursor.current();
202
+ if (closing.kind === "rbrace") {
203
+ end = closing.end;
204
+ cursor.index++;
205
+ } else {
206
+ bag.add("CHIKX1001", messages.unclosedBlock("conditional block"), {
207
+ start: variant.span.start,
208
+ end: cursor.endOffset,
209
+ file: cursor.file,
210
+ });
211
+ }
212
+
213
+ return Object.freeze({
214
+ kind: "conditional-block",
215
+ variant,
216
+ body: Object.freeze(body),
217
+ span: { start: variant.span.start, end, file: cursor.file },
218
+ });
219
+ }
220
+
221
+ function parseAtRuleInBlock(ctx: ParseContext, options: BlockOptions): BlockItem | undefined {
222
+ const { cursor, bag } = ctx;
223
+ const startToken = cursor.current();
224
+ const name = atRuleName(startToken).toLowerCase();
225
+
226
+ if (isGroupAtKeyword(startToken)) {
227
+ if (!options.allowGroup) {
228
+ rejectGroupConstruct(ctx, "a conditional block");
229
+ return undefined;
230
+ }
231
+ return parseGroupUseSite(ctx);
232
+ }
233
+
234
+ if (isNativeAtRuleName(name)) {
235
+ if (!options.allowNative) {
236
+ bag.add("CHIKX1001", messages.nativeInConditional, cursor.span(startToken));
237
+ cursor.index = skipAtRule(cursor.tokens, cursor.index, cursor.endOffset);
238
+ return undefined;
239
+ }
240
+ return parseNativeAtRule(ctx);
241
+ }
242
+
243
+ const read = readModifier(cursor.tokens, cursor.index, cursor.tokens.length, cursor.file);
244
+ if (read !== undefined) {
245
+ const openIndex = skipTriviaAt(cursor.tokens, read.nextIndex);
246
+ const open = tokenAt(cursor.tokens, openIndex, cursor.endOffset);
247
+ if (open.kind === "lbrace") {
248
+ if (!options.allowConditional) {
249
+ bag.add("CHIKX1001", messages.unexpectedAtRule, cursor.span(startToken));
250
+ cursor.index = skipAtRule(cursor.tokens, cursor.index, cursor.endOffset);
251
+ return undefined;
252
+ }
253
+ return parseConditionalBlock(ctx, read.modifier, openIndex);
254
+ }
255
+ }
256
+
257
+ if (!options.allowNative) {
258
+ bag.add("CHIKX1001", messages.unexpectedAtRule, cursor.span(startToken));
259
+ cursor.index = skipAtRule(cursor.tokens, cursor.index, cursor.endOffset);
260
+ return undefined;
261
+ }
262
+ return parseNativeAtRule(ctx);
263
+ }
264
+
265
+ /**
266
+ * Parse a native at-rule, with or without a block.
267
+ *
268
+ * The prelude stays a raw trimmed slice; block members are parsed with the
269
+ * normal rule-body grammar so `@media`/`@supports` contents keep their authored
270
+ * declarations and nested rules.
271
+ */
272
+ export function parseNativeAtRule(ctx: ParseContext): NativeAtRule | undefined {
273
+ const { cursor, bag } = ctx;
274
+ const tokens = cursor.tokens;
275
+ const file = cursor.file;
276
+ const startToken = cursor.current();
277
+ const name = atRuleName(startToken);
278
+
279
+ const closers: Array<"rparen" | "rbracket"> = [];
280
+ let index = cursor.index + 1;
281
+ let terminator: "lbrace" | "semicolon" | "end" = "end";
282
+ let boundaryIndex = index;
283
+
284
+ while (true) {
285
+ const token = tokenAt(tokens, index, cursor.endOffset);
286
+ if (token.kind === "eof") {
287
+ boundaryIndex = index;
288
+ break;
289
+ }
290
+ if (token.kind === "lparen") closers.push("rparen");
291
+ else if (token.kind === "lbracket") closers.push("rbracket");
292
+ else if (token.kind === "rparen" || token.kind === "rbracket") {
293
+ if (closers.pop() !== token.kind) {
294
+ boundaryIndex = index;
295
+ break;
296
+ }
297
+ } else if (closers.length === 0) {
298
+ if (token.kind === "lbrace") {
299
+ terminator = "lbrace";
300
+ boundaryIndex = index;
301
+ break;
302
+ }
303
+ if (token.kind === "semicolon") {
304
+ terminator = "semicolon";
305
+ boundaryIndex = index;
306
+ break;
307
+ }
308
+ if (token.kind === "rbrace") {
309
+ boundaryIndex = index;
310
+ break;
311
+ }
312
+ }
313
+ index++;
314
+ }
315
+
316
+ const preludeBounds = trimWhitespace(tokens, cursor.index + 1, boundaryIndex);
317
+ const prelude = cursor.text(preludeBounds.start, preludeBounds.end);
318
+ const preludeEnd = lastContentIndex(tokens, preludeBounds.start, preludeBounds.end);
319
+
320
+ if (terminator === "lbrace") {
321
+ cursor.index = boundaryIndex + 1;
322
+ const block = parseBlockItems(ctx, {
323
+ allowNative: true,
324
+ allowConditional: true,
325
+ allowGroup: true,
326
+ });
327
+ let end = cursor.endOffset;
328
+ const closing = cursor.current();
329
+ if (closing.kind === "rbrace") {
330
+ end = closing.end;
331
+ cursor.index++;
332
+ } else {
333
+ bag.add("CHIKX1001", messages.unclosedBlock(`at-rule "@${name}"`), {
334
+ start: startToken.start,
335
+ end: cursor.endOffset,
336
+ file,
337
+ });
338
+ }
339
+ return Object.freeze({
340
+ kind: "native-at-rule",
341
+ name,
342
+ prelude,
343
+ block,
344
+ span: { start: startToken.start, end, file },
345
+ });
346
+ }
347
+
348
+ if (terminator === "semicolon") {
349
+ const semicolon = tokenAt(tokens, boundaryIndex, cursor.endOffset);
350
+ cursor.index = boundaryIndex + 1;
351
+ return Object.freeze({
352
+ kind: "native-at-rule",
353
+ name,
354
+ prelude,
355
+ span: { start: startToken.start, end: semicolon.end, file },
356
+ });
357
+ }
358
+
359
+ cursor.index = Math.min(boundaryIndex, tokens.length);
360
+ const fallbackEnd =
361
+ preludeEnd >= preludeBounds.start
362
+ ? (tokens[preludeEnd]?.end ?? startToken.end)
363
+ : startToken.end;
364
+ return Object.freeze({
365
+ kind: "native-at-rule",
366
+ name,
367
+ prelude,
368
+ span: { start: startToken.start, end: fallbackEnd, file },
369
+ });
370
+ }
@@ -0,0 +1,47 @@
1
+ import { type TokenCursor, tokenAt } from "./cursor.js";
2
+ import { findSemicolonlessRuleStart } from "./value.js";
3
+
4
+ /** How a block statement resolves: a nested rule, a declaration, or neither. */
5
+ export type StatementKind = "rule" | "declaration" | "unknown";
6
+
7
+ /**
8
+ * Balanced lookahead that distinguishes a nested rule from a declaration
9
+ * (ADR-0002, SPECIFICATION.md §3.4).
10
+ *
11
+ * A `{` at top level before the statement terminator means the statement is a
12
+ * rule; a `;` or the enclosing `}` first means it is a declaration. Colon
13
+ * presence alone is never decisive, and strings, URLs, functions, parentheses
14
+ * and brackets are already single tokens or tracked depth.
15
+ *
16
+ * One exception protects ADR-0011: when the statement opens as a declaration
17
+ * and the `{` is preceded by a selector-start token (for example
18
+ * `color: red` then `&:hover {`), the statement is a declaration that lost its
19
+ * semicolon, not a rule whose selector swallowed the value. It classifies as a
20
+ * declaration so the caller reports CHIKX1002 and keeps the nested rule
21
+ * (`findSemicolonlessRuleStart` owns the lookahead).
22
+ */
23
+ export function classifyStatement(cursor: TokenCursor): StatementKind {
24
+ const tokens = cursor.tokens;
25
+ const closers: Array<"rparen" | "rbracket"> = [];
26
+ let index = cursor.index;
27
+
28
+ while (true) {
29
+ const token = tokenAt(tokens, index, cursor.endOffset);
30
+ if (token.kind === "eof") break;
31
+ if (token.kind === "lparen") closers.push("rparen");
32
+ else if (token.kind === "lbracket") closers.push("rbracket");
33
+ else if (token.kind === "rparen" || token.kind === "rbracket") {
34
+ if (closers.pop() !== token.kind) return "unknown";
35
+ } else if (closers.length === 0) {
36
+ if (token.kind === "lbrace") {
37
+ return findSemicolonlessRuleStart(tokens, cursor.index, index) === -1
38
+ ? "rule"
39
+ : "declaration";
40
+ }
41
+ if (token.kind === "semicolon" || token.kind === "rbrace") return "declaration";
42
+ }
43
+ index++;
44
+ }
45
+
46
+ return closers.length === 0 ? "declaration" : "unknown";
47
+ }