coffeehaml 0.6.0 → 0.7.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 (66) hide show
  1. package/README.md +21 -5
  2. package/dist/ast.js +334 -123
  3. package/dist/cli.js +363 -291
  4. package/dist/compiler.js +126 -89
  5. package/dist/emitter.js +771 -736
  6. package/dist/errors.js +5 -2
  7. package/dist/expressions.js +211 -154
  8. package/dist/index.js +22 -4
  9. package/dist/lexer.js +536 -340
  10. package/dist/parser.js +533 -432
  11. package/dist/prettier/coffeescript-formatter.js +309 -0
  12. package/dist/prettier/index.js +105 -0
  13. package/dist/prettier/options.js +232 -0
  14. package/dist/prettier/printer.js +559 -0
  15. package/dist/src/ast.js +338 -0
  16. package/dist/src/cli.js +430 -0
  17. package/dist/src/compiler.js +132 -0
  18. package/dist/src/emitter.js +791 -0
  19. package/dist/src/errors.js +5 -0
  20. package/dist/src/expressions.js +223 -0
  21. package/dist/src/index.js +22 -0
  22. package/dist/src/lexer.js +504 -0
  23. package/dist/src/parser.js +575 -0
  24. package/dist/src/types.js +122 -0
  25. package/dist/src/vite-plugin.js +78 -0
  26. package/dist/types.js +122 -12
  27. package/dist/vite-plugin.js +78 -63
  28. package/docs/examples.md +201 -1
  29. package/docs/grammar.md +90 -1
  30. package/docs/index.html +260 -37
  31. package/package.json +12 -10
  32. package/syntaxes/CoffeeHaml.tmLanguage +815 -868
  33. package/syntaxes/coffeehaml.tmLanguage.json +352 -161
  34. package/dist/ast.d.ts +0 -117
  35. package/dist/ast.d.ts.map +0 -1
  36. package/dist/ast.js.map +0 -1
  37. package/dist/cli.d.ts +0 -3
  38. package/dist/cli.d.ts.map +0 -1
  39. package/dist/cli.js.map +0 -1
  40. package/dist/compiler.d.ts +0 -10
  41. package/dist/compiler.d.ts.map +0 -1
  42. package/dist/compiler.js.map +0 -1
  43. package/dist/emitter.d.ts +0 -4
  44. package/dist/emitter.d.ts.map +0 -1
  45. package/dist/emitter.js.map +0 -1
  46. package/dist/errors.d.ts +0 -3
  47. package/dist/errors.d.ts.map +0 -1
  48. package/dist/errors.js.map +0 -1
  49. package/dist/expressions.d.ts +0 -22
  50. package/dist/expressions.d.ts.map +0 -1
  51. package/dist/expressions.js.map +0 -1
  52. package/dist/index.d.ts +0 -5
  53. package/dist/index.d.ts.map +0 -1
  54. package/dist/index.js.map +0 -1
  55. package/dist/lexer.d.ts +0 -30
  56. package/dist/lexer.d.ts.map +0 -1
  57. package/dist/lexer.js.map +0 -1
  58. package/dist/parser.d.ts +0 -10
  59. package/dist/parser.d.ts.map +0 -1
  60. package/dist/parser.js.map +0 -1
  61. package/dist/types.d.ts +0 -76
  62. package/dist/types.d.ts.map +0 -1
  63. package/dist/types.js.map +0 -1
  64. package/dist/vite-plugin.d.ts +0 -7
  65. package/dist/vite-plugin.d.ts.map +0 -1
  66. package/dist/vite-plugin.js.map +0 -1
