yukigo-haskell-parser 0.1.0 → 0.1.2

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 (65) hide show
  1. package/.mocharc.json +3 -3
  2. package/CHANGELOG.md +6 -0
  3. package/README.md +10 -10
  4. package/dist/index.d.ts +13 -2
  5. package/dist/index.js +112 -32
  6. package/dist/index.js.map +1 -1
  7. package/dist/parser/grammar.js +220 -231
  8. package/dist/parser/grammar.js.map +1 -1
  9. package/dist/parser/lexer.d.ts +70 -15
  10. package/dist/parser/lexer.js +259 -50
  11. package/dist/parser/lexer.js.map +1 -1
  12. package/dist/prelude.d.ts +1 -1
  13. package/dist/prelude.js +280 -204
  14. package/dist/prelude.js.map +1 -1
  15. package/dist/typechecker/DeclarationCollector.d.ts +6 -1
  16. package/dist/typechecker/DeclarationCollector.js +39 -0
  17. package/dist/typechecker/DeclarationCollector.js.map +1 -1
  18. package/dist/typechecker/TypeBuilder.d.ts +1 -1
  19. package/dist/typechecker/TypeBuilder.js +6 -2
  20. package/dist/typechecker/TypeBuilder.js.map +1 -1
  21. package/dist/typechecker/checker.d.ts +15 -4
  22. package/dist/typechecker/checker.js +108 -25
  23. package/dist/typechecker/checker.js.map +1 -1
  24. package/dist/typechecker/core.d.ts +4 -0
  25. package/dist/typechecker/core.js +45 -19
  26. package/dist/typechecker/core.js.map +1 -1
  27. package/dist/typechecker/inference.d.ts +5 -1
  28. package/dist/typechecker/inference.js +95 -21
  29. package/dist/typechecker/inference.js.map +1 -1
  30. package/dist/utils/helpers.d.ts +1 -1
  31. package/dist/utils/helpers.js +1 -1
  32. package/dist/utils/helpers.js.map +1 -1
  33. package/dist/utils/types.d.ts +1 -1
  34. package/dist/utils/types.js +7 -0
  35. package/dist/utils/types.js.map +1 -1
  36. package/package.json +3 -3
  37. package/src/index.ts +192 -55
  38. package/src/parser/grammar.ne +519 -463
  39. package/src/parser/grammar.ts +230 -239
  40. package/src/parser/lexer.ts +353 -77
  41. package/src/prelude.ts +282 -206
  42. package/src/typechecker/DeclarationCollector.ts +157 -104
  43. package/src/typechecker/TypeBuilder.ts +150 -148
  44. package/src/typechecker/checker.ts +501 -395
  45. package/src/typechecker/core.ts +192 -162
  46. package/src/typechecker/inference.ts +1421 -1327
  47. package/src/utils/helpers.ts +29 -29
  48. package/src/utils/types.ts +63 -56
  49. package/tests/hspec.spec.ts +92 -0
  50. package/tests/lexer.spec.ts +175 -0
  51. package/tests/parser.spec.ts +829 -823
  52. package/tests/prelude.spec.ts +17 -22
  53. package/tests/typechecker.spec.ts +327 -310
  54. package/tsconfig.json +26 -17
  55. package/tsconfig.tsbuildinfo +1 -0
  56. package/dist/parser/layoutPreprocessor.d.ts +0 -1
  57. package/dist/parser/layoutPreprocessor.js +0 -22
  58. package/dist/parser/layoutPreprocessor.js.map +0 -1
  59. package/dist/parser/preprocessor.d.ts +0 -28
  60. package/dist/parser/preprocessor.js +0 -453
  61. package/dist/parser/preprocessor.js.map +0 -1
  62. package/src/parser/layoutPreprocessor.ts +0 -21
  63. package/src/parser/preprocessor.ne +0 -375
  64. package/src/parser/preprocessor.ts +0 -502
  65. package/tests/preprocessor.spec.ts +0 -69
