xbintsc 0.2.9 → 0.3.3
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 +17 -2
- package/README.zh-CN.md +16 -2
- package/bin/xbintsc.js +49 -10
- package/dist/src/ast/expressions.d.ts +2 -0
- package/dist/src/binder/binder.d.ts +24 -98
- package/dist/src/binder/binder.js +13 -559
- package/dist/src/binder/binder.js.map +1 -1
- package/dist/src/binder/declarations.d.ts +16 -0
- package/dist/src/binder/declarations.js +168 -0
- package/dist/src/binder/declarations.js.map +1 -0
- package/dist/src/binder/helpers.d.ts +11 -0
- package/dist/src/binder/helpers.js +46 -0
- package/dist/src/binder/helpers.js.map +1 -0
- package/dist/src/binder/references.d.ts +21 -0
- package/dist/src/binder/references.js +349 -0
- package/dist/src/binder/references.js.map +1 -0
- package/dist/src/binder/types.d.ts +114 -0
- package/dist/src/binder/types.js +27 -0
- package/dist/src/binder/types.js.map +1 -0
- package/dist/src/cli/main.d.ts +1 -0
- package/dist/src/cli/main.js +40 -0
- package/dist/src/cli/main.js.map +1 -1
- package/dist/src/codegen/generator/calls/access.d.ts +13 -0
- package/dist/src/codegen/generator/calls/access.js +115 -0
- package/dist/src/codegen/generator/calls/access.js.map +1 -0
- package/dist/src/codegen/generator/calls/closures.d.ts +11 -0
- package/dist/src/codegen/generator/calls/closures.js +50 -0
- package/dist/src/codegen/generator/calls/closures.js.map +1 -0
- package/dist/src/codegen/generator/calls/invocation.d.ts +18 -0
- package/dist/src/codegen/generator/calls/invocation.js +242 -0
- package/dist/src/codegen/generator/calls/invocation.js.map +1 -0
- package/dist/src/codegen/generator/calls/literals.d.ts +10 -0
- package/dist/src/codegen/generator/calls/literals.js +69 -0
- package/dist/src/codegen/generator/calls/literals.js.map +1 -0
- package/dist/src/codegen/generator/calls/super.d.ts +10 -0
- package/dist/src/codegen/generator/calls/super.js +47 -0
- package/dist/src/codegen/generator/calls/super.js.map +1 -0
- package/dist/src/codegen/generator/calls.d.ts +15 -24
- package/dist/src/codegen/generator/calls.js +15 -482
- package/dist/src/codegen/generator/calls.js.map +1 -1
- package/dist/src/codegen/generator/context.d.ts +1 -1
- package/dist/src/codegen/generator/context.js +2 -2
- package/dist/src/codegen/generator/context.js.map +1 -1
- package/dist/src/codegen/generator/expressions/assignment.d.ts +13 -0
- package/dist/src/codegen/generator/expressions/assignment.js +203 -0
- package/dist/src/codegen/generator/expressions/assignment.js.map +1 -0
- package/dist/src/codegen/generator/expressions/operators.d.ts +14 -0
- package/dist/src/codegen/generator/expressions/operators.js +163 -0
- package/dist/src/codegen/generator/expressions/operators.js.map +1 -0
- package/dist/src/codegen/generator/expressions/primary.d.ts +17 -0
- package/dist/src/codegen/generator/expressions/primary.js +260 -0
- package/dist/src/codegen/generator/expressions/primary.js.map +1 -0
- package/dist/src/codegen/generator/expressions.d.ts +11 -21
- package/dist/src/codegen/generator/expressions.js +11 -494
- package/dist/src/codegen/generator/expressions.js.map +1 -1
- package/dist/src/codegen/generator/module.js +16 -4
- package/dist/src/codegen/generator/module.js.map +1 -1
- package/dist/src/codegen/generator/state.js +8 -0
- package/dist/src/codegen/generator/state.js.map +1 -1
- package/dist/src/codegen/generator/statements/control-flow.d.ts +14 -0
- package/dist/src/codegen/generator/statements/control-flow.js +199 -0
- package/dist/src/codegen/generator/statements/control-flow.js.map +1 -0
- package/dist/src/codegen/generator/statements/dispatch.d.ts +12 -0
- package/dist/src/codegen/generator/statements/dispatch.js +100 -0
- package/dist/src/codegen/generator/statements/dispatch.js.map +1 -0
- package/dist/src/codegen/generator/statements/loops.d.ts +13 -0
- package/dist/src/codegen/generator/statements/loops.js +152 -0
- package/dist/src/codegen/generator/statements/loops.js.map +1 -0
- package/dist/src/codegen/generator/statements/variables.d.ts +13 -0
- package/dist/src/codegen/generator/statements/variables.js +146 -0
- package/dist/src/codegen/generator/statements/variables.js.map +1 -0
- package/dist/src/codegen/generator/statements.d.ts +13 -21
- package/dist/src/codegen/generator/statements.js +13 -568
- package/dist/src/codegen/generator/statements.js.map +1 -1
- package/dist/src/codegen/generator/tables.js +7 -0
- package/dist/src/codegen/generator/tables.js.map +1 -1
- package/dist/src/codegen/values.d.ts +17 -17
- package/dist/src/codegen/values.js +21 -57
- package/dist/src/codegen/values.js.map +1 -1
- package/dist/src/diagnostics/diagnostic.d.ts +1 -0
- package/dist/src/diagnostics/diagnostic.js +1 -0
- package/dist/src/diagnostics/diagnostic.js.map +1 -1
- package/dist/src/diagnostics/source.d.ts +1 -1
- package/dist/src/diagnostics/source.js +7 -12
- package/dist/src/diagnostics/source.js.map +1 -1
- package/dist/src/driver/compiler.d.ts +4 -0
- package/dist/src/driver/compiler.js +51 -16
- package/dist/src/driver/compiler.js.map +1 -1
- package/dist/src/driver/index.d.ts +5 -2
- package/dist/src/driver/index.js +5 -2
- package/dist/src/driver/index.js.map +1 -1
- package/dist/src/driver/paths.d.ts +20 -0
- package/dist/src/driver/paths.js +49 -0
- package/dist/src/driver/paths.js.map +1 -1
- package/dist/src/driver/runtime-lib.d.ts +18 -0
- package/dist/src/driver/runtime-lib.js +31 -0
- package/dist/src/driver/runtime-lib.js.map +1 -0
- package/dist/src/driver/toolchain-download.d.ts +50 -0
- package/dist/src/driver/toolchain-download.js +76 -0
- package/dist/src/driver/toolchain-download.js.map +1 -0
- package/dist/src/driver/toolchain-provider.d.ts +33 -0
- package/dist/src/driver/toolchain-provider.js +71 -0
- package/dist/src/driver/toolchain-provider.js.map +1 -0
- package/dist/src/driver/toolchain.d.ts +9 -4
- package/dist/src/driver/toolchain.js +6 -10
- package/dist/src/driver/toolchain.js.map +1 -1
- package/dist/src/extensions/node/child_process/index.d.ts +4 -2
- package/dist/src/extensions/node/child_process/index.js +4 -2
- package/dist/src/extensions/node/child_process/index.js.map +1 -1
- package/dist/src/extensions/node/events/index.d.ts +10 -0
- package/dist/src/extensions/node/events/index.js +22 -0
- package/dist/src/extensions/node/events/index.js.map +1 -0
- package/dist/src/extensions/node/index.js +6 -0
- package/dist/src/extensions/node/index.js.map +1 -1
- package/dist/src/extensions/node/querystring/index.d.ts +10 -0
- package/dist/src/extensions/node/querystring/index.js +25 -0
- package/dist/src/extensions/node/querystring/index.js.map +1 -0
- package/dist/src/extensions/node/util/index.d.ts +11 -0
- package/dist/src/extensions/node/util/index.js +41 -0
- package/dist/src/extensions/node/util/index.js.map +1 -0
- package/dist/src/extensions/registry.d.ts +6 -0
- package/dist/src/extensions/registry.js.map +1 -1
- package/dist/src/index.d.ts +1 -1
- package/dist/src/index.js +1 -1
- package/dist/src/index.js.map +1 -1
- package/dist/src/lexer/scanner/identifiers.d.ts +9 -0
- package/dist/src/lexer/scanner/identifiers.js +33 -0
- package/dist/src/lexer/scanner/identifiers.js.map +1 -0
- package/dist/src/lexer/scanner/numbers.d.ts +10 -0
- package/dist/src/lexer/scanner/numbers.js +89 -0
- package/dist/src/lexer/scanner/numbers.js.map +1 -0
- package/dist/src/lexer/scanner/punctuation.d.ts +9 -0
- package/dist/src/lexer/scanner/punctuation.js +151 -0
- package/dist/src/lexer/scanner/punctuation.js.map +1 -0
- package/dist/src/lexer/scanner/regex.d.ts +11 -0
- package/dist/src/lexer/scanner/regex.js +69 -0
- package/dist/src/lexer/scanner/regex.js.map +1 -0
- package/dist/src/lexer/scanner/strings.d.ts +13 -0
- package/dist/src/lexer/scanner/strings.js +118 -0
- package/dist/src/lexer/scanner/strings.js.map +1 -0
- package/dist/src/lexer/scanner.d.ts +20 -27
- package/dist/src/lexer/scanner.js +11 -415
- package/dist/src/lexer/scanner.js.map +1 -1
- package/dist/src/parser/declarations/classes.d.ts +14 -0
- package/dist/src/parser/declarations/classes.js +191 -0
- package/dist/src/parser/declarations/classes.js.map +1 -0
- package/dist/src/parser/declarations/functions.d.ts +17 -0
- package/dist/src/parser/declarations/functions.js +241 -0
- package/dist/src/parser/declarations/functions.js.map +1 -0
- package/dist/src/parser/declarations/type-members.d.ts +16 -0
- package/dist/src/parser/declarations/type-members.js +156 -0
- package/dist/src/parser/declarations/type-members.js.map +1 -0
- package/dist/src/parser/declarations.d.ts +13 -24
- package/dist/src/parser/declarations.js +13 -571
- package/dist/src/parser/declarations.js.map +1 -1
- package/dist/src/parser/helpers.js +13 -4
- package/dist/src/parser/helpers.js.map +1 -1
- package/dist/src/parser/literals.js +10 -1
- package/dist/src/parser/literals.js.map +1 -1
- package/dist/tests/codegen/llvm.test.js +24 -0
- package/dist/tests/codegen/llvm.test.js.map +1 -1
- package/dist/tests/driver/compiler.test.js +3 -0
- package/dist/tests/driver/compiler.test.js.map +1 -1
- package/dist/tests/driver/paths.test.d.ts +1 -0
- package/dist/tests/driver/paths.test.js +26 -0
- package/dist/tests/driver/paths.test.js.map +1 -0
- package/dist/tests/driver/toolchain-download.test.d.ts +1 -0
- package/dist/tests/driver/toolchain-download.test.js +38 -0
- package/dist/tests/driver/toolchain-download.test.js.map +1 -0
- package/dist/tests/driver/toolchain-provider.test.d.ts +1 -0
- package/dist/tests/driver/toolchain-provider.test.js +37 -0
- package/dist/tests/driver/toolchain-provider.test.js.map +1 -0
- package/dist/tests/e2e/compile-node.test.d.ts +7 -0
- package/dist/tests/e2e/compile-node.test.js +123 -0
- package/dist/tests/e2e/compile-node.test.js.map +1 -0
- package/dist/tests/e2e/compile.test.d.ts +4 -1
- package/dist/tests/e2e/compile.test.js +78 -130
- package/dist/tests/e2e/compile.test.js.map +1 -1
- package/dist/tests/e2e/harness.d.ts +36 -0
- package/dist/tests/e2e/harness.js +64 -0
- package/dist/tests/e2e/harness.js.map +1 -0
- package/dist/tests/e2e/node-modules.test.js +60 -0
- package/dist/tests/e2e/node-modules.test.js.map +1 -1
- package/dist/tests/extensions/registry.test.js +4 -0
- package/dist/tests/extensions/registry.test.js.map +1 -1
- package/package.json +5 -3
- package/runtime/ext_node/buffer/buffer.c +12 -552
- package/runtime/ext_node/buffer/parts/byte-access.inc +35 -0
- package/runtime/ext_node/buffer/parts/construction.inc +60 -0
- package/runtime/ext_node/buffer/parts/integer-io.inc +119 -0
- package/runtime/ext_node/buffer/parts/methods.inc +135 -0
- package/runtime/ext_node/buffer/parts/prototype.inc +54 -0
- package/runtime/ext_node/buffer/parts/search.inc +86 -0
- package/runtime/ext_node/buffer/parts/static-api.inc +58 -0
- package/runtime/ext_node/child_process/child_process.c +37 -3
- package/runtime/ext_node/events/events.c +285 -0
- package/runtime/ext_node/http/http.c +11 -736
- package/runtime/ext_node/http/parts/client.inc +290 -0
- package/runtime/ext_node/http/parts/helpers.inc +45 -0
- package/runtime/ext_node/http/parts/prototypes.inc +57 -0
- package/runtime/ext_node/http/parts/response.inc +149 -0
- package/runtime/ext_node/http/parts/server.inc +133 -0
- package/runtime/ext_node/http/parts/string-builder.inc +58 -0
- package/runtime/ext_node/net/net.c +12 -504
- package/runtime/ext_node/net/parts/construction.inc +22 -0
- package/runtime/ext_node/net/parts/platform.inc +60 -0
- package/runtime/ext_node/net/parts/prototypes.inc +35 -0
- package/runtime/ext_node/net/parts/read-loop.inc +58 -0
- package/runtime/ext_node/net/parts/server.inc +131 -0
- package/runtime/ext_node/net/parts/socket-methods.inc +100 -0
- package/runtime/ext_node/net/parts/static-api.inc +93 -0
- package/runtime/ext_node/querystring/querystring.c +251 -0
- package/runtime/ext_node/util/util.c +456 -0
- package/runtime/rt.h +8 -2
- package/runtime/rt_internal.h +34 -0
- package/runtime/xt_bigint/arithmetic.inc +135 -0
- package/runtime/xt_bigint/bits.inc +98 -0
- package/runtime/xt_bigint/bitwise.inc +103 -0
- package/runtime/xt_bigint/compare.inc +30 -0
- package/runtime/xt_bigint/conversions.inc +51 -0
- package/runtime/xt_bigint/division.inc +85 -0
- package/runtime/xt_bigint/format.inc +145 -0
- package/runtime/xt_bigint/helpers.inc +92 -0
- package/runtime/xt_bigint/magnitude.inc +88 -0
- package/runtime/xt_bigint/public.inc +59 -0
- package/runtime/xt_bigint/small-arithmetic.inc +58 -0
- package/runtime/xt_bigint.c +42 -0
- package/runtime/xt_builtins.c +1 -0
- package/runtime/xt_io.c +7 -0
- package/runtime/xt_stdlib/arrays.inc +161 -0
- package/runtime/xt_stdlib/dispatch.inc +116 -0
- package/runtime/xt_stdlib/helpers.inc +23 -0
- package/runtime/xt_stdlib/math.inc +72 -0
- package/runtime/xt_stdlib/strings.inc +178 -0
- package/runtime/xt_stdlib.c +10 -549
- package/runtime/xt_stdlib2/array-statics.inc +37 -0
- package/runtime/xt_stdlib2/arrays.inc +189 -0
- package/runtime/xt_stdlib2/container.inc +23 -0
- package/runtime/xt_stdlib2/date.inc +72 -0
- package/runtime/xt_stdlib2/error.inc +22 -0
- package/runtime/xt_stdlib2/helpers.inc +43 -0
- package/runtime/xt_stdlib2/json.inc +213 -0
- package/runtime/xt_stdlib2/map.inc +95 -0
- package/runtime/xt_stdlib2/number-string-statics.inc +73 -0
- package/runtime/xt_stdlib2/numbers.inc +38 -0
- package/runtime/xt_stdlib2/objects.inc +41 -0
- package/runtime/xt_stdlib2/regexp-match.inc +204 -0
- package/runtime/xt_stdlib2/regexp.inc +20 -0
- package/runtime/xt_stdlib2/set.inc +105 -0
- package/runtime/xt_stdlib2/strings.inc +94 -0
- package/runtime/xt_stdlib2.c +19 -1278
- package/runtime/xt_values.c +155 -12
- package/scripts/build-runtime.ts +109 -0
- package/scripts/fetch-toolchain.ts +101 -0
- package/scripts/package-release.ts +192 -0
- package/src/ast/expressions.ts +2 -0
- package/src/binder/binder.ts +40 -725
- package/src/binder/declarations.ts +203 -0
- package/src/binder/helpers.ts +47 -0
- package/src/binder/references.ts +410 -0
- package/src/binder/types.ts +134 -0
- package/src/cli/main.ts +43 -0
- package/src/codegen/generator/calls/access.ts +139 -0
- package/src/codegen/generator/calls/closures.ts +60 -0
- package/src/codegen/generator/calls/invocation.ts +282 -0
- package/src/codegen/generator/calls/literals.ts +77 -0
- package/src/codegen/generator/calls/super.ts +61 -0
- package/src/codegen/generator/calls.ts +30 -552
- package/src/codegen/generator/context.ts +2 -2
- package/src/codegen/generator/expressions/assignment.ts +226 -0
- package/src/codegen/generator/expressions/operators.ts +182 -0
- package/src/codegen/generator/expressions/primary.ts +304 -0
- package/src/codegen/generator/expressions.ts +22 -553
- package/src/codegen/generator/module.ts +14 -4
- package/src/codegen/generator/state.ts +8 -0
- package/src/codegen/generator/statements/control-flow.ts +225 -0
- package/src/codegen/generator/statements/dispatch.ts +122 -0
- package/src/codegen/generator/statements/loops.ts +173 -0
- package/src/codegen/generator/statements/variables.ts +152 -0
- package/src/codegen/generator/statements.ts +26 -612
- package/src/codegen/generator/tables.ts +7 -0
- package/src/codegen/values.ts +22 -57
- package/src/diagnostics/diagnostic.ts +1 -0
- package/src/diagnostics/source.ts +7 -12
- package/src/driver/compiler.ts +61 -18
- package/src/driver/index.ts +17 -2
- package/src/driver/paths.ts +54 -0
- package/src/driver/runtime-lib.ts +32 -0
- package/src/driver/toolchain-download.ts +108 -0
- package/src/driver/toolchain-provider.ts +91 -0
- package/src/driver/toolchain.ts +29 -11
- package/src/extensions/node/child_process/index.ts +4 -2
- package/src/extensions/node/events/index.ts +27 -0
- package/src/extensions/node/index.ts +6 -0
- package/src/extensions/node/querystring/index.ts +29 -0
- package/src/extensions/node/util/index.ts +48 -0
- package/src/extensions/registry.ts +6 -0
- package/src/index.ts +1 -0
- package/src/lexer/scanner/identifiers.ts +37 -0
- package/src/lexer/scanner/numbers.ts +84 -0
- package/src/lexer/scanner/punctuation.ts +132 -0
- package/src/lexer/scanner/regex.ts +79 -0
- package/src/lexer/scanner/strings.ts +128 -0
- package/src/lexer/scanner.ts +35 -401
- package/src/parser/declarations/classes.ts +215 -0
- package/src/parser/declarations/functions.ts +254 -0
- package/src/parser/declarations/type-members.ts +181 -0
- package/src/parser/declarations.ts +32 -609
- package/src/parser/helpers.ts +13 -6
- package/src/parser/literals.ts +10 -1
- package/scripts/build-runtime.mjs +0 -55
|
@@ -1,613 +1,36 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Declaration parsing:
|
|
3
|
-
*
|
|
2
|
+
* Declaration parsing: aggregates the function, class and type-level
|
|
3
|
+
* declaration groups into the `DeclarationMethods` mixin consumed by the
|
|
4
|
+
* parser.
|
|
5
|
+
*
|
|
6
|
+
* The implementation is split by functional area under `declarations/`:
|
|
7
|
+
* - `functions` — function signatures, type parameters, parameters,
|
|
8
|
+
* binding names and return types
|
|
9
|
+
* - `classes` — class bodies, heritage clauses and members
|
|
10
|
+
* - `type-members` — interfaces, aliases, enums, namespaces, type members
|
|
4
11
|
*/
|
|
5
12
|
|
|
6
|
-
import { DiagnosticCode } from "../diagnostics/diagnostic.js";
|
|
7
|
-
import { TokenKind } from "../lexer/token.js";
|
|
8
13
|
import {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
type
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
type
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
type PropertySignature,
|
|
33
|
-
type StringLiteral,
|
|
34
|
-
type TypeAliasDeclaration,
|
|
35
|
-
type TypeElement,
|
|
36
|
-
type TypeNode,
|
|
37
|
-
type TypeParameterDeclaration,
|
|
38
|
-
} from "../ast/nodes.js";
|
|
39
|
-
import { expressionToPropertyName } from "./helpers.js";
|
|
40
|
-
import { SpeculationError } from "./speculation.js";
|
|
41
|
-
import type { Parser } from "./parser.js";
|
|
42
|
-
|
|
43
|
-
export interface DeclarationMethods {
|
|
44
|
-
parseFunctionDeclaration(this: Parser, modifiers: Modifier[]): FunctionDeclaration;
|
|
45
|
-
parseTypeParameters(this: Parser): TypeParameterDeclaration[];
|
|
46
|
-
parseGreaterThan(this: Parser): void;
|
|
47
|
-
parseParameters(this: Parser): Parameter[];
|
|
48
|
-
parseBindingName(this: Parser): import("../ast/declarations.js").BindingName;
|
|
49
|
-
parseBindingElement(this: Parser, allowPropertyName: boolean): import("../ast/declarations.js").BindingElement;
|
|
50
|
-
parseReturnType(this: Parser): TypeNode;
|
|
51
|
-
parseClassDeclaration(this: Parser, modifiers: Modifier[]): ClassDeclaration;
|
|
52
|
-
parseHeritageClauses(this: Parser): HeritageClause[];
|
|
53
|
-
parseClassMembers(this: Parser): ClassElement[];
|
|
54
|
-
parseClassElement(this: Parser): ClassElement | undefined;
|
|
55
|
-
parsePropertyName(this: Parser): PropertyName;
|
|
56
|
-
parseInterfaceDeclaration(this: Parser, modifiers: Modifier[]): InterfaceDeclaration;
|
|
57
|
-
parseTypeAliasDeclaration(this: Parser, modifiers: Modifier[]): TypeAliasDeclaration;
|
|
58
|
-
parseEnumDeclaration(this: Parser, modifiers: Modifier[]): EnumDeclaration;
|
|
59
|
-
parseModuleDeclaration(this: Parser, modifiers: Modifier[]): ModuleDeclaration;
|
|
60
|
-
parseTypeMembers(this: Parser): TypeElement[];
|
|
61
|
-
parseTypeMember(this: Parser): TypeElement | undefined;
|
|
62
|
-
parseSemicolonOrComma(this: Parser): void;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
export const declarationMethods: DeclarationMethods = {
|
|
66
|
-
// -- functions ------------------------------------------------------------
|
|
67
|
-
|
|
68
|
-
parseFunctionDeclaration(this: Parser, modifiers: Modifier[]): FunctionDeclaration {
|
|
69
|
-
const start = this.parseExpected(TokenKind.FunctionKeyword).start;
|
|
70
|
-
let flags = NodeFlags.None;
|
|
71
|
-
if (this.at(TokenKind.Asterisk)) {
|
|
72
|
-
this.nextToken();
|
|
73
|
-
flags |= NodeFlags.Generator;
|
|
74
|
-
}
|
|
75
|
-
if (this.hasModifier(modifiers, ModifierKind.Async)) flags |= NodeFlags.Async;
|
|
76
|
-
if (this.hasModifier(modifiers, ModifierKind.Declare)) flags |= NodeFlags.Declare;
|
|
77
|
-
let name: Identifier | undefined;
|
|
78
|
-
if (!this.at(TokenKind.OpenParen) && !this.at(TokenKind.LessThan)) name = this.parseIdentifier();
|
|
79
|
-
const typeParameters = this.parseTypeParameters();
|
|
80
|
-
const parameters = this.parseParameters();
|
|
81
|
-
let returnType: TypeNode | undefined;
|
|
82
|
-
if (this.at(TokenKind.Colon)) {
|
|
83
|
-
this.nextToken();
|
|
84
|
-
returnType = this.parseReturnType();
|
|
85
|
-
}
|
|
86
|
-
let body: Block | undefined;
|
|
87
|
-
if (this.at(TokenKind.OpenBrace)) body = this.parseBlock();
|
|
88
|
-
else this.parseSemicolon();
|
|
89
|
-
return {
|
|
90
|
-
kind: SyntaxKind.FunctionDeclaration,
|
|
91
|
-
name,
|
|
92
|
-
modifiers,
|
|
93
|
-
typeParameters,
|
|
94
|
-
parameters,
|
|
95
|
-
returnType,
|
|
96
|
-
body,
|
|
97
|
-
flags,
|
|
98
|
-
start,
|
|
99
|
-
end: body?.end ?? returnType?.end ?? parameters[parameters.length - 1]?.end ?? start,
|
|
100
|
-
};
|
|
101
|
-
},
|
|
102
|
-
|
|
103
|
-
parseTypeParameters(this: Parser): TypeParameterDeclaration[] {
|
|
104
|
-
if (!this.at(TokenKind.LessThan)) return [];
|
|
105
|
-
this.nextToken();
|
|
106
|
-
const params: TypeParameterDeclaration[] = [];
|
|
107
|
-
while (!this.at(TokenKind.GreaterThan) && !this.at(TokenKind.EndOfFile)) {
|
|
108
|
-
const name = this.parseIdentifier();
|
|
109
|
-
let constraint: TypeNode | undefined;
|
|
110
|
-
if (this.at(TokenKind.ExtendsKeyword)) {
|
|
111
|
-
this.nextToken();
|
|
112
|
-
constraint = this.parseType();
|
|
113
|
-
}
|
|
114
|
-
let defaultType: TypeNode | undefined;
|
|
115
|
-
if (this.at(TokenKind.Equals)) {
|
|
116
|
-
this.nextToken();
|
|
117
|
-
defaultType = this.parseType();
|
|
118
|
-
}
|
|
119
|
-
params.push({ kind: SyntaxKind.TypeParameterDeclaration, name, constraint, default: defaultType, start: name.start, end: defaultType?.end ?? constraint?.end ?? name.end });
|
|
120
|
-
if (this.at(TokenKind.Comma)) this.nextToken();
|
|
121
|
-
else break;
|
|
122
|
-
}
|
|
123
|
-
this.parseGreaterThan();
|
|
124
|
-
return params;
|
|
125
|
-
},
|
|
126
|
-
|
|
127
|
-
/** Consume a `>` that may have been lexed as `>>` / `>>>`, splitting it. */
|
|
128
|
-
parseGreaterThan(this: Parser): void {
|
|
129
|
-
const token = this.token;
|
|
130
|
-
if (token.kind === TokenKind.GreaterThan) {
|
|
131
|
-
this.nextToken();
|
|
132
|
-
return;
|
|
133
|
-
}
|
|
134
|
-
if (token.kind === TokenKind.GreaterThanGreaterThan || token.kind === TokenKind.GreaterThanGreaterThanGreaterThan) {
|
|
135
|
-
const extra = token.kind === TokenKind.GreaterThanGreaterThan ? 1 : 2;
|
|
136
|
-
this.tokens[this.index] = {
|
|
137
|
-
kind: extra === 1 ? TokenKind.GreaterThan : TokenKind.GreaterThanGreaterThan,
|
|
138
|
-
start: token.start + 1,
|
|
139
|
-
end: token.end,
|
|
140
|
-
text: token.text.slice(1),
|
|
141
|
-
precededByLineBreak: false,
|
|
142
|
-
};
|
|
143
|
-
return;
|
|
144
|
-
}
|
|
145
|
-
this.parseExpected(TokenKind.GreaterThan);
|
|
146
|
-
},
|
|
147
|
-
|
|
148
|
-
parseParameters(this: Parser): Parameter[] {
|
|
149
|
-
this.parseExpected(TokenKind.OpenParen);
|
|
150
|
-
const params: Parameter[] = [];
|
|
151
|
-
while (!this.at(TokenKind.CloseParen) && !this.at(TokenKind.EndOfFile)) {
|
|
152
|
-
const start = this.token.start;
|
|
153
|
-
const modifiers = this.tryParseModifiers();
|
|
154
|
-
let dotDotDotToken = false;
|
|
155
|
-
if (this.at(TokenKind.DotDotDot)) {
|
|
156
|
-
this.nextToken();
|
|
157
|
-
dotDotDotToken = true;
|
|
158
|
-
}
|
|
159
|
-
let name: import("../ast/declarations.js").BindingName;
|
|
160
|
-
let isThisParameter = false;
|
|
161
|
-
if (this.at(TokenKind.ThisKeyword)) {
|
|
162
|
-
const thisToken = this.nextToken();
|
|
163
|
-
name = { kind: SyntaxKind.Identifier, text: "this", start: thisToken.start, end: thisToken.end };
|
|
164
|
-
isThisParameter = true;
|
|
165
|
-
} else {
|
|
166
|
-
name = this.parseBindingName();
|
|
167
|
-
}
|
|
168
|
-
let questionToken = false;
|
|
169
|
-
if (this.at(TokenKind.Question)) {
|
|
170
|
-
this.nextToken();
|
|
171
|
-
questionToken = true;
|
|
172
|
-
}
|
|
173
|
-
let type: TypeNode | undefined;
|
|
174
|
-
if (this.at(TokenKind.Colon)) {
|
|
175
|
-
this.nextToken();
|
|
176
|
-
type = this.parseType();
|
|
177
|
-
}
|
|
178
|
-
let initializer: Expression | undefined;
|
|
179
|
-
if (this.at(TokenKind.Equals)) {
|
|
180
|
-
this.nextToken();
|
|
181
|
-
initializer = this.parseAssignmentExpression();
|
|
182
|
-
}
|
|
183
|
-
// A `this` parameter is a type-level annotation only and is never a
|
|
184
|
-
// runtime argument, so it must not occupy an `argv` slot.
|
|
185
|
-
if (!isThisParameter) {
|
|
186
|
-
params.push({ kind: SyntaxKind.Parameter, name, modifiers, dotDotDotToken, questionToken, type, initializer, start, end: initializer?.end ?? type?.end ?? name.end });
|
|
187
|
-
}
|
|
188
|
-
if (this.at(TokenKind.Comma)) this.nextToken();
|
|
189
|
-
else break;
|
|
190
|
-
}
|
|
191
|
-
this.parseExpected(TokenKind.CloseParen);
|
|
192
|
-
return params;
|
|
193
|
-
},
|
|
194
|
-
|
|
195
|
-
parseBindingName(this: Parser): import("../ast/declarations.js").BindingName {
|
|
196
|
-
if (this.at(TokenKind.OpenBracket)) {
|
|
197
|
-
const open = this.nextToken();
|
|
198
|
-
const elements: (import("../ast/declarations.js").BindingElement | undefined)[] = [];
|
|
199
|
-
while (!this.at(TokenKind.CloseBracket) && !this.at(TokenKind.EndOfFile)) {
|
|
200
|
-
if (this.at(TokenKind.Comma)) {
|
|
201
|
-
elements.push(undefined);
|
|
202
|
-
this.nextToken();
|
|
203
|
-
continue;
|
|
204
|
-
}
|
|
205
|
-
elements.push(this.parseBindingElement(false));
|
|
206
|
-
if (this.at(TokenKind.Comma)) this.nextToken();
|
|
207
|
-
else break;
|
|
208
|
-
}
|
|
209
|
-
const close = this.parseExpected(TokenKind.CloseBracket);
|
|
210
|
-
return { kind: SyntaxKind.ArrayBindingPattern, elements, start: open.start, end: close.end };
|
|
211
|
-
}
|
|
212
|
-
if (this.at(TokenKind.OpenBrace)) {
|
|
213
|
-
const open = this.nextToken();
|
|
214
|
-
const elements: import("../ast/declarations.js").BindingElement[] = [];
|
|
215
|
-
while (!this.at(TokenKind.CloseBrace) && !this.at(TokenKind.EndOfFile)) {
|
|
216
|
-
elements.push(this.parseBindingElement(true));
|
|
217
|
-
if (this.at(TokenKind.Comma)) this.nextToken();
|
|
218
|
-
else break;
|
|
219
|
-
}
|
|
220
|
-
const close = this.parseExpected(TokenKind.CloseBrace);
|
|
221
|
-
return { kind: SyntaxKind.ObjectBindingPattern, elements, start: open.start, end: close.end };
|
|
222
|
-
}
|
|
223
|
-
return this.parseIdentifier();
|
|
224
|
-
},
|
|
225
|
-
|
|
226
|
-
parseBindingElement(this: Parser, allowPropertyName: boolean): import("../ast/declarations.js").BindingElement {
|
|
227
|
-
const start = this.token.start;
|
|
228
|
-
let dotDotDotToken = false;
|
|
229
|
-
if (this.at(TokenKind.DotDotDot)) {
|
|
230
|
-
this.nextToken();
|
|
231
|
-
dotDotDotToken = true;
|
|
232
|
-
}
|
|
233
|
-
let propertyName: PropertyName | undefined;
|
|
234
|
-
let name: import("../ast/declarations.js").BindingName;
|
|
235
|
-
if (allowPropertyName && this.at(TokenKind.OpenBracket)) {
|
|
236
|
-
// Computed property key: `{ [expr]: target }`.
|
|
237
|
-
this.nextToken();
|
|
238
|
-
const expr = this.parseAssignmentExpression();
|
|
239
|
-
this.parseExpected(TokenKind.CloseBracket);
|
|
240
|
-
propertyName = expressionToPropertyName(expr) ?? { kind: SyntaxKind.Identifier, text: "<computed>", start: expr.start, end: expr.end };
|
|
241
|
-
this.parseExpected(TokenKind.Colon);
|
|
242
|
-
name = this.parseBindingName();
|
|
243
|
-
} else if (allowPropertyName && !this.at(TokenKind.OpenBrace) && !this.at(TokenKind.OpenBracket)) {
|
|
244
|
-
const first = this.parseIdentifierName();
|
|
245
|
-
if (this.at(TokenKind.Colon)) {
|
|
246
|
-
this.nextToken();
|
|
247
|
-
propertyName = first;
|
|
248
|
-
name = this.parseBindingName();
|
|
249
|
-
} else {
|
|
250
|
-
name = first;
|
|
251
|
-
}
|
|
252
|
-
} else {
|
|
253
|
-
// Array element, or a nested pattern used as an object value.
|
|
254
|
-
name = this.parseBindingName();
|
|
255
|
-
}
|
|
256
|
-
let initializer: Expression | undefined;
|
|
257
|
-
if (this.at(TokenKind.Equals)) {
|
|
258
|
-
this.nextToken();
|
|
259
|
-
initializer = this.parseAssignmentExpression();
|
|
260
|
-
}
|
|
261
|
-
const end = initializer?.end ?? (name.kind === SyntaxKind.Identifier ? name.end : name.end);
|
|
262
|
-
return { kind: SyntaxKind.BindingElement, name, propertyName, dotDotDotToken, initializer, start, end };
|
|
263
|
-
},
|
|
264
|
-
|
|
265
|
-
parseReturnType(this: Parser): TypeNode {
|
|
266
|
-
if (this.at(TokenKind.AssertsKeyword)) {
|
|
267
|
-
const start = this.nextToken().start;
|
|
268
|
-
let paramName: Identifier | undefined;
|
|
269
|
-
if (this.isIdentifierLike(this.token) || this.at(TokenKind.ThisKeyword)) paramName = this.parseIdentifierName();
|
|
270
|
-
let predicateType: TypeNode | undefined;
|
|
271
|
-
if (this.at(TokenKind.IsKeyword)) {
|
|
272
|
-
this.nextToken();
|
|
273
|
-
predicateType = this.parseType();
|
|
274
|
-
}
|
|
275
|
-
const stub: Identifier = { kind: SyntaxKind.Identifier, text: paramName?.text ?? "this", start: paramName?.start ?? start, end: paramName?.end ?? start };
|
|
276
|
-
return { kind: SyntaxKind.TypePredicate, parameterName: stub, type: predicateType, start, end: predicateType?.end ?? start };
|
|
277
|
-
}
|
|
278
|
-
if (this.isIdentifierLike(this.token) && this.atAhead(1, TokenKind.IsKeyword)) {
|
|
279
|
-
const paramName = this.parseIdentifierName();
|
|
280
|
-
this.nextToken();
|
|
281
|
-
const predicateType = this.parseType();
|
|
282
|
-
return { kind: SyntaxKind.TypePredicate, parameterName: paramName, type: predicateType, start: paramName.start, end: predicateType.end };
|
|
283
|
-
}
|
|
284
|
-
return this.parseType();
|
|
285
|
-
},
|
|
286
|
-
|
|
287
|
-
// -- classes --------------------------------------------------------------
|
|
288
|
-
|
|
289
|
-
parseClassDeclaration(this: Parser, modifiers: Modifier[]): ClassDeclaration {
|
|
290
|
-
const start = this.parseExpected(TokenKind.ClassKeyword).start;
|
|
291
|
-
let name: Identifier | undefined;
|
|
292
|
-
if (!this.at(TokenKind.OpenBrace) && !this.at(TokenKind.ExtendsKeyword) && !this.at(TokenKind.ImplementsKeyword)) {
|
|
293
|
-
name = this.parseIdentifier();
|
|
294
|
-
}
|
|
295
|
-
const typeParameters = this.parseTypeParameters();
|
|
296
|
-
const heritage = this.parseHeritageClauses();
|
|
297
|
-
const members = this.parseClassMembers();
|
|
298
|
-
return { kind: SyntaxKind.ClassDeclaration, name, modifiers, typeParameters, heritage, members, start, end: members[members.length - 1]?.end ?? start };
|
|
299
|
-
},
|
|
300
|
-
|
|
301
|
-
parseHeritageClauses(this: Parser): HeritageClause[] {
|
|
302
|
-
const clauses: HeritageClause[] = [];
|
|
303
|
-
while (this.at(TokenKind.ExtendsKeyword) || this.at(TokenKind.ImplementsKeyword)) {
|
|
304
|
-
const token = this.nextToken();
|
|
305
|
-
const tokenKind: "extends" | "implements" = token.kind === TokenKind.ExtendsKeyword ? "extends" : "implements";
|
|
306
|
-
const types: ExpressionWithTypeArguments[] = [];
|
|
307
|
-
do {
|
|
308
|
-
const expression = this.parseLeftHandSideExpression();
|
|
309
|
-
const typeArguments = this.at(TokenKind.LessThan) ? this.parseTypeArguments() : [];
|
|
310
|
-
types.push({ kind: SyntaxKind.ExpressionWithTypeArguments, expression, typeArguments, start: expression.start, end: typeArguments[typeArguments.length - 1]?.end ?? expression.end });
|
|
311
|
-
} while (this.at(TokenKind.Comma) && this.nextToken());
|
|
312
|
-
clauses.push({ kind: SyntaxKind.HeritageClause, token: tokenKind, types, start: token.start, end: types[types.length - 1]?.end ?? token.end });
|
|
313
|
-
}
|
|
314
|
-
return clauses;
|
|
315
|
-
},
|
|
316
|
-
|
|
317
|
-
parseClassMembers(this: Parser): ClassElement[] {
|
|
318
|
-
this.parseExpected(TokenKind.OpenBrace);
|
|
319
|
-
const members: ClassElement[] = [];
|
|
320
|
-
while (!this.at(TokenKind.CloseBrace) && !this.at(TokenKind.EndOfFile)) {
|
|
321
|
-
const before = this.consumed;
|
|
322
|
-
const member = this.parseClassElement();
|
|
323
|
-
if (member) members.push(member);
|
|
324
|
-
if (this.consumed === before) this.nextToken();
|
|
325
|
-
}
|
|
326
|
-
this.parseExpected(TokenKind.CloseBrace);
|
|
327
|
-
return members;
|
|
328
|
-
},
|
|
329
|
-
|
|
330
|
-
parseClassElement(this: Parser): ClassElement | undefined {
|
|
331
|
-
const start = this.token.start;
|
|
332
|
-
const modifiers = this.tryParseModifiers();
|
|
333
|
-
if (this.at(TokenKind.Semicolon)) {
|
|
334
|
-
this.nextToken();
|
|
335
|
-
return undefined;
|
|
336
|
-
}
|
|
337
|
-
if (
|
|
338
|
-
(this.at(TokenKind.GetKeyword) || this.at(TokenKind.SetKeyword)) &&
|
|
339
|
-
!this.atAhead(1, TokenKind.OpenParen) &&
|
|
340
|
-
!this.atAhead(1, TokenKind.Colon) &&
|
|
341
|
-
!this.atAhead(1, TokenKind.Equals) &&
|
|
342
|
-
!this.atAhead(1, TokenKind.Question) &&
|
|
343
|
-
!this.atAhead(1, TokenKind.LessThan) &&
|
|
344
|
-
!this.atAhead(1, TokenKind.Semicolon)
|
|
345
|
-
) {
|
|
346
|
-
const accessorToken = this.nextToken();
|
|
347
|
-
const name = this.parsePropertyName();
|
|
348
|
-
const parameters = this.parseParameters();
|
|
349
|
-
let returnType: TypeNode | undefined;
|
|
350
|
-
if (this.at(TokenKind.Colon)) {
|
|
351
|
-
this.nextToken();
|
|
352
|
-
returnType = this.parseReturnType();
|
|
353
|
-
}
|
|
354
|
-
let body: Block | undefined;
|
|
355
|
-
if (this.at(TokenKind.OpenBrace)) body = this.parseBlock();
|
|
356
|
-
else this.parseSemicolon();
|
|
357
|
-
const accessor: MethodDeclaration = {
|
|
358
|
-
kind: SyntaxKind.MethodDeclaration,
|
|
359
|
-
name,
|
|
360
|
-
modifiers,
|
|
361
|
-
typeParameters: [],
|
|
362
|
-
parameters,
|
|
363
|
-
returnType,
|
|
364
|
-
body,
|
|
365
|
-
flags: NodeFlags.None,
|
|
366
|
-
optional: false,
|
|
367
|
-
accessor: accessorToken.kind === TokenKind.GetKeyword ? "get" : "set",
|
|
368
|
-
start,
|
|
369
|
-
end: body?.end ?? returnType?.end ?? name.end,
|
|
370
|
-
};
|
|
371
|
-
return accessor;
|
|
372
|
-
}
|
|
373
|
-
if (this.at(TokenKind.ConstructorKeyword)) {
|
|
374
|
-
const name = this.nextToken();
|
|
375
|
-
const parameters = this.parseParameters();
|
|
376
|
-
let body: Block | undefined;
|
|
377
|
-
if (this.at(TokenKind.OpenBrace)) body = this.parseBlock();
|
|
378
|
-
else this.parseSemicolon();
|
|
379
|
-
const ctor: ConstructorDeclaration = { kind: SyntaxKind.ConstructorDeclaration, modifiers, parameters, body, start, end: body?.end ?? name.end };
|
|
380
|
-
return ctor;
|
|
381
|
-
}
|
|
382
|
-
let flags = NodeFlags.None;
|
|
383
|
-
if (this.at(TokenKind.Asterisk)) {
|
|
384
|
-
this.nextToken();
|
|
385
|
-
flags |= NodeFlags.Generator;
|
|
386
|
-
}
|
|
387
|
-
if (this.hasModifier(modifiers, ModifierKind.Async)) flags |= NodeFlags.Async;
|
|
388
|
-
const name = this.parsePropertyName();
|
|
389
|
-
let questionToken = false;
|
|
390
|
-
let exclamationToken = false;
|
|
391
|
-
if (this.at(TokenKind.Question)) {
|
|
392
|
-
this.nextToken();
|
|
393
|
-
questionToken = true;
|
|
394
|
-
}
|
|
395
|
-
if (this.at(TokenKind.Exclamation)) {
|
|
396
|
-
this.nextToken();
|
|
397
|
-
exclamationToken = true;
|
|
398
|
-
}
|
|
399
|
-
const typeParameters = this.at(TokenKind.LessThan) ? this.parseTypeParameters() : [];
|
|
400
|
-
if (this.at(TokenKind.OpenParen)) {
|
|
401
|
-
const parameters = this.parseParameters();
|
|
402
|
-
let returnType: TypeNode | undefined;
|
|
403
|
-
if (this.at(TokenKind.Colon)) {
|
|
404
|
-
this.nextToken();
|
|
405
|
-
returnType = this.parseReturnType();
|
|
406
|
-
}
|
|
407
|
-
let body: Block | undefined;
|
|
408
|
-
if (this.at(TokenKind.OpenBrace)) body = this.parseBlock();
|
|
409
|
-
else this.parseSemicolon();
|
|
410
|
-
const method: MethodDeclaration = {
|
|
411
|
-
kind: SyntaxKind.MethodDeclaration,
|
|
412
|
-
name,
|
|
413
|
-
modifiers,
|
|
414
|
-
typeParameters,
|
|
415
|
-
parameters,
|
|
416
|
-
returnType,
|
|
417
|
-
body,
|
|
418
|
-
flags,
|
|
419
|
-
optional: questionToken,
|
|
420
|
-
start,
|
|
421
|
-
end: body?.end ?? returnType?.end ?? name.end,
|
|
422
|
-
};
|
|
423
|
-
return method;
|
|
424
|
-
}
|
|
425
|
-
let type: TypeNode | undefined;
|
|
426
|
-
if (this.at(TokenKind.Colon)) {
|
|
427
|
-
this.nextToken();
|
|
428
|
-
type = this.parseType();
|
|
429
|
-
}
|
|
430
|
-
let initializer: Expression | undefined;
|
|
431
|
-
if (this.at(TokenKind.Equals)) {
|
|
432
|
-
this.nextToken();
|
|
433
|
-
initializer = this.parseAssignmentExpression();
|
|
434
|
-
}
|
|
435
|
-
this.parseSemicolon();
|
|
436
|
-
const property: PropertyDeclaration = {
|
|
437
|
-
kind: SyntaxKind.PropertyDeclaration,
|
|
438
|
-
name,
|
|
439
|
-
modifiers,
|
|
440
|
-
questionToken,
|
|
441
|
-
exclamationToken,
|
|
442
|
-
type,
|
|
443
|
-
initializer,
|
|
444
|
-
start,
|
|
445
|
-
end: initializer?.end ?? type?.end ?? name.end,
|
|
446
|
-
};
|
|
447
|
-
return property;
|
|
448
|
-
},
|
|
449
|
-
|
|
450
|
-
parsePropertyName(this: Parser): PropertyName {
|
|
451
|
-
const token = this.token;
|
|
452
|
-
if (token.kind === TokenKind.StringLiteral) {
|
|
453
|
-
this.nextToken();
|
|
454
|
-
return { kind: SyntaxKind.StringLiteral, text: token.text, raw: token.text, value: String(token.value ?? ""), start: token.start, end: token.end };
|
|
455
|
-
}
|
|
456
|
-
if (token.kind === TokenKind.NumericLiteral) {
|
|
457
|
-
this.nextToken();
|
|
458
|
-
return { kind: SyntaxKind.NumericLiteral, text: token.text, value: Number(token.value ?? 0), start: token.start, end: token.end };
|
|
459
|
-
}
|
|
460
|
-
if (token.kind === TokenKind.OpenBracket) {
|
|
461
|
-
const start = this.nextToken().start;
|
|
462
|
-
const expr = this.parseAssignmentExpression();
|
|
463
|
-
const close = this.parseExpected(TokenKind.CloseBracket);
|
|
464
|
-
return { kind: SyntaxKind.ComputedPropertyName, expression: expr, start, end: close.end };
|
|
465
|
-
}
|
|
466
|
-
return this.parseIdentifierName();
|
|
467
|
-
},
|
|
468
|
-
|
|
469
|
-
// -- interfaces / aliases / enums / namespaces ----------------------------
|
|
470
|
-
|
|
471
|
-
parseInterfaceDeclaration(this: Parser, modifiers: Modifier[]): InterfaceDeclaration {
|
|
472
|
-
const start = this.parseExpected(TokenKind.InterfaceKeyword).start;
|
|
473
|
-
const name = this.parseIdentifier();
|
|
474
|
-
const typeParameters = this.parseTypeParameters();
|
|
475
|
-
const heritage = this.parseHeritageClauses();
|
|
476
|
-
const members = this.parseTypeMembers();
|
|
477
|
-
return { kind: SyntaxKind.InterfaceDeclaration, name, modifiers, typeParameters, heritage, members, start, end: members[members.length - 1]?.end ?? name.end };
|
|
478
|
-
},
|
|
479
|
-
|
|
480
|
-
parseTypeAliasDeclaration(this: Parser, modifiers: Modifier[]): TypeAliasDeclaration {
|
|
481
|
-
const start = this.parseExpected(TokenKind.TypeKeyword).start;
|
|
482
|
-
const name = this.parseIdentifier();
|
|
483
|
-
const typeParameters = this.parseTypeParameters();
|
|
484
|
-
this.parseExpected(TokenKind.Equals);
|
|
485
|
-
const type = this.parseType();
|
|
486
|
-
this.parseSemicolon();
|
|
487
|
-
return { kind: SyntaxKind.TypeAliasDeclaration, name, modifiers, typeParameters, type, start, end: type.end };
|
|
488
|
-
},
|
|
489
|
-
|
|
490
|
-
parseEnumDeclaration(this: Parser, modifiers: Modifier[]): EnumDeclaration {
|
|
491
|
-
const start = this.parseExpected(TokenKind.EnumKeyword).start;
|
|
492
|
-
const name = this.parseIdentifier();
|
|
493
|
-
this.parseExpected(TokenKind.OpenBrace);
|
|
494
|
-
const members: EnumMember[] = [];
|
|
495
|
-
while (!this.at(TokenKind.CloseBrace) && !this.at(TokenKind.EndOfFile)) {
|
|
496
|
-
const memberName = this.parsePropertyName();
|
|
497
|
-
let initializer: Expression | undefined;
|
|
498
|
-
if (this.at(TokenKind.Equals)) {
|
|
499
|
-
this.nextToken();
|
|
500
|
-
initializer = this.parseAssignmentExpression();
|
|
501
|
-
}
|
|
502
|
-
members.push({ kind: SyntaxKind.EnumMember, name: memberName, initializer, start: memberName.start, end: initializer?.end ?? memberName.end });
|
|
503
|
-
if (this.at(TokenKind.Comma)) this.nextToken();
|
|
504
|
-
else break;
|
|
505
|
-
}
|
|
506
|
-
const close = this.parseExpected(TokenKind.CloseBrace);
|
|
507
|
-
return { kind: SyntaxKind.EnumDeclaration, name, modifiers, members, start, end: close.end };
|
|
508
|
-
},
|
|
509
|
-
|
|
510
|
-
parseModuleDeclaration(this: Parser, modifiers: Modifier[]): ModuleDeclaration {
|
|
511
|
-
const start = this.nextToken().start;
|
|
512
|
-
let name: Identifier | StringLiteral;
|
|
513
|
-
if (this.at(TokenKind.StringLiteral)) {
|
|
514
|
-
const token = this.nextToken();
|
|
515
|
-
name = { kind: SyntaxKind.StringLiteral, text: token.text, raw: token.text, value: String(token.value ?? ""), start: token.start, end: token.end };
|
|
516
|
-
} else {
|
|
517
|
-
name = this.parseIdentifierName();
|
|
518
|
-
}
|
|
519
|
-
while (this.at(TokenKind.Dot)) {
|
|
520
|
-
this.nextToken();
|
|
521
|
-
this.parseIdentifierName();
|
|
522
|
-
}
|
|
523
|
-
let body: Block | undefined;
|
|
524
|
-
if (this.at(TokenKind.OpenBrace)) body = this.parseBlock();
|
|
525
|
-
else this.parseSemicolon();
|
|
526
|
-
return { kind: SyntaxKind.ModuleDeclaration, name, modifiers, body, start, end: body?.end ?? name.end };
|
|
527
|
-
},
|
|
528
|
-
|
|
529
|
-
// -- type members ---------------------------------------------------------
|
|
530
|
-
|
|
531
|
-
parseTypeMembers(this: Parser): TypeElement[] {
|
|
532
|
-
this.parseExpected(TokenKind.OpenBrace);
|
|
533
|
-
const members: TypeElement[] = [];
|
|
534
|
-
while (!this.at(TokenKind.CloseBrace) && !this.at(TokenKind.EndOfFile)) {
|
|
535
|
-
const before = this.consumed;
|
|
536
|
-
const member = this.parseTypeMember();
|
|
537
|
-
if (member) members.push(member);
|
|
538
|
-
if (this.consumed === before) this.nextToken();
|
|
539
|
-
}
|
|
540
|
-
this.parseExpected(TokenKind.CloseBrace);
|
|
541
|
-
return members;
|
|
542
|
-
},
|
|
543
|
-
|
|
544
|
-
parseTypeMember(this: Parser): TypeElement | undefined {
|
|
545
|
-
const start = this.token.start;
|
|
546
|
-
const readonlyToken = this.at(TokenKind.ReadonlyKeyword);
|
|
547
|
-
if (readonlyToken) this.nextToken();
|
|
548
|
-
if (this.at(TokenKind.OpenParen) || this.at(TokenKind.LessThan)) {
|
|
549
|
-
const typeParameters = this.parseTypeParameters();
|
|
550
|
-
const parameters = this.parseParameters();
|
|
551
|
-
let returnType: TypeNode | undefined;
|
|
552
|
-
if (this.at(TokenKind.Colon)) {
|
|
553
|
-
this.nextToken();
|
|
554
|
-
returnType = this.parseType();
|
|
555
|
-
}
|
|
556
|
-
this.parseSemicolonOrComma();
|
|
557
|
-
const name: Identifier = { kind: SyntaxKind.Identifier, text: "__call", start, end: start };
|
|
558
|
-
return { kind: SyntaxKind.MethodSignature, name, questionToken: false, typeParameters, parameters, returnType, start, end: returnType?.end ?? start };
|
|
559
|
-
}
|
|
560
|
-
if (this.at(TokenKind.OpenBracket)) {
|
|
561
|
-
const indexSignature = this.tryParse<TypeElement>(() => {
|
|
562
|
-
const open = this.nextToken();
|
|
563
|
-
const parameters: Parameter[] = [];
|
|
564
|
-
const paramName = this.parseIdentifier();
|
|
565
|
-
let paramType: TypeNode | undefined;
|
|
566
|
-
if (this.at(TokenKind.Colon)) {
|
|
567
|
-
this.nextToken();
|
|
568
|
-
paramType = this.parseType();
|
|
569
|
-
} else {
|
|
570
|
-
throw new SpeculationError();
|
|
571
|
-
}
|
|
572
|
-
this.parseExpected(TokenKind.CloseBracket);
|
|
573
|
-
if (!this.at(TokenKind.Colon)) throw new SpeculationError();
|
|
574
|
-
this.nextToken();
|
|
575
|
-
const type = this.parseType();
|
|
576
|
-
this.parseSemicolonOrComma();
|
|
577
|
-
parameters.push({ kind: SyntaxKind.Parameter, name: paramName, modifiers: [], dotDotDotToken: false, questionToken: false, type: paramType, start: paramName.start, end: paramType?.end ?? paramName.end });
|
|
578
|
-
return { kind: SyntaxKind.IndexSignature, parameters, type, start: open.start, end: type.end };
|
|
579
|
-
});
|
|
580
|
-
if (indexSignature) return indexSignature;
|
|
581
|
-
}
|
|
582
|
-
const name = this.parsePropertyName();
|
|
583
|
-
let questionToken = false;
|
|
584
|
-
if (this.at(TokenKind.Question)) {
|
|
585
|
-
this.nextToken();
|
|
586
|
-
questionToken = true;
|
|
587
|
-
}
|
|
588
|
-
const typeParameters = this.at(TokenKind.LessThan) ? this.parseTypeParameters() : [];
|
|
589
|
-
if (this.at(TokenKind.OpenParen) || typeParameters.length > 0) {
|
|
590
|
-
const parameters = this.parseParameters();
|
|
591
|
-
let returnType: TypeNode | undefined;
|
|
592
|
-
if (this.at(TokenKind.Colon)) {
|
|
593
|
-
this.nextToken();
|
|
594
|
-
returnType = this.parseType();
|
|
595
|
-
}
|
|
596
|
-
this.parseSemicolonOrComma();
|
|
597
|
-
const sig: MethodSignature = { kind: SyntaxKind.MethodSignature, name, questionToken, typeParameters, parameters, returnType, start, end: returnType?.end ?? name.end };
|
|
598
|
-
return sig;
|
|
599
|
-
}
|
|
600
|
-
let type: TypeNode | undefined;
|
|
601
|
-
if (this.at(TokenKind.Colon)) {
|
|
602
|
-
this.nextToken();
|
|
603
|
-
type = this.parseType();
|
|
604
|
-
}
|
|
605
|
-
this.parseSemicolonOrComma();
|
|
606
|
-
const signature: PropertySignature = { kind: SyntaxKind.PropertySignature, name, questionToken, readonlyToken, type, start, end: type?.end ?? name.end };
|
|
607
|
-
return signature;
|
|
608
|
-
},
|
|
609
|
-
|
|
610
|
-
parseSemicolonOrComma(this: Parser): void {
|
|
611
|
-
if (this.at(TokenKind.Semicolon) || this.at(TokenKind.Comma)) this.nextToken();
|
|
612
|
-
},
|
|
613
|
-
};
|
|
14
|
+
declarationFunctionMethods,
|
|
15
|
+
type DeclarationFunctionMethods,
|
|
16
|
+
} from "./declarations/functions.js";
|
|
17
|
+
import {
|
|
18
|
+
declarationClassMethods,
|
|
19
|
+
type DeclarationClassMethods,
|
|
20
|
+
} from "./declarations/classes.js";
|
|
21
|
+
import {
|
|
22
|
+
declarationTypeMethods,
|
|
23
|
+
type DeclarationTypeMethods,
|
|
24
|
+
} from "./declarations/type-members.js";
|
|
25
|
+
|
|
26
|
+
export interface DeclarationMethods
|
|
27
|
+
extends DeclarationFunctionMethods,
|
|
28
|
+
DeclarationClassMethods,
|
|
29
|
+
DeclarationTypeMethods {}
|
|
30
|
+
|
|
31
|
+
export const declarationMethods: DeclarationMethods = Object.assign(
|
|
32
|
+
{},
|
|
33
|
+
declarationFunctionMethods,
|
|
34
|
+
declarationClassMethods,
|
|
35
|
+
declarationTypeMethods,
|
|
36
|
+
);
|
package/src/parser/helpers.ts
CHANGED
|
@@ -35,10 +35,17 @@ export function propertyNameText(name: PropertyName): string {
|
|
|
35
35
|
}
|
|
36
36
|
|
|
37
37
|
export function isAssignmentTarget(expr: Expression): boolean {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
38
|
+
switch (expr.kind) {
|
|
39
|
+
case SyntaxKind.Identifier:
|
|
40
|
+
case SyntaxKind.PropertyAccessExpression:
|
|
41
|
+
case SyntaxKind.ElementAccessExpression:
|
|
42
|
+
case SyntaxKind.ParenthesizedExpression:
|
|
43
|
+
// Array/object literals are valid on the left of `=` as destructuring
|
|
44
|
+
// patterns (`[a, b] = xs`, `({ a } = obj)`).
|
|
45
|
+
case SyntaxKind.ArrayLiteralExpression:
|
|
46
|
+
case SyntaxKind.ObjectLiteralExpression:
|
|
47
|
+
return true;
|
|
48
|
+
default:
|
|
49
|
+
return false;
|
|
50
|
+
}
|
|
44
51
|
}
|
package/src/parser/literals.ts
CHANGED
|
@@ -85,7 +85,16 @@ export const literalMethods: LiteralMethods = {
|
|
|
85
85
|
const fn: FunctionExpression = { kind: SyntaxKind.FunctionExpression, typeParameters, parameters, returnType, body, flags: NodeFlags.None, start, end: body.end };
|
|
86
86
|
properties.push({ kind: SyntaxKind.PropertyAssignment, name, initializer: fn, start, end: body.end });
|
|
87
87
|
} else if (name.kind === SyntaxKind.Identifier) {
|
|
88
|
-
|
|
88
|
+
// `{ a }` shorthand, optionally with a default (`{ a = 1 }`) that is
|
|
89
|
+
// only meaningful when the object is a destructuring assignment target.
|
|
90
|
+
let initializer: Expression | undefined;
|
|
91
|
+
let end = name.end;
|
|
92
|
+
if (this.at(TokenKind.Equals)) {
|
|
93
|
+
this.nextToken();
|
|
94
|
+
initializer = this.parseAssignmentExpression();
|
|
95
|
+
end = initializer.end;
|
|
96
|
+
}
|
|
97
|
+
const shorthand: ShorthandPropertyAssignment = { kind: SyntaxKind.ShorthandPropertyAssignment, name, initializer, start, end };
|
|
89
98
|
properties.push(shorthand);
|
|
90
99
|
} else {
|
|
91
100
|
this.error(DiagnosticCode.UnexpectedToken, "Invalid object literal member");
|