@katnip-org/compiler 0.1.0

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 (70) hide show
  1. package/LICENSE +202 -0
  2. package/NOTICE +5 -0
  3. package/README.md +42 -0
  4. package/build/cli.d.ts +2 -0
  5. package/build/cli.js +141 -0
  6. package/build/cli.js.map +1 -0
  7. package/build/codegen/SB3Generator.d.ts +1 -0
  8. package/build/codegen/SB3Generator.js +2 -0
  9. package/build/codegen/SB3Generator.js.map +1 -0
  10. package/build/index.d.ts +3 -0
  11. package/build/index.js +26 -0
  12. package/build/index.js.map +1 -0
  13. package/build/ir/IRGenerator.d.ts +1 -0
  14. package/build/ir/IRGenerator.js +2 -0
  15. package/build/ir/IRGenerator.js.map +1 -0
  16. package/build/ir/IRNode.d.ts +1 -0
  17. package/build/ir/IRNode.js +2 -0
  18. package/build/ir/IRNode.js.map +1 -0
  19. package/build/lexer/Lexer.d.ts +69 -0
  20. package/build/lexer/Lexer.js +441 -0
  21. package/build/lexer/Lexer.js.map +1 -0
  22. package/build/lexer/LexerState.d.ts +14 -0
  23. package/build/lexer/LexerState.js +16 -0
  24. package/build/lexer/LexerState.js.map +1 -0
  25. package/build/lexer/Token.d.ts +105 -0
  26. package/build/lexer/Token.js +114 -0
  27. package/build/lexer/Token.js.map +1 -0
  28. package/build/parser/AST-nodes.d.ts +230 -0
  29. package/build/parser/AST-nodes.js +10 -0
  30. package/build/parser/AST-nodes.js.map +1 -0
  31. package/build/parser/BindingPowerTable.d.ts +8 -0
  32. package/build/parser/BindingPowerTable.js +37 -0
  33. package/build/parser/BindingPowerTable.js.map +1 -0
  34. package/build/parser/Parser.d.ts +208 -0
  35. package/build/parser/Parser.js +1215 -0
  36. package/build/parser/Parser.js.map +1 -0
  37. package/build/semantic/InternalTypes.d.ts +43 -0
  38. package/build/semantic/InternalTypes.js +95 -0
  39. package/build/semantic/InternalTypes.js.map +1 -0
  40. package/build/semantic/SemanticAnalyzer.d.ts +109 -0
  41. package/build/semantic/SemanticAnalyzer.js +998 -0
  42. package/build/semantic/SemanticAnalyzer.js.map +1 -0
  43. package/build/semantic/StdlibLoader.d.ts +18 -0
  44. package/build/semantic/StdlibLoader.js +42 -0
  45. package/build/semantic/StdlibLoader.js.map +1 -0
  46. package/build/semantic/SymbolTable.d.ts +64 -0
  47. package/build/semantic/SymbolTable.js +28 -0
  48. package/build/semantic/SymbolTable.js.map +1 -0
  49. package/build/utils/ErrorReporter.d.ts +29 -0
  50. package/build/utils/ErrorReporter.js +84 -0
  51. package/build/utils/ErrorReporter.js.map +1 -0
  52. package/build/utils/Logger.d.ts +32 -0
  53. package/build/utils/Logger.js +62 -0
  54. package/build/utils/Logger.js.map +1 -0
  55. package/build/utils/colors.d.ts +8 -0
  56. package/build/utils/colors.js +14 -0
  57. package/build/utils/colors.js.map +1 -0
  58. package/package.json +58 -0
  59. package/stdlib/clone.knip +5 -0
  60. package/stdlib/console.knip +6 -0
  61. package/stdlib/control.knip +3 -0
  62. package/stdlib/dict.knip +7 -0
  63. package/stdlib/events.knip +3 -0
  64. package/stdlib/list.knip +10 -0
  65. package/stdlib/looks.knip +4 -0
  66. package/stdlib/math.knip +7 -0
  67. package/stdlib/motion.knip +25 -0
  68. package/stdlib/pen.knip +14 -0
  69. package/stdlib/prelude.knip +25 -0
  70. package/stdlib/str.knip +3 -0
