@sdkgen/parser 0.0.0-dev.20231002144112 → 0.0.0-dev.20231002191238

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.
Files changed (100) hide show
  1. package/dist/ast.js +298 -0
  2. package/dist/compatibility/index.js +206 -0
  3. package/dist/helpers.js +20 -0
  4. package/dist/index.js +9 -0
  5. package/dist/json.js +225 -0
  6. package/dist/lexer.js +283 -0
  7. package/dist/parser.js +419 -0
  8. package/dist/restparser.js +59 -0
  9. package/dist/semantic/01_check_multiple_declaration.js +19 -0
  10. package/dist/semantic/02_give_struct_and_enum_names.js +58 -0
  11. package/dist/semantic/03_match_type_definitions.js +15 -0
  12. package/dist/semantic/04_check_dont_return_secret.js +39 -0
  13. package/dist/semantic/05_expand_spreads.js +57 -0
  14. package/dist/semantic/06_check_empty_struct_or_enum.js +18 -0
  15. package/dist/semantic/07_validate_recursive_types.js +46 -0
  16. package/dist/semantic/08_collect_struct_and_enum_types.js +22 -0
  17. package/dist/semantic/09_validate_annotations.js +122 -0
  18. package/dist/semantic/10_check_duplicate_members_on_enum.js +13 -0
  19. package/dist/semantic/analyser.js +27 -0
  20. package/dist/semantic/transformer.js +46 -0
  21. package/dist/semantic/visitor.js +61 -0
  22. package/dist/token.js +93 -0
  23. package/dist/utils.js +25 -0
  24. package/package.json +11 -4
  25. package/.eslintrc.json +0 -3
  26. package/.prettierrc +0 -12
  27. package/dist/spec/lexer.spec.d.ts +0 -1
  28. package/dist/spec/lexer.spec.js +0 -152
  29. package/dist/spec/parser.spec.d.ts +0 -1
  30. package/dist/spec/parser.spec.js +0 -967
  31. package/dist/src/ast.js +0 -355
  32. package/dist/src/compatibility/index.js +0 -211
  33. package/dist/src/helpers.js +0 -23
  34. package/dist/src/index.js +0 -29
  35. package/dist/src/json.js +0 -234
  36. package/dist/src/lexer.js +0 -287
  37. package/dist/src/parser.js +0 -423
  38. package/dist/src/restparser.js +0 -65
  39. package/dist/src/semantic/01_check_multiple_declaration.js +0 -22
  40. package/dist/src/semantic/02_give_struct_and_enum_names.js +0 -61
  41. package/dist/src/semantic/03_match_type_definitions.js +0 -18
  42. package/dist/src/semantic/04_check_dont_return_secret.js +0 -42
  43. package/dist/src/semantic/05_expand_spreads.js +0 -60
  44. package/dist/src/semantic/06_check_empty_struct_or_enum.js +0 -21
  45. package/dist/src/semantic/07_validate_recursive_types.js +0 -49
  46. package/dist/src/semantic/08_collect_struct_and_enum_types.js +0 -25
  47. package/dist/src/semantic/09_validate_annotations.js +0 -125
  48. package/dist/src/semantic/10_check_duplicate_members_on_enum.js +0 -16
  49. package/dist/src/semantic/analyser.js +0 -30
  50. package/dist/src/semantic/transformer.js +0 -50
  51. package/dist/src/semantic/visitor.js +0 -65
  52. package/dist/src/token.js +0 -119
  53. package/dist/src/utils.js +0 -27
  54. package/dist/tsconfig.tsbuildinfo +0 -1
  55. package/src/ast.ts +0 -273
  56. package/src/compatibility/index.ts +0 -273
  57. package/src/helpers.ts +0 -28
  58. package/src/index.ts +0 -8
  59. package/src/json.ts +0 -350
  60. package/src/lexer.ts +0 -343
  61. package/src/parser.ts +0 -604
  62. package/src/restparser.ts +0 -81
  63. package/src/semantic/01_check_multiple_declaration.ts +0 -21
  64. package/src/semantic/02_give_struct_and_enum_names.ts +0 -57
  65. package/src/semantic/03_match_type_definitions.ts +0 -19
  66. package/src/semantic/04_check_dont_return_secret.ts +0 -40
  67. package/src/semantic/05_expand_spreads.ts +0 -67
  68. package/src/semantic/06_check_empty_struct_or_enum.ts +0 -21
  69. package/src/semantic/07_validate_recursive_types.ts +0 -47
  70. package/src/semantic/08_collect_struct_and_enum_types.ts +0 -23
  71. package/src/semantic/09_validate_annotations.ts +0 -158
  72. package/src/semantic/10_check_duplicate_members_on_enum.ts +0 -15
  73. package/src/semantic/analyser.ts +0 -29
  74. package/src/semantic/transformer.ts +0 -41
  75. package/src/semantic/visitor.ts +0 -60
  76. package/src/token.ts +0 -92
  77. package/src/utils.ts +0 -63
  78. /package/dist/{src/ast.d.ts → ast.d.ts} +0 -0
  79. /package/dist/{src/compatibility → compatibility}/index.d.ts +0 -0
  80. /package/dist/{src/helpers.d.ts → helpers.d.ts} +0 -0
  81. /package/dist/{src/index.d.ts → index.d.ts} +0 -0
  82. /package/dist/{src/json.d.ts → json.d.ts} +0 -0
  83. /package/dist/{src/lexer.d.ts → lexer.d.ts} +0 -0
  84. /package/dist/{src/parser.d.ts → parser.d.ts} +0 -0
  85. /package/dist/{src/restparser.d.ts → restparser.d.ts} +0 -0
  86. /package/dist/{src/semantic → semantic}/01_check_multiple_declaration.d.ts +0 -0
  87. /package/dist/{src/semantic → semantic}/02_give_struct_and_enum_names.d.ts +0 -0
  88. /package/dist/{src/semantic → semantic}/03_match_type_definitions.d.ts +0 -0
  89. /package/dist/{src/semantic → semantic}/04_check_dont_return_secret.d.ts +0 -0
  90. /package/dist/{src/semantic → semantic}/05_expand_spreads.d.ts +0 -0
  91. /package/dist/{src/semantic → semantic}/06_check_empty_struct_or_enum.d.ts +0 -0
  92. /package/dist/{src/semantic → semantic}/07_validate_recursive_types.d.ts +0 -0
  93. /package/dist/{src/semantic → semantic}/08_collect_struct_and_enum_types.d.ts +0 -0
  94. /package/dist/{src/semantic → semantic}/09_validate_annotations.d.ts +0 -0
  95. /package/dist/{src/semantic → semantic}/10_check_duplicate_members_on_enum.d.ts +0 -0
  96. /package/dist/{src/semantic → semantic}/analyser.d.ts +0 -0
  97. /package/dist/{src/semantic → semantic}/transformer.d.ts +0 -0
  98. /package/dist/{src/semantic → semantic}/visitor.d.ts +0 -0
  99. /package/dist/{src/token.d.ts → token.d.ts} +0 -0
  100. /package/dist/{src/utils.d.ts → utils.d.ts} +0 -0
