@tylix/compiler 0.4.2 → 0.5.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/package.json +2 -2
- package/src/ast/nodes.js +31 -2
- package/src/codegen/emitStatementLines.js +108 -22
- package/src/codegen/generateExpression.js +42 -17
- package/src/codegen/generateStatement.js +54 -57
- package/src/codegen/generateTemplateExpression.js +110 -16
- package/src/lexer/Lexer.js +69 -5
- package/src/lexer/tokenTypes.js +19 -2
- package/src/parser/Parser.js +265 -34
- package/src/renderPageDocument.js +25 -2
- package/src/runtime/notFound.js +6 -0
- package/src/runtime/useApi.js +365 -5
- package/src/typecheck/tylix-globals.d.ts +50 -12
- package/src/typecheck/typedTemplateExpr.js +70 -6
package/src/parser/Parser.js
CHANGED
|
@@ -34,6 +34,10 @@ import {
|
|
|
34
34
|
ContinueStatement,
|
|
35
35
|
CapabilityRefNode,
|
|
36
36
|
MetaEntryNode,
|
|
37
|
+
BlockStatement,
|
|
38
|
+
ThrowStatement,
|
|
39
|
+
WhileStatement,
|
|
40
|
+
SwitchStatement,
|
|
37
41
|
} from '../ast/nodes.js'
|
|
38
42
|
import { parseExpressionString } from './parseExpressionString.js'
|
|
39
43
|
|
|
@@ -42,6 +46,7 @@ const COMPARISON_SYMBOLS = {
|
|
|
42
46
|
[TokenType.LT]: '<',
|
|
43
47
|
[TokenType.GTE]: '>=',
|
|
44
48
|
[TokenType.LTE]: '<=',
|
|
49
|
+
[TokenType.INSTANCEOF]: 'instanceof',
|
|
45
50
|
}
|
|
46
51
|
|
|
47
52
|
const ADDITIVE_SYMBOLS = {
|
|
@@ -68,28 +73,38 @@ const COMPOUND_ASSIGNMENT_OPS = {
|
|
|
68
73
|
[TokenType.MINUS_EQUALS]: '-',
|
|
69
74
|
[TokenType.STAR_EQUALS]: '*',
|
|
70
75
|
[TokenType.SLASH_EQUALS]: '/',
|
|
76
|
+
[TokenType.PERCENT_EQUALS]: '%',
|
|
71
77
|
}
|
|
72
78
|
|
|
73
79
|
/**
|
|
74
80
|
* Parses the token stream from the Lexer into a Page AST.
|
|
75
81
|
*
|
|
76
82
|
* Expressions are parsed with real precedence-climbing, low to high:
|
|
77
|
-
* arrow function (x => expr, (a, b) => expr
|
|
78
|
-
*
|
|
79
|
-
*
|
|
80
|
-
*
|
|
83
|
+
* arrow function (x => expr, (a, b) => expr, block bodies
|
|
84
|
+
* x => { ... }, and the async forms of all of these) -- tried
|
|
85
|
+
* first, with backtracking, since it can start with the same
|
|
86
|
+
* tokens as a grouped expression or a bare identifier
|
|
87
|
+
* assignment (= += -= *= /= %= ??=)
|
|
81
88
|
* ternary (?:)
|
|
89
|
+
* nullish (??)
|
|
82
90
|
* logical or (or)
|
|
83
91
|
* logical and (and)
|
|
84
92
|
* equality (is / is not)
|
|
85
|
-
* comparison (> < >= <=)
|
|
93
|
+
* comparison (> < >= <= instanceof)
|
|
86
94
|
* predicate (has / exists / missing)
|
|
87
95
|
* additive (+ -)
|
|
88
96
|
* multiplicative (* / %)
|
|
89
|
-
*
|
|
90
|
-
*
|
|
97
|
+
* exponent (**, right-associative)
|
|
98
|
+
* unary (not, -, await, typeof, prefix ++/--)
|
|
99
|
+
* postfix (x++, x--)
|
|
100
|
+
* member/call (a.b, a(), a?.b, a?.())
|
|
91
101
|
* primary (literals, identifiers, grouping, arrays, objects,
|
|
92
102
|
* template literals)
|
|
103
|
+
*
|
|
104
|
+
* `x++` / `++x` desugar to `x = x + 1` (same trick as compound
|
|
105
|
+
* assignment), so the expression's value is the NEW value in both
|
|
106
|
+
* forms, unlike JS where postfix yields the old one. Fine for
|
|
107
|
+
* statements and event handlers, which is where it is used.
|
|
93
108
|
*/
|
|
94
109
|
export class Parser {
|
|
95
110
|
constructor(tokens) {
|
|
@@ -205,7 +220,7 @@ export class Parser {
|
|
|
205
220
|
)
|
|
206
221
|
}
|
|
207
222
|
|
|
208
|
-
// These
|
|
223
|
+
// These double as ordinary field names in practice (title
|
|
209
224
|
// especially), so unlike the keywords above they can't be trusted as
|
|
210
225
|
// section boundaries on token type alone -- isAtSectionBoundary below
|
|
211
226
|
// checks the following token to disambiguate.
|
|
@@ -262,7 +277,7 @@ export class Parser {
|
|
|
262
277
|
throw new Error(`${message} at line ${token.line}, got ${token.type}`)
|
|
263
278
|
}
|
|
264
279
|
|
|
265
|
-
|
|
280
|
+
parsePropEntry() {
|
|
266
281
|
const startLine = this.peek().line
|
|
267
282
|
const name = this.parseNameToken('Expected prop name')
|
|
268
283
|
const optional = this.match(TokenType.QUESTION)
|
|
@@ -361,7 +376,7 @@ export class Parser {
|
|
|
361
376
|
return { ...StateNode(name, value, typeAnnotation), line: startLine }
|
|
362
377
|
}
|
|
363
378
|
|
|
364
|
-
|
|
379
|
+
parseMethod() {
|
|
365
380
|
const startLine = this.peek().line
|
|
366
381
|
const isAsync = this.match(TokenType.ASYNC)
|
|
367
382
|
const name = this.parseNameToken('Expected method name')
|
|
@@ -397,19 +412,20 @@ export class Parser {
|
|
|
397
412
|
line: startLine,
|
|
398
413
|
}
|
|
399
414
|
}
|
|
415
|
+
|
|
400
416
|
// A binding target is anywhere a name is bound to a value: a plain
|
|
401
417
|
// identifier, or -- for destructuring -- a nested object/array
|
|
402
418
|
// pattern. Shared by top-level const/let targets, object pattern
|
|
403
419
|
// property values, and array pattern elements, so `const { a: { b } }
|
|
404
420
|
// = x` and `const [[a, b]] = x` both fall out for free instead of
|
|
405
421
|
// needing separate one-level-deep parsing logic in three places.
|
|
406
|
-
|
|
422
|
+
parseBindingTarget() {
|
|
407
423
|
if (this.check(TokenType.LBRACE)) return this.parseObjectPattern()
|
|
408
424
|
if (this.check(TokenType.LBRACKET)) return this.parseArrayPattern()
|
|
409
425
|
return this.parseNameToken('Expected a binding name')
|
|
410
426
|
}
|
|
411
427
|
|
|
412
|
-
|
|
428
|
+
parseObjectPattern() {
|
|
413
429
|
this.expect(TokenType.LBRACE, "Expected '{' to start destructuring pattern")
|
|
414
430
|
const properties = []
|
|
415
431
|
let rest = null
|
|
@@ -437,6 +453,7 @@ export class Parser {
|
|
|
437
453
|
this.expect(TokenType.RBRACE, "Expected '}' to close destructuring pattern")
|
|
438
454
|
return ObjectPattern(properties, rest)
|
|
439
455
|
}
|
|
456
|
+
|
|
440
457
|
parseArrayPattern() {
|
|
441
458
|
this.expect(
|
|
442
459
|
TokenType.LBRACKET,
|
|
@@ -534,6 +551,19 @@ export class Parser {
|
|
|
534
551
|
return capability
|
|
535
552
|
}
|
|
536
553
|
|
|
554
|
+
// `{ stmt stmt ... }` -- shared by the newer statement forms
|
|
555
|
+
// (while, switch clauses, block-bodied arrows). `what` only feeds
|
|
556
|
+
// the error messages.
|
|
557
|
+
parseBlockStatements(what) {
|
|
558
|
+
this.expect(TokenType.LBRACE, `Expected '{' to start ${what}`)
|
|
559
|
+
const body = []
|
|
560
|
+
while (!this.check(TokenType.RBRACE)) {
|
|
561
|
+
body.push(this.parseStatement())
|
|
562
|
+
}
|
|
563
|
+
this.expect(TokenType.RBRACE, `Expected '}' to close ${what}`)
|
|
564
|
+
return body
|
|
565
|
+
}
|
|
566
|
+
|
|
537
567
|
parseStatementInner() {
|
|
538
568
|
if (this.match(TokenType.RETURN)) {
|
|
539
569
|
const hasValue =
|
|
@@ -543,10 +573,24 @@ export class Parser {
|
|
|
543
573
|
return ReturnStatement(argument)
|
|
544
574
|
}
|
|
545
575
|
|
|
576
|
+
if (this.match(TokenType.THROW)) {
|
|
577
|
+
const argument = this.parseExpression()
|
|
578
|
+
this.match(TokenType.SEMICOLON)
|
|
579
|
+
return ThrowStatement(argument)
|
|
580
|
+
}
|
|
581
|
+
|
|
546
582
|
if (this.match(TokenType.IF)) {
|
|
547
583
|
return this.parseIfStatement()
|
|
548
584
|
}
|
|
549
585
|
|
|
586
|
+
if (this.match(TokenType.WHILE)) {
|
|
587
|
+
return this.parseWhileStatement()
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
if (this.match(TokenType.SWITCH)) {
|
|
591
|
+
return this.parseSwitchStatement()
|
|
592
|
+
}
|
|
593
|
+
|
|
550
594
|
if (this.match(TokenType.FOR)) {
|
|
551
595
|
return this.parseForStatement()
|
|
552
596
|
}
|
|
@@ -582,6 +626,7 @@ export class Parser {
|
|
|
582
626
|
this.match(TokenType.SEMICOLON)
|
|
583
627
|
return ExpressionStatement(expression)
|
|
584
628
|
}
|
|
629
|
+
|
|
585
630
|
parseIfStatement() {
|
|
586
631
|
const hasParen = this.match(TokenType.LPAREN)
|
|
587
632
|
const condition = this.parseExpression()
|
|
@@ -613,6 +658,57 @@ export class Parser {
|
|
|
613
658
|
return IfStatement(condition, consequent, alternate)
|
|
614
659
|
}
|
|
615
660
|
|
|
661
|
+
// `while cond { ... }` or `while (cond) { ... }`. Parentheses need no
|
|
662
|
+
// special handling: `(cond)` already parses as a grouped expression,
|
|
663
|
+
// which also keeps `while (a) and (b) { ... }` working.
|
|
664
|
+
parseWhileStatement() {
|
|
665
|
+
const condition = this.parseExpression()
|
|
666
|
+
const body = this.parseBlockStatements('while body')
|
|
667
|
+
return WhileStatement(condition, body)
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
// Tylix-style switch: braces for every clause, no fallthrough, and
|
|
671
|
+
// `case a, b { ... }` instead of stacked labels. Codegen emits the
|
|
672
|
+
// `break` itself.
|
|
673
|
+
//
|
|
674
|
+
// switch status {
|
|
675
|
+
// case "paid", "settled" { ... }
|
|
676
|
+
// default { ... }
|
|
677
|
+
// }
|
|
678
|
+
parseSwitchStatement() {
|
|
679
|
+
const discriminant = this.parseExpression()
|
|
680
|
+
this.expect(TokenType.LBRACE, "Expected '{' to start switch body")
|
|
681
|
+
|
|
682
|
+
const cases = []
|
|
683
|
+
let hasDefault = false
|
|
684
|
+
while (!this.check(TokenType.RBRACE)) {
|
|
685
|
+
if (this.match(TokenType.CASE)) {
|
|
686
|
+
const tests = [this.parseExpression()]
|
|
687
|
+
while (this.match(TokenType.COMMA)) {
|
|
688
|
+
tests.push(this.parseExpression())
|
|
689
|
+
}
|
|
690
|
+
cases.push({ tests, body: this.parseBlockStatements('case body') })
|
|
691
|
+
} else if (this.check(TokenType.DEFAULT)) {
|
|
692
|
+
const line = this.advance().line
|
|
693
|
+
if (hasDefault) {
|
|
694
|
+
throw new Error(`Duplicate 'default' in switch at line ${line}`)
|
|
695
|
+
}
|
|
696
|
+
hasDefault = true
|
|
697
|
+
cases.push({
|
|
698
|
+
tests: null,
|
|
699
|
+
body: this.parseBlockStatements('default body'),
|
|
700
|
+
})
|
|
701
|
+
} else {
|
|
702
|
+
throw new Error(
|
|
703
|
+
`Expected 'case' or 'default' in switch at line ${this.peek().line}, got ${this.peek().type}`,
|
|
704
|
+
)
|
|
705
|
+
}
|
|
706
|
+
}
|
|
707
|
+
this.expect(TokenType.RBRACE, "Expected '}' to close switch body")
|
|
708
|
+
|
|
709
|
+
return SwitchStatement(discriminant, cases)
|
|
710
|
+
}
|
|
711
|
+
|
|
616
712
|
parseForStatement() {
|
|
617
713
|
const varName = this.expect(
|
|
618
714
|
TokenType.IDENTIFIER,
|
|
@@ -729,16 +825,92 @@ export class Parser {
|
|
|
729
825
|
return left
|
|
730
826
|
}
|
|
731
827
|
|
|
828
|
+
// Parses a type expression speculatively: on failure the position is
|
|
829
|
+
// restored and null is returned instead of throwing. Needed because
|
|
830
|
+
// `cond ? label : 'none'` looks exactly like the start of a typed
|
|
831
|
+
// arrow parameter (`label: Type => ...`) until the type fails to parse.
|
|
832
|
+
tryParseTypeExpression() {
|
|
833
|
+
const savedPos = this.pos
|
|
834
|
+
try {
|
|
835
|
+
return this.parseTypeExpression()
|
|
836
|
+
} catch {
|
|
837
|
+
this.pos = savedPos
|
|
838
|
+
return null
|
|
839
|
+
}
|
|
840
|
+
}
|
|
841
|
+
|
|
842
|
+
// After `=>`: either a block body or a single expression.
|
|
843
|
+
//
|
|
844
|
+
// A `{` after `=>` is a BLOCK (JS semantics), except when the
|
|
845
|
+
// tokens right after it make that impossible -- `{ ...x }`,
|
|
846
|
+
// `{ key: value }`, `{ a, b }` -- in which case it's an object
|
|
847
|
+
// literal, so the older `x => { id: x }` style keeps working. A
|
|
848
|
+
// block can't legally start with `name :` (no labels) or `name ,`
|
|
849
|
+
// (no comma operator), so the lookahead is unambiguous. The two
|
|
850
|
+
// cases it can't resolve are `{}` and `{ name }`; those are blocks,
|
|
851
|
+
// exactly as in JS. Write `({})` / `({ name })` for an object.
|
|
852
|
+
parseArrowBody() {
|
|
853
|
+
if (this.check(TokenType.LBRACE) && !this.looksLikeObjectLiteral()) {
|
|
854
|
+
return BlockStatement(this.parseBlockStatements('arrow function body'))
|
|
855
|
+
}
|
|
856
|
+
return this.parseAssignment()
|
|
857
|
+
}
|
|
858
|
+
|
|
859
|
+
looksLikeObjectLiteral() {
|
|
860
|
+
const first = this.tokens[this.pos + 1]
|
|
861
|
+
const second = this.tokens[this.pos + 2]
|
|
862
|
+
if (!first) return false
|
|
863
|
+
if (first.type === TokenType.SPREAD) return true
|
|
864
|
+
const isKey =
|
|
865
|
+
first.type === TokenType.STRING ||
|
|
866
|
+
first.type === TokenType.IDENTIFIER ||
|
|
867
|
+
KEYWORD_TOKEN_TYPES.has(first.type)
|
|
868
|
+
return (
|
|
869
|
+
isKey &&
|
|
870
|
+
!!second &&
|
|
871
|
+
(second.type === TokenType.COLON || second.type === TokenType.COMMA)
|
|
872
|
+
)
|
|
873
|
+
}
|
|
874
|
+
|
|
875
|
+
// Entry point for arrow functions, including the async forms:
|
|
876
|
+
// async x => ... async (a, b) => ...
|
|
877
|
+
// async x: Type => ... async (a: Type) => ...
|
|
878
|
+
//
|
|
879
|
+
// `async` is a keyword token but also a legal identifier name, so
|
|
880
|
+
// it is only treated as the async modifier when what follows could
|
|
881
|
+
// start an arrow's parameter list (an identifier/keyword name or
|
|
882
|
+
// '('). `async => 1` (a param literally named async) still goes
|
|
883
|
+
// down the normal path, and so does `async(x)` as a plain call,
|
|
884
|
+
// because the speculative parse below backs out and restores the
|
|
885
|
+
// position when no `=>` follows.
|
|
886
|
+
tryParseArrowFunction() {
|
|
887
|
+
if (this.check(TokenType.ASYNC)) {
|
|
888
|
+
const next = this.tokens[this.pos + 1]
|
|
889
|
+
const startsParams =
|
|
890
|
+
next &&
|
|
891
|
+
(next.type === TokenType.LPAREN ||
|
|
892
|
+
next.type === TokenType.IDENTIFIER ||
|
|
893
|
+
(next.type !== TokenType.ARROW && KEYWORD_TOKEN_TYPES.has(next.type)))
|
|
894
|
+
if (startsParams) {
|
|
895
|
+
const savedPos = this.pos
|
|
896
|
+
this.advance() // consume 'async'
|
|
897
|
+
const arrow = this.tryParseSyncArrow()
|
|
898
|
+
if (arrow) return { ...arrow, isAsync: true }
|
|
899
|
+
this.pos = savedPos
|
|
900
|
+
return null
|
|
901
|
+
}
|
|
902
|
+
}
|
|
903
|
+
return this.tryParseSyncArrow()
|
|
904
|
+
}
|
|
905
|
+
|
|
732
906
|
// Arrow functions are tried with backtracking before falling
|
|
733
907
|
// through to the normal ternary/assignment chain, since both forms
|
|
734
908
|
// (`x => ...` and `(a, b) => ...`) start with tokens that are
|
|
735
909
|
// otherwise perfectly valid as a bare identifier or a grouped
|
|
736
|
-
// expression.
|
|
737
|
-
//
|
|
738
|
-
//
|
|
739
|
-
|
|
740
|
-
// position.
|
|
741
|
-
tryParseArrowFunction() {
|
|
910
|
+
// expression. The body is an expression or, via parseArrowBody, a
|
|
911
|
+
// `{ ... }` statement block (script code only -- templates reject
|
|
912
|
+
// BlockStatement in their expression generators).
|
|
913
|
+
tryParseSyncArrow() {
|
|
742
914
|
// single-param form: x => expr or x: Type => expr
|
|
743
915
|
if (
|
|
744
916
|
this.check(TokenType.IDENTIFIER) ||
|
|
@@ -748,20 +920,21 @@ export class Parser {
|
|
|
748
920
|
if (next && next.type === TokenType.ARROW) {
|
|
749
921
|
const param = this.advance().value
|
|
750
922
|
this.advance() // consume '=>'
|
|
751
|
-
const body = this.
|
|
923
|
+
const body = this.parseArrowBody()
|
|
752
924
|
return ArrowFunctionExpr([param], body, [null])
|
|
753
925
|
}
|
|
754
926
|
if (next && next.type === TokenType.COLON) {
|
|
755
927
|
const savedPos = this.pos
|
|
756
928
|
const param = this.advance().value
|
|
757
929
|
this.advance() // consume ':'
|
|
758
|
-
const paramType = this.
|
|
759
|
-
if (this.check(TokenType.ARROW)) {
|
|
930
|
+
const paramType = this.tryParseTypeExpression()
|
|
931
|
+
if (paramType !== null && this.check(TokenType.ARROW)) {
|
|
760
932
|
this.advance()
|
|
761
|
-
const body = this.
|
|
933
|
+
const body = this.parseArrowBody()
|
|
762
934
|
return ArrowFunctionExpr([param], body, [paramType])
|
|
763
935
|
}
|
|
764
|
-
// wasn't actually a typed arrow param
|
|
936
|
+
// wasn't actually a typed arrow param (e.g. the `x : y` part
|
|
937
|
+
// of a ternary) -- back out
|
|
765
938
|
this.pos = savedPos
|
|
766
939
|
}
|
|
767
940
|
return null
|
|
@@ -786,9 +959,17 @@ export class Parser {
|
|
|
786
959
|
break
|
|
787
960
|
}
|
|
788
961
|
params.push(this.advance().value)
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
)
|
|
962
|
+
|
|
963
|
+
let paramType = null
|
|
964
|
+
if (this.match(TokenType.COLON)) {
|
|
965
|
+
paramType = this.tryParseTypeExpression()
|
|
966
|
+
if (paramType === null) {
|
|
967
|
+
isValidParamList = false
|
|
968
|
+
break
|
|
969
|
+
}
|
|
970
|
+
}
|
|
971
|
+
paramTypes.push(paramType)
|
|
972
|
+
|
|
792
973
|
if (this.match(TokenType.COMMA)) continue
|
|
793
974
|
break
|
|
794
975
|
}
|
|
@@ -798,7 +979,7 @@ export class Parser {
|
|
|
798
979
|
this.pos++
|
|
799
980
|
if (this.check(TokenType.ARROW)) {
|
|
800
981
|
this.pos++
|
|
801
|
-
const body = this.
|
|
982
|
+
const body = this.parseArrowBody()
|
|
802
983
|
return ArrowFunctionExpr(params, body, paramTypes)
|
|
803
984
|
}
|
|
804
985
|
}
|
|
@@ -896,15 +1077,27 @@ export class Parser {
|
|
|
896
1077
|
}
|
|
897
1078
|
|
|
898
1079
|
parseMultiplicative() {
|
|
899
|
-
let left = this.
|
|
1080
|
+
let left = this.parseExponent()
|
|
900
1081
|
while (MULTIPLICATIVE_SYMBOLS[this.peek().type]) {
|
|
901
1082
|
const operator = MULTIPLICATIVE_SYMBOLS[this.advance().type]
|
|
902
|
-
const right = this.
|
|
1083
|
+
const right = this.parseExponent()
|
|
903
1084
|
left = BinaryExpr(operator, left, right)
|
|
904
1085
|
}
|
|
905
1086
|
return left
|
|
906
1087
|
}
|
|
907
1088
|
|
|
1089
|
+
// `**` binds tighter than * / % and is right-associative:
|
|
1090
|
+
// 2 ** 3 ** 2 is 2 ** (3 ** 2). The right side recurses into
|
|
1091
|
+
// parseExponent itself to get that associativity.
|
|
1092
|
+
parseExponent() {
|
|
1093
|
+
const base = this.parseUnary()
|
|
1094
|
+
if (this.match(TokenType.STAR_STAR)) {
|
|
1095
|
+
const exponent = this.parseExponent()
|
|
1096
|
+
return BinaryExpr('**', base, exponent)
|
|
1097
|
+
}
|
|
1098
|
+
return base
|
|
1099
|
+
}
|
|
1100
|
+
|
|
908
1101
|
parseUnary() {
|
|
909
1102
|
if (this.match(TokenType.AWAIT)) {
|
|
910
1103
|
return AwaitExpr(this.parseUnary())
|
|
@@ -915,7 +1108,26 @@ export class Parser {
|
|
|
915
1108
|
if (this.match(TokenType.MINUS)) {
|
|
916
1109
|
return UnaryExpr('-', this.parseUnary())
|
|
917
1110
|
}
|
|
918
|
-
|
|
1111
|
+
if (this.match(TokenType.TYPEOF)) {
|
|
1112
|
+
return UnaryExpr('typeof', this.parseUnary())
|
|
1113
|
+
}
|
|
1114
|
+
if (this.match(TokenType.PLUS_PLUS)) {
|
|
1115
|
+
const target = this.parseUnary()
|
|
1116
|
+
return AssignmentExpr(target, BinaryExpr('+', target, Literal(1)))
|
|
1117
|
+
}
|
|
1118
|
+
if (this.match(TokenType.MINUS_MINUS)) {
|
|
1119
|
+
const target = this.parseUnary()
|
|
1120
|
+
return AssignmentExpr(target, BinaryExpr('-', target, Literal(1)))
|
|
1121
|
+
}
|
|
1122
|
+
|
|
1123
|
+
const expr = this.parseMemberExpression()
|
|
1124
|
+
if (this.match(TokenType.PLUS_PLUS)) {
|
|
1125
|
+
return AssignmentExpr(expr, BinaryExpr('+', expr, Literal(1)))
|
|
1126
|
+
}
|
|
1127
|
+
if (this.match(TokenType.MINUS_MINUS)) {
|
|
1128
|
+
return AssignmentExpr(expr, BinaryExpr('-', expr, Literal(1)))
|
|
1129
|
+
}
|
|
1130
|
+
return expr
|
|
919
1131
|
}
|
|
920
1132
|
|
|
921
1133
|
parseMemberExpression() {
|
|
@@ -941,6 +1153,11 @@ export class Parser {
|
|
|
941
1153
|
}
|
|
942
1154
|
|
|
943
1155
|
if (this.match(TokenType.QUESTION_DOT)) {
|
|
1156
|
+
// Optional call: fn?.(args)
|
|
1157
|
+
if (this.check(TokenType.LPAREN)) {
|
|
1158
|
+
expr = { ...CallExpr(expr, this.parseCallArguments()), optional: true }
|
|
1159
|
+
continue
|
|
1160
|
+
}
|
|
944
1161
|
if (this.match(TokenType.LBRACKET)) {
|
|
945
1162
|
const property = this.parseExpression()
|
|
946
1163
|
this.expect(
|
|
@@ -1047,8 +1264,9 @@ export class Parser {
|
|
|
1047
1264
|
}
|
|
1048
1265
|
|
|
1049
1266
|
// Type expressions are captured as plain strings, not a full type
|
|
1050
|
-
// AST -- Tylix's own codegen never interprets
|
|
1051
|
-
//
|
|
1267
|
+
// AST -- Tylix's own codegen never interprets them, it only needs
|
|
1268
|
+
// the exact TS source text to paste into the virtual .ts file handed
|
|
1269
|
+
// to the TypeScript compiler API.
|
|
1052
1270
|
parseTypeExpression() {
|
|
1053
1271
|
let left = this.parseUnionTypeOperand()
|
|
1054
1272
|
while (this.check(TokenType.PIPE)) {
|
|
@@ -1100,7 +1318,7 @@ export class Parser {
|
|
|
1100
1318
|
return name
|
|
1101
1319
|
}
|
|
1102
1320
|
|
|
1103
|
-
|
|
1321
|
+
parseObjectTypeLiteral() {
|
|
1104
1322
|
this.expect(TokenType.LBRACE, "Expected '{' to start object type")
|
|
1105
1323
|
const members = []
|
|
1106
1324
|
while (!this.check(TokenType.RBRACE)) {
|
|
@@ -1135,6 +1353,19 @@ export class Parser {
|
|
|
1135
1353
|
parseObjectLiteral() {
|
|
1136
1354
|
const properties = []
|
|
1137
1355
|
while (!this.check(TokenType.RBRACE)) {
|
|
1356
|
+
// Object spread: { ...base, extra: 1 }. Represented as a
|
|
1357
|
+
// property with `spread: true` and no key, so codegen must check
|
|
1358
|
+
// `p.spread` before reading `p.key`.
|
|
1359
|
+
if (this.match(TokenType.SPREAD)) {
|
|
1360
|
+
properties.push({
|
|
1361
|
+
key: null,
|
|
1362
|
+
spread: true,
|
|
1363
|
+
value: this.parseExpression(),
|
|
1364
|
+
})
|
|
1365
|
+
this.match(TokenType.COMMA)
|
|
1366
|
+
continue
|
|
1367
|
+
}
|
|
1368
|
+
|
|
1138
1369
|
const isString = this.check(TokenType.STRING)
|
|
1139
1370
|
const isKeywordKey =
|
|
1140
1371
|
!isString && KEYWORD_TOKEN_TYPES.has(this.peek().type)
|
|
@@ -1186,4 +1417,4 @@ export class Parser {
|
|
|
1186
1417
|
`${message} at line ${this.peek().line}, got ${this.peek().type}`,
|
|
1187
1418
|
)
|
|
1188
1419
|
}
|
|
1189
|
-
}
|
|
1420
|
+
}
|
|
@@ -33,6 +33,11 @@ const USE_CHANNEL_SOURCE = fs
|
|
|
33
33
|
.readFileSync(path.join(__dirname, 'runtime', 'useChannel.js'), 'utf-8')
|
|
34
34
|
.replace(/export\s+/g, '')
|
|
35
35
|
|
|
36
|
+
|
|
37
|
+
const NOT_FOUND_SOURCE = fs
|
|
38
|
+
.readFileSync(path.join(__dirname, 'runtime', 'notFound.js'), 'utf-8')
|
|
39
|
+
.replace(/export\s+/g, '')
|
|
40
|
+
|
|
36
41
|
const capabilityCache = new Map() // capabilitiesDir -> { signature, code }
|
|
37
42
|
|
|
38
43
|
async function loadCapabilityDefinitions(capabilitiesDir) {
|
|
@@ -155,6 +160,10 @@ export async function renderPageDocument(
|
|
|
155
160
|
props = {},
|
|
156
161
|
apiHelpers = '',
|
|
157
162
|
channelsPort = null,
|
|
163
|
+
// Stylesheets to link in <head>, in order. createApp passes the ones
|
|
164
|
+
// that actually exist in public/. The default keeps older callers
|
|
165
|
+
// working exactly as before.
|
|
166
|
+
stylesheets = ['/tailwind.css'],
|
|
158
167
|
capabilitiesDir = path.join(process.cwd(), 'app', 'capabilities'),
|
|
159
168
|
} = {},
|
|
160
169
|
) {
|
|
@@ -228,6 +237,8 @@ ${layoutCompiled.code}
|
|
|
228
237
|
? `<script>window.__TYLIX_CHANNELS_PORT__ = ${JSON.stringify(channelsPort)};</script>`
|
|
229
238
|
: ''
|
|
230
239
|
|
|
240
|
+
// If __ready() rejects (a failed needs/prefetch call), log it and keep
|
|
241
|
+
// going so the page still renders, instead of showing a blank screen.
|
|
231
242
|
const inlineScript = `
|
|
232
243
|
${RUNTIME_SOURCE}
|
|
233
244
|
|
|
@@ -239,6 +250,8 @@ ${LOADING_INDICATOR_SOURCE}
|
|
|
239
250
|
|
|
240
251
|
${USE_API_SOURCE}
|
|
241
252
|
|
|
253
|
+
${NOT_FOUND_SOURCE}
|
|
254
|
+
|
|
242
255
|
${USE_CHANNEL_SOURCE}
|
|
243
256
|
|
|
244
257
|
${apiHelpers}
|
|
@@ -255,7 +268,11 @@ document.addEventListener("DOMContentLoaded", () => {
|
|
|
255
268
|
${childRegistrations}
|
|
256
269
|
(async function (document, instance, components) {
|
|
257
270
|
if (typeof instance.__ready === "function") {
|
|
258
|
-
|
|
271
|
+
try {
|
|
272
|
+
await instance.__ready();
|
|
273
|
+
} catch (err) {
|
|
274
|
+
console.error("ready failed:", err);
|
|
275
|
+
}
|
|
259
276
|
}
|
|
260
277
|
${code}
|
|
261
278
|
${layoutMountCode}
|
|
@@ -272,12 +289,17 @@ ${layoutMountCode}
|
|
|
272
289
|
|
|
273
290
|
const styleTag = style.trim().length > 0 ? `<style>\n${style}\n</style>` : ''
|
|
274
291
|
|
|
292
|
+
const styleLinks = stylesheets
|
|
293
|
+
.map((href) => `<link rel="stylesheet" href="${href}">`)
|
|
294
|
+
.join('\n ')
|
|
295
|
+
|
|
275
296
|
const scriptSegments = [
|
|
276
297
|
{ name: 'runtime (reactive.js)', code: RUNTIME_SOURCE },
|
|
277
298
|
{ name: 'runtime (capability.js)', code: CAPABILITY_SOURCE },
|
|
278
299
|
{ name: 'capability definitions', code: capabilityDefinitions },
|
|
279
300
|
{ name: 'loading indicator', code: LOADING_INDICATOR_SOURCE },
|
|
280
301
|
{ name: 'useApi', code: USE_API_SOURCE },
|
|
302
|
+
{ name: 'notFound', code: NOT_FOUND_SOURCE },
|
|
281
303
|
{ name: 'useChannel', code: USE_CHANNEL_SOURCE },
|
|
282
304
|
{ name: 'app/useApi helpers', code: apiHelpers },
|
|
283
305
|
{ name: 'page class', code: classSource },
|
|
@@ -323,7 +345,8 @@ ${layoutMountCode}
|
|
|
323
345
|
})();
|
|
324
346
|
</script>
|
|
325
347
|
<title>${pageNode.title ?? pageName}</title>
|
|
326
|
-
<link rel="
|
|
348
|
+
<link rel="icon" type="image/png" href="/favicon.png">
|
|
349
|
+
${styleLinks}
|
|
327
350
|
${styleTag}
|
|
328
351
|
</head>
|
|
329
352
|
<body>
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
// Swaps the current page for the closest _not-found.tyx. The server renders
|
|
2
|
+
// it with a 404 status and puts the original URL back in the address bar.
|
|
3
|
+
export function notFound() {
|
|
4
|
+
const here = window.location.pathname + window.location.search
|
|
5
|
+
window.location.replace('/_tylix/not-found?path=' + encodeURIComponent(here))
|
|
6
|
+
}
|