@@ -0,0 +1,114 @@
1
+ /**
2
+ * @fileoverview Token definitions for the Katnip lexer.
3
+ */
4
+ // Valued token types
5
+ export const ValuedTokenType = {
6
+ // Literals
7
+ String: "String",
8
+ InterpolatedString: "InterpolatedString",
9
+ InterpolatedStringEnd: "InterpolatedStringEnd",
10
+ Number: "Number",
11
+ // Comments
12
+ Comment_SingleExpanded: "Comment_SingleExpanded",
13
+ Comment_SingleCollapsed: "Comment_SingleCollapsed",
14
+ Comment_SingleIgnored: "Comment_SingleIgnored",
15
+ Comment_MultilineExpanded: "Comment_MultilineExpanded",
16
+ Comment_MultilineCollapsed: "Comment_MultilineCollapsed",
17
+ Comment_MultilineIgnored: "Comment_MultilineIgnored",
18
+ // Misc
19
+ Identifier: "Identifier",
20
+ // Used for error handling, not a real token type
21
+ ErrorToken: "ErrorToken",
22
+ };
23
+ // Unit token types
24
+ export const UnitTokenType = {
25
+ // Grouping
26
+ BracketOpen: "[",
27
+ BracketClose: "]",
28
+ BraceOpen: "{",
29
+ BraceClose: "}",
30
+ ParenOpen: "(",
31
+ ParenClose: ")",
32
+ // Punctuation
33
+ Dot: ".",
34
+ Comma: ",",
35
+ Colon: ":",
36
+ Semicolon: ";",
37
+ AtSymbol: "@",
38
+ // Misc punctuation
39
+ Pound: "#",
40
+ QuestionMark: "?",
41
+ // Operators
42
+ Plus: "+",
43
+ Minus: "-",
44
+ Asterisk: "*",
45
+ FwdSlash: "/",
46
+ Percent: "%",
47
+ Caret: "^",
48
+ Exclamation: "!",
49
+ Ampersand: "&",
50
+ Pipe: "|",
51
+ LeftChevron: "<",
52
+ RightChevron: ">",
53
+ Equals: "=",
54
+ // Double-character operators
55
+ Power: "**",
56
+ EqualsTo: "==",
57
+ LessThanOrEqualsTo: "<=",
58
+ GreaterThanOrEqualsTo: ">=",
59
+ AND: "&&",
60
+ OR: "||",
61
+ NAND: "!&",
62
+ NOR: "!|",
63
+ XNOR: "!^",
64
+ // Assignment operators
65
+ PlusEquals: "+=",
66
+ MinusEquals: "-=",
67
+ AsteriskEquals: "*=",
68
+ PowerEquals: "**=",
69
+ FwdSlashEquals: "/=",
70
+ PercentEquals: "%=",
71
+ // Special
72
+ FunctionReturn: "->",
73
+ Newline: "\n",
74
+ EOF: "<EOF>",
75
+ };
76
+ export const unitTokenByLexeme = new Map(Object.entries(UnitTokenType).map(([_, value]) => [value, value]));
77
+ export const singleCharUnitTokens = new Set(Object.values(UnitTokenType).filter(t => t.length === 1));
78
+ export class OperatorTrieNode {
79
+ children = new Map();
80
+ tokenType = null;
81
+ }
82
+ export class OperatorTrie {
83
+ root = new OperatorTrieNode();
84
+ constructor(operators) {
85
+ for (const op of operators) {
86
+ this.insert(op);
87
+ }
88
+ }
89
+ insert(op) {
90
+ let node = this.root;
91
+ for (const char of op) {
92
+ let next = node.children.get(char);
93
+ if (!next) {
94
+ next = new OperatorTrieNode();
95
+ node.children.set(char, next);
96
+ }
97
+ node = next;
98
+ }
99
+ node.tokenType = op;
100
+ }
101
+ start() { return this.root; }
102
+ step(node, char) { return node.children.get(char) ?? null; }
103
+ }
104
+ export const operatorTrie = new OperatorTrie(Object.values(UnitTokenType));
105
+ // Type guards
106
+ const valuedTokenTypeSet = new Set(Object.values(ValuedTokenType));
107
+ const unitTokenTypeSet = new Set(Object.values(UnitTokenType));
108
+ export function isValuedTokenType(type) {
109
+ return valuedTokenTypeSet.has(type);
110
+ }
111
+ export function isUnitTokenType(type) {
112
+ return unitTokenTypeSet.has(type);
113
+ }
114
+ //# sourceMappingURL=Token.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Token.js","sourceRoot":"","sources":["../../src/lexer/Token.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,qBAAqB;AACrB,MAAM,CAAC,MAAM,eAAe,GAAG;IAC3B,WAAW;IACX,MAAM,EAAE,QAAQ;IAChB,kBAAkB,EAAE,oBAAoB;IACxC,qBAAqB,EAAE,uBAAuB;IAC9C,MAAM,EAAE,QAAQ;IAEhB,WAAW;IACX,sBAAsB,EAAE,wBAAwB;IAChD,uBAAuB,EAAE,yBAAyB;IAClD,qBAAqB,EAAE,uBAAuB;IAE9C,yBAAyB,EAAE,2BAA2B;IACtD,0BAA0B,EAAE,4BAA4B;IACxD,wBAAwB,EAAE,0BAA0B;IAEpD,OAAO;IACP,UAAU,EAAE,YAAY;IAExB,iDAAiD;IACjD,UAAU,EAAE,YAAY;CAClB,CAAC;AAKX,mBAAmB;AACnB,MAAM,CAAC,MAAM,aAAa,GAAG;IACzB,WAAW;IACX,WAAW,EAAE,GAAG;IAChB,YAAY,EAAE,GAAG;IACjB,SAAS,EAA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Y,CAAC,MAAM,CAAC,MAAM,CAAC,aAAa,CAAC,CAAC,CAAC;AA6B3E,cAAc;AACd,MAAM,kBAAkB,GAAG,IAAI,GAAG,CAC9B,MAAM,CAAC,MAAM,CAAC,eAAe,CAAC,CACjC,CAAC;AAEF,MAAM,gBAAgB,GAAG,IAAI,GAAG,CAC5B,MAAM,CAAC,MAAM,CAAC,aAAa,CAAC,CAC/B,CAAC;AAEF,MAAM,UAAU,iBAAiB,CAAC,IAAY;IAC1C,OAAO,kBAAkB,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;AACxC,CAAC;AAED,MAAM,UAAU,eAAe,CAAC,IAAY;IACxC,OAAO,gBAAgB,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;AACtC,CAAC"}
@@ -0,0 +1,230 @@
1
+ /**
2
+ * @fileoverview Contains all the AST node interfaces for the Katnip parser.
3
+ */
4
+ export interface AST {
5
+ body: StatementNode[];
6
+ }
7
+ export interface NodeBase {
8
+ type: string;
9
+ loc: SourceLocation;
10
+ comment?: CommentNode;
11
+ }
12
+ export interface SourceLocation {
13
+ start: {
14
+ line: number;
15
+ column: number;
16
+ };
17
+ end: {
18
+ line: number;
19
+ column: number;
20
+ };
21
+ }
22
+ export interface CommentNode {
23
+ type: "Comment";
24
+ loc: SourceLocation;
25
+ content: string;
26
+ isExpanded: boolean;
27
+ }
28
+ export interface SingleTypeNode extends NodeBase {
29
+ type: "Type";
30
+ typeName: string;
31
+ typeParams?: TypeNode[];
32
+ }
33
+ export interface UnionTypeNode extends NodeBase {
34
+ type: "UnionType";
35
+ left: TypeNode;
36
+ right: TypeNode;
37
+ }
38
+ export interface TupleTypeNode extends NodeBase {
39
+ type: "TupleType";
40
+ elements: TypeNode[];
41
+ }
42
+ export type TypeNode = SingleTypeNode | UnionTypeNode | TupleTypeNode;
43
+ export interface ProcedureDeclarationNode extends NodeBase {
44
+ type: "ProcedureDeclaration";
45
+ access: VariableDeclarationType;
46
+ name: string;
47
+ decorators: DecoratorNode[];
48
+ parameters: ParameterNode[];
49
+ returnType: TypeNode | null;
50
+ body: BlockNode;
51
+ }
52
+ export interface DecoratorNode extends NodeBase {
53
+ type: "Decorator";
54
+ name: string;
55
+ value: ExpressionNode;
56
+ }
57
+ export interface ParameterNode extends NodeBase {
58
+ type: "Parameter";
59
+ name: string;
60
+ paramType: TypeNode;
61
+ default?: ExpressionNode;
62
+ }
63
+ export interface EnumDeclarationNode extends NodeBase {
64
+ type: "EnumDeclaration";
65
+ access: VariableDeclarationType;
66
+ name: string;
67
+ members: (string | number)[];
68
+ }
69
+ export type StatementNode = ExpressionStatementNode | VariableDeclarationNode | VariableAssignmentNode | HandlerStatementNode | IfStatementNode | WhileStatementNode | DoWhileStatementNode | ForStatementNode | ProcedureDeclarationNode | EnumDeclarationNode | SwitchDeclarationNode | CaseDeclarationNode | DefaultCaseDeclarationNode | SpriteDeclarationNode | ReturnStatementNode | ErrorStatementNode;
70
+ export type StatementNodeType = StatementNode[];
71
+ export interface BlockNode extends NodeBase {
72
+ type: "Block";
73
+ body: StatementNode[];
74
+ }
75
+ export interface BlockStatementBase extends NodeBase {
76
+ body: BlockNode;
77
+ }
78
+ export interface HandlerStatementNode extends BlockStatementBase {
79
+ type: "HandlerStatement";
80
+ call: CallExpressionNode;
81
+ }
82
+ export interface ElifClauseNode extends NodeBase {
83
+ type: "ElifClause";
84
+ condition: ExpressionNode;
85
+ block: BlockNode;
86
+ }
87
+ export interface IfStatementNode extends NodeBase {
88
+ type: "IfStatement";
89
+ condition: ExpressionNode;
90
+ thenBlock: BlockNode;
91
+ elifs: ElifClauseNode[];
92
+ elseBlock: BlockNode | null;
93
+ }
94
+ export interface WhileStatementNode extends BlockStatementBase {
95
+ type: "WhileStatement";
96
+ condition: ExpressionNode;
97
+ }
98
+ export interface DoWhileStatementNode extends BlockStatementBase {
99
+ type: "DoWhileStatement";
100
+ condition: ExpressionNode;
101
+ }
102
+ export interface ForStatementNode extends BlockStatementBase {
103
+ type: "ForStatement";
104
+ pattern: (IdentifierExpressionNode | TupleExpressionNode);
105
+ iterable: ExpressionNode;
106
+ }
107
+ export interface ExpressionStatementNode extends NodeBase {
108
+ type: "ExpressionStatement";
109
+ expression: ExpressionNode;
110
+ }
111
+ export interface ReturnStatementNode extends NodeBase {
112
+ type: "ReturnStatement";
113
+ argument: ExpressionNode | null;
114
+ }
115
+ export declare enum VariableDeclarationType {
116
+ private = "private",
117
+ public = "public",
118
+ temp = "temp"
119
+ }
120
+ export interface VariableDeclarationNode extends NodeBase {
121
+ type: "VariableDeclaration";
122
+ access: VariableDeclarationType;
123
+ name: string;
124
+ varType: TypeNode | null;
125
+ initializer: ExpressionNode | null;
126
+ }
127
+ export interface VariableAssignmentNode extends NodeBase {
128
+ type: "VariableAssignment";
129
+ operator: AssignmentOperator;
130
+ left: ExpressionNode;
131
+ right: ExpressionNode;
132
+ }
133
+ export interface CaseDeclarationNode extends BlockStatementBase {
134
+ type: "CaseDeclaration";
135
+ values: ExpressionNode[];
136
+ }
137
+ export interface DefaultCaseDeclarationNode extends BlockStatementBase {
138
+ type: "DefaultCaseDeclaration";
139
+ }
140
+ export interface SwitchDeclarationNode extends NodeBase {
141
+ type: "SwitchDeclaration";
142
+ value: ExpressionNode;
143
+ body: (CaseDeclarationNode | DefaultCaseDeclarationNode)[];
144
+ }
145
+ export interface SpriteDeclarationNode extends BlockStatementBase {
146
+ type: "SpriteDeclaration";
147
+ name: string;
148
+ }
149
+ export interface ErrorStatementNode extends NodeBase {
150
+ type: "ErrorStatement";
151
+ message: string;
152
+ }
153
+ export type ExpressionNode = IdentifierExpressionNode | LiteralExpressionNode | InterpolatedStringExpressionNode | BinaryExpressionNode | CallExpressionNode | IndexerAccessNode | SliceAccessNode | UnaryExpressionNode | MemberExpressionNode | ListExpressionNode | DictExpressionNode | TupleExpressionNode | EmptyExpressionNode | ErrorExpressionNode;
154
+ export interface IdentifierExpressionNode extends NodeBase {
155
+ type: "Identifier";
156
+ name: string;
157
+ }
158
+ export interface LiteralExpressionNode extends NodeBase {
159
+ type: "Literal";
160
+ value: string | number | boolean | null;
161
+ valueType: "String" | "Number" | "Boolean" | "Null";
162
+ }
163
+ export interface InterpolatedStringExpressionNode extends NodeBase {
164
+ type: "InterpolatedString";
165
+ parts: (string | ExpressionNode)[];
166
+ }
167
+ export type BinaryOperator = "+" | "-" | "*" | "/" | "%" | "**" | "==" | "<" | ">" | "<=" | ">=" | "&&" | "||" | "!&" | "!|" | "!^" | "^";
168
+ export type AssignmentOperator = "=" | "+=" | "-=" | "*=" | "**=" | "/=" | "%=";
169
+ export type UnaryOperator = "!" | "-";
170
+ export interface BinaryExpressionNode extends NodeBase {
171
+ type: "BinaryExpression";
172
+ operator: BinaryOperator;
173
+ left: ExpressionNode;
174
+ right: ExpressionNode;
175
+ }
176
+ export interface NamedArgumentNode extends NodeBase {
177
+ type: "NamedArgument";
178
+ name: string;
179
+ value: ExpressionNode;
180
+ }
181
+ export interface CallExpressionNode extends NodeBase {
182
+ type: "CallExpression";
183
+ object: ExpressionNode;
184
+ arguments: (ExpressionNode | NamedArgumentNode)[];
185
+ }
186
+ export interface IndexerAccessNode extends NodeBase {
187
+ type: "IndexerAccess";
188
+ object: ExpressionNode;
189
+ index: ExpressionNode;
190
+ }
191
+ export interface SliceAccessNode extends NodeBase {
192
+ type: "SliceAccess";
193
+ object: ExpressionNode;
194
+ start: ExpressionNode | null;
195
+ end: ExpressionNode | null;
196
+ step: ExpressionNode | null;
197
+ }
198
+ export interface UnaryExpressionNode extends NodeBase {
199
+ type: "UnaryExpression";
200
+ operator: UnaryOperator;
201
+ argument: ExpressionNode;
202
+ }
203
+ export interface MemberExpressionNode extends NodeBase {
204
+ type: "MemberExpression";
205
+ object: ExpressionNode;
206
+ property: IdentifierExpressionNode;
207
+ }
208
+ export interface ListExpressionNode extends NodeBase {
209
+ type: "ListExpression";
210
+ elements: ExpressionNode[];
211
+ }
212
+ export interface DictEntryNode {
213
+ key: ExpressionNode;
214
+ value: ExpressionNode;
215
+ }
216
+ export interface DictExpressionNode extends NodeBase {
217
+ type: "DictExpression";
218
+ entries: DictEntryNode[];
219
+ }
220
+ export interface TupleExpressionNode extends NodeBase {
221
+ type: "TupleExpression";
222
+ elements: ExpressionNode[];
223
+ }
224
+ export interface EmptyExpressionNode extends NodeBase {
225
+ type: "EmptyExpression";
226
+ }
227
+ export interface ErrorExpressionNode extends NodeBase {
228
+ type: "ErrorToken";
229
+ value: string;
230
+ }
@@ -0,0 +1,10 @@
1
+ /**
2
+ * @fileoverview Contains all the AST node interfaces for the Katnip parser.
3
+ */
4
+ export var VariableDeclarationType;
5
+ (function (VariableDeclarationType) {
6
+ VariableDeclarationType["private"] = "private";
7
+ VariableDeclarationType["public"] = "public";
8
+ VariableDeclarationType["temp"] = "temp";
9
+ })(VariableDeclarationType || (VariableDeclarationType = {}));
10
+ //# sourceMappingURL=AST-nodes.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"AST-nodes.js","sourceRoot":"","sources":["../../src/parser/AST-nodes.ts"],"names":[],"mappings":"AAAA;;GAEG;AAuJH,MAAM,CAAN,IAAY,uBAIX;AAJD,WAAY,uBAAuB;IAC/B,8CAAmB,CAAA;IACnB,4CAAiB,CAAA;IACjB,wCAAa,CAAA;AACjB,CAAC,EAJW,uBAAuB,KAAvB,uBAAuB,QAIlC"}
@@ -0,0 +1,8 @@
1
+ import type { Token } from "../lexer/Token.js";
2
+ interface BindingPower {
3
+ lbp: number;
4
+ rbp: number;
5
+ }
6
+ export declare const bindingPowerTable: Record<string, BindingPower>;
7
+ export declare function getBindingPower(token: Token | null): BindingPower;
8
+ export {};
@@ -0,0 +1,37 @@
1
+ export const bindingPowerTable = {
2
+ // logical operators
3
+ "||": { lbp: 20, rbp: 19 },
4
+ "&&": { lbp: 30, rbp: 29 },
5
+ "!|": { lbp: 25, rbp: 24 },
6
+ "!&": { lbp: 25, rbp: 24 },
7
+ "!^": { lbp: 25, rbp: 24 },
8
+ "^": { lbp: 25, rbp: 24 },
9
+ // comparison operators
10
+ "==": { lbp: 45, rbp: 44 },
11
+ "<": { lbp: 50, rbp: 49 },
12
+ ">": { lbp: 50, rbp: 49 },
13
+ "<=": { lbp: 50, rbp: 49 },
14
+ ">=": { lbp: 50, rbp: 49 },
15
+ // arithmetic operators
16
+ "+": { lbp: 60, rbp: 59 },
17
+ "-": { lbp: 60, rbp: 59 },
18
+ "*": { lbp: 70, rbp: 69 },
19
+ "/": { lbp: 70, rbp: 69 },
20
+ "%": { lbp: 70, rbp: 69 },
21
+ // right associative
22
+ "**": { lbp: 80, rbp: 81 },
23
+ // unary operators
24
+ "!": { lbp: 0, rbp: 79 },
25
+ "UnaryMinus": { lbp: 0, rbp: 79 }, // unary minus
26
+ // access/call
27
+ ".": { lbp: 100, rbp: 100 }, // member access, non-associative
28
+ "(": { lbp: 110, rbp: 110 }, // function call, non-associative
29
+ "[": { lbp: 110, rbp: 110 }, // indexing, non-associative
30
+ };
31
+ export function getBindingPower(token) {
32
+ if (!token)
33
+ return { lbp: 0, rbp: 0 };
34
+ const key = token.token.type;
35
+ return bindingPowerTable[key] || { lbp: 0, rbp: 0 };
36
+ }
37
+ //# sourceMappingURL=BindingPowerTable.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"BindingPowerTable.js","sourceRoot":"","sources":["../../src/parser/BindingPowerTable.ts"],"names":[],"mappings":"AAOA,MAAM,CAAC,MAAM,iBAAiB,GAAiC;IAC7D,oBAAoB;IACpB,IAAI,EAAE,EAAE,GAAG,EAAE,EAAE,EAAE,GAAG,EAAE,EAAE,EAAE;IAC1B,IAAI,EAAE,EAAE,GAAG,EAAE,EAAE,EAAE,GAAG,EAAE,EAAE,EAAE;IAC1B,IAAI,EAAE,EAAE,GAAG,EAAE,EAAE,EAAE,GAAG,EAAE,EAAE,EAAE;IAC1B,IAAI,EAAE,EAAE,GAAG,EAAE,EAAE,EAAE,GAAG,EAAE,EAAE,EAAE;IAC1B,IAAI,EAAE,EAAE,GAAG,EAAE,EAAE,EAAE,GA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AE,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,EAAE,iCAAiC;IAC9D,GAAG,EAAE,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,EAAE,4BAA4B;CAC1D,CAAC;AAEF,MAAM,UAAU,eAAe,CAAC,KAAmB;IAC/C,IAAI,CAAC,KAAK;QAAE,OAAO,EAAE,GAAG,EAAE,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE,CAAC;IAEtC,MAAM,GAAG,GAAG,KAAK,CAAC,KAAK,CAAC,IAAI,CAAC;IAC7B,OAAO,iBAAiB,CAAC,GAAG,CAAC,IAAI,EAAE,GAAG,EAAE,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE,CAAC;AACxD,CAAC"}
@@ -0,0 +1,208 @@
1
+ /**
2
+ * @fileoverview Contains the main parser class for the Katnip compiler.
3
+ */
4
+ import type { Token } from "../lexer/Token.js";
5
+ import { ErrorReporter } from "../utils/ErrorReporter.js";
6
+ import { Logger } from "../utils/Logger.js";
7
+ import { type AST } from "./AST-nodes.js";
8
+ export declare class Parser {
9
+ private reporter;
10
+ private logger;
11
+ private tokens;
12
+ private position;
13
+ constructor(reporter: ErrorReporter, logger?: Logger);
14
+ /**
15
+ * Parses a list of tokens into an Abstract Syntax Tree (AST).
16
+ * @param tokens - The list of tokens to parse.
17
+ * @returns The resulting AST or void if parsing fails.
18
+ */
19
+ parse(tokens: Token[]): AST | void;
20
+ /**
21
+ * Retrieves the current token without advancing the position.
22
+ * @param lookAhead - Number of tokens to look ahead.
23
+ * @returns The current token or null if at the end.
24
+ */
25
+ private peek;
26
+ /**
27
+ * Retrieves the previous token.
28
+ * @returns The previous token or null if at the beginning.
29
+ */
30
+ private previous;
31
+ /**
32
+ * Advances the position and retrieves the previous token.
33
+ * @returns The previous token or null if at the beginning.
34
+ */
35
+ private advance;
36
+ /**
37
+ * Checks if the parser has reached the end of the token list.
38
+ * @returns True if at the end, false otherwise.
39
+ */
40
+ private isAtEnd;
41
+ /**
42
+ * Checks if the current token matches the specified type or value.
43
+ * @param kind - The kind of check ("type" or "value").
44
+ * @param patterns - The patterns to match against.
45
+ * @returns True if the token matches, false otherwise.
46
+ */
47
+ private checkToken;
48
+ /**
49
+ * Attempts to consume the current token if it matches the specified type or value.
50
+ * @param kind - The kind of check ("type" or "value").
51
+ * @param patterns - The patterns to match against.
52
+ * @returns True if the token was consumed, false otherwise.
53
+ */
54
+ private tryConsume;
55
+ /**
56
+ * Consumes the current token if it matches the specified type or value.
57
+ * Reports an error if the token does not match.
58
+ * @param options - The options specifying the type and/or value to match.
59
+ * @param message - The error message to report if the token does not match.
60
+ * @returns The consumed token information or an error token.
61
+ */
62
+ private consume;
63
+ /**
64
+ * Checks whether the current token matches without consuming it.
65
+ * Reports an error if it does not match.
66
+ * @param options - Token type/value options to check.
67
+ * @param message - Error message to report on failure.
68
+ * @returns True when the token matches, false otherwise.
69
+ */
70
+ private expect;
71
+ /**
72
+ * Advances tokens until a matching type/value is found or EOF is reached.
73
+ * Reports an error if the end is reached without finding a match.
74
+ * @param options - Token type/value options to synchronize on.
75
+ * @param message - Error message to report on failure.
76
+ */
77
+ private synchronize;
78
+ /**
79
+ * Consumes a statement-terminating ';'.
80
+ * If it is missing, reports the errorbut does NOT advance.
81
+ * The current token starts the next statement.
82
+ */
83
+ private expectSemicolon;
84
+ /**
85
+ * Parses through and returns a type annotation.
86
+ * @returns The parsed type annotation
87
+ */
88
+ private parseTypeAnnotation;
89
+ /**
90
+ * Parses a union type expression (e.g., A | B).
91
+ * @returns The parsed type node.
92
+ */
93
+ private parseUnionType;
94
+ /**
95
+ * Parses a primary type, including optional generic parameters.
96
+ * @returns The parsed type node.
97
+ */
98
+ private parsePrimaryType;
99
+ /**
100
+ * Parses a tuple type, e.g. (str, num). Used for fixed-shape sequences
101
+ * like the element type of list<(str, num)>.
102
+ * @returns The parsed tuple type node.
103
+ */
104
+ private parseTupleType;
105
+ /**
106
+ * Parses a single statement from the token list.
107
+ * @returns The parsed statement or an error token.
108
+ */
109
+ private parseStatement;
110
+ /**
111
+ * Parses a return statement: `return;` or `return <expression>;`.
112
+ * @returns The parsed return statement node.
113
+ */
114
+ private parseReturnStatement;
115
+ /**
116
+ * Parses an optional leading access modifier (`public`/`private`/`temp`).
117
+ * Defaults to `private`. `start` is the modifier's position (or null) to anchor at loc.
118
+ */
119
+ private parseAccessModifier;
120
+ /**
121
+ * Parses a procedure definition from the token list.
122
+ * @returns The parsed procedure declaration node.
123
+ */
124
+ private parseProcedureDefinition;
125
+ /**
126
+ * Parses an enum definition from the token list.
127
+ * @returns The parsed enum declaration node.
128
+ */
129
+ private parseEnumDefinition;
130
+ /**
131
+ * Parses a variable declaration from the token list.
132
+ * @returns The parsed variable declaration node.
133
+ */
134
+ private parseVariableDeclaration;
135
+ private parseSpriteDefinition;
136
+ /**
137
+ * Parses an assignment expression using a previously parsed left-hand side.
138
+ * @param left - The expression to assign into.
139
+ * @returns The assignment node or null if no assignment operator follows.
140
+ */
141
+ private parseAssignmentExpression;
142
+ /**
143
+ * Parses a handler declaration following a call expression.
144
+ * @param left - The call expression that starts the handler declaration.
145
+ * @returns The handler declaration node or null if not applicable.
146
+ */
147
+ private parseHandlerDeclaration;
148
+ /**
149
+ * Parses a for statement from the token list.
150
+ * @returns The parsed for statement node.
151
+ */
152
+ private parseForStatement;
153
+ /**
154
+ * Parses a while statement from the token list.
155
+ * @returns The parsed while statement node.
156
+ */
157
+ private parseWhileStatement;
158
+ /**
159
+ * Parses a do-while statement from the token list.
160
+ * @returns The parsed do-while statement node.
161
+ */
162
+ private parseDoWhileStatement;
163
+ private parseSwitchStatement;
164
+ private parseCaseStatement;
165
+ private parseDefaultCaseStatement;
166
+ /**
167
+ * Parses an if/elif/else statement from the token list.
168
+ * @returns The parsed if statement node.
169
+ */
170
+ private parseIfStatement;
171
+ /**
172
+ * Parses an expression from the token list.
173
+ * @returns The parsed expression node.
174
+ */
175
+ private parseExpression;
176
+ /**
177
+ * Checks whether a token can begin an expression.
178
+ * @param token - The token to evaluate.
179
+ * @returns True when the token can start an expression, otherwise false.
180
+ */
181
+ private canStartExpression;
182
+ /**
183
+ * Parses an interpolated string expression.
184
+ * @returns The parsed interpolated string expression node.
185
+ */
186
+ private parseInterpolatedString;
187
+ /**
188
+ * Parses the prefix part of an expression.
189
+ * @returns The parsed expression node.
190
+ */
191
+ private parsePrefix;
192
+ /**
193
+ * Parses a single call argument: either a positional expression or a named argument of the form `name = value`.
194
+ * @returns The parsed argument node.
195
+ */
196
+ private parseArgument;
197
+ /**
198
+ * Parses the infix part of an expression.
199
+ * @returns The parsed expression node.
200
+ */
201
+ private parseInfix;
202
+ /**
203
+ * Parses a block expression following a method call.
204
+ * @param allowedStatements - A list of allowed statement types inside this body.
205
+ * @returns The parsed block expression node.
206
+ */
207
+ private parseBlockExpression;
208
+ }