@@ -1,57 +0,0 @@
1
- import { Transformer } from "./transformer";
2
- import { SemanticError } from "./visitor";
3
- import type { AstNode } from "../ast";
4
- import { EnumValue, FunctionOperation, EnumType, ErrorNode, Field, StructType, TypeDefinition } from "../ast";
5
-
6
- export class GiveStructAndEnumNamesTransformer extends Transformer {
7
- path: string[] = [];
8
-
9
- names = new Map<string, { type: StructType | EnumType; path: string[] }>();
10
-
11
- transform<T extends AstNode>(node: T): T {
12
- if (node instanceof TypeDefinition) {
13
- this.path = [node.name];
14
- return super.transform(node);
15
- } else if (node instanceof ErrorNode) {
16
- this.path = [`${node.name}Data`];
17
- return super.transform(node);
18
- } else if (node instanceof FunctionOperation) {
19
- this.path = [node.name];
20
- return super.transform(node);
21
- } else if (node instanceof Field) {
22
- try {
23
- this.path.push(node.name);
24
- return super.transform(node);
25
- } finally {
26
- this.path.pop();
27
- }
28
- } else if (node instanceof EnumValue) {
29
- try {
30
- this.path.push(node.value);
31
- return super.transform(node);
32
- } finally {
33
- this.path.pop();
34
- }
35
- } else if (node instanceof StructType || node instanceof EnumType) {
36
- node.name = this.path.map(s => s[0].toUpperCase() + s.slice(1)).join("");
37
- const previous = this.names.get(node.name);
38
-
39
- if (previous) {
40
- if (previous.type.constructor !== node.constructor || JSON.stringify(previous.type) !== JSON.stringify(node)) {
41
- throw new SemanticError(
42
- `The name of the type '${this.path.join(".")}' at ${node.location} will conflict with '${previous.path.join(".")}' at ${
43
- previous.type.location
44
- }`,
45
- );
46
- }
47
-
48
- return previous.type as unknown as T;
49
- }
50
-
51
- this.names.set(node.name, { path: [...this.path], type: node });
52
- return super.transform(node);
53
- } else {
54
- return super.transform(node);
55
- }
56
- }
57
- }
@@ -1,19 +0,0 @@
1
- import { SemanticError, Visitor } from "./visitor";
2
- import type { AstNode } from "../ast";
3
- import { TypeReference } from "../ast";
4
-
5
- export class MatchTypeDefinitionsVisitor extends Visitor {
6
- visit(node: AstNode): void {
7
- if (node instanceof TypeReference) {
8
- const definition = this.root.typeDefinitions.find(t => t.name === node.name);
9
-
10
- if (definition === undefined) {
11
- throw new SemanticError(`Could not find type '${node.name}' at ${node.location}`);
12
- }
13
-
14
- node.type = definition.type;
15
- }
16
-
17
- super.visit(node);
18
- }
19
- }
@@ -1,40 +0,0 @@
1
- import { SemanticError, Visitor } from "./visitor";
2
- import type { AstNode } from "../ast";
3
- import { TypeReference, FunctionOperation, Field } from "../ast";
4
-
5
- export class CheckDontReturnSecretVisitor extends Visitor {
6
- isInReturn = false;
7
-
8
- path: string[] = [];
9
-
10
- visited = new Set<AstNode>();
11
-
12
- visit(node: AstNode): void {
13
- if (this.visited.has(node)) {
14
- return;
15
- }
16
-
17
- this.visited.add(node);
18
-
19
- if (node instanceof FunctionOperation) {
20
- this.isInReturn = true;
21
- this.visited.clear();
22
- this.path.push(`${node.name}(...)`);
23
- this.visit(node.returnType);
24
- this.path.pop();
25
- this.isInReturn = false;
26
- } else if (node instanceof TypeReference) {
27
- this.visit(node.type);
28
- } else if (node instanceof Field) {
29
- this.path.push(node.name);
30
- if (this.isInReturn && node.secret) {
31
- throw new SemanticError(`Can't return a secret value at ${this.path.join(".")} at ${node.location}`);
32
- }
33
-
34
- super.visit(node);
35
- this.path.pop();
36
- } else {
37
- super.visit(node);
38
- }
39
- }
40
- }
@@ -1,67 +0,0 @@
1
- import { SemanticError, Visitor } from "./visitor";
2
- import type { Spread } from "..";
3
- import type { AstNode } from "../ast";
4
- import { FunctionOperation, Field, StructType } from "../ast";
5
-
6
- export class ExpandSpreadsVisitor extends Visitor {
7
- /*
8
- * Here we may visit the same struct multiple times
9
- * We must make sure we only process each one once
10
- */
11
- processed = new Set<StructType>();
12
-
13
- expandSpread(fields: Field[], fieldsAndSpreads: Array<Field | Spread>) {
14
- const fieldIndex = new Map<string, number>();
15
-
16
- for (const fieldOrSpread of fieldsAndSpreads) {
17
- let fieldsToAdd: Field[];
18
-
19
- if (fieldOrSpread instanceof Field) {
20
- fieldsToAdd = [fieldOrSpread];
21
- } else {
22
- const struct = fieldOrSpread.typeReference.type;
23
-
24
- if (!(struct instanceof StructType)) {
25
- throw new SemanticError(
26
- `A spread operator can't refer to something that is not a struct, in '${fieldOrSpread.typeReference.name}' at ${fieldOrSpread.location}.`,
27
- );
28
- }
29
-
30
- this.visit(struct); // Recursion!
31
-
32
- fieldsToAdd = struct.fields;
33
- }
34
-
35
- for (const field of fieldsToAdd) {
36
- const index = fieldIndex.get(field.name);
37
-
38
- if (index) {
39
- fields[index] = field;
40
- } else {
41
- fieldIndex.set(field.name, fields.length);
42
- fields.push(field);
43
- }
44
- }
45
- }
46
- }
47
-
48
- visit(node: AstNode): void {
49
- if (node instanceof StructType) {
50
- if (this.processed.has(node)) {
51
- return;
52
- }
53
-
54
- this.processed.add(node);
55
- }
56
-
57
- super.visit(node);
58
-
59
- if (node instanceof StructType) {
60
- this.expandSpread(node.fields, node.fieldsAndSpreads);
61
- node.fieldsAndSpreads = [];
62
- } else if (node instanceof FunctionOperation) {
63
- this.expandSpread(node.args, node.fieldsAndSpreads);
64
- node.fieldsAndSpreads = [];
65
- }
66
- }
67
- }
@@ -1,21 +0,0 @@
1
- import { SemanticError, Visitor } from "./visitor";
2
- import type { AstNode } from "../ast";
3
- import { EnumType, StructType } from "../ast";
4
-
5
- export class CheckEmptyStructOrEnumVisitor extends Visitor {
6
- visit(node: AstNode): void {
7
- super.visit(node);
8
-
9
- if (node instanceof EnumType) {
10
- if (node.values.length === 0) {
11
- throw new SemanticError(`Enum '${node.name}' at ${node.location} is empty`);
12
- }
13
- }
14
-
15
- if (node instanceof StructType) {
16
- if (node.fields.length === 0) {
17
- throw new SemanticError(`Struct '${node.name}' at ${node.location} is empty`);
18
- }
19
- }
20
- }
21
- }
@@ -1,47 +0,0 @@
1
- import { SemanticError, Visitor } from "./visitor";
2
- import type { Type, AstNode } from "../ast";
3
- import { EnumType, StructType, ArrayType, OptionalType, TypeDefinition, TypeReference } from "../ast";
4
-
5
- export class ValidateRecursiveTypes extends Visitor {
6
- visit(node: AstNode): void {
7
- super.visit(node);
8
-
9
- if (node instanceof TypeDefinition && this.isRecursiveType(node, node.type)) {
10
- if (!(node.type instanceof StructType || node.type instanceof EnumType)) {
11
- throw new SemanticError(`Type '${node.name}' at ${node.location} is recursive but is not an struct`);
12
- }
13
-
14
- if (this.isInfinitelyRecursiveType(node, node.type)) {
15
- throw new SemanticError(`Type '${node.name}' at ${node.location} is infinitely recursive`);
16
- }
17
- }
18
- }
19
-
20
- isRecursiveType(node: TypeDefinition, type: Type): boolean {
21
- if (type instanceof TypeReference) {
22
- return node.name === type.name;
23
- } else if (type instanceof ArrayType) {
24
- return this.isRecursiveType(node, type.base);
25
- } else if (type instanceof OptionalType) {
26
- return this.isRecursiveType(node, type.base);
27
- } else if (type instanceof StructType) {
28
- return type.fields.some(field => this.isRecursiveType(node, field.type));
29
- } else if (type instanceof EnumType) {
30
- return type.values.some(value => value.struct && this.isRecursiveType(node, value.struct));
31
- }
32
-
33
- return false;
34
- }
35
-
36
- isInfinitelyRecursiveType(node: TypeDefinition, type: Type): boolean {
37
- if (type instanceof TypeReference) {
38
- return type.name === node.name;
39
- } else if (type instanceof StructType) {
40
- return type.fields.some(field => this.isInfinitelyRecursiveType(node, field.type));
41
- } else if (type instanceof EnumType) {
42
- return type.values.every(value => value.struct && this.isInfinitelyRecursiveType(node, value.struct));
43
- }
44
-
45
- return false;
46
- }
47
- }
@@ -1,23 +0,0 @@
1
- import { Visitor } from "./visitor";
2
- import type { AstNode } from "../ast";
3
- import { EnumType, StructType } from "../ast";
4
-
5
- export class CollectStructAndEnumTypesVisitor extends Visitor {
6
- visited = new Set<AstNode>();
7
-
8
- visit(node: AstNode): void {
9
- super.visit(node);
10
-
11
- if (this.visited.has(node)) {
12
- return;
13
- }
14
-
15
- this.visited.add(node);
16
-
17
- if (node instanceof StructType) {
18
- this.root.structTypes.push(node);
19
- } else if (node instanceof EnumType) {
20
- this.root.enumTypes.push(node);
21
- }
22
- }
23
- }
@@ -1,158 +0,0 @@
1
- import { SemanticError, Visitor } from "./visitor";
2
- import type { AstNode, Type } from "../ast";
3
- import {
4
- ErrorNode,
5
- StatusCodeAnnotation,
6
- DecimalPrimitiveType,
7
- FunctionOperation,
8
- Base64PrimitiveType,
9
- BigIntPrimitiveType,
10
- BoolPrimitiveType,
11
- CnpjPrimitiveType,
12
- CpfPrimitiveType,
13
- DatePrimitiveType,
14
- DateTimePrimitiveType,
15
- DescriptionAnnotation,
16
- EnumType,
17
- EnumValue,
18
- Field,
19
- FloatPrimitiveType,
20
- HexPrimitiveType,
21
- HiddenAnnotation,
22
- IntPrimitiveType,
23
- MoneyPrimitiveType,
24
- OptionalType,
25
- RestAnnotation,
26
- StringPrimitiveType,
27
- ThrowsAnnotation,
28
- TypeDefinition,
29
- TypeReference,
30
- UIntPrimitiveType,
31
- UuidPrimitiveType,
32
- VoidPrimitiveType,
33
- } from "../ast";
34
-
35
- function isRestEncodable(type: Type) {
36
- return (
37
- type instanceof BoolPrimitiveType ||
38
- type instanceof IntPrimitiveType ||
39
- type instanceof UIntPrimitiveType ||
40
- type instanceof BigIntPrimitiveType ||
41
- type instanceof FloatPrimitiveType ||
42
- type instanceof StringPrimitiveType ||
43
- type instanceof DatePrimitiveType ||
44
- type instanceof DateTimePrimitiveType ||
45
- type instanceof MoneyPrimitiveType ||
46
- type instanceof DecimalPrimitiveType ||
47
- type instanceof CpfPrimitiveType ||
48
- type instanceof CnpjPrimitiveType ||
49
- type instanceof UuidPrimitiveType ||
50
- type instanceof HexPrimitiveType ||
51
- type instanceof Base64PrimitiveType ||
52
- type instanceof EnumType
53
- );
54
- }
55
-
56
- function extractRealType(type: Type): Type {
57
- return type instanceof TypeReference ? extractRealType(type.type) : type;
58
- }
59
-
60
- export class ValidateAnnotationsVisitor extends Visitor {
61
- visit(node: AstNode): void {
62
- if (node instanceof EnumValue) {
63
- for (const annotation of node.annotations) {
64
- if (annotation instanceof DescriptionAnnotation) {
65
- // Ok
66
- } else {
67
- throw new SemanticError(`Cannot have @${annotation.constructor.name.replace("Annotation", "").toLowerCase()} at ${annotation.location}`);
68
- }
69
- }
70
- } else if (node instanceof TypeDefinition) {
71
- for (const annotation of node.annotations) {
72
- if (annotation instanceof DescriptionAnnotation) {
73
- // Ok
74
- } else {
75
- throw new SemanticError(`Cannot have @${annotation.constructor.name.replace("Annotation", "").toLowerCase()} at ${annotation.location}`);
76
- }
77
- }
78
- } else if (node instanceof Field) {
79
- for (const annotation of node.annotations) {
80
- if (annotation instanceof DescriptionAnnotation) {
81
- // Ok
82
- } else {
83
- throw new SemanticError(`Cannot have @${annotation.constructor.name.replace("Annotation", "").toLowerCase()} at ${annotation.location}`);
84
- }
85
- }
86
- } else if (node instanceof FunctionOperation) {
87
- for (const annotation of node.annotations) {
88
- if (annotation instanceof DescriptionAnnotation) {
89
- // Ok
90
- } else if (annotation instanceof ThrowsAnnotation) {
91
- if (!this.root.errors.some(error => error.name === annotation.error)) {
92
- throw new SemanticError(`Unknown error type '${annotation.error}' at ${annotation.location}`);
93
- }
94
- } else if (annotation instanceof RestAnnotation) {
95
- const allVariables = [...annotation.pathVariables, ...annotation.queryVariables, ...annotation.headers.values()];
96
-
97
- if (allVariables.length !== new Set(allVariables).size) {
98
- throw new SemanticError(`Arguments must appear only once for rest annotation at ${annotation.location}`);
99
- }
100
-
101
- for (const name of allVariables) {
102
- const arg = node.args.find(x => x.name === name);
103
-
104
- if (!arg) {
105
- throw new SemanticError(`Argument '${name}' not found at ${annotation.location}`);
106
- }
107
-
108
- if (annotation.pathVariables.includes(name) && arg.type instanceof OptionalType) {
109
- throw new SemanticError(`The path argument '${name}' can't be nullable at ${annotation.location}`);
110
- }
111
-
112
- const baseType = arg.type instanceof OptionalType ? arg.type.base : arg.type;
113
-
114
- if (!isRestEncodable(extractRealType(baseType))) {
115
- throw new SemanticError(`Argument '${name}' can't have type '${arg.type.name}' for rest annotation at ${annotation.location}`);
116
- }
117
- }
118
-
119
- for (const arg of node.args) {
120
- if (!allVariables.includes(arg.name) && annotation.bodyVariable !== arg.name) {
121
- throw new SemanticError(`Argument '${arg.name}' is missing from the rest annotation at ${annotation.location}`);
122
- }
123
-
124
- const queryAndPathVariables = [...annotation.pathVariables, ...annotation.queryVariables];
125
-
126
- if (annotation.method === "GET" && queryAndPathVariables.includes(arg.name) && arg.secret) {
127
- throw new SemanticError(
128
- `Argument marked as secret cannot be used in the path or query parts of a GET endpoint at ${annotation.location}`,
129
- );
130
- }
131
- }
132
-
133
- if (annotation.method === "GET" && node.returnType instanceof VoidPrimitiveType) {
134
- throw new SemanticError(`A GET rest endpoint must return something at ${annotation.location}`);
135
- }
136
- } else if (annotation instanceof HiddenAnnotation) {
137
- // Ok
138
- } else {
139
- throw new SemanticError(`Cannot have @${annotation.constructor.name.replace("Annotation", "").toLowerCase()} at ${annotation.location}`);
140
- }
141
- }
142
- } else if (node instanceof ErrorNode) {
143
- for (const annotation of node.annotations) {
144
- if (annotation instanceof StatusCodeAnnotation) {
145
- if (annotation.statusCode < 400 || annotation.statusCode > 599) {
146
- throw new SemanticError(`Error status code can only be 4xx or 5xx at ${annotation.location}`);
147
- }
148
- } else {
149
- throw new SemanticError(`Cannot have @${annotation.constructor.name.replace("Annotation", "").toLowerCase()} at ${annotation.location}`);
150
- }
151
- }
152
-
153
- if (node.annotations.filter(ann => ann instanceof StatusCodeAnnotation).length > 1) {
154
- throw new SemanticError(`Can't provide more than one status code at ${node.location}`);
155
- }
156
- }
157
- }
158
- }
@@ -1,15 +0,0 @@
1
- import { SemanticError, Visitor } from "./visitor";
2
- import type { AstNode } from "../ast";
3
- import { EnumType } from "../ast";
4
-
5
- export class CheckDuplicatedMembersOnEnumVisitor extends Visitor {
6
- visit(node: AstNode): void {
7
- super.visit(node);
8
-
9
- if (node instanceof EnumType) {
10
- if (node.values.length !== new Set(node.values.map(x => x.value)).size) {
11
- throw new SemanticError(`Enum '${node.name}' at ${node.location} has duplicated members`);
12
- }
13
- }
14
- }
15
- }
@@ -1,29 +0,0 @@
1
- import { CheckMultipleDeclarationVisitor } from "./01_check_multiple_declaration";
2
- import { GiveStructAndEnumNamesTransformer } from "./02_give_struct_and_enum_names";
3
- import { MatchTypeDefinitionsVisitor } from "./03_match_type_definitions";
4
- import { CheckDontReturnSecretVisitor } from "./04_check_dont_return_secret";
5
- import { ExpandSpreadsVisitor } from "./05_expand_spreads";
6
- import { CheckEmptyStructOrEnumVisitor } from "./06_check_empty_struct_or_enum";
7
- import { ValidateRecursiveTypes } from "./07_validate_recursive_types";
8
- import { CollectStructAndEnumTypesVisitor } from "./08_collect_struct_and_enum_types";
9
- import { ValidateAnnotationsVisitor } from "./09_validate_annotations";
10
- import { CheckDuplicatedMembersOnEnumVisitor } from "./10_check_duplicate_members_on_enum";
11
- import type { AstRoot } from "../ast";
12
- import { ErrorNode, VoidPrimitiveType } from "../ast";
13
-
14
- export function analyse(root: AstRoot): void {
15
- if (!root.errors.some(error => error.name === "Fatal")) {
16
- root.errors.push(new ErrorNode("Fatal", new VoidPrimitiveType()));
17
- }
18
-
19
- new CheckMultipleDeclarationVisitor(root).process();
20
- new GiveStructAndEnumNamesTransformer(root).process();
21
- new MatchTypeDefinitionsVisitor(root).process();
22
- new CheckDontReturnSecretVisitor(root).process();
23
- new ExpandSpreadsVisitor(root).process();
24
- new CheckEmptyStructOrEnumVisitor(root).process();
25
- new ValidateRecursiveTypes(root).process();
26
- new CollectStructAndEnumTypesVisitor(root).process();
27
- new ValidateAnnotationsVisitor(root).process();
28
- new CheckDuplicatedMembersOnEnumVisitor(root).process();
29
- }
@@ -1,41 +0,0 @@
1
- import type { AstNode, AstRoot } from "../ast";
2
- import { EnumValue, EnumType, Spread, FunctionOperation, ArrayType, ErrorNode, Field, OptionalType, StructType, TypeDefinition } from "../ast";
3
-
4
- export class SemanticError extends Error {}
5
-
6
- export abstract class Transformer {
7
- constructor(protected root: AstRoot) {}
8
-
9
- process(): void {
10
- this.root.errors = this.root.errors.map(x => this.transform(x));
11
- this.root.typeDefinitions = this.root.typeDefinitions.map(x => this.transform(x));
12
- this.root.operations = this.root.operations.map(x => this.transform(x));
13
- }
14
-
15
- transform<T extends AstNode>(node: T): T {
16
- if (node instanceof FunctionOperation) {
17
- node.args = node.args.map(x => this.transform(x));
18
- node.fieldsAndSpreads = node.fieldsAndSpreads.map(x => this.transform(x));
19
- node.returnType = this.transform(node.returnType);
20
- } else if (node instanceof Field || node instanceof TypeDefinition) {
21
- node.type = this.transform(node.type);
22
- } else if (node instanceof StructType) {
23
- node.fields = node.fields.map(x => this.transform(x));
24
- node.fieldsAndSpreads = node.fieldsAndSpreads.map(x => this.transform(x));
25
- } else if (node instanceof EnumType) {
26
- node.values = node.values.map(x => this.transform(x));
27
- } else if (node instanceof EnumValue) {
28
- if (node.struct) {
29
- node.struct = this.transform(node.struct);
30
- }
31
- } else if (node instanceof ArrayType || node instanceof OptionalType) {
32
- node.base = this.transform(node.base);
33
- } else if (node instanceof ErrorNode) {
34
- node.dataType = this.transform(node.dataType);
35
- } else if (node instanceof Spread) {
36
- node.typeReference = this.transform(node.typeReference);
37
- }
38
-
39
- return node;
40
- }
41
- }
@@ -1,60 +0,0 @@
1
- import type { AstNode, AstRoot } from "../ast";
2
- import { EnumValue, EnumType, Spread, FunctionOperation, ArrayType, ErrorNode, Field, OptionalType, StructType, TypeDefinition } from "../ast";
3
-
4
- export class SemanticError extends Error {}
5
-
6
- export abstract class Visitor {
7
- constructor(protected root: AstRoot) {}
8
-
9
- process(): void {
10
- for (const error of this.root.errors) {
11
- this.visit(error);
12
- }
13
-
14
- for (const typeDefinition of this.root.typeDefinitions) {
15
- this.visit(typeDefinition);
16
- }
17
-
18
- for (const operation of this.root.operations) {
19
- this.visit(operation);
20
- }
21
- }
22
-
23
- visit(node: AstNode): void {
24
- if (node instanceof FunctionOperation) {
25
- for (const arg of node.args) {
26
- this.visit(arg);
27
- }
28
-
29
- for (const arg of node.fieldsAndSpreads) {
30
- this.visit(arg);
31
- }
32
-
33
- this.visit(node.returnType);
34
- } else if (node instanceof Field || node instanceof TypeDefinition) {
35
- this.visit(node.type);
36
- } else if (node instanceof StructType) {
37
- for (const field of node.fields) {
38
- this.visit(field);
39
- }
40
-
41
- for (const field of node.fieldsAndSpreads) {
42
- this.visit(field);
43
- }
44
- } else if (node instanceof EnumType) {
45
- for (const enumValue of node.values) {
46
- this.visit(enumValue);
47
- }
48
- } else if (node instanceof EnumValue) {
49
- if (node.struct) {
50
- this.visit(node.struct);
51
- }
52
- } else if (node instanceof ArrayType || node instanceof OptionalType) {
53
- this.visit(node.base);
54
- } else if (node instanceof ErrorNode) {
55
- this.visit(node.dataType);
56
- } else if (node instanceof Spread) {
57
- this.visit(node.typeReference);
58
- }
59
- }
60
- }
package/src/token.ts DELETED
@@ -1,92 +0,0 @@
1
- export class TokenLocation {
2
- public filename = "?";
3
-
4
- public line = 0;
5
-
6
- public column = 0;
7
-
8
- toString(): string {
9
- return `${this.filename}:${this.line}:${this.column}`;
10
- }
11
- }
12
-
13
- export class Token {
14
- public location = new TokenLocation();
15
-
16
- constructor(public value = "") {
17
- Object.defineProperty(this, "location", { enumerable: false });
18
- }
19
-
20
- maybeAsIdentifier(): Token {
21
- return this;
22
- }
23
-
24
- toString(): string {
25
- const name = this.constructor.name.replace("Token", "");
26
-
27
- return this.value === "" ? name : `${name}(${JSON.stringify(this.value)})`;
28
- }
29
- }
30
-
31
- export class IdentifierToken extends Token {}
32
- export class StringLiteralToken extends Token {}
33
- export class EqualSymbolToken extends Token {}
34
- export class ExclamationMarkSymbolToken extends Token {}
35
- export class CurlyOpenSymbolToken extends Token {}
36
- export class CurlyCloseSymbolToken extends Token {}
37
- export class ParensOpenSymbolToken extends Token {}
38
- export class ParensCloseSymbolToken extends Token {}
39
- export class ColonSymbolToken extends Token {}
40
- export class OptionalSymbolToken extends Token {}
41
- export class ArraySymbolToken extends Token {}
42
- export class CommaSymbolToken extends Token {}
43
- export class SpreadSymbolToken extends Token {}
44
- export class AnnotationToken extends Token {}
45
-
46
- export class ImportKeywordToken extends Token {
47
- maybeAsIdentifier(): IdentifierToken {
48
- return new IdentifierToken("import");
49
- }
50
- }
51
-
52
- export class TypeKeywordToken extends Token {
53
- maybeAsIdentifier(): IdentifierToken {
54
- return new IdentifierToken("type");
55
- }
56
- }
57
-
58
- export class EnumKeywordToken extends Token {
59
- maybeAsIdentifier(): IdentifierToken {
60
- return new IdentifierToken("enum");
61
- }
62
- }
63
-
64
- export class FnKeywordToken extends Token {
65
- maybeAsIdentifier(): IdentifierToken {
66
- return new IdentifierToken("fn");
67
- }
68
- }
69
-
70
- export class ErrorKeywordToken extends Token {
71
- maybeAsIdentifier(): IdentifierToken {
72
- return new IdentifierToken("error");
73
- }
74
- }
75
-
76
- export class TrueKeywordToken extends Token {
77
- maybeAsIdentifier(): IdentifierToken {
78
- return new IdentifierToken("true");
79
- }
80
- }
81
-
82
- export class FalseKeywordToken extends Token {
83
- maybeAsIdentifier(): IdentifierToken {
84
- return new IdentifierToken("false");
85
- }
86
- }
87
-
88
- export class PrimitiveTypeToken extends Token {
89
- maybeAsIdentifier(): IdentifierToken {
90
- return new IdentifierToken(this.value);
91
- }
92
- }