1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const immutable_1 = require("immutable");
|
4 | const lex_1 = require("./lex");
|
5 | const nearley_1 = require("nearley");
|
6 | const restrictsyntax_1 = require("./restrictsyntax");
|
7 | const expressionRules = require("../lib/expression-rules");
|
8 |
|
9 | const programRules = require("../lib/program-rules");
|
10 | function parseExpression(str, options) {
|
11 | const opt = options ? options : {};
|
12 | const parser = new nearley_1.Parser(nearley_1.Grammar.fromCompiled(expressionRules));
|
13 | parser.lexer = new lex_1.TypeLexer(opt.types);
|
14 | parser.feed(str);
|
15 | const parsed = parser.finish();
|
16 | if (parsed.length > 1) {
|
17 | throw new Error("Ambiguous parse!");
|
18 | }
|
19 | else if (parsed.length === 0) {
|
20 | throw new Error("Incomplete parse");
|
21 | }
|
22 | else {
|
23 | return restrictsyntax_1.restrictExpression(opt.lang || "C1", parsed[0]);
|
24 | }
|
25 | }
|
26 | exports.parseExpression = parseExpression;
|
27 | function parseStatement(str, options) {
|
28 | programRules.lexer = new lex_1.TypeLexer(options ? options.types : undefined);
|
29 | const parser = new nearley_1.Parser(nearley_1.Grammar.fromCompiled(programRules));
|
30 | parser.feed(str);
|
31 | return restrictsyntax_1.restrictStatement;
|
32 | }
|
33 | exports.parseStatement = parseStatement;
|
34 | function parseProgramRaw(str) {
|
35 | const parser = new nearley_1.Parser(nearley_1.Grammar.fromCompiled(programRules));
|
36 | const lexer = (parser.lexer = new lex_1.TypeLexer());
|
37 | const segments = str.split(";");
|
38 | let decls = immutable_1.List();
|
39 | segments.forEach((segment, index) => {
|
40 | parser.feed(segment);
|
41 | const parsed = parser.finish();
|
42 | if (parsed.length > 1) {
|
43 | console.log(JSON.stringify(parsed[0]));
|
44 | console.log(JSON.stringify(parsed[parsed.length - 1]));
|
45 | throw new Error(`Parse ambiguous (${parsed.length} parses)`);
|
46 | }
|
47 | else if (parsed.length === 0) {
|
48 | if (index === segments.length - 1) {
|
49 | throw new Error("Incomplete parse at the end of the file");
|
50 | }
|
51 | else {
|
52 |
|
53 | parser.feed(";");
|
54 | }
|
55 | }
|
56 | else {
|
57 |
|
58 | if (index === segments.length - 1) {
|
59 |
|
60 | decls = decls.concat(parsed[0]);
|
61 | }
|
62 | else {
|
63 | const parsedGlobalDecls = parsed[0];
|
64 | decls = decls.concat(parsedGlobalDecls);
|
65 | const possibleTypedef = parsedGlobalDecls[parsedGlobalDecls.length - 1];
|
66 |
|
67 |
|
68 | const typeIdentifier = possibleTypedef[1];
|
69 | lexer.addIdentifier(typeIdentifier);
|
70 | parser.feed(" ");
|
71 | }
|
72 | }
|
73 | });
|
74 | return decls;
|
75 | }
|
76 | exports.parseProgramRaw = parseProgramRaw;
|
77 | function parseProgram(lang, str) {
|
78 | return parseProgramRaw(str).map(decl => {
|
79 | if (typeof decl === "string")
|
80 | return decl;
|
81 | if (decl instanceof Array)
|
82 | return decl[0];
|
83 | return restrictsyntax_1.restrictDeclaration(lang, decl);
|
84 | });
|
85 | }
|
86 | exports.parseProgram = parseProgram;
|
87 |
|
\ | No newline at end of file |