UNPKG

lua_to_cpls

Version:

A tool capable of compiling a subset of Lua to Please lang compiled files

99 lines (94 loc) 8.78 kB
// Generated automatically by nearley, version 2.20.1 // http://github.com/Hardmath123/nearley (function () { function id(x) { return x[0]; } const {lexer} = require('./lexer.js'); lexer.ignore('whitespace'); const { makeWord, makeValue, makeCall, makeFunction, makeDeclaration, binaryOperation, makeIf, makeArray, makeFor, processArgs, processParameters, processBlock, } = require('./codegen-helpers.js') var grammar = { Lexer: lexer, ParserRules: [ {"name": "block$ebnf$1", "symbols": []}, {"name": "block$ebnf$1", "symbols": ["block$ebnf$1", "statement"], "postprocess": function arrpush(d) {return d[0].concat([d[1]]);}}, {"name": "block", "symbols": ["block$ebnf$1", "returnStatement"], "postprocess": processBlock}, {"name": "returnStatement", "symbols": ["statement"], "postprocess": id}, {"name": "returnStatement", "symbols": ["exp"], "postprocess": id}, {"name": "statement", "symbols": [{"literal":";"}], "postprocess": () => undefined}, {"name": "statement", "symbols": ["do"], "postprocess": id}, {"name": "statement", "symbols": ["var", {"literal":"="}, "exp"], "postprocess": (match) => makeCall(makeWord("assign"), [match[0], match[2]])}, {"name": "statement", "symbols": [{"literal":"while"}, "exp", "do"], "postprocess": (match) => makeCall(makeWord("while"), [match[1], match[2]])}, {"name": "statement$ebnf$1$subexpression$1", "symbols": [{"literal":"else"}, "block"]}, {"name": "statement$ebnf$1", "symbols": ["statement$ebnf$1$subexpression$1"], "postprocess": id}, {"name": "statement$ebnf$1", "symbols": [], "postprocess": function(d) {return null;}}, {"name": "statement", "symbols": [{"literal":"if"}, "exp", {"literal":"then"}, "block", "statement$ebnf$1", {"literal":"end"}], "postprocess": makeIf}, {"name": "statement", "symbols": [{"literal":"function"}, (lexer.has("identifier") ? {type: "identifier"} : identifier), "functionBody"], "postprocess": makeFunction}, {"name": "statement", "symbols": [{"literal":"local"}, (lexer.has("identifier") ? {type: "identifier"} : identifier), {"literal":"="}, "exp"], "postprocess": makeDeclaration}, {"name": "statement", "symbols": [{"literal":"for"}, (lexer.has("identifier") ? {type: "identifier"} : identifier), {"literal":"in"}, "exp", "do"], "postprocess": makeFor}, {"name": "do", "symbols": [{"literal":"do"}, "block", {"literal":"end"}], "postprocess": (match) => makeCall(makeWord(match[0].value), [match[1]])}, {"name": "functionBody", "symbols": ["parameters", "block", {"literal":"end"}], "postprocess": ([a, b]) => { a.push(b); return a; }}, {"name": "parameters$ebnf$1$subexpression$1$ebnf$1", "symbols": []}, {"name": "parameters$ebnf$1$subexpression$1$ebnf$1$subexpression$1", "symbols": [{"literal":","}, (lexer.has("identifier") ? {type: "identifier"} : identifier)]}, {"name": "parameters$ebnf$1$subexpression$1$ebnf$1", "symbols": ["parameters$ebnf$1$subexpression$1$ebnf$1", "parameters$ebnf$1$subexpression$1$ebnf$1$subexpression$1"], "postprocess": function arrpush(d) {return d[0].concat([d[1]]);}}, {"name": "parameters$ebnf$1$subexpression$1", "symbols": [(lexer.has("identifier") ? {type: "identifier"} : identifier), "parameters$ebnf$1$subexpression$1$ebnf$1"]}, {"name": "parameters$ebnf$1", "symbols": ["parameters$ebnf$1$subexpression$1"], "postprocess": id}, {"name": "parameters$ebnf$1", "symbols": [], "postprocess": function(d) {return null;}}, {"name": "parameters", "symbols": [{"literal":"("}, "parameters$ebnf$1", {"literal":")"}], "postprocess": processParameters}, {"name": "functionCall", "symbols": ["prefixExp", "args"], "postprocess": (match) => makeCall(match[0], match[1])}, {"name": "args$ebnf$1$subexpression$1$ebnf$1", "symbols": []}, {"name": "args$ebnf$1$subexpression$1$ebnf$1$subexpression$1", "symbols": [{"literal":","}, "exp"]}, {"name": "args$ebnf$1$subexpression$1$ebnf$1", "symbols": ["args$ebnf$1$subexpression$1$ebnf$1", "args$ebnf$1$subexpression$1$ebnf$1$subexpression$1"], "postprocess": function arrpush(d) {return d[0].concat([d[1]]);}}, {"name": "args$ebnf$1$subexpression$1", "symbols": ["exp", "args$ebnf$1$subexpression$1$ebnf$1"]}, {"name": "args$ebnf$1", "symbols": ["args$ebnf$1$subexpression$1"], "postprocess": id}, {"name": "args$ebnf$1", "symbols": [], "postprocess": function(d) {return null;}}, {"name": "args", "symbols": [{"literal":"("}, "args$ebnf$1", {"literal":")"}], "postprocess": processArgs}, {"name": "var", "symbols": [(lexer.has("identifier") ? {type: "identifier"} : identifier)], "postprocess": (match) => makeWord(match[0].value)}, {"name": "var", "symbols": ["prefixExp", {"literal":"["}, (lexer.has("number") ? {type: "number"} : number), {"literal":"]"}], "postprocess": (match) => makeCall(makeWord("element"), [match[0], makeValue(match[2].value - 1)])}, {"name": "prefixExp", "symbols": ["var"], "postprocess": id}, {"name": "prefixExp", "symbols": ["parenthesized"], "postprocess": id}, {"name": "prefixExp", "symbols": ["functionCall"], "postprocess": id}, {"name": "parenthesized", "symbols": [{"literal":"("}, "exp", {"literal":")"}], "postprocess": ([_, a]) => a}, {"name": "exp", "symbols": ["expOr"], "postprocess": id}, {"name": "expOr", "symbols": ["expOr", {"literal":"or"}, "expAnd"], "postprocess": (match) => { match[1].value = "||"; return binaryOperation(match)}}, {"name": "expOr", "symbols": ["expAnd"], "postprocess": id}, {"name": "expAnd", "symbols": ["expAnd", {"literal":"and"}, "expComparison"], "postprocess": (match) => { match[1].value = "&&"; return binaryOperation(match)}}, {"name": "expAnd", "symbols": ["expComparison"], "postprocess": id}, {"name": "expComparison", "symbols": ["expComparison", {"literal":"<"}, "expSum"], "postprocess": binaryOperation}, {"name": "expComparison", "symbols": ["expComparison", {"literal":">"}, "expSum"], "postprocess": binaryOperation}, {"name": "expComparison", "symbols": ["expComparison", {"literal":"~="}, "expSum"], "postprocess": (match) => { match[1].value = "!="; return binaryOperation(match)}}, {"name": "expComparison", "symbols": ["expComparison", {"literal":"=="}, "expSum"], "postprocess": binaryOperation}, {"name": "expComparison", "symbols": ["expSum"], "postprocess": id}, {"name": "expSum", "symbols": ["expSum", {"literal":"+"}, "expProduct"], "postprocess": binaryOperation}, {"name": "expSum", "symbols": ["expSum", {"literal":"-"}, "expProduct"], "postprocess": binaryOperation}, {"name": "expSum", "symbols": ["expProduct"], "postprocess": id}, {"name": "expProduct", "symbols": ["expProduct", {"literal":"*"}, "val"], "postprocess": binaryOperation}, {"name": "expProduct", "symbols": ["expProduct", {"literal":"/"}, "val"], "postprocess": binaryOperation}, {"name": "expProduct", "symbols": ["val"], "postprocess": id}, {"name": "val", "symbols": [(lexer.has("number") ? {type: "number"} : number)], "postprocess": (match) => makeValue(match[0].value)}, {"name": "val", "symbols": [(lexer.has("string") ? {type: "string"} : string)], "postprocess": (match) => makeValue(match[0].value)}, {"name": "val", "symbols": ["prefixExp"], "postprocess": id}, {"name": "val", "symbols": [{"literal":"nil"}], "postprocess": () => makeWord("undefined")}, {"name": "val", "symbols": [{"literal":"false"}], "postprocess": (match) => makeWord(match[0].value)}, {"name": "val", "symbols": [{"literal":"true"}], "postprocess": (match) => makeWord(match[0].value)}, {"name": "val", "symbols": ["table"], "postprocess": id}, {"name": "table$ebnf$1$subexpression$1$ebnf$1", "symbols": []}, {"name": "table$ebnf$1$subexpression$1$ebnf$1$subexpression$1", "symbols": ["fieldSeparator", "exp"]}, {"name": "table$ebnf$1$subexpression$1$ebnf$1", "symbols": ["table$ebnf$1$subexpression$1$ebnf$1", "table$ebnf$1$subexpression$1$ebnf$1$subexpression$1"], "postprocess": function arrpush(d) {return d[0].concat([d[1]]);}}, {"name": "table$ebnf$1$subexpression$1", "symbols": ["exp", "table$ebnf$1$subexpression$1$ebnf$1"]}, {"name": "table$ebnf$1", "symbols": ["table$ebnf$1$subexpression$1"], "postprocess": id}, {"name": "table$ebnf$1", "symbols": [], "postprocess": function(d) {return null;}}, {"name": "table", "symbols": [{"literal":"{"}, "table$ebnf$1", {"literal":"}"}], "postprocess": makeArray}, {"name": "fieldSeparator", "symbols": [{"literal":","}], "postprocess": id}, {"name": "fieldSeparator", "symbols": [{"literal":";"}], "postprocess": id} ] , ParserStart: "block" } if (typeof module !== 'undefined'&& typeof module.exports !== 'undefined') { module.exports = grammar; } else { window.grammar = grammar; } })();