package/src/index.ts CHANGED
@@ -1,55 +1,192 @@
1
- import grammar from "./parser/grammar.js";
2
- import nearley from "nearley";
3
- import { groupFunctionDeclarations } from "./utils/helpers.js";
4
- import { TypeChecker } from "./typechecker/checker.js";
5
- import { AST, YukigoParser } from "yukigo-ast";
6
- import { inspect } from "util";
7
- import { preprocessor } from "./parser/layoutPreprocessor.js";
8
- import { preludeCode } from "./prelude.js";
9
-
10
- export class YukigoHaskellParser implements YukigoParser {
11
- public errors: string[] = [];
12
- private prelude: string;
13
- constructor(prelude?: string) {
14
- this.errors = [];
15
- this.prelude = prelude ?? preludeCode;
16
- }
17
-
18
- public parse(code: string): AST {
19
- const processedCode = preprocessor(code);
20
- const parser = new nearley.Parser(nearley.Grammar.fromCompiled(grammar));
21
- try {
22
- parser.feed(this.prelude + "\n\n" + processedCode);
23
- parser.finish();
24
- } catch (error) {
25
- console.log(error)
26
- if ("token" in error) {
27
- const token = error.token;
28
- const message = `Parser: Unexpected '${token.type}' token '${token.value}' at line ${token.line} col ${token.col}.`;
29
- this.errors.push(message);
30
- throw Error(message);
31
- }
32
- throw error;
33
- }
34
- if (parser.results.length > 1) {
35
- const msg = `Parser: Too much ambiguity. ${parser.results.length} ASTs parsed. Output not generated.`;
36
- this.errors.push(msg);
37
- throw Error(msg);
38
- }
39
- if (parser.results.length == 0) {
40
- this.errors.push("Parser did not generate an AST.");
41
- throw Error("Parser did not generate an AST.");
42
- }
43
- const ast = groupFunctionDeclarations(parser.results[0]);
44
- try {
45
- const typeChecker = new TypeChecker();
46
- const errors = typeChecker.check(ast);
47
- if (errors.length > 0) {
48
- this.errors.push(...errors);
49
- }
50
- } catch (error) {
51
- console.log(error);
52
- }
53
- return ast;
54
- }
55
- }
1
+ import grammar from "./parser/grammar.js";
2
+ import nearley from "nearley";
3
+ import { groupFunctionDeclarations } from "./utils/helpers.js";
4
+ import { TypeChecker } from "./typechecker/checker.js";
5
+ import {
6
+ ArithmeticUnaryOperation,
7
+ AST,
8
+ Equation,
9
+ Expression,
10
+ Function,
11
+ Instance,
12
+ ListType,
13
+ Return,
14
+ Sequence,
15
+ SimpleType,
16
+ StringOperation,
17
+ StringPrimitive,
18
+ SymbolPrimitive,
19
+ TypePattern,
20
+ UnguardedBody,
21
+ VariablePattern,
22
+ YukigoParser,
23
+ } from "yukigo-ast";
24
+ import { preludeCode } from "./prelude.js";
25
+ import { typeMappings } from "./utils/types.js";
26
+ import { Token } from "moo";
27
+
28
+ class UnexpectedToken extends Error {
29
+ constructor(token: Token) {
30
+ super(
31
+ `Parser: Unexpected '${token.type}' token '${token.value}' at line ${token.line} col ${token.col}.`,
32
+ );
33
+ }
34
+ }
35
+ class AmbiguityError extends Error {
36
+ constructor(amountAST: number) {
37
+ super(
38
+ `Parser: Too much ambiguity. ${amountAST} ASTs parsed. Output not generated.`,
39
+ );
40
+ }
41
+ }
42
+ class TypeError extends Error {
43
+ constructor(errors: string[]) {
44
+ super(`Found type errors.\n\t-${errors.join("\n\t-")}`);
45
+ }
46
+ }
47
+
48
+ export type HaskellConfig = {
49
+ typecheck: boolean;
50
+ includePrims: boolean;
51
+ };
52
+
53
+ const HaskellDefaultConfig = {
54
+ typecheck: true,
55
+ includePrims: true,
56
+ };
57
+
58
+ export class YukigoHaskellParser implements YukigoParser {
59
+ public errors: string[] = [];
60
+ private prelude: AST;
61
+ private config: HaskellConfig;
62
+ private checker?: TypeChecker;
63
+
64
+ constructor(
65
+ prelude: string = preludeCode,
66
+ config: HaskellConfig = HaskellDefaultConfig,
67
+ ) {
68
+ this.errors = [];
69
+ this.prelude = this.feedParser(prelude);
70
+ this.config = config;
71
+ }
72
+
73
+ private preprocessor(code: string): string {
74
+ return code.replace(/Exception\.evaluate/g, "evaluate");
75
+ }
76
+
77
+ public parse(code: string): AST {
78
+ const processedCode = this.preprocessor(code);
79
+ const result = this.feedParser(processedCode);
80
+ const fullAst = this.prelude.concat(result);
81
+
82
+ const makePrim = (name: string) => new Function(new SymbolPrimitive(name), [
83
+ new Equation(
84
+ [new VariablePattern(new SymbolPrimitive("x"))],
85
+ new UnguardedBody(new Sequence([new Return(new ArithmeticUnaryOperation("ToString", new SymbolPrimitive("x")))]))
86
+ )
87
+ ]);
88
+
89
+ const prims = [
90
+ makePrim("primShow"),
91
+ makePrim("primShowChar"),
92
+ makePrim("primShowString"),
93
+ makePrim("primShowList")
94
+ ];
95
+
96
+ const resolveYukigoType = (t: any): any => {
97
+ if (t instanceof SimpleType) {
98
+ const mapped = typeMappings[t.value];
99
+ if (mapped) return new SimpleType(mapped, t.constraints, t.loc);
100
+ return t;
101
+ }
102
+ if (t instanceof ListType) return t;
103
+ return t;
104
+ };
105
+
106
+ const primShowString = new Function(new SymbolPrimitive("primShowString"), [
107
+ new Equation(
108
+ [new VariablePattern(new SymbolPrimitive("s"))],
109
+ new UnguardedBody(new Sequence([
110
+ new Return(
111
+ new StringOperation(
112
+ "Concat",
113
+ new StringPrimitive("\""),
114
+ new StringOperation("Concat", new SymbolPrimitive("s"), new StringPrimitive("\""))
115
+ )
116
+ )
117
+ ]))
118
+ )
119
+ ]);
120
+
121
+ // Transform Instance nodes into Function nodes with TypePatterns
122
+ const transformedAst: AST = this.config.includePrims ? [...prims, primShowString] : [];
123
+ for (const node of fullAst) {
124
+ if (node instanceof Instance) {
125
+ const instanceNode = node as Instance;
126
+ const yukigoType = resolveYukigoType(instanceNode.type);
127
+
128
+ for (const func of instanceNode.functions) {
129
+ const overloadedEquations = func.equations.map((eq) => {
130
+ const firstPattern = eq.patterns[0];
131
+ const typePattern = new TypePattern(
132
+ yukigoType,
133
+ firstPattern
134
+ );
135
+ return new Equation(
136
+ [typePattern, ...eq.patterns.slice(1)],
137
+ eq.body,
138
+ eq.returnExpr,
139
+ eq.loc
140
+ );
141
+ });
142
+ transformedAst.unshift(
143
+ new Function(func.identifier, overloadedEquations, func.loc)
144
+ );
145
+ }
146
+ } else {
147
+ transformedAst.push(node);
148
+ }
149
+ }
150
+
151
+ const ast = groupFunctionDeclarations(transformedAst);
152
+ if (this.config.typecheck) {
153
+ this.checker = new TypeChecker();
154
+ const errors = this.checker.check(ast);
155
+ if (errors.length > 0) {
156
+ this.errors.push(...errors);
157
+ throw new TypeError(errors);
158
+ }
159
+ }
160
+ return ast;
161
+ }
162
+ public parseExpression(code: string): Expression {
163
+ const processedCode = this.preprocessor(code);
164
+ const expr = this.feedParser(processedCode)[0];
165
+ return expr;
166
+ }
167
+ private feedParser(code: string): any {
168
+ const parser = new nearley.Parser(nearley.Grammar.fromCompiled(grammar));
169
+ try {
170
+ parser.feed(code);
171
+ parser.finish();
172
+ } catch (error) {
173
+ if ("token" in error && error.token) throw new UnexpectedToken(error.token);
174
+ throw error;
175
+ }
176
+ const { results } = parser;
177
+ if (results.length > 1) throw new AmbiguityError(results.length);
178
+ if (results.length == 0) return [];
179
+ return results[0];
180
+ }
181
+ public typeOfExpression(code: string): string {
182
+ if (!this.config.typecheck || !this.checker)
183
+ throw new Error("Type checking not initialized. Did you load a file?");
184
+
185
+ const expr = this.parseExpression(code);
186
+ return this.checker.inferExpression(expr);
187
+ }
188
+ public getKnownSymbols(): string[] {
189
+ if (!this.config.typecheck || !this.checker) return [];
190
+ return this.checker.getKnownSymbols();
191
+ }
192
+ }