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.
- package/README.md +21 -5
- package/dist/ast.js +334 -123
- package/dist/cli.js +363 -291
- package/dist/compiler.js +126 -89
- package/dist/emitter.js +771 -736
- package/dist/errors.js +5 -2
- package/dist/expressions.js +211 -154
- package/dist/index.js +22 -4
- package/dist/lexer.js +536 -340
- package/dist/parser.js +533 -432
- package/dist/prettier/coffeescript-formatter.js +309 -0
- package/dist/prettier/index.js +105 -0
- package/dist/prettier/options.js +232 -0
- package/dist/prettier/printer.js +559 -0
- package/dist/src/ast.js +338 -0
- package/dist/src/cli.js +430 -0
- package/dist/src/compiler.js +132 -0
- package/dist/src/emitter.js +791 -0
- package/dist/src/errors.js +5 -0
- package/dist/src/expressions.js +223 -0
- package/dist/src/index.js +22 -0
- package/dist/src/lexer.js +504 -0
- package/dist/src/parser.js +575 -0
- package/dist/src/types.js +122 -0
- package/dist/src/vite-plugin.js +78 -0
- package/dist/types.js +122 -12
- package/dist/vite-plugin.js +78 -63
- package/docs/examples.md +201 -1
- package/docs/grammar.md +90 -1
- package/docs/index.html +260 -37
- package/package.json +12 -10
- package/syntaxes/CoffeeHaml.tmLanguage +815 -868
- package/syntaxes/coffeehaml.tmLanguage.json +352 -161
- package/dist/ast.d.ts +0 -117
- package/dist/ast.d.ts.map +0 -1
- package/dist/ast.js.map +0 -1
- package/dist/cli.d.ts +0 -3
- package/dist/cli.d.ts.map +0 -1
- package/dist/cli.js.map +0 -1
- package/dist/compiler.d.ts +0 -10
- package/dist/compiler.d.ts.map +0 -1
- package/dist/compiler.js.map +0 -1
- package/dist/emitter.d.ts +0 -4
- package/dist/emitter.d.ts.map +0 -1
- package/dist/emitter.js.map +0 -1
- package/dist/errors.d.ts +0 -3
- package/dist/errors.d.ts.map +0 -1
- package/dist/errors.js.map +0 -1
- package/dist/expressions.d.ts +0 -22
- package/dist/expressions.d.ts.map +0 -1
- package/dist/expressions.js.map +0 -1
- package/dist/index.d.ts +0 -5
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/lexer.d.ts +0 -30
- package/dist/lexer.d.ts.map +0 -1
- package/dist/lexer.js.map +0 -1
- package/dist/parser.d.ts +0 -10
- package/dist/parser.d.ts.map +0 -1
- package/dist/parser.js.map +0 -1
- package/dist/types.d.ts +0 -76
- package/dist/types.d.ts.map +0 -1
- package/dist/types.js.map +0 -1
- package/dist/vite-plugin.d.ts +0 -7
- package/dist/vite-plugin.d.ts.map +0 -1
- package/dist/vite-plugin.js.map +0 -1
package/dist/parser.js
CHANGED
|
@@ -1,474 +1,575 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
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
|
-
|
|
13
|
-
|
|
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
|
-
|
|
16
|
-
|
|
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
|
-
|
|
20
|
-
|
|
21
|
-
|
|
56
|
+
var token;
|
|
57
|
+
token = this.current();
|
|
58
|
+
this.pos++;
|
|
59
|
+
return token;
|
|
22
60
|
}
|
|
23
|
-
|
|
61
|
+
|
|
24
62
|
error(message, code = 'PARSE_ERROR') {
|
|
25
|
-
|
|
63
|
+
var ref;
|
|
64
|
+
return this.errors.push(new CompileError(message, 'parser', code, (ref = this.current()) != null ? ref.location : void 0));
|
|
26
65
|
}
|
|
27
|
-
|
|
66
|
+
|
|
28
67
|
expect(type) {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
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
|
-
|
|
77
|
+
|
|
39
78
|
recover() {
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
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
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
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
|
-
|
|
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
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
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
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
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
|
-
|
|
83
|
-
|
|
141
|
+
}
|
|
142
|
+
return nodes;
|
|
143
|
+
};
|
|
144
|
+
|
|
84
145
|
// ─── Node Dispatcher ───────────────────────────────────────
|
|
85
|
-
function
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
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
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
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
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
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
|
-
|
|
223
|
-
|
|
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
|
-
|
|
226
|
-
|
|
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
|
-
|
|
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
|
|
232
|
-
|
|
233
|
-
|
|
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
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
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
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
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
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
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
|
-
|
|
265
|
-
state.
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
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
|
-
|
|
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
|
-
|
|
295
|
-
|
|
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
|
-
|
|
351
|
-
|
|
450
|
+
}
|
|
451
|
+
return new Filter(filterName, content, token.location);
|
|
452
|
+
};
|
|
453
|
+
|
|
352
454
|
// ─── Doctype ───────────────────────────────────────────────
|
|
353
|
-
function
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
return new Doctype(
|
|
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
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
return new Text(
|
|
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
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
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
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
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
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
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
|
-
|
|
469
|
-
|
|
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
|
-
|
|
473
|
-
}
|
|
474
|
-
|
|
570
|
+
i++;
|
|
571
|
+
}
|
|
572
|
+
return -1;
|
|
573
|
+
};
|
|
574
|
+
|
|
575
|
+
// ParseResult shape: { document: Document, errors: CompileError[] }
|