export default /* * Generated by PEG.js 0.10.0. * * http://pegjs.org/ */ (function() { "use strict"; function peg$subclass(child, parent) { function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); } function peg$SyntaxError(message, expected, found, location) { this.message = message; this.expected = expected; this.found = found; this.location = location; this.name = "SyntaxError"; if (typeof Error.captureStackTrace === "function") { Error.captureStackTrace(this, peg$SyntaxError); } } peg$subclass(peg$SyntaxError, Error); peg$SyntaxError.buildMessage = function(expected, found) { var DESCRIBE_EXPECTATION_FNS = { literal: function(expectation) { return "\"" + literalEscape(expectation.text) + "\""; }, "class": function(expectation) { var escapedParts = "", i; for (i = 0; i < expectation.parts.length; i++) { escapedParts += expectation.parts[i] instanceof Array ? classEscape(expectation.parts[i][0]) + "-" + classEscape(expectation.parts[i][1]) : classEscape(expectation.parts[i]); } return "[" + (expectation.inverted ? "^" : "") + escapedParts + "]"; }, any: function(expectation) { return "any character"; }, end: function(expectation) { return "end of input"; }, other: function(expectation) { return expectation.description; } }; function hex(ch) { return ch.charCodeAt(0).toString(16).toUpperCase(); } function literalEscape(s) { return s .replace(/\\/g, '\\\\') .replace(/"/g, '\\"') .replace(/\0/g, '\\0') .replace(/\t/g, '\\t') .replace(/\n/g, '\\n') .replace(/\r/g, '\\r') .replace(/[\x00-\x0F]/g, function(ch) { return '\\x0' + hex(ch); }) .replace(/[\x10-\x1F\x7F-\x9F]/g, function(ch) { return '\\x' + hex(ch); }); } function classEscape(s) { return s .replace(/\\/g, '\\\\') .replace(/\]/g, '\\]') .replace(/\^/g, '\\^') .replace(/-/g, '\\-') .replace(/\0/g, '\\0') .replace(/\t/g, '\\t') .replace(/\n/g, '\\n') .replace(/\r/g, '\\r') .replace(/[\x00-\x0F]/g, function(ch) { return '\\x0' + hex(ch); }) .replace(/[\x10-\x1F\x7F-\x9F]/g, function(ch) { return '\\x' + hex(ch); }); } function describeExpectation(expectation) { return DESCRIBE_EXPECTATION_FNS[expectation.type](expectation); } function describeExpected(expected) { var descriptions = new Array(expected.length), i, j; for (i = 0; i < expected.length; i++) { descriptions[i] = describeExpectation(expected[i]); } descriptions.sort(); if (descriptions.length > 0) { for (i = 1, j = 1; i < descriptions.length; i++) { if (descriptions[i - 1] !== descriptions[i]) { descriptions[j] = descriptions[i]; j++; } } descriptions.length = j; } switch (descriptions.length) { case 1: return descriptions[0]; case 2: return descriptions[0] + " or " + descriptions[1]; default: return descriptions.slice(0, -1).join(", ") + ", or " + descriptions[descriptions.length - 1]; } } function describeFound(found) { return found ? "\"" + literalEscape(found) + "\"" : "end of input"; } return "Expected " + describeExpected(expected) + " but " + describeFound(found) + " found."; }; function peg$parse(input, options) { options = options !== void 0 ? options : {}; var peg$FAILED = {}, peg$startRuleFunctions = { start: peg$parsestart }, peg$startRuleFunction = peg$parsestart, peg$c0 = "(", peg$c1 = peg$literalExpectation("(", false), peg$c2 = ")", peg$c3 = peg$literalExpectation(")", false), peg$c4 = function(expression) { return expression; }, peg$c5 = function(id, args) { return { type: "callExpression", id: id, arguments: optionalList(args) } }, peg$c6 = function(head, tail) { return infixToPostfixAst([head, tail].flat(2)) }, peg$c7 = function(op, expr) { return [Object.assign({ type: 'operator' }, operators[op]), expr] }, peg$c8 = "^", peg$c9 = peg$literalExpectation("^", false), peg$c10 = "*", peg$c11 = peg$literalExpectation("*", false), peg$c12 = "/", peg$c13 = peg$literalExpectation("/", false), peg$c14 = "+", peg$c15 = peg$literalExpectation("+", false), peg$c16 = "-", peg$c17 = peg$literalExpectation("-", false), peg$c18 = function(head, op, tail) { var name; switch(op) { case "||": name = "or" break; case "&&": name = "and" break; default: } return { type: "callExpression", id: name, arguments: [head, tail] } }, peg$c19 = function(head, op, tail) { var name; switch(op) { case "<": name = "lessThan" break; case "<=": name = "lessThanOrEqual" break; case ">": name = "greaterThan" break; case ">=": name = "greaterThanOrEqual" break; case "==": case "=": name = "equal" break; case "!=": case "<>": name = "unequal" break; case "&": name = "add" break; default: } return { type: "callExpression", id: name, arguments: [head, tail] } }, peg$c20 = "<=", peg$c21 = peg$literalExpectation("<=", false), peg$c22 = ">=", peg$c23 = peg$literalExpectation(">=", false), peg$c24 = "<>", peg$c25 = peg$literalExpectation("<>", false), peg$c26 = "<", peg$c27 = peg$literalExpectation("<", false), peg$c28 = ">", peg$c29 = peg$literalExpectation(">", false), peg$c30 = "==", peg$c31 = peg$literalExpectation("==", false), peg$c32 = "=", peg$c33 = peg$literalExpectation("=", false), peg$c34 = "!=", peg$c35 = peg$literalExpectation("!=", false), peg$c36 = "&&", peg$c37 = peg$literalExpectation("&&", false), peg$c38 = "||", peg$c39 = peg$literalExpectation("||", false), peg$c40 = "&", peg$c41 = peg$literalExpectation("&", false), peg$c42 = function(tail) { return { type: "callExpression", id: "not", arguments: [tail] } }, peg$c43 = "!", peg$c44 = peg$literalExpectation("!", false), peg$c45 = function(args) { return args; }, peg$c46 = ",", peg$c47 = peg$literalExpectation(",", false), peg$c48 = function(arg) { return arg; }, peg$c49 = function(args) { return args; }, peg$c50 = /^[A-Za-z]/, peg$c51 = peg$classExpectation([["A", "Z"], ["a", "z"]], false, false), peg$c52 = function(id) { return id.join("").toLowerCase(); }, peg$c53 = function(name) { return name; }, peg$c54 = function(head, tail) { return { type: "identifier", name: head + tail.join("") }; }, peg$c55 = "_", peg$c56 = peg$literalExpectation("_", false), peg$c57 = "$", peg$c58 = peg$literalExpectation("$", false), peg$c59 = ".", peg$c60 = peg$literalExpectation(".", false), peg$c61 = function(chars) { return stringLiteral(chars); }, peg$c62 = function(sign) { var splitted = text().replace(/[\+\-]/g, "").split(".") return { type: "literal", value: parseFloat(text()), dataType: "number", options: { length: splitted[0].length, scale: splitted[1].length, } }; }, peg$c63 = function(sign) { return { type: "literal", value: parseInt(text()), dataType: "number", options: { length: text().replace(/[\+\-]/g, "").length, scale: 0, } }; }, peg$c64 = "0", peg$c65 = peg$literalExpectation("0", false), peg$c66 = /^[0-9]/, peg$c67 = peg$classExpectation([["0", "9"]], false, false), peg$c68 = /^[1-9]/, peg$c69 = peg$classExpectation([["1", "9"]], false, false), peg$c70 = "true", peg$c71 = peg$literalExpectation("true", true), peg$c72 = function() { return { type: "literal", value: true, dataType: "checkbox", options: {} } }, peg$c73 = "false", peg$c74 = peg$literalExpectation("false", true), peg$c75 = function() { return { type: "literal", value: false, dataType: "checkbox", options: {} } }, peg$c76 = "null", peg$c77 = peg$literalExpectation("null", true), peg$c78 = function() { return { type: "literal", value: null, dataType: "null", options: {} } }, peg$c79 = "\\", peg$c80 = peg$literalExpectation("\\", false), peg$c81 = function() { return text(); }, peg$c82 = "'", peg$c83 = peg$literalExpectation("'", false), peg$c84 = "\"", peg$c85 = peg$literalExpectation("\"", false), peg$c86 = peg$anyExpectation(), peg$c87 = /^[\n\r\u2028\u2029]/, peg$c88 = peg$classExpectation(["\n", "\r", "\u2028", "\u2029"], false, false), peg$c89 = "\t", peg$c90 = peg$literalExpectation("\t", false), peg$c91 = "\x0B", peg$c92 = peg$literalExpectation("\x0B", false), peg$c93 = "\f", peg$c94 = peg$literalExpectation("\f", false), peg$c95 = " ", peg$c96 = peg$literalExpectation(" ", false), peg$c97 = "\xA0", peg$c98 = peg$literalExpectation("\xA0", false), peg$c99 = "\u200B", peg$c100 = peg$literalExpectation("\u200B", false), peg$c101 = "\uFEFF", peg$c102 = peg$literalExpectation("\uFEFF", false), peg$currPos = 0, peg$savedPos = 0, peg$posDetailsCache = [{ line: 1, column: 1 }], peg$maxFailPos = 0, peg$maxFailExpected = [], peg$silentFails = 0, peg$result; if ("startRule" in options) { if (!(options.startRule in peg$startRuleFunctions)) { throw new Error("Can't start parsing from rule \"" + options.startRule + "\"."); } peg$startRuleFunction = peg$startRuleFunctions[options.startRule]; } function text() { return input.substring(peg$savedPos, peg$currPos); } function location() { return peg$computeLocation(peg$savedPos, peg$currPos); } function expected(description, location) { location = location !== void 0 ? location : peg$computeLocation(peg$savedPos, peg$currPos) throw peg$buildStructuredError( [peg$otherExpectation(description)], input.substring(peg$savedPos, peg$currPos), location ); } function error(message, location) { location = location !== void 0 ? location : peg$computeLocation(peg$savedPos, peg$currPos) throw peg$buildSimpleError(message, location); } function peg$literalExpectation(text, ignoreCase) { return { type: "literal", text: text, ignoreCase: ignoreCase }; } function peg$classExpectation(parts, inverted, ignoreCase) { return { type: "class", parts: parts, inverted: inverted, ignoreCase: ignoreCase }; } function peg$anyExpectation() { return { type: "any" }; } function peg$endExpectation() { return { type: "end" }; } function peg$otherExpectation(description) { return { type: "other", description: description }; } function peg$computePosDetails(pos) { var details = peg$posDetailsCache[pos], p; if (details) { return details; } else { p = pos - 1; while (!peg$posDetailsCache[p]) { p--; } details = peg$posDetailsCache[p]; details = { line: details.line, column: details.column }; while (p < pos) { if (input.charCodeAt(p) === 10) { details.line++; details.column = 1; } else { details.column++; } p++; } peg$posDetailsCache[pos] = details; return details; } } function peg$computeLocation(startPos, endPos) { var startPosDetails = peg$computePosDetails(startPos), endPosDetails = peg$computePosDetails(endPos); return { start: { offset: startPos, line: startPosDetails.line, column: startPosDetails.column }, end: { offset: endPos, line: endPosDetails.line, column: endPosDetails.column } }; } function peg$fail(expected) { if (peg$currPos < peg$maxFailPos) { return; } if (peg$currPos > peg$maxFailPos) { peg$maxFailPos = peg$currPos; peg$maxFailExpected = []; } peg$maxFailExpected.push(expected); } function peg$buildSimpleError(message, location) { return new peg$SyntaxError(message, null, null, location); } function peg$buildStructuredError(expected, found, location) { return new peg$SyntaxError( peg$SyntaxError.buildMessage(expected, found), expected, found, location ); } function peg$parsestart() { var s0; s0 = peg$parsePrimaryExpression(); return s0; } function peg$parsePrimaryExpression() { var s0; s0 = peg$parseLogicalConcatinationExpression(); if (s0 === peg$FAILED) { s0 = peg$parseUnaryExpression(); if (s0 === peg$FAILED) { s0 = peg$parseCallExpression(); if (s0 === peg$FAILED) { s0 = peg$parseIdentifier(); if (s0 === peg$FAILED) { s0 = peg$parseLiteral(); } } } } return s0; } function peg$parseTerm() { var s0, s1, s2, s3, s4, s5; s0 = peg$parseCallExpression(); if (s0 === peg$FAILED) { s0 = peg$parseIdentifier(); if (s0 === peg$FAILED) { s0 = peg$parseLiteral(); if (s0 === peg$FAILED) { s0 = peg$currPos; if (input.charCodeAt(peg$currPos) === 40) { s1 = peg$c0; peg$currPos++; } else { s1 = peg$FAILED; if (peg$silentFails === 0) { peg$fail(peg$c1); } } if (s1 !== peg$FAILED) { s2 = peg$parse__(); if (s2 !== peg$FAILED) { s3 = peg$parseArithmeticExpression(); if (s3 !== peg$FAILED) { s4 = peg$parse__(); if (s4 !== peg$FAILED) { if (input.charCodeAt(peg$currPos) === 41) { s5 = peg$c2; peg$currPos++; } else { s5 = peg$FAILED; if (peg$silentFails === 0) { peg$fail(peg$c3); } } if (s5 !== peg$FAILED) { peg$savedPos = s0; s1 = peg$c4(s3); s0 = s1; } else { peg$currPos = s0; s0 = peg$FAILED; } } else { peg$currPos = s0; s0 = peg$FAILED; } } else { peg$currPos = s0; s0 = peg$FAILED; } } else { peg$currPos = s0; s0 = peg$FAILED; } } else { peg$currPos = s0; s0 = peg$FAILED; } } } } return s0; } function peg$parseCallExpression() { var s0, s1, s2, s3; s0 = peg$currPos; s1 = peg$parseFunctionIdentifier(); if (s1 !== peg$FAILED) { s2 = peg$parse__(); if (s2 !== peg$FAILED) { s3 = peg$parseArguments(); if (s3 !== peg$FAILED) { peg$savedPos = s0; s1 = peg$c5(s1, s3); s0 = s1; } else { peg$currPos = s0; s0 = peg$FAILED; } } else { peg$currPos = s0; s0 = peg$FAILED; } } else { peg$currPos = s0; s0 = peg$FAILED; } return s0; } function peg$parseArithmeticExpression() { var s0, s1, s2, s3, s4; s0 = peg$currPos; s1 = peg$parseTerm(); if (s1 !== peg$FAILED) { s2 = peg$parse__(); if (s2 !== peg$FAILED) { s3 = []; s4 = peg$parseArithmeticPart(); while (s4 !== peg$FAILED) { s3.push(s4); s4 = peg$parseArithmeticPart(); } if (s3 !== peg$FAILED) { peg$savedPos = s0; s1 = peg$c6(s1, s3); s0 = s1; } else { peg$currPos = s0; s0 = peg$FAILED; } } else { peg$currPos = s0; s0 = peg$FAILED; } } else { peg$currPos = s0; s0 = peg$FAILED; } return s0; } function peg$parseArithmeticPart() { var s0, s1, s2, s3, s4; s0 = peg$currPos; s1 = peg$parse__(); if (s1 !== peg$FAILED) { s2 = peg$parseArithmeticOperator(); if (s2 !== peg$FAILED) { s3 = peg$parse__(); if (s3 !== peg$FAILED) { s4 = peg$parseTerm(); if (s4 !== peg$FAILED) { peg$savedPos = s0; s1 = peg$c7(s2, s4); s0 = s1; } else { peg$currPos = s0; s0 = peg$FAILED; } } else { peg$currPos = s0; s0 = peg$FAILED; } } else { peg$currPos = s0; s0 = peg$FAILED; } } else { peg$currPos = s0; s0 = peg$FAILED; } return s0; } function peg$parseArithmeticOperator() { var s0; if (input.charCodeAt(peg$currPos) === 94) { s0 = peg$c8; peg$currPos++; } else { s0 = peg$FAILED; if (peg$silentFails === 0) { peg$fail(peg$c9); } } if (s0 === peg$FAILED) { if (input.charCodeAt(peg$currPos) === 42) { s0 = peg$c10; peg$currPos++; } else { s0 = peg$FAILED; if (peg$silentFails === 0) { peg$fail(peg$c11); } } if (s0 === peg$FAILED) { if (input.charCodeAt(peg$currPos) === 47) { s0 = peg$c12; peg$currPos++; } else { s0 = peg$FAILED; if (peg$silentFails === 0) { peg$fail(peg$c13); } } if (s0 === peg$FAILED) { if (input.charCodeAt(peg$currPos) === 43) { s0 = peg$c14; peg$currPos++; } else { s0 = peg$FAILED; if (peg$silentFails === 0) { peg$fail(peg$c15); } } if (s0 === peg$FAILED) { if (input.charCodeAt(peg$currPos) === 45) { s0 = peg$c16; peg$currPos++; } else { s0 = peg$FAILED; if (peg$silentFails === 0) { peg$fail(peg$c17); } } } } } } return s0; } function peg$parseLogicalConcatinationExpression() { var s0, s1, s2, s3, s4, s5; s0 = peg$currPos; s1 = peg$parseLogicalCompareExpression(); if (s1 !== peg$FAILED) { s2 = peg$parse__(); if (s2 !== peg$FAILED) { s3 = peg$parseLogicalConcatinationOperator(); if (s3 !== peg$FAILED) { s4 = peg$parse__(); if (s4 !== peg$FAILED) { s5 = peg$parseLogicalConcatinationExpression(); if (s5 !== peg$FAILED) { peg$savedPos = s0; s1 = peg$c18(s1, s3, s5); s0 = s1; } else { peg$currPos = s0; s0 = peg$FAILED; } } else { peg$currPos = s0; s0 = peg$FAILED; } } else { peg$currPos = s0; s0 = peg$FAILED; } } else { peg$currPos = s0; s0 = peg$FAILED; } } else { peg$currPos = s0; s0 = peg$FAILED; } if (s0 === peg$FAILED) { s0 = peg$parseLogicalCompareExpression(); } return s0; } function peg$parseLogicalCompareExpression() { var s0, s1, s2, s3, s4, s5; s0 = peg$currPos; s1 = peg$parseArithmeticExpression(); if (s1 !== peg$FAILED) { s2 = peg$parse__(); if (s2 !== peg$FAILED) { s3 = peg$parseLogicalCompareOperator(); if (s3 === peg$FAILED) { s3 = peg$parseConcatinationOperator(); } if (s3 !== peg$FAILED) { s4 = peg$parse__(); if (s4 !== peg$FAILED) { s5 = peg$parseLogicalCompareExpression(); if (s5 !== peg$FAILED) { peg$savedPos = s0; s1 = peg$c19(s1, s3, s5); s0 = s1; } else { peg$currPos = s0; s0 = peg$FAILED; } } else { peg$currPos = s0; s0 = peg$FAILED; } } else { peg$currPos = s0; s0 = peg$FAILED; } } else { peg$currPos = s0; s0 = peg$FAILED; } } else { peg$currPos = s0; s0 = peg$FAILED; } if (s0 === peg$FAILED) { s0 = peg$parseArithmeticExpression(); } return s0; } function peg$parseLogicalCompareOperator() { var s0; if (input.substr(peg$currPos, 2) === peg$c20) { s0 = peg$c20; peg$currPos += 2; } else { s0 = peg$FAILED; if (peg$silentFails === 0) { peg$fail(peg$c21); } } if (s0 === peg$FAILED) { if (input.substr(peg$currPos, 2) === peg$c22) { s0 = peg$c22; peg$currPos += 2; } else { s0 = peg$FAILED; if (peg$silentFails === 0) { peg$fail(peg$c23); } } if (s0 === peg$FAILED) { if (input.substr(peg$currPos, 2) === peg$c24) { s0 = peg$c24; peg$currPos += 2; } else { s0 = peg$FAILED; if (peg$silentFails === 0) { peg$fail(peg$c25); } } if (s0 === peg$FAILED) { if (input.charCodeAt(peg$currPos) === 60) { s0 = peg$c26; peg$currPos++; } else { s0 = peg$FAILED; if (peg$silentFails === 0) { peg$fail(peg$c27); } } if (s0 === peg$FAILED) { if (input.charCodeAt(peg$currPos) === 62) { s0 = peg$c28; peg$currPos++; } else { s0 = peg$FAILED; if (peg$silentFails === 0) { peg$fail(peg$c29); } } if (s0 === peg$FAILED) { if (input.substr(peg$currPos, 2) === peg$c30) { s0 = peg$c30; peg$currPos += 2; } else { s0 = peg$FAILED; if (peg$silentFails === 0) { peg$fail(peg$c31); } } if (s0 === peg$FAILED) { if (input.charCodeAt(peg$currPos) === 61) { s0 = peg$c32; peg$currPos++; } else { s0 = peg$FAILED; if (peg$silentFails === 0) { peg$fail(peg$c33); } } if (s0 === peg$FAILED) { if (input.substr(peg$currPos, 2) === peg$c34) { s0 = peg$c34; peg$currPos += 2; } else { s0 = peg$FAILED; if (peg$silentFails === 0) { peg$fail(peg$c35); } } } } } } } } } return s0; } function peg$parseLogicalConcatinationOperator() { var s0; if (input.substr(peg$currPos, 2) === peg$c36) { s0 = peg$c36; peg$currPos += 2; } else { s0 = peg$FAILED; if (peg$silentFails === 0) { peg$fail(peg$c37); } } if (s0 === peg$FAILED) { if (input.substr(peg$currPos, 2) === peg$c38) { s0 = peg$c38; peg$currPos += 2; } else { s0 = peg$FAILED; if (peg$silentFails === 0) { peg$fail(peg$c39); } } } return s0; } function peg$parseConcatinationOperator() { var s0; if (input.charCodeAt(peg$currPos) === 38) { s0 = peg$c40; peg$currPos++; } else { s0 = peg$FAILED; if (peg$silentFails === 0) { peg$fail(peg$c41); } } return s0; } function peg$parseUnaryExpression() { var s0, s1, s2, s3; s0 = peg$currPos; s1 = peg$parseUnaryOperator(); if (s1 !== peg$FAILED) { s2 = peg$parse__(); if (s2 !== peg$FAILED) { s3 = peg$parsePrimaryExpression(); if (s3 !== peg$FAILED) { peg$savedPos = s0; s1 = peg$c42(s3); s0 = s1; } else { peg$currPos = s0; s0 = peg$FAILED; } } else { peg$currPos = s0; s0 = peg$FAILED; } } else { peg$currPos = s0; s0 = peg$FAILED; } return s0; } function peg$parseUnaryOperator() { var s0; if (input.charCodeAt(peg$currPos) === 33) { s0 = peg$c43; peg$currPos++; } else { s0 = peg$FAILED; if (peg$silentFails === 0) { peg$fail(peg$c44); } } return s0; } function peg$parseArguments() { var s0, s1, s2, s3, s4, s5; s0 = peg$currPos; if (input.charCodeAt(peg$currPos) === 40) { s1 = peg$c0; peg$currPos++; } else { s1 = peg$FAILED; if (peg$silentFails === 0) { peg$fail(peg$c1); } } if (s1 !== peg$FAILED) { s2 = peg$parse__(); if (s2 !== peg$FAILED) { s3 = peg$currPos; s4 = peg$parseArgumentList(); if (s4 !== peg$FAILED) { s5 = peg$parse__(); if (s5 !== peg$FAILED) { s4 = [s4, s5]; s3 = s4; } else { peg$currPos = s3; s3 = peg$FAILED; } } else { peg$currPos = s3; s3 = peg$FAILED; } if (s3 === peg$FAILED) { s3 = null; } if (s3 !== peg$FAILED) { if (input.charCodeAt(peg$currPos) === 41) { s4 = peg$c2; peg$currPos++; } else { s4 = peg$FAILED; if (peg$silentFails === 0) { peg$fail(peg$c3); } } if (s4 !== peg$FAILED) { peg$savedPos = s0; s1 = peg$c45(s3); s0 = s1; } else { peg$currPos = s0; s0 = peg$FAILED; } } else { peg$currPos = s0; s0 = peg$FAILED; } } else { peg$currPos = s0; s0 = peg$FAILED; } } else { peg$currPos = s0; s0 = peg$FAILED; } return s0; } function peg$parseArgumentList() { var s0, s1, s2, s3, s4, s5, s6; s0 = peg$currPos; s1 = []; s2 = peg$currPos; s3 = peg$parsePrimaryExpression(); if (s3 !== peg$FAILED) { s4 = peg$parse__(); if (s4 !== peg$FAILED) { if (input.charCodeAt(peg$currPos) === 44) { s5 = peg$c46; peg$currPos++; } else { s5 = peg$FAILED; if (peg$silentFails === 0) { peg$fail(peg$c47); } } if (s5 === peg$FAILED) { s5 = null; } if (s5 !== peg$FAILED) { s6 = peg$parse__(); if (s6 !== peg$FAILED) { peg$savedPos = s2; s3 = peg$c48(s3); s2 = s3; } else { peg$currPos = s2; s2 = peg$FAILED; } } else { peg$currPos = s2; s2 = peg$FAILED; } } else { peg$currPos = s2; s2 = peg$FAILED; } } else { peg$currPos = s2; s2 = peg$FAILED; } if (s2 !== peg$FAILED) { while (s2 !== peg$FAILED) { s1.push(s2); s2 = peg$currPos; s3 = peg$parsePrimaryExpression(); if (s3 !== peg$FAILED) { s4 = peg$parse__(); if (s4 !== peg$FAILED) { if (input.charCodeAt(peg$currPos) === 44) { s5 = peg$c46; peg$currPos++; } else { s5 = peg$FAILED; if (peg$silentFails === 0) { peg$fail(peg$c47); } } if (s5 === peg$FAILED) { s5 = null; } if (s5 !== peg$FAILED) { s6 = peg$parse__(); if (s6 !== peg$FAILED) { peg$savedPos = s2; s3 = peg$c48(s3); s2 = s3; } else { peg$currPos = s2; s2 = peg$FAILED; } } else { peg$currPos = s2; s2 = peg$FAILED; } } else { peg$currPos = s2; s2 = peg$FAILED; } } else { peg$currPos = s2; s2 = peg$FAILED; } } } else { s1 = peg$FAILED; } if (s1 !== peg$FAILED) { peg$savedPos = s0; s1 = peg$c49(s1); } s0 = s1; return s0; } function peg$parseFunctionIdentifier() { var s0, s1, s2; s0 = peg$currPos; s1 = []; if (peg$c50.test(input.charAt(peg$currPos))) { s2 = input.charAt(peg$currPos); peg$currPos++; } else { s2 = peg$FAILED; if (peg$silentFails === 0) { peg$fail(peg$c51); } } if (s2 !== peg$FAILED) { while (s2 !== peg$FAILED) { s1.push(s2); if (peg$c50.test(input.charAt(peg$currPos))) { s2 = input.charAt(peg$currPos); peg$currPos++; } else { s2 = peg$FAILED; if (peg$silentFails === 0) { peg$fail(peg$c51); } } } } else { s1 = peg$FAILED; } if (s1 !== peg$FAILED) { peg$savedPos = s0; s1 = peg$c52(s1); } s0 = s1; return s0; } function peg$parseIdentifier() { var s0, s1, s2; s0 = peg$currPos; s1 = peg$currPos; peg$silentFails++; s2 = peg$parseReservedKeyword(); peg$silentFails--; if (s2 === peg$FAILED) { s1 = void 0; } else { peg$currPos = s1; s1 = peg$FAILED; } if (s1 !== peg$FAILED) { s2 = peg$parseIdentifierName(); if (s2 !== peg$FAILED) { peg$savedPos = s0; s1 = peg$c53(s2); s0 = s1; } else { peg$currPos = s0; s0 = peg$FAILED; } } else { peg$currPos = s0; s0 = peg$FAILED; } return s0; } function peg$parseIdentifierName() { var s0, s1, s2, s3; s0 = peg$currPos; s1 = peg$parseIdentifierStart(); if (s1 !== peg$FAILED) { s2 = []; s3 = peg$parseIdentifierPart(); while (s3 !== peg$FAILED) { s2.push(s3); s3 = peg$parseIdentifierPart(); } if (s2 !== peg$FAILED) { peg$savedPos = s0; s1 = peg$c54(s1, s2); s0 = s1; } else { peg$currPos = s0; s0 = peg$FAILED; } } else { peg$currPos = s0; s0 = peg$FAILED; } return s0; } function peg$parseIdentifierStart() { var s0; if (input.charCodeAt(peg$currPos) === 95) { s0 = peg$c55; peg$currPos++; } else { s0 = peg$FAILED; if (peg$silentFails === 0) { peg$fail(peg$c56); } } if (s0 === peg$FAILED) { if (input.charCodeAt(peg$currPos) === 36) { s0 = peg$c57; peg$currPos++; } else { s0 = peg$FAILED; if (peg$silentFails === 0) { peg$fail(peg$c58); } } if (s0 === peg$FAILED) { if (peg$c50.test(input.charAt(peg$currPos))) { s0 = input.charAt(peg$currPos); peg$currPos++; } else { s0 = peg$FAILED; if (peg$silentFails === 0) { peg$fail(peg$c51); } } } } return s0; } function peg$parseIdentifierPart() { var s0; s0 = peg$parseIdentifierStart(); if (s0 === peg$FAILED) { if (input.charCodeAt(peg$currPos) === 46) { s0 = peg$c59; peg$currPos++; } else { s0 = peg$FAILED; if (peg$silentFails === 0) { peg$fail(peg$c60); } } if (s0 === peg$FAILED) { s0 = peg$parseDecimalDigit(); } } return s0; } function peg$parseLiteral() { var s0; s0 = peg$parseStringLiteral(); if (s0 === peg$FAILED) { s0 = peg$parseDecimalLiteral(); if (s0 === peg$FAILED) { s0 = peg$parseBooleanLiteral(); if (s0 === peg$FAILED) { s0 = peg$parseNullLiteral(); } } } return s0; } function peg$parseStringLiteral() { var s0, s1, s2, s3; s0 = peg$currPos; s1 = peg$parseSingleQuote(); if (s1 !== peg$FAILED) { s2 = []; s3 = peg$parseCharacter(); if (s3 === peg$FAILED) { s3 = peg$parseDoubleQuote(); } while (s3 !== peg$FAILED) { s2.push(s3); s3 = peg$parseCharacter(); if (s3 === peg$FAILED) { s3 = peg$parseDoubleQuote(); } } if (s2 !== peg$FAILED) { s3 = peg$parseSingleQuote(); if (s3 !== peg$FAILED) { peg$savedPos = s0; s1 = peg$c61(s2); s0 = s1; } else { peg$currPos = s0; s0 = peg$FAILED; } } else { peg$currPos = s0; s0 = peg$FAILED; } } else { peg$currPos = s0; s0 = peg$FAILED; } if (s0 === peg$FAILED) { s0 = peg$currPos; s1 = peg$parseDoubleQuote(); if (s1 !== peg$FAILED) { s2 = []; s3 = peg$parseCharacter(); if (s3 === peg$FAILED) { s3 = peg$parseSingleQuote(); } while (s3 !== peg$FAILED) { s2.push(s3); s3 = peg$parseCharacter(); if (s3 === peg$FAILED) { s3 = peg$parseSingleQuote(); } } if (s2 !== peg$FAILED) { s3 = peg$parseDoubleQuote(); if (s3 !== peg$FAILED) { peg$savedPos = s0; s1 = peg$c61(s2); s0 = s1; } else { peg$currPos = s0; s0 = peg$FAILED; } } else { peg$currPos = s0; s0 = peg$FAILED; } } else { peg$currPos = s0; s0 = peg$FAILED; } } return s0; } function peg$parseDecimalLiteral() { var s0, s1, s2, s3, s4, s5; s0 = peg$currPos; s1 = peg$currPos; s2 = peg$parse__(); if (s2 !== peg$FAILED) { if (input.charCodeAt(peg$currPos) === 43) { s3 = peg$c14; peg$currPos++; } else { s3 = peg$FAILED; if (peg$silentFails === 0) { peg$fail(peg$c15); } } if (s3 !== peg$FAILED) { s2 = [s2, s3]; s1 = s2; } else { peg$currPos = s1; s1 = peg$FAILED; } } else { peg$currPos = s1; s1 = peg$FAILED; } if (s1 === peg$FAILED) { s1 = peg$currPos; if (input.charCodeAt(peg$currPos) === 45) { s2 = peg$c16; peg$currPos++; } else { s2 = peg$FAILED; if (peg$silentFails === 0) { peg$fail(peg$c17); } } if (s2 !== peg$FAILED) { s3 = peg$parse__(); if (s3 !== peg$FAILED) { s2 = [s2, s3]; s1 = s2; } else { peg$currPos = s1; s1 = peg$FAILED; } } else { peg$currPos = s1; s1 = peg$FAILED; } } if (s1 === peg$FAILED) { s1 = null; } if (s1 !== peg$FAILED) { s2 = peg$parseDecimalIntegerLiteral(); if (s2 !== peg$FAILED) { if (input.charCodeAt(peg$currPos) === 46) { s3 = peg$c59; peg$currPos++; } else { s3 = peg$FAILED; if (peg$silentFails === 0) { peg$fail(peg$c60); } } if (s3 !== peg$FAILED) { s4 = []; s5 = peg$parseDecimalDigit(); while (s5 !== peg$FAILED) { s4.push(s5); s5 = peg$parseDecimalDigit(); } if (s4 !== peg$FAILED) { peg$savedPos = s0; s1 = peg$c62(s1); s0 = s1; } else { peg$currPos = s0; s0 = peg$FAILED; } } else { peg$currPos = s0; s0 = peg$FAILED; } } else { peg$currPos = s0; s0 = peg$FAILED; } } else { peg$currPos = s0; s0 = peg$FAILED; } if (s0 === peg$FAILED) { s0 = peg$currPos; s1 = peg$currPos; s2 = peg$parse__(); if (s2 !== peg$FAILED) { if (input.charCodeAt(peg$currPos) === 43) { s3 = peg$c14; peg$currPos++; } else { s3 = peg$FAILED; if (peg$silentFails === 0) { peg$fail(peg$c15); } } if (s3 !== peg$FAILED) { s2 = [s2, s3]; s1 = s2; } else { peg$currPos = s1; s1 = peg$FAILED; } } else { peg$currPos = s1; s1 = peg$FAILED; } if (s1 === peg$FAILED) { s1 = peg$currPos; if (input.charCodeAt(peg$currPos) === 45) { s2 = peg$c16; peg$currPos++; } else { s2 = peg$FAILED; if (peg$silentFails === 0) { peg$fail(peg$c17); } } if (s2 !== peg$FAILED) { s3 = peg$parse__(); if (s3 !== peg$FAILED) { s2 = [s2, s3]; s1 = s2; } else { peg$currPos = s1; s1 = peg$FAILED; } } else { peg$currPos = s1; s1 = peg$FAILED; } } if (s1 === peg$FAILED) { s1 = null; } if (s1 !== peg$FAILED) { s2 = peg$parseDecimalIntegerLiteral(); if (s2 !== peg$FAILED) { peg$savedPos = s0; s1 = peg$c63(s1); s0 = s1; } else { peg$currPos = s0; s0 = peg$FAILED; } } else { peg$currPos = s0; s0 = peg$FAILED; } } return s0; } function peg$parseDecimalIntegerLiteral() { var s0, s1, s2, s3; if (input.charCodeAt(peg$currPos) === 48) { s0 = peg$c64; peg$currPos++; } else { s0 = peg$FAILED; if (peg$silentFails === 0) { peg$fail(peg$c65); } } if (s0 === peg$FAILED) { s0 = peg$currPos; s1 = peg$parseNonZeroDigit(); if (s1 !== peg$FAILED) { s2 = []; s3 = peg$parseDecimalDigit(); while (s3 !== peg$FAILED) { s2.push(s3); s3 = peg$parseDecimalDigit(); } if (s2 !== peg$FAILED) { s1 = [s1, s2]; s0 = s1; } else { peg$currPos = s0; s0 = peg$FAILED; } } else { peg$currPos = s0; s0 = peg$FAILED; } } return s0; } function peg$parseDecimalDigit() { var s0; if (peg$c66.test(input.charAt(peg$currPos))) { s0 = input.charAt(peg$currPos); peg$currPos++; } else { s0 = peg$FAILED; if (peg$silentFails === 0) { peg$fail(peg$c67); } } return s0; } function peg$parseNonZeroDigit() { var s0; if (peg$c68.test(input.charAt(peg$currPos))) { s0 = input.charAt(peg$currPos); peg$currPos++; } else { s0 = peg$FAILED; if (peg$silentFails === 0) { peg$fail(peg$c69); } } return s0; } function peg$parseBooleanLiteral() { var s0, s1; s0 = peg$currPos; if (input.substr(peg$currPos, 4).toLowerCase() === peg$c70) { s1 = input.substr(peg$currPos, 4); peg$currPos += 4; } else { s1 = peg$FAILED; if (peg$silentFails === 0) { peg$fail(peg$c71); } } if (s1 !== peg$FAILED) { peg$savedPos = s0; s1 = peg$c72(); } s0 = s1; if (s0 === peg$FAILED) { s0 = peg$currPos; if (input.substr(peg$currPos, 5).toLowerCase() === peg$c73) { s1 = input.substr(peg$currPos, 5); peg$currPos += 5; } else { s1 = peg$FAILED; if (peg$silentFails === 0) { peg$fail(peg$c74); } } if (s1 !== peg$FAILED) { peg$savedPos = s0; s1 = peg$c75(); } s0 = s1; } return s0; } function peg$parseNullLiteral() { var s0, s1; s0 = peg$currPos; if (input.substr(peg$currPos, 4).toLowerCase() === peg$c76) { s1 = input.substr(peg$currPos, 4); peg$currPos += 4; } else { s1 = peg$FAILED; if (peg$silentFails === 0) { peg$fail(peg$c77); } } if (s1 !== peg$FAILED) { peg$savedPos = s0; s1 = peg$c78(); } s0 = s1; return s0; } function peg$parseCharacter() { var s0, s1, s2; s0 = peg$currPos; s1 = peg$currPos; peg$silentFails++; s2 = peg$parseQuote(); if (s2 === peg$FAILED) { if (input.charCodeAt(peg$currPos) === 92) { s2 = peg$c79; peg$currPos++; } else { s2 = peg$FAILED; if (peg$silentFails === 0) { peg$fail(peg$c80); } } } peg$silentFails--; if (s2 === peg$FAILED) { s1 = void 0; } else { peg$currPos = s1; s1 = peg$FAILED; } if (s1 !== peg$FAILED) { s2 = peg$parseSourceCharacter(); if (s2 !== peg$FAILED) { peg$savedPos = s0; s1 = peg$c81(); s0 = s1; } else { peg$currPos = s0; s0 = peg$FAILED; } } else { peg$currPos = s0; s0 = peg$FAILED; } return s0; } function peg$parseQuote() { var s0; s0 = peg$parseSingleQuote(); if (s0 === peg$FAILED) { s0 = peg$parseDoubleQuote(); } return s0; } function peg$parseSingleQuote() { var s0; if (input.charCodeAt(peg$currPos) === 39) { s0 = peg$c82; peg$currPos++; } else { s0 = peg$FAILED; if (peg$silentFails === 0) { peg$fail(peg$c83); } } return s0; } function peg$parseDoubleQuote() { var s0; if (input.charCodeAt(peg$currPos) === 34) { s0 = peg$c84; peg$currPos++; } else { s0 = peg$FAILED; if (peg$silentFails === 0) { peg$fail(peg$c85); } } return s0; } function peg$parseSourceCharacter() { var s0; if (input.length > peg$currPos) { s0 = input.charAt(peg$currPos); peg$currPos++; } else { s0 = peg$FAILED; if (peg$silentFails === 0) { peg$fail(peg$c86); } } return s0; } function peg$parseLineTerminator() { var s0; if (peg$c87.test(input.charAt(peg$currPos))) { s0 = input.charAt(peg$currPos); peg$currPos++; } else { s0 = peg$FAILED; if (peg$silentFails === 0) { peg$fail(peg$c88); } } return s0; } function peg$parseReservedKeyword() { var s0; s0 = peg$parseBooleanLiteral(); if (s0 === peg$FAILED) { s0 = peg$parseNullLiteral(); } return s0; } function peg$parse__() { var s0, s1; s0 = []; s1 = peg$parseWhiteSpace(); while (s1 !== peg$FAILED) { s0.push(s1); s1 = peg$parseWhiteSpace(); } return s0; } function peg$parseWhiteSpace() { var s0; if (input.charCodeAt(peg$currPos) === 9) { s0 = peg$c89; peg$currPos++; } else { s0 = peg$FAILED; if (peg$silentFails === 0) { peg$fail(peg$c90); } } if (s0 === peg$FAILED) { s0 = peg$parseLineTerminator(); if (s0 === peg$FAILED) { if (input.charCodeAt(peg$currPos) === 11) { s0 = peg$c91; peg$currPos++; } else { s0 = peg$FAILED; if (peg$silentFails === 0) { peg$fail(peg$c92); } } if (s0 === peg$FAILED) { if (input.charCodeAt(peg$currPos) === 12) { s0 = peg$c93; peg$currPos++; } else { s0 = peg$FAILED; if (peg$silentFails === 0) { peg$fail(peg$c94); } } if (s0 === peg$FAILED) { if (input.charCodeAt(peg$currPos) === 32) { s0 = peg$c95; peg$currPos++; } else { s0 = peg$FAILED; if (peg$silentFails === 0) { peg$fail(peg$c96); } } if (s0 === peg$FAILED) { if (input.charCodeAt(peg$currPos) === 160) { s0 = peg$c97; peg$currPos++; } else { s0 = peg$FAILED; if (peg$silentFails === 0) { peg$fail(peg$c98); } } if (s0 === peg$FAILED) { if (input.charCodeAt(peg$currPos) === 8203) { s0 = peg$c99; peg$currPos++; } else { s0 = peg$FAILED; if (peg$silentFails === 0) { peg$fail(peg$c100); } } if (s0 === peg$FAILED) { if (input.charCodeAt(peg$currPos) === 65279) { s0 = peg$c101; peg$currPos++; } else { s0 = peg$FAILED; if (peg$silentFails === 0) { peg$fail(peg$c102); } } } } } } } } } return s0; } const operators = { '^': { functionName: 'exponentiate', rightAssociative: true, precedence: 4, }, '*': { functionName: 'multiply', rightAssociative: false, precedence: 3, }, '/': { functionName: 'divide', rightAssociative: false, precedence: 3, }, '+': { functionName: 'add', rightAssociative: false, precedence: 2, }, '-': { functionName: 'subtract', rightAssociative: false, precedence: 2, } } function stringLiteral(chars) { return { type: "literal", value: chars.join(""), dataType: "text", options: { length: chars.length } }; } function optionalList(value) { return value !== null ? value[0] : []; } // Shunting Yard Algorithm to order operands by arithmetic precedence function infixToPostfixAst(tokens) { const operatorStack = [] const operandStack = [] tokens.forEach((token) => { if(token.type === 'operator') { while(operatorStack.length > 0) { if(operatorStack[operatorStack.length - 1].precedence < token.precedence) { break } let operator = operatorStack.pop() let operand2 = operandStack.pop() let operand1 = operandStack.pop() operandStack.push(buildOperatorNode(operator, operand1, operand2)) } operatorStack.push(token) } else { operandStack.push(token) } }) while(operatorStack.length > 0) { let operator = operatorStack.pop() let operand2 = operandStack.pop() let operand1 = operandStack.pop() operandStack.push(buildOperatorNode(operator, operand1, operand2)) } return operandStack.pop() } function buildOperatorNode(operator, arg1, arg2) { return { type: "callExpression", id: operator.functionName, arguments: [arg1, arg2] } } peg$result = peg$startRuleFunction(); if (peg$result !== peg$FAILED && peg$currPos === input.length) { return peg$result; } else { if (peg$result !== peg$FAILED && peg$currPos < input.length) { peg$fail(peg$endExpectation()); } throw peg$buildStructuredError( peg$maxFailExpected, peg$maxFailPos < input.length ? input.charAt(peg$maxFailPos) : null, peg$maxFailPos < input.length ? peg$computeLocation(peg$maxFailPos, peg$maxFailPos + 1) : peg$computeLocation(peg$maxFailPos, peg$maxFailPos) ); } } return { SyntaxError: peg$SyntaxError, parse: peg$parse }; })();