const fs = require("fs"); const path = require("path"); const lexer = require("./lib/parser-util").pickyLexer; const nearley = require("nearley"); const grammar = require("./lib/program-rules"); let parser = new nearley.Parser(nearley.Grammar.fromCompiled(grammar)); const parserUtil = require("./lib/parser-util"); parser = new nearley.Parser(nearley.Grammar.fromCompiled(grammar)); parser.feed(" int main() { int x; x = 0"); parser = new nearley.Parser(nearley.Grammar.fromCompiled(grammar)); parser.feed("int main() { return 0; } "); //console.log(parser.finish()) function parse(str) { parserUtil.typeIdentifiers = new Set(); const parser = new nearley.Parser(nearley.Grammar.fromCompiled(grammar)); const segments = str.split(";"); segments.forEach((segment) => { parser.feed(segment); const parsed = parser.finish(); //console.log(`Finished segment ${segment.length}`); if (parsed.length > 1) { console.log(JSON.stringify(parsed[0])); console.log(JSON.stringify(parsed[parsed.length - 1])); console.log(parsed.length); throw new Error(`Parse ambiguous (${parsed.length})`); } else if (parsed.length === 1) { //console.log("complete!"); const [parsedWs, parsedGlobalDecls] = parsed[0]; const possibleTypedef = parsedGlobalDecls[parsedGlobalDecls.length - 1][0]; // TODO: check that it's a typedef const typeIdentifier = possibleTypedef[4].value; parserUtil.typeIdentifiers.add(typeIdentifier); //console.log(typeIdentifier); //console.log(typeIdentifier); //console.log(typeIdentifiers); parser.feed(" "); } else { //console.log("incomplete"); parser.feed(";"); } }); } //parse(fs.readFileSync("./tests/fp-basic/funptr04.c1", {encoding: "utf-8"})) //throw new Error(); const dir = './tests'; fs.readdirSync(dir).forEach((subdir) => { if (fs.lstatSync(path.join(dir,subdir)).isDirectory()) { fs.readdirSync(path.join(dir, subdir)).forEach((file) => { switch (path.extname(file)) { case ".l1": case ".l2": case ".l3": case ".l4": case ".c0": case ".c1": { const filepath = path.join(dir, subdir, file); const contents = fs.readFileSync(filepath, {encoding: 'utf-8'}); if (contents.startsWith("//test error")) { // pass } else if (contents.startsWith("//test")) { try { parse(contents); } catch (e) { console.log(`${filepath} ${e}`); } } } } }); }});