UNPKG

3.38 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const immutable_1 = require("immutable");
4const lex_1 = require("./lex");
5const nearley_1 = require("nearley");
6const restrictsyntax_1 = require("./restrictsyntax");
7const expressionRules = require("../lib/expression-rules");
8//const statementRules = require("../lib/statement-rules");
9const programRules = require("../lib/program-rules");
10function 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}
26exports.parseExpression = parseExpression;
27function 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}
33exports.parseStatement = parseStatement;
34function 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 //console.log(` -- continuing to parse`);
53 parser.feed(";");
54 }
55 }
56 else {
57 // parsed.length === 1
58 if (index === segments.length - 1) {
59 //console.log(` -- end`);
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 // TODO: check that it's a typedef
67 //console.log(` -- typedef ${possibleTypedef[1]}`);
68 const typeIdentifier = possibleTypedef[1];
69 lexer.addIdentifier(typeIdentifier);
70 parser.feed(" ");
71 }
72 }
73 });
74 return decls;
75}
76exports.parseProgramRaw = parseProgramRaw;
77function 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}
86exports.parseProgram = parseProgram;
87//# sourceMappingURL=parse.js.map
\No newline at end of file