@xaendar/compiler 0.6.17 → 0.6.19
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/dist/xaendar-compiler.es.js +138 -41
- package/package.json +3 -3
|
@@ -38,6 +38,10 @@ var LexerState = /* @__PURE__ */ function(LexerState) {
|
|
|
38
38
|
*/
|
|
39
39
|
LexerState["EVENT"] = "event";
|
|
40
40
|
/**
|
|
41
|
+
* Consuming a DOM event parameter
|
|
42
|
+
*/
|
|
43
|
+
LexerState["EVENT_PARAMETER"] = "parameter";
|
|
44
|
+
/**
|
|
41
45
|
* Dispatching a flow-control keyword (@if, @for, @switch, etc.).
|
|
42
46
|
*/
|
|
43
47
|
LexerState["FLOW_CONTROL"] = "flow-control";
|
|
@@ -111,57 +115,61 @@ var TokenType = /* @__PURE__ */ function(TokenType) {
|
|
|
111
115
|
*/
|
|
112
116
|
TokenType[TokenType["EVENT"] = 7] = "EVENT";
|
|
113
117
|
/**
|
|
118
|
+
* An event paremeter included in a event call '()'
|
|
119
|
+
*/
|
|
120
|
+
TokenType[TokenType["EVENT_PAREMETER"] = 8] = "EVENT_PAREMETER";
|
|
121
|
+
/**
|
|
114
122
|
* A template-literal interpolation string enclosed in `` {`...`} ``.
|
|
115
123
|
*/
|
|
116
|
-
TokenType[TokenType["INTERPOLATION_LITERAL"] =
|
|
124
|
+
TokenType[TokenType["INTERPOLATION_LITERAL"] = 9] = "INTERPOLATION_LITERAL";
|
|
117
125
|
/**
|
|
118
126
|
* A JavaScript expression interpolation enclosed in `{ }`.
|
|
119
127
|
*/
|
|
120
|
-
TokenType[TokenType["INTERPOLATION_EXPRESSION"] =
|
|
128
|
+
TokenType[TokenType["INTERPOLATION_EXPRESSION"] = 10] = "INTERPOLATION_EXPRESSION";
|
|
121
129
|
/**
|
|
122
130
|
* Opening keyword of an `@if` directive.
|
|
123
131
|
*/
|
|
124
|
-
TokenType[TokenType["IF"] =
|
|
132
|
+
TokenType[TokenType["IF"] = 11] = "IF";
|
|
125
133
|
/**
|
|
126
134
|
* Opening keyword of a `@for` directive.
|
|
127
135
|
*/
|
|
128
|
-
TokenType[TokenType["FOR"] =
|
|
136
|
+
TokenType[TokenType["FOR"] = 12] = "FOR";
|
|
129
137
|
/**
|
|
130
138
|
* Opening keyword of an `@else` branch.
|
|
131
139
|
*/
|
|
132
|
-
TokenType[TokenType["ELSE"] =
|
|
140
|
+
TokenType[TokenType["ELSE"] = 13] = "ELSE";
|
|
133
141
|
/**
|
|
134
142
|
* Opening keyword of an `@else if` branch.
|
|
135
143
|
*/
|
|
136
|
-
TokenType[TokenType["ELSE_IF"] =
|
|
144
|
+
TokenType[TokenType["ELSE_IF"] = 14] = "ELSE_IF";
|
|
137
145
|
/**
|
|
138
146
|
* Opening keyword of a `@switch` directive.
|
|
139
147
|
*/
|
|
140
|
-
TokenType[TokenType["SWITCH"] =
|
|
148
|
+
TokenType[TokenType["SWITCH"] = 15] = "SWITCH";
|
|
141
149
|
/**
|
|
142
150
|
* Opening keyword of a `@case` branch.
|
|
143
151
|
*/
|
|
144
|
-
TokenType[TokenType["CASE"] =
|
|
152
|
+
TokenType[TokenType["CASE"] = 16] = "CASE";
|
|
145
153
|
/**
|
|
146
154
|
* Opening keyword of a `@default` branch.
|
|
147
155
|
*/
|
|
148
|
-
TokenType[TokenType["DEFAULT"] =
|
|
156
|
+
TokenType[TokenType["DEFAULT"] = 17] = "DEFAULT";
|
|
149
157
|
/**
|
|
150
158
|
* The condition expression `(...)` associated with a flow-control directive.
|
|
151
159
|
*/
|
|
152
|
-
TokenType[TokenType["CONDITION"] =
|
|
160
|
+
TokenType[TokenType["CONDITION"] = 18] = "CONDITION";
|
|
153
161
|
/**
|
|
154
162
|
* The opening `{` of a flow-control block body.
|
|
155
163
|
*/
|
|
156
|
-
TokenType[TokenType["BLOCK_OPEN"] =
|
|
164
|
+
TokenType[TokenType["BLOCK_OPEN"] = 19] = "BLOCK_OPEN";
|
|
157
165
|
/**
|
|
158
166
|
* The closing `}` of a flow-control block body.
|
|
159
167
|
*/
|
|
160
|
-
TokenType[TokenType["BLOCK_CLOSE"] =
|
|
168
|
+
TokenType[TokenType["BLOCK_CLOSE"] = 20] = "BLOCK_CLOSE";
|
|
161
169
|
/**
|
|
162
170
|
* Sentinel token emitted when the end of the input is reached.
|
|
163
171
|
*/
|
|
164
|
-
TokenType[TokenType["EOF"] =
|
|
172
|
+
TokenType[TokenType["EOF"] = 21] = "EOF";
|
|
165
173
|
return TokenType;
|
|
166
174
|
}({});
|
|
167
175
|
//#endregion
|
|
@@ -270,7 +278,7 @@ function consumeFlowControlCondition(cursor, _context) {
|
|
|
270
278
|
while (depth > 0) switch (cursor.peek()) {
|
|
271
279
|
case 40:
|
|
272
280
|
depth++;
|
|
273
|
-
expression = addCharacter$
|
|
281
|
+
expression = addCharacter$3(cursor, expression);
|
|
274
282
|
break;
|
|
275
283
|
case 41:
|
|
276
284
|
depth--;
|
|
@@ -278,9 +286,9 @@ function consumeFlowControlCondition(cursor, _context) {
|
|
|
278
286
|
cursor.advance();
|
|
279
287
|
break;
|
|
280
288
|
}
|
|
281
|
-
expression = addCharacter$
|
|
289
|
+
expression = addCharacter$3(cursor, expression);
|
|
282
290
|
break;
|
|
283
|
-
default: expression = addCharacter$
|
|
291
|
+
default: expression = addCharacter$3(cursor, expression);
|
|
284
292
|
}
|
|
285
293
|
return expression;
|
|
286
294
|
}
|
|
@@ -291,7 +299,7 @@ function consumeFlowControlCondition(cursor, _context) {
|
|
|
291
299
|
* @param expression - The current accumulated expression string.
|
|
292
300
|
* @returns The updated string with the newly consumed character appended.
|
|
293
301
|
*/
|
|
294
|
-
function addCharacter$
|
|
302
|
+
function addCharacter$3(cursor, expression) {
|
|
295
303
|
cursor.advance();
|
|
296
304
|
return `${expression}${cursor.currentChar.value}`;
|
|
297
305
|
}
|
|
@@ -340,6 +348,64 @@ function consumeDefaultFlowControlCondition(cursor, _context) {
|
|
|
340
348
|
};
|
|
341
349
|
}
|
|
342
350
|
//#endregion
|
|
351
|
+
//#region ../packages/compiler/src/lexer/states/event-parameter.state.ts
|
|
352
|
+
/**
|
|
353
|
+
* Consumes an event parameter and reads until a ',' or ')'.
|
|
354
|
+
* Emits an EVENT_ATTRIBUTE token containing the raw paremeter string.
|
|
355
|
+
*
|
|
356
|
+
* @param cursor - The lexer cursor positioned on the `@` character.
|
|
357
|
+
* @param _context - Unused lexer context.
|
|
358
|
+
* @returns Transition result with the EVENT_PAREMETER token and the EVENT state.
|
|
359
|
+
*/
|
|
360
|
+
function consumeEventParameter(cursor, _context) {
|
|
361
|
+
let read = true;
|
|
362
|
+
let eventParameter = "";
|
|
363
|
+
let charDelimiter = "";
|
|
364
|
+
const retVal = {
|
|
365
|
+
state: LexerState.TAG_BODY,
|
|
366
|
+
tokens: []
|
|
367
|
+
};
|
|
368
|
+
cursor.advance();
|
|
369
|
+
cursor.skipSpaces();
|
|
370
|
+
while (read) switch (cursor.peek()) {
|
|
371
|
+
case 91:
|
|
372
|
+
case 123:
|
|
373
|
+
case 34:
|
|
374
|
+
case 39:
|
|
375
|
+
case 40:
|
|
376
|
+
eventParameter = addCharacter$2(cursor, eventParameter);
|
|
377
|
+
if (!charDelimiter) charDelimiter = cursor.currentChar.value;
|
|
378
|
+
else if (charDelimiter === cursor.currentChar.value) charDelimiter = "";
|
|
379
|
+
break;
|
|
380
|
+
case 44:
|
|
381
|
+
if (!charDelimiter) {
|
|
382
|
+
retVal.tokens.push({
|
|
383
|
+
type: TokenType.EVENT_PAREMETER,
|
|
384
|
+
parts: [eventParameter]
|
|
385
|
+
});
|
|
386
|
+
cursor.advance();
|
|
387
|
+
cursor.skipSpaces();
|
|
388
|
+
eventParameter = "";
|
|
389
|
+
} else eventParameter = addCharacter$2(cursor, eventParameter);
|
|
390
|
+
break;
|
|
391
|
+
case 41:
|
|
392
|
+
cursor.advance();
|
|
393
|
+
if (!charDelimiter) {
|
|
394
|
+
retVal.tokens.push({
|
|
395
|
+
type: TokenType.EVENT_PAREMETER,
|
|
396
|
+
parts: [eventParameter]
|
|
397
|
+
});
|
|
398
|
+
read = false;
|
|
399
|
+
}
|
|
400
|
+
default: eventParameter = addCharacter$2(cursor, eventParameter);
|
|
401
|
+
}
|
|
402
|
+
return retVal;
|
|
403
|
+
}
|
|
404
|
+
function addCharacter$2(cursor, eventParameter) {
|
|
405
|
+
cursor.advance();
|
|
406
|
+
return `${eventParameter}${cursor.currentChar.value}`;
|
|
407
|
+
}
|
|
408
|
+
//#endregion
|
|
343
409
|
//#region ../packages/compiler/src/lexer/states/event.state.ts
|
|
344
410
|
/**
|
|
345
411
|
* Consumes a DOM event binding starting with `@` and reads until a delimiter
|
|
@@ -353,34 +419,31 @@ function consumeEvent(cursor, _context) {
|
|
|
353
419
|
let read = true;
|
|
354
420
|
let event = "";
|
|
355
421
|
let retVal;
|
|
356
|
-
let deep = 0;
|
|
357
422
|
cursor.advance();
|
|
358
423
|
while (read) switch (cursor.peek()) {
|
|
359
424
|
case 32:
|
|
360
425
|
case 47:
|
|
361
426
|
case 62:
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
state: LexerState.TAG_BODY,
|
|
365
|
-
tokens: [{
|
|
366
|
-
type: TokenType.EVENT,
|
|
367
|
-
parts: [event]
|
|
368
|
-
}]
|
|
369
|
-
};
|
|
370
|
-
read = false;
|
|
371
|
-
}
|
|
427
|
+
retVal = { state: LexerState.TAG_BODY };
|
|
428
|
+
read = false;
|
|
372
429
|
break;
|
|
373
430
|
case 40:
|
|
374
|
-
|
|
375
|
-
|
|
431
|
+
case 44:
|
|
432
|
+
retVal = {
|
|
433
|
+
state: LexerState.EVENT_PARAMETER,
|
|
434
|
+
tokens: [{
|
|
435
|
+
type: TokenType.EVENT,
|
|
436
|
+
parts: [event]
|
|
437
|
+
}]
|
|
438
|
+
};
|
|
439
|
+
read = false;
|
|
376
440
|
break;
|
|
377
441
|
case 41:
|
|
378
|
-
|
|
379
|
-
cursor.advance();
|
|
442
|
+
retVal = { state: LexerState.TAG_BODY };
|
|
380
443
|
break;
|
|
381
444
|
default:
|
|
382
445
|
cursor.advance();
|
|
383
|
-
|
|
446
|
+
event = `${event}${cursor.currentChar.value}`;
|
|
384
447
|
}
|
|
385
448
|
return retVal;
|
|
386
449
|
}
|
|
@@ -1025,11 +1088,12 @@ var Lexer = class {
|
|
|
1025
1088
|
[LexerState.TAG_CLOSE]: consumeTagClose,
|
|
1026
1089
|
[LexerState.ATTRIBUTE]: consumeAttribute,
|
|
1027
1090
|
[LexerState.ATTRIBUTE_VALUE]: consumeAttributeValue,
|
|
1091
|
+
[LexerState.EVENT]: consumeEvent,
|
|
1092
|
+
[LexerState.EVENT_PARAMETER]: consumeEventParameter,
|
|
1028
1093
|
[LexerState.FLOW_CONTROL]: consumeFlowControl,
|
|
1029
1094
|
[LexerState.FLOW_CONTROL_CONDITION]: consumeDefaultFlowControlCondition,
|
|
1030
1095
|
[LexerState.CASE_FLOW_CONTROL_CONDITION]: consumeCaseFlowControlCondition,
|
|
1031
1096
|
[LexerState.FLOW_CONTROL_BLOCK]: consumeFlowControlBlock,
|
|
1032
|
-
[LexerState.EVENT]: consumeEvent,
|
|
1033
1097
|
[LexerState.INTERPOLATION]: consumeInterpolation,
|
|
1034
1098
|
[LexerState.INTERPOLATION_EXPRESSION]: consumeInterpolationExpression,
|
|
1035
1099
|
[LexerState.INTERPOLATION_LITERAL]: consumeInterpolationliteral
|
|
@@ -1100,7 +1164,7 @@ var ParserCursor = class {
|
|
|
1100
1164
|
/**
|
|
1101
1165
|
* Returns a read-only snapshot of the current token.
|
|
1102
1166
|
*/
|
|
1103
|
-
|
|
1167
|
+
getCcurrentToken() {
|
|
1104
1168
|
return this._currentToken;
|
|
1105
1169
|
}
|
|
1106
1170
|
/**
|
|
@@ -1435,9 +1499,15 @@ function parseEvent(cursor, _parseNode, token) {
|
|
|
1435
1499
|
const raw = token.parts[0];
|
|
1436
1500
|
const [name, value] = raw.split("=");
|
|
1437
1501
|
if (!name || !value) throw new Error(`[Parser] Invalid event format: ${raw}`);
|
|
1502
|
+
const parameters = new Array();
|
|
1503
|
+
while (cursor.peek().type === TokenType.EVENT_PAREMETER) {
|
|
1504
|
+
cursor.advance();
|
|
1505
|
+
parameters.push(validateExpression(cursor.getCcurrentToken().value.parts[0]).node);
|
|
1506
|
+
}
|
|
1438
1507
|
return {
|
|
1439
1508
|
name,
|
|
1440
|
-
handler: value.replace(/^[""]|[""]$/g, "")
|
|
1509
|
+
handler: value.replace(/^[""]|[""]$/g, ""),
|
|
1510
|
+
parameters
|
|
1441
1511
|
};
|
|
1442
1512
|
}
|
|
1443
1513
|
//#endregion
|
|
@@ -1600,7 +1670,7 @@ function parseForExpression(source, baseOffset) {
|
|
|
1600
1670
|
function parseImplicitAliases(source, baseOffset, out) {
|
|
1601
1671
|
const entries = source.split(",");
|
|
1602
1672
|
let cursor = 0;
|
|
1603
|
-
const IMPLICIT_VARIABLES = new Set([
|
|
1673
|
+
const IMPLICIT_VARIABLES = /* @__PURE__ */ new Set([
|
|
1604
1674
|
"$index",
|
|
1605
1675
|
"$last",
|
|
1606
1676
|
"$first",
|
|
@@ -1898,6 +1968,9 @@ var CompilerContext = class {
|
|
|
1898
1968
|
if (this.hasIdentifier(name)) throw new Error(`Identifier "${name}" is already declared in this scope.`);
|
|
1899
1969
|
this._identifiers.push(name);
|
|
1900
1970
|
}
|
|
1971
|
+
removeIdentifier(name) {
|
|
1972
|
+
this._identifiers = this._identifiers.filter((identifier) => identifier !== name);
|
|
1973
|
+
}
|
|
1901
1974
|
/**
|
|
1902
1975
|
* Returns `true` if an identifier with the given name is declared in this
|
|
1903
1976
|
* scope or any of its ancestor scopes.
|
|
@@ -1926,7 +1999,7 @@ var CompilerContext = class {
|
|
|
1926
1999
|
*
|
|
1927
2000
|
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects
|
|
1928
2001
|
*/
|
|
1929
|
-
var GLOBAL_IDENTIFIERS = new Set([
|
|
2002
|
+
var GLOBAL_IDENTIFIERS = /* @__PURE__ */ new Set([
|
|
1930
2003
|
"undefined",
|
|
1931
2004
|
"NaN",
|
|
1932
2005
|
"Infinity",
|
|
@@ -2220,7 +2293,7 @@ function getBlockIdentifier(parentNode, index, prefix) {
|
|
|
2220
2293
|
*/
|
|
2221
2294
|
function processElement(node, nodeName, parentNode, compilerContext) {
|
|
2222
2295
|
const attributes = mapAttributes(node.attributes, compilerContext);
|
|
2223
|
-
const events = mapEvents(node.events);
|
|
2296
|
+
const events = mapEvents(node.events, compilerContext);
|
|
2224
2297
|
const code = [`const ${nodeName} = _renderElement(${parentNode}, context, '${node.tagName}',`];
|
|
2225
2298
|
attributes.length ? code.push(...indent([
|
|
2226
2299
|
"[",
|
|
@@ -2255,10 +2328,34 @@ function mapAttributes(attributes, compilerContext) {
|
|
|
2255
2328
|
* to the component instance handler and exposing the native event as `$event`.
|
|
2256
2329
|
*
|
|
2257
2330
|
* @param events - The event nodes to bind to the element.
|
|
2331
|
+
* @param compilerContext - Current render scope context, used to resolve identifier references.
|
|
2258
2332
|
* @returns Array of generated code lines, one per event listener.
|
|
2259
2333
|
*/
|
|
2260
|
-
function mapEvents(events) {
|
|
2261
|
-
|
|
2334
|
+
function mapEvents(events, compilerContext) {
|
|
2335
|
+
compilerContext.addIdentifier("$event");
|
|
2336
|
+
const mappedEvents = events?.map((event) => {
|
|
2337
|
+
let parsedEventParameter = false;
|
|
2338
|
+
const parameters = event.parameters.map((parameter) => {
|
|
2339
|
+
const resolvedParameter = resolveExpression(parameter, compilerContext);
|
|
2340
|
+
if (!parsedEventParameter && resolvedParameter === "$event") {
|
|
2341
|
+
parsedEventParameter = true;
|
|
2342
|
+
return `($event) => ${resolvedParameter},`;
|
|
2343
|
+
} else return `() => ${resolvedParameter},`;
|
|
2344
|
+
});
|
|
2345
|
+
return [
|
|
2346
|
+
"{",
|
|
2347
|
+
...indent([
|
|
2348
|
+
`name: '${event.name}',`,
|
|
2349
|
+
`handler: '${event.handler}',`,
|
|
2350
|
+
"parameters: [",
|
|
2351
|
+
...indent(parameters),
|
|
2352
|
+
"]"
|
|
2353
|
+
]),
|
|
2354
|
+
"}"
|
|
2355
|
+
];
|
|
2356
|
+
}).flat();
|
|
2357
|
+
compilerContext.removeIdentifier("$event");
|
|
2358
|
+
return mappedEvents;
|
|
2262
2359
|
}
|
|
2263
2360
|
//#endregion
|
|
2264
2361
|
//#region ../packages/compiler/src/render-generator/states/process-for.state.ts
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xaendar/compiler",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.19",
|
|
4
4
|
"description": "A library for transpiling Xaendar Templates into JavaScript code",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"type": "module",
|
|
@@ -16,8 +16,8 @@
|
|
|
16
16
|
}
|
|
17
17
|
},
|
|
18
18
|
"dependencies": {
|
|
19
|
-
"@xaendar/common": "0.6.
|
|
20
|
-
"@xaendar/types": "0.6.
|
|
19
|
+
"@xaendar/common": "0.6.19",
|
|
20
|
+
"@xaendar/types": "0.6.19",
|
|
21
21
|
"typescript": "^6.0.3"
|
|
22
22
|
}
|
|
23
23
|
}
|