package/dist/parser.js CHANGED
@@ -1,474 +1,575 @@
1
- import { TokenType } from './lexer.js';
2
- import { Document, Element, ImplicitDiv, Text, Output, ControlFlow, Comment, Filter, Doctype, Expression, } from './ast.js';
3
- import { CompileError } from './types.js';
4
- // ─── Parser State ──────────────────────────────────────────
5
- class ParserState {
6
- constructor(tokens, filename) {
7
- this.errors = [];
8
- this.tokens = tokens;
9
- this.pos = 0;
10
- this.filename = filename;
1
+ // Generated by CoffeeScript 2.7.0
2
+ // parser.coffee — recursive descent parser
3
+ // Converts token stream into AST
4
+ var ParserState, findColon, isElse, parseAttributeBlock, parseBlock, parseComment, parseControlFlow, parseControlKind, parseDoctype, parseElement, parseFilter, parseHtmlComment, parseImplicitDiv, parseNode, parseOutput, parseText, splitAttributePairs, stripControlKeyword;
5
+
6
+ import {
7
+ TokenType
8
+ } from './lexer.js';
9
+
10
+ import {
11
+ Document,
12
+ Element,
13
+ ImplicitDiv,
14
+ Text,
15
+ Output,
16
+ ControlFlow,
17
+ Comment,
18
+ Filter,
19
+ Doctype,
20
+ Expression,
21
+ Attribute,
22
+ SpreadAttribute
23
+ } from './ast.js';
24
+
25
+ import {
26
+ CompileError
27
+ } from './types.js';
28
+
29
+ ParserState = (function() {
30
+ // ─── Parser State ──────────────────────────────────────────
31
+ class ParserState {
32
+ constructor(tokens1, filename1 = null) {
33
+ this.tokens = tokens1;
34
+ this.filename = filename1;
35
+ this.pos = 0;
36
+ this.errors = [];
11
37
  }
12
- get current() {
13
- return this.pos < this.tokens.length ? this.tokens[this.pos] : null;
38
+
39
+ current() {
40
+ if (this.pos < this.tokens.length) {
41
+ return this.tokens[this.pos];
42
+ } else {
43
+ return null;
44
+ }
14
45
  }
15
- get peek() {
16
- return this.pos + 1 < this.tokens.length ? this.tokens[this.pos + 1] : null;
46
+
47
+ peek() {
48
+ if (this.pos + 1 < this.tokens.length) {
49
+ return this.tokens[this.pos + 1];
50
+ } else {
51
+ return null;
52
+ }
17
53
  }
54
+
18
55
  advance() {
19
- const token = this.current;
20
- this.pos++;
21
- return token;
56
+ var token;
57
+ token = this.current();
58
+ this.pos++;
59
+ return token;
22
60
  }
23
- /** Record an error and attempt recovery by skipping to the next safe boundary. */
61
+
24
62
  error(message, code = 'PARSE_ERROR') {
25
- this.errors.push(new CompileError(message, 'parser', code, this.current?.location));
63
+ var ref;
64
+ return this.errors.push(new CompileError(message, 'parser', code, (ref = this.current()) != null ? ref.location : void 0));
26
65
  }
27
- /** Expect a token type. On mismatch, records error and recovers (never throws). */
66
+
28
67
  expect(type) {
29
- const token = this.current;
30
- if (!token || token.type !== type) {
31
- this.error(`Expected ${type} but got ${token?.type ?? 'EOF'}`, 'UNEXPECTED_TOKEN');
32
- // Recovery: skip to next structural token
33
- this.recover();
34
- return null;
35
- }
36
- return this.advance();
68
+ var ref, token;
69
+ token = this.current();
70
+ if (!token || token.type !== type) {
71
+ this.error(`Expected ${type} but got ${(ref = token != null ? token.type : void 0) != null ? ref : 'EOF'}`, 'UNEXPECTED_TOKEN');
72
+ this.recover();
73
+ return null;
74
+ }
75
+ return this.advance();
37
76
  }
38
- /** Skip tokens until a structural boundary (INDENT/DEDENT/block-level token). */
77
+
39
78
  recover() {
40
- while (this.current) {
41
- const t = this.current.type;
42
- if (t === TokenType.TAG || t === TokenType.CLASS || t === TokenType.ID ||
43
- t === TokenType.OUTPUT || t === TokenType.OUTPUT_UNESC || t === TokenType.CONTROL ||
44
- t === TokenType.FILTER || t === TokenType.COMMENT || t === TokenType.HTML_COMMENT ||
45
- t === TokenType.DOCTYPE || t === TokenType.INDENT || t === TokenType.DEDENT) {
46
- return;
47
- }
48
- this.advance();
79
+ var t;
80
+ while (this.current()) {
81
+ t = this.current().type;
82
+ if (t === TokenType.TAG || t === TokenType.CLASS || t === TokenType.ID || t === TokenType.OUTPUT || t === TokenType.OUTPUT_UNESC || t === TokenType.CONTROL || t === TokenType.FILTER || t === TokenType.COMMENT || t === TokenType.HTML_COMMENT || t === TokenType.DOCTYPE || t === TokenType.INDENT || t === TokenType.DEDENT) {
83
+ return;
49
84
  }
85
+ this.advance();
86
+ }
50
87
  }
88
+
51
89
  skip(type) {
52
- if (this.current?.type === type) {
53
- this.advance();
54
- return true;
55
- }
56
- return false;
90
+ var ref;
91
+ if (((ref = this.current()) != null ? ref.type : void 0) === type) {
92
+ this.advance();
93
+ return true;
94
+ }
95
+ return false;
57
96
  }
97
+
58
98
  isAt(type) {
59
- return this.current?.type === type;
99
+ var ref;
100
+ return ((ref = this.current()) != null ? ref.type : void 0) === type;
60
101
  }
61
- }
102
+
103
+ };
104
+
105
+ ParserState.prototype.tokens = [];
106
+
107
+ ParserState.prototype.pos = 0;
108
+
109
+ ParserState.prototype.filename = null;
110
+
111
+ ParserState.prototype.errors = [];
112
+
113
+ return ParserState;
114
+
115
+ }).call(this);
116
+
62
117
  // ─── Public API ────────────────────────────────────────────
63
- export function parse(tokens, filename) {
64
- const state = new ParserState(tokens, filename);
65
- // Collect prologue (raw JS lines before the first HAML construct)
66
- const prologue = [];
67
- while (state.current?.type === TokenType.PROLOGUE) {
68
- prologue.push(state.advance().value);
69
- }
70
- const children = parseBlock(state);
71
- return { document: new Document(children, prologue), errors: state.errors };
72
- }
118
+ export var parse = function(tokens, filename = null) {
119
+ var children, prologue, ref, state;
120
+ state = new ParserState(tokens, filename);
121
+ prologue = [];
122
+ while (((ref = state.current()) != null ? ref.type : void 0) === TokenType.PROLOGUE) {
123
+ prologue.push(state.advance().value);
124
+ }
125
+ children = parseBlock(state);
126
+ return {
127
+ document: new Document(children, prologue),
128
+ errors: state.errors
129
+ };
130
+ };
131
+
73
132
  // ─── Block Parsing ─────────────────────────────────────────
74
- /** Parse nodes until DEDENT or EOF. */
75
- function parseBlock(state) {
76
- const nodes = [];
77
- while (state.current && state.current.type !== TokenType.DEDENT) {
78
- const node = parseNode(state);
79
- if (node)
80
- nodes.push(node);
133
+ parseBlock = function(state) {
134
+ var node, nodes;
135
+ nodes = [];
136
+ while (state.current() && state.current().type !== TokenType.DEDENT) {
137
+ node = parseNode(state);
138
+ if (node) {
139
+ nodes.push(node);
81
140
  }
82
- return nodes;
83
- }
141
+ }
142
+ return nodes;
143
+ };
144
+
84
145
  // ─── Node Dispatcher ───────────────────────────────────────
85
- function parseNode(state) {
86
- const token = state.current;
87
- if (!token)
88
- return null;
89
- switch (token.type) {
90
- case TokenType.TAG:
91
- return parseElement(state);
92
- case TokenType.CLASS:
93
- case TokenType.ID:
94
- return parseImplicitDiv(state);
95
- case TokenType.OUTPUT:
96
- case TokenType.OUTPUT_UNESC:
97
- return parseOutput(state);
98
- case TokenType.CONTROL:
99
- return parseControlFlow(state);
100
- case TokenType.COMMENT:
101
- return parseComment(state);
102
- case TokenType.HTML_COMMENT:
103
- return parseHtmlComment(state);
104
- case TokenType.FILTER:
105
- return parseFilter(state);
106
- case TokenType.DOCTYPE:
107
- return parseDoctype(state);
108
- case TokenType.TEXT:
109
- return parseText(state);
110
- case TokenType.INDENT:
111
- state.advance(); // skip, handled by parseBlock
112
- return null;
113
- case TokenType.DEDENT:
114
- return null; // handled by parseBlock loop
115
- case TokenType.NEWLINE:
116
- state.advance();
117
- return null;
118
- default:
119
- state.advance();
120
- return null;
121
- }
122
- }
146
+ parseNode = function(state) {
147
+ var token;
148
+ token = state.current();
149
+ if (!token) {
150
+ return null;
151
+ }
152
+ switch (token.type) {
153
+ case TokenType.TAG:
154
+ return parseElement(state);
155
+ case TokenType.CLASS:
156
+ return parseImplicitDiv(state);
157
+ case TokenType.ID:
158
+ return parseImplicitDiv(state);
159
+ case TokenType.OUTPUT:
160
+ return parseOutput(state);
161
+ case TokenType.OUTPUT_UNESC:
162
+ return parseOutput(state);
163
+ case TokenType.CONTROL:
164
+ return parseControlFlow(state);
165
+ case TokenType.COMMENT:
166
+ return parseComment(state);
167
+ case TokenType.HTML_COMMENT:
168
+ return parseHtmlComment(state);
169
+ case TokenType.FILTER:
170
+ return parseFilter(state);
171
+ case TokenType.DOCTYPE:
172
+ return parseDoctype(state);
173
+ case TokenType.TEXT:
174
+ return parseText(state);
175
+ case TokenType.INDENT:
176
+ state.advance();
177
+ return null;
178
+ case TokenType.DEDENT:
179
+ return null;
180
+ case TokenType.NEWLINE:
181
+ state.advance();
182
+ return null;
183
+ default:
184
+ state.advance();
185
+ return null;
186
+ }
187
+ };
188
+
123
189
  // ─── Element ───────────────────────────────────────────────
124
- function parseElement(state) {
125
- const tagToken = state.expect(TokenType.TAG);
126
- if (!tagToken)
127
- return new Element('div', { location: state.current?.location });
128
- const tag = tagToken.value;
129
- const isComponent = /^[A-Z]/.test(tag);
130
- let classes = [];
131
- let id = null;
132
- let attributes = [];
133
- let isSelfClosing = false;
134
- // Parse modifiers and attributes
135
- while (state.current &&
136
- (state.current.type === TokenType.CLASS ||
137
- state.current.type === TokenType.ID ||
138
- state.current.type === TokenType.ATTRS_BRACE ||
139
- state.current.type === TokenType.ATTRS_PAREN ||
140
- state.current.type === TokenType.SELF_CLOSE)) {
141
- const tok = state.current;
142
- if (tok.type === TokenType.CLASS) {
143
- state.advance();
144
- classes.push(tok.value);
145
- }
146
- else if (tok.type === TokenType.ID) {
147
- state.advance();
148
- id = tok.value; // last #id wins
149
- }
150
- else if (tok.type === TokenType.ATTRS_BRACE) {
151
- state.advance();
152
- attributes.push(...parseAttributeBlock(tok.value, '{}', tagToken.location));
153
- }
154
- else if (tok.type === TokenType.ATTRS_PAREN) {
155
- state.advance();
156
- attributes.push(...parseAttributeBlock(tok.value, '()', tagToken.location));
157
- }
158
- else if (tok.type === TokenType.SELF_CLOSE) {
159
- state.advance();
160
- isSelfClosing = true;
161
- }
162
- }
163
- // Parse inline text or output if present
164
- let children = [];
165
- if (state.current && !isSelfClosing) {
166
- if (state.current.type === TokenType.OUTPUT) {
167
- const tok = state.advance();
168
- children.push(new Output(new Expression(tok.value), 'escaped', tok.location));
169
- }
170
- else if (state.current.type === TokenType.OUTPUT_UNESC) {
171
- const tok = state.advance();
172
- children.push(new Output(new Expression(tok.value), 'unescaped', tok.location));
173
- }
174
- else if (state.current.type === TokenType.TEXT) {
175
- const textToken = state.advance();
176
- children.push(new Text(textToken.value, textToken.location));
177
- }
178
- }
179
- // Parse child block if INDENT follows
180
- if (state.current?.type === TokenType.INDENT && !isSelfClosing) {
181
- state.advance(); // consume INDENT
182
- children = children.concat(parseBlock(state));
183
- state.expect(TokenType.DEDENT);
184
- }
185
- return new Element(tag, { classes, id, attributes, children, isComponent, isSelfClosing, location: tagToken.location });
186
- }
187
- // ─── ImplicitDiv ───────────────────────────────────────────
188
- function parseImplicitDiv(state) {
189
- let classes = [];
190
- let id = null;
191
- let attributes = [];
192
- const firstToken = state.current; // for location
193
- while (state.current &&
194
- (state.current.type === TokenType.CLASS ||
195
- state.current.type === TokenType.ID ||
196
- state.current.type === TokenType.ATTRS_BRACE ||
197
- state.current.type === TokenType.ATTRS_PAREN)) {
198
- const tok = state.current;
199
- if (tok.type === TokenType.CLASS) {
200
- state.advance();
201
- classes.push(tok.value);
202
- }
203
- else if (tok.type === TokenType.ID) {
204
- state.advance();
205
- id = tok.value;
206
- }
207
- else if (tok.type === TokenType.ATTRS_BRACE) {
208
- state.advance();
209
- attributes.push(...parseAttributeBlock(tok.value, '{}', firstToken.location));
210
- }
211
- else if (tok.type === TokenType.ATTRS_PAREN) {
212
- state.advance();
213
- attributes.push(...parseAttributeBlock(tok.value, '()', firstToken.location));
214
- }
190
+ parseElement = function(state) {
191
+ var attrStyle, attributes, children, classes, id, isComponent, isSelfClosing, ref, ref1, ref2, tag, tagToken, textToken, tok;
192
+ tagToken = state.expect(TokenType.TAG);
193
+ if (!tagToken) {
194
+ return new Element('div', {
195
+ location: (ref = state.current()) != null ? ref.location : void 0
196
+ });
197
+ }
198
+ tag = tagToken.value;
199
+ isComponent = /^[A-Z]/.test(tag);
200
+ classes = [];
201
+ id = null;
202
+ attributes = [];
203
+ isSelfClosing = false;
204
+ attrStyle = null;
205
+ while (state.current() && ((ref1 = state.current().type) === TokenType.CLASS || ref1 === TokenType.ID || ref1 === TokenType.ATTRS_BRACE || ref1 === TokenType.ATTRS_PAREN || ref1 === TokenType.SELF_CLOSE)) {
206
+ tok = state.current();
207
+ switch (tok.type) {
208
+ case TokenType.CLASS:
209
+ state.advance();
210
+ classes.push(tok.value);
211
+ break;
212
+ case TokenType.ID:
213
+ state.advance();
214
+ id = tok.value;
215
+ break;
216
+ case TokenType.ATTRS_BRACE:
217
+ state.advance();
218
+ attributes.push(...parseAttributeBlock(tok.value, '{}', tagToken.location));
219
+ attrStyle = 'braces';
220
+ break;
221
+ case TokenType.ATTRS_PAREN:
222
+ state.advance();
223
+ attributes.push(...parseAttributeBlock(tok.value, '()', tagToken.location));
224
+ attrStyle = 'parens';
225
+ break;
226
+ case TokenType.SELF_CLOSE:
227
+ state.advance();
228
+ isSelfClosing = true;
215
229
  }
216
- // Parse inline text
217
- let children = [];
218
- if (state.current && state.current.type === TokenType.TEXT) {
219
- const textToken = state.advance();
230
+ }
231
+ // Parse inline text or output
232
+ children = [];
233
+ if (state.current() && !isSelfClosing) {
234
+ switch (state.current().type) {
235
+ case TokenType.OUTPUT:
236
+ tok = state.advance();
237
+ children.push(new Output(new Expression(tok.value), 'escaped', tok.location));
238
+ break;
239
+ case TokenType.OUTPUT_UNESC:
240
+ tok = state.advance();
241
+ children.push(new Output(new Expression(tok.value), 'unescaped', tok.location));
242
+ break;
243
+ case TokenType.TEXT:
244
+ textToken = state.advance();
220
245
  children.push(new Text(textToken.value, textToken.location));
221
246
  }
222
- // Parse child block
223
- if (state.current?.type === TokenType.INDENT) {
247
+ }
248
+ // Parse child block if INDENT follows
249
+ if (((ref2 = state.current()) != null ? ref2.type : void 0) === TokenType.INDENT && !isSelfClosing) {
250
+ state.advance();
251
+ children = children.concat(parseBlock(state));
252
+ state.expect(TokenType.DEDENT);
253
+ }
254
+ return new Element(tag, {
255
+ classes,
256
+ id,
257
+ attributes,
258
+ children,
259
+ isComponent,
260
+ isSelfClosing,
261
+ attrStyle,
262
+ location: tagToken.location
263
+ });
264
+ };
265
+
266
+ // ─── ImplicitDiv ───────────────────────────────────────────
267
+ parseImplicitDiv = function(state) {
268
+ var attrStyle, attributes, children, classes, firstToken, id, ref, ref1, ref2, textToken, tok;
269
+ classes = [];
270
+ id = null;
271
+ attributes = [];
272
+ attrStyle = null;
273
+ firstToken = state.current();
274
+ while (state.current() && ((ref = state.current().type) === TokenType.CLASS || ref === TokenType.ID || ref === TokenType.ATTRS_BRACE || ref === TokenType.ATTRS_PAREN)) {
275
+ tok = state.current();
276
+ switch (tok.type) {
277
+ case TokenType.CLASS:
224
278
  state.advance();
225
- children = children.concat(parseBlock(state));
226
- state.expect(TokenType.DEDENT);
279
+ classes.push(tok.value);
280
+ break;
281
+ case TokenType.ID:
282
+ state.advance();
283
+ id = tok.value;
284
+ break;
285
+ case TokenType.ATTRS_BRACE:
286
+ state.advance();
287
+ attributes.push(...parseAttributeBlock(tok.value, '{}', firstToken.location));
288
+ attrStyle = 'braces';
289
+ break;
290
+ case TokenType.ATTRS_PAREN:
291
+ state.advance();
292
+ attributes.push(...parseAttributeBlock(tok.value, '()', firstToken.location));
293
+ attrStyle = 'parens';
227
294
  }
228
- return new ImplicitDiv({ classes, id, attributes, children, location: firstToken.location });
229
- }
295
+ }
296
+ // Parse inline text
297
+ children = [];
298
+ if (((ref1 = state.current()) != null ? ref1.type : void 0) === TokenType.TEXT) {
299
+ textToken = state.advance();
300
+ children.push(new Text(textToken.value, textToken.location));
301
+ }
302
+ // Parse child block
303
+ if (((ref2 = state.current()) != null ? ref2.type : void 0) === TokenType.INDENT) {
304
+ state.advance();
305
+ children = children.concat(parseBlock(state));
306
+ state.expect(TokenType.DEDENT);
307
+ }
308
+ return new ImplicitDiv({
309
+ classes,
310
+ id,
311
+ attributes,
312
+ children,
313
+ attrStyle,
314
+ location: firstToken.location
315
+ });
316
+ };
317
+
230
318
  // ─── Output ────────────────────────────────────────────────
231
- function parseOutput(state) {
232
- const token = state.current;
233
- const outputKind = token.type === TokenType.OUTPUT ? 'escaped' : 'unescaped';
319
+ parseOutput = function(state) {
320
+ var children, expr, outputKind, ref, token;
321
+ token = state.current();
322
+ outputKind = token.type === TokenType.OUTPUT ? 'escaped' : 'unescaped';
323
+ state.advance();
324
+ expr = new Expression(token.value);
325
+ children = [];
326
+ if (((ref = state.current()) != null ? ref.type : void 0) === TokenType.INDENT) {
234
327
  state.advance();
235
- const expr = new Expression(token.value, undefined);
236
- // Parse continuation children if INDENT follows
237
- let children = [];
238
- if (state.current?.type === TokenType.INDENT) {
239
- state.advance();
240
- children = parseBlock(state);
241
- state.expect(TokenType.DEDENT);
242
- }
243
- return new Output(expr, outputKind, token.location, children);
244
- }
328
+ children = parseBlock(state);
329
+ state.expect(TokenType.DEDENT);
330
+ }
331
+ return new Output(expr, outputKind, token.location, children);
332
+ };
333
+
245
334
  // ─── Control Flow ──────────────────────────────────────────
246
- function parseControlFlow(state) {
247
- const token = state.expect(TokenType.CONTROL);
248
- if (!token)
249
- return new ControlFlow('statement', new Expression(''), [], null, state.current?.location);
250
- let source = token.value;
251
- // Normalize "else if" → "if" so it chains naturally in ternary
252
- if (/^\s*else\s+if\b/.test(source)) {
253
- source = source.replace(/^\s*else\s+/, '');
335
+ parseControlFlow = function(state) {
336
+ var children, controlKind, expr, exprSource, next, nextSource, ref, ref1, ref2, source, token;
337
+ token = state.expect(TokenType.CONTROL);
338
+ if (!token) {
339
+ return new ControlFlow('statement', new Expression(''), [], null, (ref = state.current()) != null ? ref.location : void 0);
340
+ }
341
+ source = token.value;
342
+ // Normalize "else if" → "if"
343
+ if (/^\s*else\s+if\b/.test(source)) {
344
+ source = source.replace(/^\s*else\s+/, '');
345
+ }
346
+ controlKind = parseControlKind(source);
347
+ exprSource = stripControlKeyword(source, controlKind);
348
+ expr = new Expression(exprSource);
349
+ children = [];
350
+ if (((ref1 = state.current()) != null ? ref1.type : void 0) === TokenType.INDENT) {
351
+ state.advance();
352
+ children = parseBlock(state);
353
+ state.expect(TokenType.DEDENT);
354
+ }
355
+ // Check for chained else / else if
356
+ next = null;
357
+ if (((ref2 = state.current()) != null ? ref2.type : void 0) === TokenType.CONTROL) {
358
+ nextSource = state.current().value.trimStart();
359
+ if (isElse(nextSource)) {
360
+ next = parseControlFlow(state);
254
361
  }
255
- // Parse the control kind from the expression
256
- const controlKind = parseControlKind(source);
257
- // Strip the keyword from the expression — store only the condition/iterable
258
- const exprSource = stripControlKeyword(source, controlKind);
259
- const expr = new Expression(exprSource, undefined);
260
- // Parse body if INDENT follows
261
- let children = [];
262
- if (state.current?.type === TokenType.INDENT) {
362
+ }
363
+ return new ControlFlow(controlKind, expr, children, next, token.location);
364
+ };
365
+
366
+ parseControlKind = function(source) {
367
+ var keyword;
368
+ keyword = source.trimStart().split(/\s+/)[0];
369
+ switch (keyword) {
370
+ case 'if':
371
+ return 'if';
372
+ case 'unless':
373
+ return 'unless';
374
+ case 'for':
375
+ return 'for';
376
+ case 'while':
377
+ return 'while';
378
+ case 'else':
379
+ return 'else';
380
+ default:
381
+ return 'statement';
382
+ }
383
+ };
384
+
385
+ stripControlKeyword = function(source, kind) {
386
+ var keyword, re, trimmed;
387
+ trimmed = source.trimStart();
388
+ if (kind === 'statement') {
389
+ return trimmed;
390
+ }
391
+ keyword = kind === 'else' ? 'else' : kind;
392
+ re = new RegExp(`^\\s*${keyword}\\b\\s*`);
393
+ return trimmed.replace(re, '');
394
+ };
395
+
396
+ isElse = function(source) {
397
+ return /^\s*else\b/.test(source);
398
+ };
399
+
400
+ // ─── Comments ──────────────────────────────────────────────
401
+ parseComment = function(state) {
402
+ var token;
403
+ token = state.expect(TokenType.COMMENT);
404
+ if (!token) {
405
+ return new Comment('haml', '');
406
+ }
407
+ return new Comment('haml', token.value, token.location);
408
+ };
409
+
410
+ parseHtmlComment = function(state) {
411
+ var token;
412
+ token = state.expect(TokenType.HTML_COMMENT);
413
+ if (!token) {
414
+ return new Comment('html', '');
415
+ }
416
+ return new Comment('html', token.value, token.location);
417
+ };
418
+
419
+ // ─── Filter ────────────────────────────────────────────────
420
+ parseFilter = function(state) {
421
+ var content, filterName, lines, parts, tok, token;
422
+ token = state.expect(TokenType.FILTER);
423
+ if (!token) {
424
+ return new Filter('', '');
425
+ }
426
+ parts = token.value.split('\n');
427
+ filterName = parts[0];
428
+ content = parts.slice(1).join('\n');
429
+ // Consume indented body
430
+ if (state.isAt(TokenType.INDENT)) {
431
+ state.advance();
432
+ lines = [];
433
+ while (state.current() && !state.isAt(TokenType.DEDENT)) {
434
+ tok = state.current();
435
+ if (tok.type === TokenType.TEXT) {
436
+ lines.push(tok.value);
263
437
  state.advance();
264
- children = parseBlock(state);
265
- state.expect(TokenType.DEDENT);
266
- }
267
- // Check for chained else / else if
268
- let next = null;
269
- if (state.current?.type === TokenType.CONTROL) {
270
- const nextSource = state.current.value.trimStart();
271
- if (isElse(nextSource)) {
272
- next = parseControlFlow(state);
273
- }
274
- }
275
- return new ControlFlow(controlKind, expr, children, next, token.location);
276
- }
277
- function parseControlKind(source) {
278
- const trimmed = source.trimStart();
279
- const keyword = trimmed.split(/\s+/)[0];
280
- switch (keyword) {
281
- case 'if': return 'if';
282
- case 'unless': return 'unless';
283
- case 'for': return 'for';
284
- case 'while': return 'while';
285
- case 'else': return 'else';
286
- default: return 'statement';
438
+ } else if (tok.type === TokenType.NEWLINE) {
439
+ state.advance();
440
+ } else {
441
+ break;
442
+ }
287
443
  }
288
- }
289
- function stripControlKeyword(source, kind) {
290
- const trimmed = source.trimStart();
291
- if (kind === 'statement') {
292
- return trimmed; // no keyword to strip — entire source is the statement
444
+ if (state.isAt(TokenType.DEDENT)) {
445
+ state.advance();
293
446
  }
294
- // Remove the keyword and any whitespace after it
295
- const keyword = kind === 'else' ? 'else' : kind;
296
- const re = new RegExp("^\\s*" + keyword + "\\b\\s*");
297
- return trimmed.replace(re, '');
298
- }
299
- function isElse(source) {
300
- return /^\s*else\b/.test(source);
301
- }
302
- // ─── Comments ──────────────────────────────────────────────
303
- function parseComment(state) {
304
- const token = state.expect(TokenType.COMMENT);
305
- if (!token)
306
- return new Comment('haml', '');
307
- return new Comment('haml', token.value, token.location);
308
- }
309
- function parseHtmlComment(state) {
310
- const token = state.expect(TokenType.HTML_COMMENT);
311
- if (!token)
312
- return new Comment('html', '');
313
- return new Comment('html', token.value, token.location);
314
- }
315
- // ─── Filter ────────────────────────────────────────────────
316
- function parseFilter(state) {
317
- const token = state.expect(TokenType.FILTER);
318
- if (!token)
319
- return new Filter('', '');
320
- const parts = token.value.split('\n');
321
- const filterName = parts[0];
322
- let content = parts.slice(1).join('\n');
323
- // Consume indented children as filter body (Haml convention).
324
- // After `:markdown`, all indented content belongs to the filter.
325
- if (state.isAt(TokenType.INDENT)) {
326
- state.advance(); // skip INDENT
327
- const lines = [];
328
- while (state.current && !state.isAt(TokenType.DEDENT)) {
329
- const tok = state.current;
330
- if (tok.type === TokenType.TEXT) {
331
- lines.push(tok.value);
332
- state.advance();
333
- }
334
- else if (tok.type === TokenType.NEWLINE) {
335
- state.advance();
336
- }
337
- else {
338
- // Unexpected token in filter body — break to avoid infinite loop
339
- break;
340
- }
341
- }
342
- // Consume DEDENT
343
- if (state.isAt(TokenType.DEDENT)) {
344
- state.advance();
345
- }
346
- if (lines.length > 0) {
347
- content = (content ? content + '\n' : '') + lines.join('\n');
348
- }
447
+ if (lines.length > 0) {
448
+ content = content ? content + '\n' + lines.join('\n') : lines.join('\n');
349
449
  }
350
- return new Filter(filterName, content, token.location);
351
- }
450
+ }
451
+ return new Filter(filterName, content, token.location);
452
+ };
453
+
352
454
  // ─── Doctype ───────────────────────────────────────────────
353
- function parseDoctype(state) {
354
- const token = state.expect(TokenType.DOCTYPE);
355
- if (!token)
356
- return new Doctype('html');
357
- return new Doctype(token.value || 'html', token.location);
358
- }
455
+ parseDoctype = function(state) {
456
+ var token;
457
+ token = state.expect(TokenType.DOCTYPE);
458
+ if (!token) {
459
+ return new Doctype('html');
460
+ }
461
+ return new Doctype(token.value || 'html', token.location);
462
+ };
463
+
359
464
  // ─── Text ──────────────────────────────────────────────────
360
- function parseText(state) {
361
- const token = state.expect(TokenType.TEXT);
362
- if (!token)
363
- return new Text('');
364
- return new Text(token.value, token.location);
365
- }
465
+ parseText = function(state) {
466
+ var token;
467
+ token = state.expect(TokenType.TEXT);
468
+ if (!token) {
469
+ return new Text('');
470
+ }
471
+ return new Text(token.value, token.location);
472
+ };
473
+
366
474
  // ─── Attribute Block Parser ────────────────────────────────
367
- /** Parse a CoffeeScript object literal into attributes.
368
- * Uses a simple key-value parser that handles:
369
- * {key: value, key2: value2}
370
- * {shorthand}
371
- * {key: "string", 'quoted-key': val, nested: {a: 1}}
372
- */
373
- function parseAttributeBlock(source, _style, _location) {
374
- const attrs = [];
375
- if (!source.trim())
376
- return attrs;
377
- // Simple attribute parser: split on commas outside of brackets/strings
378
- const pairs = splitAttributePairs(source);
379
- for (const pair of pairs) {
380
- const trimmed = pair.trim();
381
- if (!trimmed)
382
- continue;
383
- // Spread attribute: props... (CoffeeScript form) or ...props (JSX form)
384
- if (trimmed.endsWith('...')) {
385
- const expr = trimmed.slice(0, -3).trim();
386
- if (expr) {
387
- attrs.push({ spread: true, expression: new Expression(expr) });
388
- }
389
- continue;
390
- }
391
- if (trimmed.startsWith('...')) {
392
- const expr = trimmed.slice(3).trim();
393
- if (expr) {
394
- attrs.push({ spread: true, expression: new Expression(expr) });
395
- }
396
- continue;
397
- }
398
- const colonIdx = findColon(trimmed);
399
- if (colonIdx === -1) {
400
- // Shorthand: {foo} → foo={foo}
401
- attrs.push({
402
- name: trimmed,
403
- value: new Expression(trimmed),
404
- shorthand: true,
405
- });
406
- }
407
- else {
408
- const name = trimmed.slice(0, colonIdx).trim();
409
- const value = trimmed.slice(colonIdx + 1).trim();
410
- // Strip quotes from key if present
411
- const cleanName = name.replace(/^['"]|['"]$/g, '');
412
- attrs.push({
413
- name: cleanName,
414
- value: new Expression(value),
415
- shorthand: false,
416
- });
417
- }
418
- }
475
+ parseAttributeBlock = function(source, _style, _location = null) {
476
+ var attrs, cleanName, colonIdx, expr, j, len, name, pair, pairs, trimmed, value;
477
+ attrs = [];
478
+ if (!source.trim()) {
419
479
  return attrs;
420
- }
421
- /** Split attribute string on commas, respecting nested brackets and string literals. */
422
- function splitAttributePairs(source) {
423
- const result = [];
424
- let depth = 0;
425
- let start = 0;
426
- for (let i = 0; i < source.length; i++) {
427
- const ch = source[i];
428
- if (ch === '{' || ch === '[' || ch === '(')
429
- depth++;
430
- else if (ch === '}' || ch === ']' || ch === ')')
431
- depth--;
432
- else if ((ch === '"' || ch === "'") && depth === 0) {
433
- // Skip over string literals
434
- const q = ch;
435
- i++;
436
- while (i < source.length && source[i] !== q) {
437
- if (source[i] === '\\')
438
- i++;
439
- i++;
440
- }
441
- }
442
- else if (ch === ',' && depth === 0) {
443
- result.push(source.slice(start, i));
444
- start = i + 1;
445
- }
480
+ }
481
+ pairs = splitAttributePairs(source);
482
+ for (j = 0, len = pairs.length; j < len; j++) {
483
+ pair = pairs[j];
484
+ trimmed = pair.trim();
485
+ if (!trimmed) {
486
+ continue;
487
+ }
488
+ // Spread: props... or ...props
489
+ if (trimmed.endsWith('...')) {
490
+ expr = trimmed.slice(0, -3).trim();
491
+ if (expr) {
492
+ attrs.push(new SpreadAttribute(new Expression(expr)));
493
+ }
494
+ continue;
446
495
  }
447
- result.push(source.slice(start));
448
- return result;
449
- }
450
- /** Find the first colon that is not inside brackets or strings. */
451
- function findColon(source) {
452
- let depth = 0;
453
- for (let i = 0; i < source.length; i++) {
454
- const ch = source[i];
455
- if (ch === '{' || ch === '[' || ch === '(')
456
- depth++;
457
- else if (ch === '}' || ch === ']' || ch === ')')
458
- depth--;
459
- else if ((ch === '"' || ch === "'") && depth === 0) {
460
- const q = ch;
461
- i++;
462
- while (i < source.length && source[i] !== q) {
463
- if (source[i] === '\\')
464
- i++;
465
- i++;
466
- }
496
+ if (trimmed.startsWith('...')) {
497
+ expr = trimmed.slice(3).trim();
498
+ if (expr) {
499
+ attrs.push(new SpreadAttribute(new Expression(expr)));
500
+ }
501
+ continue;
502
+ }
503
+ colonIdx = findColon(trimmed);
504
+ if (colonIdx === -1) {
505
+ // Shorthand: {foo} → foo={foo}
506
+ attrs.push(new Attribute(trimmed, new Expression(trimmed), true));
507
+ } else {
508
+ name = trimmed.slice(0, colonIdx).trim();
509
+ value = trimmed.slice(colonIdx + 1).trim();
510
+ cleanName = name.replace(/^['"]|['"]$/g, '');
511
+ attrs.push(new Attribute(cleanName, new Expression(value), false));
512
+ }
513
+ }
514
+ return attrs;
515
+ };
516
+
517
+ splitAttributePairs = function(source) {
518
+ var ch, depth, i, q, result, start;
519
+ result = [];
520
+ depth = 0;
521
+ start = 0;
522
+ i = 0;
523
+ while (i < source.length) {
524
+ ch = source[i];
525
+ if (ch === '{' || ch === '[' || ch === '(') {
526
+ depth++;
527
+ } else if (ch === '}' || ch === ']' || ch === ')') {
528
+ depth--;
529
+ } else if ((ch === '"' || ch === "'") && depth === 0) {
530
+ q = ch;
531
+ i++;
532
+ while (i < source.length && source[i] !== q) {
533
+ if (source[i] === '\\') {
534
+ i++;
467
535
  }
468
- else if (ch === ':' && depth === 0) {
469
- return i;
536
+ i++;
537
+ }
538
+ } else if (ch === ',' && depth === 0) {
539
+ result.push(source.slice(start, i));
540
+ start = i + 1;
541
+ }
542
+ i++;
543
+ }
544
+ result.push(source.slice(start));
545
+ return result;
546
+ };
547
+
548
+ findColon = function(source) {
549
+ var ch, depth, i, q;
550
+ depth = 0;
551
+ i = 0;
552
+ while (i < source.length) {
553
+ ch = source[i];
554
+ if (ch === '{' || ch === '[' || ch === '(') {
555
+ depth++;
556
+ } else if (ch === '}' || ch === ']' || ch === ')') {
557
+ depth--;
558
+ } else if ((ch === '"' || ch === "'") && depth === 0) {
559
+ q = ch;
560
+ i++;
561
+ while (i < source.length && source[i] !== q) {
562
+ if (source[i] === '\\') {
563
+ i++;
470
564
  }
565
+ i++;
566
+ }
567
+ } else if (ch === ':' && depth === 0) {
568
+ return i;
471
569
  }
472
- return -1;
473
- }
474
- //# sourceMappingURL=parser.js.map
570
+ i++;
571
+ }
572
+ return -1;
573
+ };
574
+
575
+ // ParseResult shape: { document: Document, errors: CompileError[] }