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/lexer.js
CHANGED
|
@@ -1,366 +1,562 @@
|
|
|
1
|
+
// Generated by CoffeeScript 2.7.0
|
|
2
|
+
// lexer.coffee — CoffeeHaml tokenizer
|
|
3
|
+
// Converts raw .chaml source into a token stream with INDENT/DEDENT
|
|
4
|
+
var countIndent, dedentToken, extractBracketed, indentToken, isHamlConstruct, parseModifiersAndAttrs, preMergeBracketLines, tokenizeLine;
|
|
5
|
+
|
|
6
|
+
import {
|
|
7
|
+
SourceLocation
|
|
8
|
+
} from './types.js';
|
|
9
|
+
|
|
1
10
|
// ─── Token Types ───────────────────────────────────────────
|
|
2
|
-
export var TokenType
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
// ───
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
11
|
+
export var TokenType = {
|
|
12
|
+
TAG: 'TAG',
|
|
13
|
+
CLASS: 'CLASS',
|
|
14
|
+
ID: 'ID',
|
|
15
|
+
ATTRS_PAREN: 'ATTRS_PAREN',
|
|
16
|
+
ATTRS_BRACE: 'ATTRS_BRACE',
|
|
17
|
+
OUTPUT: 'OUTPUT',
|
|
18
|
+
OUTPUT_UNESC: 'OUTPUT_UNESC',
|
|
19
|
+
CONTROL: 'CONTROL',
|
|
20
|
+
COMMENT: 'COMMENT',
|
|
21
|
+
HTML_COMMENT: 'HTML_COMMENT',
|
|
22
|
+
FILTER: 'FILTER',
|
|
23
|
+
DOCTYPE: 'DOCTYPE',
|
|
24
|
+
TEXT: 'TEXT',
|
|
25
|
+
PROLOGUE: 'PROLOGUE',
|
|
26
|
+
INDENT: 'INDENT',
|
|
27
|
+
DEDENT: 'DEDENT',
|
|
28
|
+
NEWLINE: 'NEWLINE',
|
|
29
|
+
SELF_CLOSE: 'SELF_CLOSE'
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
// ─── Token (plain object convention) ───────────────────────
|
|
33
|
+
// { type, value, location, indent? }
|
|
34
|
+
|
|
35
|
+
// ─── Main entry ────────────────────────────────────────────
|
|
36
|
+
|
|
37
|
+
// ─── Pre-processing: merge lines with unclosed attribute brackets ──
|
|
38
|
+
preMergeBracketLines = function(lines) {
|
|
39
|
+
var i, line, merged, rest, result;
|
|
40
|
+
result = [];
|
|
41
|
+
i = 0;
|
|
42
|
+
while (i < lines.length) {
|
|
43
|
+
line = lines[i];
|
|
44
|
+
rest = line.replace(/^\s+/, '');
|
|
45
|
+
// Check if this is a HAML tag/implicit-div line with unclosed {
|
|
46
|
+
if (/^[%.#]/.test(rest) && rest.includes('{') && !rest.includes('}')) {
|
|
47
|
+
merged = line;
|
|
48
|
+
i++;
|
|
49
|
+
while (i < lines.length) {
|
|
50
|
+
merged += '\n' + lines[i];
|
|
51
|
+
i++;
|
|
52
|
+
if (lines[i - 1].includes('}')) {
|
|
53
|
+
break;
|
|
39
54
|
}
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
55
|
+
}
|
|
56
|
+
result.push(merged);
|
|
57
|
+
} else if (/^[%.#]/.test(rest) && rest.includes('(') && !rest.includes(')')) {
|
|
58
|
+
merged = line;
|
|
59
|
+
i++;
|
|
60
|
+
while (i < lines.length) {
|
|
61
|
+
merged += '\n' + lines[i];
|
|
62
|
+
i++;
|
|
63
|
+
if (lines[i - 1].includes(')')) {
|
|
64
|
+
break;
|
|
47
65
|
}
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
66
|
+
}
|
|
67
|
+
result.push(merged);
|
|
68
|
+
} else {
|
|
69
|
+
result.push(line);
|
|
70
|
+
i++;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return result;
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
export var tokenize = function(source, filename = null) {
|
|
77
|
+
var content, currentIndent, inPrologue, indent, indentStack, j, len, line, lineIndex, lineLength, lineStartOffset, lineTokens, lines, offset, rawLine, tokens;
|
|
78
|
+
tokens = [];
|
|
79
|
+
lines = preMergeBracketLines(source.split('\n'));
|
|
80
|
+
indentStack = [0];
|
|
81
|
+
offset = 0;
|
|
82
|
+
lineIndex = 0;
|
|
83
|
+
inPrologue = true;
|
|
84
|
+
for (j = 0, len = lines.length; j < len; j++) {
|
|
85
|
+
rawLine = lines[j];
|
|
86
|
+
line = rawLine.replace(/\r$/, '');
|
|
87
|
+
lineLength = rawLine.length + 1;
|
|
88
|
+
// Skip empty lines
|
|
89
|
+
if (line.trim() === '') {
|
|
90
|
+
offset += lineLength;
|
|
91
|
+
lineIndex++;
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
indent = countIndent(line);
|
|
95
|
+
content = line.slice(indent);
|
|
96
|
+
if (content === '') {
|
|
97
|
+
offset += lineLength;
|
|
98
|
+
lineIndex++;
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
// Prologue detection: non-indented JS before first HAML construct
|
|
102
|
+
if (inPrologue && indent === 0 && !isHamlConstruct(content)) {
|
|
103
|
+
tokens.push({
|
|
104
|
+
type: TokenType.PROLOGUE,
|
|
105
|
+
value: content,
|
|
106
|
+
location: {
|
|
107
|
+
start: {
|
|
108
|
+
line: lineIndex,
|
|
109
|
+
column: indent
|
|
110
|
+
},
|
|
111
|
+
end: {
|
|
112
|
+
line: lineIndex,
|
|
113
|
+
column: indent + content.length
|
|
114
|
+
},
|
|
115
|
+
offset: offset + indent,
|
|
116
|
+
length: lineLength - indent,
|
|
117
|
+
file: filename
|
|
82
118
|
}
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
offset += lineLength;
|
|
88
|
-
lineIndex++;
|
|
119
|
+
});
|
|
120
|
+
offset += lineLength;
|
|
121
|
+
lineIndex++;
|
|
122
|
+
continue;
|
|
89
123
|
}
|
|
90
|
-
|
|
91
|
-
|
|
124
|
+
inPrologue = false;
|
|
125
|
+
// Handle indentation changes
|
|
126
|
+
currentIndent = indentStack[indentStack.length - 1];
|
|
127
|
+
if (indent > currentIndent) {
|
|
128
|
+
indentStack.push(indent);
|
|
129
|
+
tokens.push(indentToken(indent, offset, indent, filename, lineIndex));
|
|
130
|
+
} else if (indent < currentIndent) {
|
|
131
|
+
while (indentStack.length > 1 && indent < indentStack[indentStack.length - 1]) {
|
|
92
132
|
indentStack.pop();
|
|
93
133
|
tokens.push(dedentToken(offset, filename, lineIndex));
|
|
134
|
+
}
|
|
135
|
+
if (indent !== indentStack[indentStack.length - 1]) {
|
|
136
|
+
indentStack.push(indent);
|
|
137
|
+
tokens.push(indentToken(indent, offset, indent, filename, lineIndex));
|
|
138
|
+
}
|
|
94
139
|
}
|
|
95
|
-
|
|
96
|
-
|
|
140
|
+
// Parse line content
|
|
141
|
+
lineStartOffset = offset + indent;
|
|
142
|
+
lineTokens = tokenizeLine(content, lineStartOffset, lineLength - indent, filename, lineIndex);
|
|
143
|
+
tokens.push(...lineTokens);
|
|
144
|
+
offset += lineLength;
|
|
145
|
+
lineIndex++;
|
|
146
|
+
}
|
|
147
|
+
// Emit remaining DEDENT tokens at EOF
|
|
148
|
+
while (indentStack.length > 1) {
|
|
149
|
+
indentStack.pop();
|
|
150
|
+
tokens.push(dedentToken(offset, filename, lineIndex));
|
|
151
|
+
}
|
|
152
|
+
return tokens;
|
|
153
|
+
};
|
|
154
|
+
|
|
97
155
|
// ─── Line Tokenization ─────────────────────────────────────
|
|
98
|
-
function
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
156
|
+
tokenizeLine = function(content, lineStartOffset, _lineLength, filename = null, lineIndex = 0) {
|
|
157
|
+
var end, expr, filterContent, filterName, firstChar, loc, modResult, pos, remaining, spaceIdx, tagName, tokens, trimmedOffset, val;
|
|
158
|
+
tokens = [];
|
|
159
|
+
pos = 0;
|
|
160
|
+
loc = function(start, end) {
|
|
161
|
+
return {
|
|
162
|
+
start: {
|
|
163
|
+
line: lineIndex != null ? lineIndex : 0,
|
|
164
|
+
column: lineStartOffset + start
|
|
165
|
+
},
|
|
166
|
+
end: {
|
|
167
|
+
line: lineIndex != null ? lineIndex : 0,
|
|
168
|
+
column: lineStartOffset + end
|
|
169
|
+
},
|
|
170
|
+
offset: lineStartOffset + start,
|
|
171
|
+
length: end - start,
|
|
172
|
+
file: filename
|
|
173
|
+
};
|
|
174
|
+
};
|
|
175
|
+
firstChar = content[0];
|
|
176
|
+
// %tag
|
|
177
|
+
if (firstChar === '%') {
|
|
178
|
+
end = 1;
|
|
179
|
+
while (end < content.length && /[\w-]/.test(content[end])) {
|
|
180
|
+
end++;
|
|
109
181
|
}
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
}
|
|
126
|
-
// Parse modifiers and attributes on the rest of the line
|
|
127
|
-
const modResult = parseModifiersAndAttrs(content, pos, lineStartOffset, filename, lineIndex);
|
|
128
|
-
tokens.push(...modResult.tokens);
|
|
129
|
-
pos = modResult.pos;
|
|
130
|
-
// Parse inline output or text after tag and attributes
|
|
131
|
-
const remaining = content.slice(pos).trimStart();
|
|
132
|
-
const trimmedOffset = content.length - remaining.length;
|
|
133
|
-
if (remaining) {
|
|
134
|
-
if (remaining.startsWith('= ') || remaining === '=') {
|
|
135
|
-
tokens.push({ type: TokenType.OUTPUT, value: remaining.slice(1).trimStart(), location: loc(trimmedOffset + 1, content.length) });
|
|
136
|
-
}
|
|
137
|
-
else if (remaining.startsWith('!= ')) {
|
|
138
|
-
tokens.push({ type: TokenType.OUTPUT_UNESC, value: remaining.slice(2).trimStart(), location: loc(trimmedOffset + 2, content.length) });
|
|
139
|
-
}
|
|
140
|
-
else if (!remaining.startsWith('{') && !remaining.startsWith('(')) {
|
|
141
|
-
tokens.push({
|
|
142
|
-
type: TokenType.TEXT,
|
|
143
|
-
value: remaining,
|
|
144
|
-
location: loc(trimmedOffset, content.length),
|
|
145
|
-
});
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
return tokens;
|
|
182
|
+
tagName = content.slice(1, end);
|
|
183
|
+
tokens.push({
|
|
184
|
+
type: TokenType.TAG,
|
|
185
|
+
value: tagName,
|
|
186
|
+
location: loc(0, end)
|
|
187
|
+
});
|
|
188
|
+
pos = end;
|
|
189
|
+
// Self-closing /
|
|
190
|
+
if (content[pos] === '/') {
|
|
191
|
+
tokens.push({
|
|
192
|
+
type: TokenType.SELF_CLOSE,
|
|
193
|
+
value: '/',
|
|
194
|
+
location: loc(pos, pos + 1)
|
|
195
|
+
});
|
|
196
|
+
pos++;
|
|
149
197
|
}
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
//
|
|
154
|
-
if (
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
if (remaining) {
|
|
162
|
-
if (remaining.startsWith('= ') || remaining === '=') {
|
|
163
|
-
tokens.push({ type: TokenType.OUTPUT, value: remaining.slice(1).trimStart(), location: loc(trimmedOffset + 1, content.length) });
|
|
164
|
-
}
|
|
165
|
-
else if (remaining.startsWith('!= ')) {
|
|
166
|
-
tokens.push({ type: TokenType.OUTPUT_UNESC, value: remaining.slice(2).trimStart(), location: loc(trimmedOffset + 2, content.length) });
|
|
167
|
-
}
|
|
168
|
-
else if (!remaining.startsWith('{') && !remaining.startsWith('(')) {
|
|
169
|
-
tokens.push({
|
|
170
|
-
type: TokenType.TEXT,
|
|
171
|
-
value: remaining,
|
|
172
|
-
location: loc(trimmedOffset, content.length),
|
|
173
|
-
});
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
return tokens;
|
|
198
|
+
modResult = parseModifiersAndAttrs(content, pos, lineStartOffset, filename, lineIndex);
|
|
199
|
+
tokens.push(...modResult.tokens);
|
|
200
|
+
pos = modResult.pos;
|
|
201
|
+
// Self-closing / after attributes (must check BEFORE remaining text)
|
|
202
|
+
if (content[pos] === '/') {
|
|
203
|
+
tokens.push({
|
|
204
|
+
type: TokenType.SELF_CLOSE,
|
|
205
|
+
value: '/',
|
|
206
|
+
location: loc(pos, pos + 1)
|
|
207
|
+
});
|
|
208
|
+
pos++;
|
|
177
209
|
}
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
210
|
+
remaining = content.slice(pos).trimStart();
|
|
211
|
+
trimmedOffset = content.length - remaining.length;
|
|
212
|
+
if (remaining) {
|
|
213
|
+
if (remaining.startsWith('= ') || remaining === '=') {
|
|
214
|
+
tokens.push({
|
|
215
|
+
type: TokenType.OUTPUT,
|
|
216
|
+
value: remaining.slice(1).trimStart(),
|
|
217
|
+
location: loc(trimmedOffset + 1, content.length)
|
|
218
|
+
});
|
|
219
|
+
} else if (remaining.startsWith('!= ')) {
|
|
220
|
+
tokens.push({
|
|
221
|
+
type: TokenType.OUTPUT_UNESC,
|
|
222
|
+
value: remaining.slice(2).trimStart(),
|
|
223
|
+
location: loc(trimmedOffset + 2, content.length)
|
|
224
|
+
});
|
|
225
|
+
} else if (!(remaining.startsWith('{') || remaining.startsWith('('))) {
|
|
226
|
+
tokens.push({
|
|
227
|
+
type: TokenType.TEXT,
|
|
228
|
+
value: remaining,
|
|
229
|
+
location: loc(trimmedOffset, content.length)
|
|
230
|
+
});
|
|
231
|
+
}
|
|
182
232
|
}
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
233
|
+
return tokens;
|
|
234
|
+
}
|
|
235
|
+
// .class / #id (implicit div) — only if followed by valid identifier
|
|
236
|
+
if ((firstChar === '.' || firstChar === '#') && /^[.#][\w-]/.test(content)) {
|
|
237
|
+
modResult = parseModifiersAndAttrs(content, 0, lineStartOffset, filename, lineIndex);
|
|
238
|
+
tokens.push(...modResult.tokens);
|
|
239
|
+
pos = modResult.pos;
|
|
240
|
+
// Self-closing / after attributes (must check BEFORE remaining text)
|
|
241
|
+
if (content[pos] === '/') {
|
|
242
|
+
tokens.push({
|
|
243
|
+
type: TokenType.SELF_CLOSE,
|
|
244
|
+
value: '/',
|
|
245
|
+
location: loc(pos, pos + 1)
|
|
246
|
+
});
|
|
247
|
+
pos++;
|
|
248
|
+
}
|
|
249
|
+
remaining = content.slice(pos).trimStart();
|
|
250
|
+
trimmedOffset = content.length - remaining.length;
|
|
251
|
+
if (remaining) {
|
|
252
|
+
if (remaining.startsWith('= ') || remaining === '=') {
|
|
253
|
+
tokens.push({
|
|
254
|
+
type: TokenType.OUTPUT,
|
|
255
|
+
value: remaining.slice(1).trimStart(),
|
|
256
|
+
location: loc(trimmedOffset + 1, content.length)
|
|
257
|
+
});
|
|
258
|
+
} else if (remaining.startsWith('!= ')) {
|
|
259
|
+
tokens.push({
|
|
260
|
+
type: TokenType.OUTPUT_UNESC,
|
|
261
|
+
value: remaining.slice(2).trimStart(),
|
|
262
|
+
location: loc(trimmedOffset + 2, content.length)
|
|
263
|
+
});
|
|
264
|
+
} else if (!(remaining.startsWith('{') || remaining.startsWith('('))) {
|
|
265
|
+
tokens.push({
|
|
266
|
+
type: TokenType.TEXT,
|
|
267
|
+
value: remaining,
|
|
268
|
+
location: loc(trimmedOffset, content.length)
|
|
269
|
+
});
|
|
270
|
+
}
|
|
187
271
|
}
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
272
|
+
return tokens;
|
|
273
|
+
}
|
|
274
|
+
// = expression
|
|
275
|
+
if (firstChar === '=' && content[1] !== '=') {
|
|
276
|
+
expr = content.slice(1).trimStart();
|
|
277
|
+
return [
|
|
278
|
+
{
|
|
279
|
+
type: TokenType.OUTPUT,
|
|
280
|
+
value: expr,
|
|
281
|
+
location: loc(1,
|
|
282
|
+
content.length)
|
|
283
|
+
}
|
|
284
|
+
];
|
|
285
|
+
}
|
|
286
|
+
// != expression
|
|
287
|
+
if (firstChar === '!' && content[1] === '=') {
|
|
288
|
+
expr = content.slice(2).trimStart();
|
|
289
|
+
return [
|
|
290
|
+
{
|
|
291
|
+
type: TokenType.OUTPUT_UNESC,
|
|
292
|
+
value: expr,
|
|
293
|
+
location: loc(2,
|
|
294
|
+
content.length)
|
|
295
|
+
}
|
|
296
|
+
];
|
|
297
|
+
}
|
|
298
|
+
// - control / comment
|
|
299
|
+
if (firstChar === '-') {
|
|
300
|
+
if (content[1] === '#') {
|
|
301
|
+
return [
|
|
302
|
+
{
|
|
303
|
+
type: TokenType.COMMENT,
|
|
304
|
+
value: content.slice(2).trimStart(),
|
|
305
|
+
location: loc(2,
|
|
306
|
+
content.length)
|
|
193
307
|
}
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
308
|
+
];
|
|
309
|
+
}
|
|
310
|
+
expr = content.slice(1).trimStart();
|
|
311
|
+
return [
|
|
312
|
+
{
|
|
313
|
+
type: TokenType.CONTROL,
|
|
314
|
+
value: expr,
|
|
315
|
+
location: loc(1,
|
|
316
|
+
content.length)
|
|
317
|
+
}
|
|
318
|
+
];
|
|
319
|
+
}
|
|
320
|
+
// / HTML comment
|
|
321
|
+
if (firstChar === '/') {
|
|
322
|
+
return [
|
|
323
|
+
{
|
|
324
|
+
type: TokenType.HTML_COMMENT,
|
|
325
|
+
value: content.slice(1).trimStart(),
|
|
326
|
+
location: loc(1,
|
|
327
|
+
content.length)
|
|
328
|
+
}
|
|
329
|
+
];
|
|
330
|
+
}
|
|
331
|
+
// :filter
|
|
332
|
+
if (firstChar === ':') {
|
|
333
|
+
spaceIdx = content.search(/\s/);
|
|
334
|
+
filterName = spaceIdx === -1 ? content.slice(1) : content.slice(1, spaceIdx);
|
|
335
|
+
filterContent = spaceIdx === -1 ? '' : content.slice(spaceIdx + 1);
|
|
336
|
+
return [
|
|
337
|
+
{
|
|
338
|
+
type: TokenType.FILTER,
|
|
339
|
+
value: `${filterName}\n${filterContent}`,
|
|
340
|
+
location: loc(0,
|
|
341
|
+
content.length)
|
|
342
|
+
}
|
|
343
|
+
];
|
|
344
|
+
}
|
|
345
|
+
// !!! doctype
|
|
346
|
+
if (content.startsWith('!!!')) {
|
|
347
|
+
val = content.slice(3).trimStart();
|
|
348
|
+
return [
|
|
349
|
+
{
|
|
350
|
+
type: TokenType.DOCTYPE,
|
|
351
|
+
value: val || 'html',
|
|
352
|
+
location: loc(0,
|
|
353
|
+
content.length)
|
|
354
|
+
}
|
|
355
|
+
];
|
|
356
|
+
}
|
|
357
|
+
return [
|
|
358
|
+
{
|
|
359
|
+
// Plain text
|
|
360
|
+
type: TokenType.TEXT,
|
|
361
|
+
value: content,
|
|
362
|
+
location: loc(0,
|
|
363
|
+
content.length)
|
|
197
364
|
}
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
365
|
+
];
|
|
366
|
+
};
|
|
367
|
+
|
|
368
|
+
// ─── Modifiers and Attributes ──────────────────────────────
|
|
369
|
+
parseModifiersAndAttrs = function(content, startPos, lineStartOffset, filename = null, lineIndex = 0) {
|
|
370
|
+
var block, ch, className, end, idName, loc, pos, tokens;
|
|
371
|
+
tokens = [];
|
|
372
|
+
pos = startPos;
|
|
373
|
+
loc = function(start, end) {
|
|
374
|
+
return {
|
|
375
|
+
start: {
|
|
376
|
+
line: lineIndex != null ? lineIndex : 0,
|
|
377
|
+
column: lineStartOffset + start
|
|
378
|
+
},
|
|
379
|
+
end: {
|
|
380
|
+
line: lineIndex != null ? lineIndex : 0,
|
|
381
|
+
column: lineStartOffset + end
|
|
382
|
+
},
|
|
383
|
+
offset: lineStartOffset + start,
|
|
384
|
+
length: end - start,
|
|
385
|
+
file: filename
|
|
386
|
+
};
|
|
387
|
+
};
|
|
388
|
+
while (pos < content.length) {
|
|
389
|
+
ch = content[pos];
|
|
390
|
+
if (ch === ' ' || ch === '\t') {
|
|
391
|
+
pos++;
|
|
392
|
+
continue;
|
|
201
393
|
}
|
|
202
|
-
//
|
|
203
|
-
if (
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
394
|
+
// .class
|
|
395
|
+
if (ch === '.') {
|
|
396
|
+
end = pos + 1;
|
|
397
|
+
while (end < content.length && /[\w-]/.test(content[end])) {
|
|
398
|
+
end++;
|
|
399
|
+
}
|
|
400
|
+
className = content.slice(pos + 1, end);
|
|
401
|
+
tokens.push({
|
|
402
|
+
type: TokenType.CLASS,
|
|
403
|
+
value: className,
|
|
404
|
+
location: loc(pos, end)
|
|
405
|
+
});
|
|
406
|
+
pos = end;
|
|
407
|
+
continue;
|
|
208
408
|
}
|
|
209
|
-
//
|
|
210
|
-
if (
|
|
211
|
-
|
|
212
|
-
|
|
409
|
+
// #id
|
|
410
|
+
if (ch === '#') {
|
|
411
|
+
end = pos + 1;
|
|
412
|
+
while (end < content.length && /[\w-]/.test(content[end])) {
|
|
413
|
+
end++;
|
|
414
|
+
}
|
|
415
|
+
idName = content.slice(pos + 1, end);
|
|
416
|
+
tokens.push({
|
|
417
|
+
type: TokenType.ID,
|
|
418
|
+
value: idName,
|
|
419
|
+
location: loc(pos, end)
|
|
420
|
+
});
|
|
421
|
+
pos = end;
|
|
422
|
+
continue;
|
|
213
423
|
}
|
|
214
|
-
//
|
|
215
|
-
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
424
|
+
// {attribute block}
|
|
425
|
+
if (ch === '{') {
|
|
426
|
+
block = extractBracketed(content, pos, '{', '}');
|
|
427
|
+
if (block !== null) {
|
|
428
|
+
tokens.push({
|
|
429
|
+
type: TokenType.ATTRS_BRACE,
|
|
430
|
+
value: block,
|
|
431
|
+
location: loc(pos, pos + block.length + 2)
|
|
432
|
+
});
|
|
433
|
+
pos += block.length + 2;
|
|
434
|
+
} else {
|
|
435
|
+
tokens.push({
|
|
436
|
+
type: TokenType.ATTRS_BRACE,
|
|
437
|
+
value: content.slice(pos + 1),
|
|
438
|
+
location: loc(pos, content.length)
|
|
439
|
+
});
|
|
440
|
+
pos = content.length;
|
|
441
|
+
}
|
|
442
|
+
continue;
|
|
229
443
|
}
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
if (ch === '#') {
|
|
250
|
-
let end = pos + 1;
|
|
251
|
-
while (end < content.length && /[\w-]/.test(content[end])) {
|
|
252
|
-
end++;
|
|
253
|
-
}
|
|
254
|
-
const idName = content.slice(pos + 1, end);
|
|
255
|
-
tokens.push({ type: TokenType.ID, value: idName, location: loc(pos, end) });
|
|
256
|
-
pos = end;
|
|
257
|
-
continue;
|
|
258
|
-
}
|
|
259
|
-
// {attribute block}
|
|
260
|
-
if (ch === '{') {
|
|
261
|
-
const block = extractBracketed(content, pos, '{', '}');
|
|
262
|
-
if (block !== null) {
|
|
263
|
-
tokens.push({ type: TokenType.ATTRS_BRACE, value: block, location: loc(pos, pos + block.length + 2) });
|
|
264
|
-
pos += block.length + 2;
|
|
265
|
-
}
|
|
266
|
-
else {
|
|
267
|
-
// Unterminated — consume rest
|
|
268
|
-
tokens.push({ type: TokenType.ATTRS_BRACE, value: content.slice(pos + 1), location: loc(pos, content.length) });
|
|
269
|
-
pos = content.length;
|
|
270
|
-
}
|
|
271
|
-
continue;
|
|
272
|
-
}
|
|
273
|
-
// (attribute block)
|
|
274
|
-
if (ch === '(') {
|
|
275
|
-
const block = extractBracketed(content, pos, '(', ')');
|
|
276
|
-
if (block !== null) {
|
|
277
|
-
tokens.push({ type: TokenType.ATTRS_PAREN, value: block, location: loc(pos, pos + block.length + 2) });
|
|
278
|
-
pos += block.length + 2;
|
|
279
|
-
}
|
|
280
|
-
else {
|
|
281
|
-
tokens.push({ type: TokenType.ATTRS_PAREN, value: content.slice(pos + 1), location: loc(pos, content.length) });
|
|
282
|
-
pos = content.length;
|
|
283
|
-
}
|
|
284
|
-
continue;
|
|
285
|
-
}
|
|
286
|
-
// Anything else → stop, it's inline text
|
|
287
|
-
break;
|
|
444
|
+
// (attribute block)
|
|
445
|
+
if (ch === '(') {
|
|
446
|
+
block = extractBracketed(content, pos, '(', ')');
|
|
447
|
+
if (block !== null) {
|
|
448
|
+
tokens.push({
|
|
449
|
+
type: TokenType.ATTRS_PAREN,
|
|
450
|
+
value: block,
|
|
451
|
+
location: loc(pos, pos + block.length + 2)
|
|
452
|
+
});
|
|
453
|
+
pos += block.length + 2;
|
|
454
|
+
} else {
|
|
455
|
+
tokens.push({
|
|
456
|
+
type: TokenType.ATTRS_PAREN,
|
|
457
|
+
value: content.slice(pos + 1),
|
|
458
|
+
location: loc(pos, content.length)
|
|
459
|
+
});
|
|
460
|
+
pos = content.length;
|
|
461
|
+
}
|
|
462
|
+
continue;
|
|
288
463
|
}
|
|
289
|
-
|
|
290
|
-
|
|
464
|
+
// Anything else → stop
|
|
465
|
+
break;
|
|
466
|
+
}
|
|
467
|
+
return {tokens, pos};
|
|
468
|
+
};
|
|
469
|
+
|
|
291
470
|
// ─── Helpers ───────────────────────────────────────────────
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
471
|
+
isHamlConstruct = function(content) {
|
|
472
|
+
var fc;
|
|
473
|
+
fc = content[0];
|
|
474
|
+
return (fc === '%' || fc === '.' || fc === '#' || fc === '=' || fc === '-' || fc === '/' || fc === ':') || (fc === '!' && content[1] === '=') || content.startsWith('!!!');
|
|
475
|
+
};
|
|
476
|
+
|
|
477
|
+
countIndent = function(line) {
|
|
478
|
+
var ch, count, j, len;
|
|
479
|
+
count = 0;
|
|
480
|
+
for (j = 0, len = line.length; j < len; j++) {
|
|
481
|
+
ch = line[j];
|
|
482
|
+
if (ch === ' ') {
|
|
483
|
+
count++;
|
|
484
|
+
} else if (ch === '\t') {
|
|
485
|
+
count += 2;
|
|
486
|
+
} else {
|
|
487
|
+
break;
|
|
309
488
|
}
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
489
|
+
}
|
|
490
|
+
return count;
|
|
491
|
+
};
|
|
492
|
+
|
|
493
|
+
indentToken = function(_indent, offset, level, filename = null, line = 0) {
|
|
494
|
+
return {
|
|
495
|
+
type: TokenType.INDENT,
|
|
496
|
+
value: '',
|
|
497
|
+
indent: level,
|
|
498
|
+
location: {
|
|
499
|
+
start: {
|
|
500
|
+
line: line != null ? line : 0,
|
|
501
|
+
column: 0
|
|
502
|
+
},
|
|
503
|
+
end: {
|
|
504
|
+
line: line != null ? line : 0,
|
|
505
|
+
column: 0
|
|
506
|
+
},
|
|
507
|
+
offset,
|
|
508
|
+
length: 0,
|
|
509
|
+
file: filename
|
|
510
|
+
}
|
|
511
|
+
};
|
|
512
|
+
};
|
|
513
|
+
|
|
514
|
+
dedentToken = function(offset, filename = null, line = 0) {
|
|
515
|
+
return {
|
|
516
|
+
type: TokenType.DEDENT,
|
|
517
|
+
value: '',
|
|
518
|
+
indent: void 0,
|
|
519
|
+
location: {
|
|
520
|
+
start: {
|
|
521
|
+
line: line != null ? line : 0,
|
|
522
|
+
column: 0
|
|
523
|
+
},
|
|
524
|
+
end: {
|
|
525
|
+
line: line != null ? line : 0,
|
|
526
|
+
column: 0
|
|
527
|
+
},
|
|
528
|
+
offset,
|
|
529
|
+
length: 0,
|
|
530
|
+
file: filename
|
|
531
|
+
}
|
|
532
|
+
};
|
|
533
|
+
};
|
|
534
|
+
|
|
535
|
+
extractBracketed = function(content, start, open, close) {
|
|
536
|
+
var depth, pos, q, ref;
|
|
537
|
+
depth = 0;
|
|
538
|
+
pos = start;
|
|
539
|
+
while (pos < content.length) {
|
|
540
|
+
if (content[pos] === open) {
|
|
541
|
+
depth++;
|
|
542
|
+
} else if (content[pos] === close) {
|
|
543
|
+
depth--;
|
|
544
|
+
if (depth === 0) {
|
|
545
|
+
return content.slice(start + 1, pos);
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
// Skip string literals
|
|
549
|
+
if ((ref = content[pos]) === '"' || ref === "'") {
|
|
550
|
+
q = content[pos];
|
|
551
|
+
pos++;
|
|
552
|
+
while (pos < content.length && content[pos] !== q) {
|
|
553
|
+
if (content[pos] === '\\') {
|
|
554
|
+
pos++;
|
|
361
555
|
}
|
|
362
556
|
pos++;
|
|
557
|
+
}
|
|
363
558
|
}
|
|
364
|
-
|
|
365
|
-
}
|
|
366
|
-
|
|
559
|
+
pos++;
|
|
560
|
+
}
|
|
561
|
+
return null;
|
|
562
|
+
};
|