UNPKG

@bufbuild/cel

Version:

A CEL evaluator for ECMAScript

1,645 lines 202 kB
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) { if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it"); return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver); }; var _a, _ParseError_formatMessage, _b, _SyntaxError_formatMessage; var runtime; (function (runtime) { class GrammarLocation { constructor(source, start) { this.source = source; this.start = start; } toString() { return String(this.source); } offset(loc) { return { line: loc.line + this.start.line - 1, column: loc.line === 1 ? loc.column + this.start.column - 1 : loc.column, offset: loc.offset + this.start.offset, }; } static offsetStart(range) { if (range.source instanceof GrammarLocation) { return range.source.offset(range.start); } return range.start; } static offsetEnd(range) { if (range.source instanceof GrammarLocation) { return range.source.offset(range.end); } return range.end; } } runtime.GrammarLocation = GrammarLocation; function padEnd(str, targetLength, padString) { padString = padString || " "; if (str.length > targetLength) { return str; } targetLength -= str.length; padString += padString.repeat(targetLength); return str + padString.slice(0, targetLength); } runtime.padEnd = padEnd; class ParseFailure { } runtime.ParseFailure = ParseFailure; class ParseOptions { } runtime.ParseOptions = ParseOptions; function isFailure(r) { return !r.success; } runtime.isFailure = isFailure; function getLine(input, offset) { let line = 1; for (let i = 0; i < offset; i++) { if (input[i] === "\r") { if (input[i + 1] === "\n") { i++; } line++; } else if (input[i] === "\n") { line++; } } return line; } function getColumn(input, offset) { let column = 1; for (let i = offset; i > 0; i--) { if (["\n", "\r"].includes(input[i - 1])) { break; } column++; } return column; } function getLocation(source, input, start, remainder) { return { source, start: { offset: input.length - start.length, line: getLine(input, input.length - start.length), column: getColumn(input, input.length - start.length), }, end: { offset: input.length - remainder.length, line: getLine(input, input.length - remainder.length), column: getColumn(input, input.length - remainder.length), }, }; } runtime.getLocation = getLocation; function getRange(source, input, start, remainder) { return { source, start: input.length - start.length, end: input.length - remainder.length, }; } runtime.getRange = getRange; function getText(start, remainder) { return start.slice(0, remainder.length > 0 ? -remainder.length : undefined); } runtime.getText = getText; })(runtime || (runtime = {})); export class ParseError extends Error { constructor(message, location, name = "parse error") { super(__classPrivateFieldGet(_a, _a, "m", _ParseError_formatMessage).call(_a, message, location)); this.name = name; this.rawMessage = message; this.location = location; } } _a = ParseError, _ParseError_formatMessage = function _ParseError_formatMessage(message, location) { const source = location.source !== undefined ? String(location.source) : "<input>"; return (`${source}:${location.start.line}:${location.start.column}: ` + message); }; export class SyntaxError extends ParseError { constructor(expected, found, location, name = "syntax error") { super(__classPrivateFieldGet(_b, _b, "m", _SyntaxError_formatMessage).call(_b, expected, found), location, name); this.expected = expected; this.found = found; } } _b = SyntaxError, _SyntaxError_formatMessage = function _SyntaxError_formatMessage(expected, found) { function encode(s) { return ("'" + s.replace(/[\\\x07\b\f\n\r\t\v']/g, (match) => { switch (match) { case "\\": return "\\\\"; case "\x07": return "\\x07"; case "\b": return "\\b"; case "\f": return "\\f"; case "\n": return "\\n"; case "\r": return "\\r"; case "\t": return "\\t"; case "\v": return "\\v"; case "'": return "\\'"; default: throw new Error("Unexpected string encoding replacement character. This should be an unreachable error."); } }) + "'"); } function describeExpected(expected) { const descriptions = [ ...new Set(expected.map((e) => { if (e.type === "literal") { return encode(e.value); } return e.value; })), ]; descriptions.sort(); 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.length === 1 ? found : "end of input"; } return ("found " + describeFound(found) + " but expecting " + describeExpected(expected)); }; import Builder from "./builder.js"; const builder = new Builder(); const item2 = { type: "any", value: "any character", }; const item40 = { type: "class", value: "/^[\\t\\n\\f\\r ]/g", }; const item41 = { type: "other", value: "whitespace", }; const item47 = { type: "literal", value: "//", }; const item50 = { type: "class", value: "/^[^\\r\\n]/g", }; const item55 = { type: "class", value: "/^[\\r\\n]/g", }; const item56 = { type: "other", value: "new line", }; const item58 = { type: "other", value: "comment", }; const item75 = { type: "literal", value: "-", }; const item81 = { type: "other", value: "digit", }; const item83 = { type: "literal", value: ".", }; const item93 = { type: "class", value: "/^[+\\-]/g", }; const item104 = { type: "other", value: "float literal", }; const item114 = { type: "literal", value: "0x", }; const item124 = { type: "class", value: "/^[uU]/g", }; const item127 = { type: "other", value: "unsigned integer literal", }; const item143 = { type: "other", value: "integer literal", }; const item153 = { type: "class", value: "/^[rR]/g", }; const item158 = { type: "literal", value: '"""', }; const item171 = { type: "literal", value: "'''", }; const item182 = { type: "literal", value: '"', }; const item194 = { type: "literal", value: "'", }; const item222 = { type: "literal", value: "\\", }; const item224 = { type: "class", value: "/^[xX]/g", }; const item233 = { type: "other", value: "byte value", }; const item239 = { type: "literal", value: "\\u", }; const item247 = { type: "literal", value: "\\U", }; const item259 = { type: "class", value: "/^[0-3]/g", }; const item265 = { type: "other", value: "escaped bytes", }; const item266 = { type: "other", value: "byte sequence", }; const item272 = { type: "class", value: "/^[abfnrtv]/g", }; const item278 = { type: "class", value: "/^[\"'`\\\\?]/g", }; const item279 = { type: "other", value: "escaped character", }; const item326 = { type: "other", value: "quoted character sequence", }; const item328 = { type: "other", value: "string literal", }; const item334 = { type: "class", value: "/^[bB]/g", }; const item338 = { type: "other", value: "bytes literal", }; const item345 = { type: "literal", value: "true", }; const item347 = { type: "literal", value: "false", }; const item349 = { type: "other", value: "boolean literal", }; const item355 = { type: "literal", value: "null", }; const item361 = { type: "other", value: "null literal", }; const item374 = { type: "class", value: "/^[_a-zA-Z]/g", }; const item379 = { type: "other", value: "identifier", }; const item381 = { type: "literal", value: "(", }; const item386 = { type: "literal", value: ",", }; const item388 = { type: "literal", value: ")", }; const item402 = { type: "literal", value: "{", }; const item412 = { type: "literal", value: ":", }; const item421 = { type: "literal", value: "}", }; const item440 = { type: "literal", value: "[", }; const item445 = { type: "literal", value: "]", }; const item512 = { type: "literal", value: "!", }; const item528 = { type: "class", value: "/^[*\\/%]/g", }; const item565 = { type: "literal", value: "<=", }; const item567 = { type: "literal", value: "<", }; const item569 = { type: "literal", value: ">=", }; const item571 = { type: "literal", value: ">", }; const item573 = { type: "literal", value: "==", }; const item575 = { type: "literal", value: "!=", }; const item579 = { type: "literal", value: "in", }; const item581 = { type: "other", value: "relational operator", }; const item589 = { type: "literal", value: "&&", }; const item595 = { type: "literal", value: "||", }; const item604 = { type: "literal", value: "?", }; const item612 = { type: "end", value: "end of input", }; export function parse(input, options = new runtime.ParseOptions()) { const parse$source = options.grammarSource; const result = item1(input); if (result.success === true) { return result.value; } else { let remainder = input; let failedExpectations = []; for (const e of result.failedExpectations) { if (e.remainder.length < remainder.length) { remainder = e.remainder; failedExpectations = []; } if (e.remainder.length === remainder.length) { failedExpectations.push(e); } } throw new SyntaxError(failedExpectations.map((e) => e.expectation), remainder.slice(0, 1), runtime.getLocation(parse$source, input, remainder, remainder)); } function item103(location, range, text, offset, error, digits) { return builder.newDoubleExpr(offset(), digits); } function item126(location, range, text, offset, error, digits) { return builder.newUnsignedInt64Expr(offset(), digits); } function item142(location, range, text, offset, error, digits) { return builder.newInt64Expr(offset(), digits); } function item235(location, range, text, offset, error, value) { return parseInt(value, 16); } function item243(location, range, text, offset, error, value) { return parseInt(value, 16); } function item251(location, range, text, offset, error, value) { return parseInt(value, 16); } function item264(location, range, text, offset, error, value) { return parseInt(value, 8); } function item273(location, range, text, offset, error, value) { switch (value) { case "a": return "\x07"; case "b": return "\b"; case "f": return "\f"; case "n": return "\n"; case "r": return "\r"; case "t": return "\t"; case "v": return "\v"; } throw new Error(); } function item327(location, range, text, offset, error, bytes) { return builder.newStringExpr(offset(), bytes); } function item337(location, range, text, offset, error, bytes) { return builder.newBytesExpr(offset(), bytes); } function item348(location, range, text, offset, error, keyword) { return builder.newBoolExpr(offset(), keyword); } function item360(location, range, text, offset, error) { return builder.newNullExpr(offset()); } function item378(location, range, text, offset, error, id) { if ([ "true", "false", "null", "in", "as", "break", "const", "continue", "else", "for", "function", "if", "import", "let", "loop", "package", "namespace", "return", "var", "void", "while", ].includes(id)) { error("reserved identifier"); } return id; } function item390(location, range, text, offset, error, identifier, args) { return builder.newCallExpr(offset(), identifier, args); } function item416(location, range, text, offset, error, key, value) { return builder.newStructEntry(offset(), key, value); } function item423(location, range, text, offset, error, dot, name, entries) { return builder.newStructExpr(offset(), entries, (dot !== null ? dot : "") + name.join(".")); } function item430(location, range, text, offset, error, name) { return builder.newIdentExpr(offset(), name); } function item447(location, range, text, offset, error, elements) { return builder.newListExpr(offset(), elements); } function item461(location, range, text, offset, error, key, value) { return builder.newMapEntry(offset(), key, value); } function item470(location, range, text, offset, error, entries) { return builder.newStructExpr(offset(), entries); } function item486(location, range, text, offset, error, field) { return (prevExpr) => builder.newSelectExpr(offset(), prevExpr, field); } function item494(location, range, text, offset, error, identifier, args) { return (prevExpr) => builder.newMemberCallExpr(offset(), prevExpr, identifier, args); } function item501(location, range, text, offset, error, index) { return (prevExpr) => builder.newCallExpr(offset(), "_[_]", [prevExpr, index]); } function item504(location, range, text, offset, error, primary, tail) { /* : Expr */ if (tail.length === 0) { return primary; } else { return tail.reduce((expr, op) => op(expr), primary); } } function item517(location, range, text, offset, error, ops, expr) { /* : Expr */ if (ops.length % 2 === 0) { return expr; } else if (expr.exprKind.case === "callExpr" && expr.exprKind.value.function === `${ops[0]}_`) { return expr.exprKind.value.args[0]; } else { return builder.newCallExpr(offset(), `${ops[0]}_`, [expr]); } } function item529(location, range, text, offset, error, o) { return `_${o}_`; } function item532(location, range, text, offset, error, operator, nextExpr) { return (prevExpr) => builder.newCallExpr(offset(), operator, [prevExpr, nextExpr]); } function item534(location, range, text, offset, error, unary, tail) { /* : Expr */ if (tail === null) { return unary; } else { return tail.reduce((expr, op) => op(expr), unary); } } function item545(location, range, text, offset, error, o) { return `_${o}_`; } function item547(location, range, text, offset, error, operator, nextExpr) { return (prevExpr) => builder.newCallExpr(offset(), operator, [prevExpr, nextExpr]); } function item549(location, range, text, offset, error, multiplication, tail) { /* : Expr */ if (tail === null) { return multiplication; } else { return tail.reduce((expr, op) => op(expr), multiplication); } } function item576(location, range, text, offset, error, operator) { return `_${operator}_`; } function item580(location, range, text, offset, error) { return "@in"; } function item583(location, range, text, offset, error, operator, nextExpr) { return (prevExpr) => builder.newCallExpr(offset(), operator, [prevExpr, nextExpr]); } function item585(location, range, text, offset, error, addition, tail) { /* : Expr */ if (tail === null) { return addition; } else { return tail.reduce((expr, op) => op(expr), addition); } } function item591(location, range, text, offset, error, relation) { /* : Expr */ if (relation.length === 1) { return relation[0]; } else { return builder.newCallExpr(offset(), "_&&_", relation); } } function item597(location, range, text, offset, error, and) { /* : Expr */ if (and.length === 1) { return and[0]; } else { return builder.newCallExpr(offset(), "_||_", and); } } function item609(location, range, text, offset, error, t, f) { /* : [Expr, Expr] */ return [t, f]; } function item611(location, range, text, offset, error, or, tail) { /* : Expr */ if (tail === null) { return or; } else { return builder.newCallExpr(offset(), "_?_:_", [or, ...tail]); } } function item1(text) { const result = item4(text); if (result.success === true) { if (result.remainder.length === 0) { return result; } else { return { success: false, remainder: result.remainder, failedExpectations: [ { expectation: item612, remainder: result.remainder, }, ], }; } } else { return result; } } // or:ConditionalOr S // tail:TernaryTail? // { // /* : Expr */ // if (tail === null) { // return or; // } else { // return builder.newCallExpr(offset(), "_?_:_", [or, ...tail]); // } // } function item4(text) { const result = item5(text); if (result.success === true) { return { success: true, value: item611(() => runtime.getLocation(parse$source, input, text, result.remainder), () => runtime.getRange(parse$source, input, text, result.remainder), () => runtime.getText(text, result.remainder), () => input.length - text.length, (message, location = runtime.getLocation(parse$source, input, text, result.remainder), name) => { throw new ParseError(message, location, name); }, result.value[0], result.value[1]), remainder: result.remainder, failedExpectations: [], }; } else { return result; } } // or:ConditionalOr S // tail:TernaryTail? function item5(text) { const failedExpectations = []; let remainder = text; const result0 = item8(remainder); failedExpectations.push(...result0.failedExpectations); if (result0.success === false) { return { success: false, remainder: result0.remainder, failedExpectations, }; } else { remainder = result0.remainder; } const result1 = remainder.match(/^(([\t\n\f\r ])+)?(\/\/([^\r\n])*([\r\n])+)?(([\t\n\f\r ])+)?/g); failedExpectations.push({ expectation: item41, remainder: remainder, }, { expectation: item58, remainder: remainder, }); if (result1?.length !== 1) { return { success: false, remainder, failedExpectations, }; } else { remainder = remainder.slice(result1[0].length); } const result2 = item599(remainder); failedExpectations.push(...result2.failedExpectations); if (result2.success === false) { return { success: false, remainder: result2.remainder, failedExpectations, }; } else { remainder = result2.remainder; } return { success: true, value: [result0.value, result2.value], remainder, failedExpectations, }; } // and:ConditionalAnd|1.., $(S "||")| // { // /* : Expr */ // if (and.length === 1) { // return and[0]; // } else { // return builder.newCallExpr(offset(), "_||_", and); // } // } function item8(text) { const result = item10(text); if (result.success === true) { return { success: true, value: item597(() => runtime.getLocation(parse$source, input, text, result.remainder), () => runtime.getRange(parse$source, input, text, result.remainder), () => runtime.getText(text, result.remainder), () => input.length - text.length, (message, location = runtime.getLocation(parse$source, input, text, result.remainder), name) => { throw new ParseError(message, location, name); }, result.value), remainder: result.remainder, failedExpectations: [], }; } else { return result; } } // ConditionalAnd|1.., $(S "||")| function item10(text) { const values = []; const failedExpectations = []; let remainder = text; let result; do { let r = remainder; if (values.length > 0) { result = item592(r); if (result.success === false) { break; } r = result.remainder; } result = item12(r); failedExpectations.push(...result.failedExpectations); if (result.success === false) { break; } remainder = result.remainder; values.push(result.value); } while (true); if (values.length < 1 && result.success === false /* technically redundant */) { return { success: false, remainder: result.remainder, failedExpectations, }; } else { return { success: true, value: values, remainder, failedExpectations }; } } // relation:Relation|1.., $(S "&&")| // { // /* : Expr */ // if (relation.length === 1) { // return relation[0]; // } else { // return builder.newCallExpr(offset(), "_&&_", relation); // } // } function item12(text) { const result = item14(text); if (result.success === true) { return { success: true, value: item591(() => runtime.getLocation(parse$source, input, text, result.remainder), () => runtime.getRange(parse$source, input, text, result.remainder), () => runtime.getText(text, result.remainder), () => input.length - text.length, (message, location = runtime.getLocation(parse$source, input, text, result.remainder), name) => { throw new ParseError(message, location, name); }, result.value), remainder: result.remainder, failedExpectations: [], }; } else { return result; } } // Relation|1.., $(S "&&")| function item14(text) { const values = []; const failedExpectations = []; let remainder = text; let result; do { let r = remainder; if (values.length > 0) { result = item586(r); if (result.success === false) { break; } r = result.remainder; } result = item16(r); failedExpectations.push(...result.failedExpectations); if (result.success === false) { break; } remainder = result.remainder; values.push(result.value); } while (true); if (values.length < 1 && result.success === false /* technically redundant */) { return { success: false, remainder: result.remainder, failedExpectations, }; } else { return { success: true, value: values, remainder, failedExpectations }; } } // addition:Addition tail:RelationTail? // { // /* : Expr */ // if (tail === null) { // return addition; // } else { // return tail.reduce((expr, op) => op(expr), addition); // } // } function item16(text) { const result = item17(text); if (result.success === true) { return { success: true, value: item585(() => runtime.getLocation(parse$source, input, text, result.remainder), () => runtime.getRange(parse$source, input, text, result.remainder), () => runtime.getText(text, result.remainder), () => input.length - text.length, (message, location = runtime.getLocation(parse$source, input, text, result.remainder), name) => { throw new ParseError(message, location, name); }, result.value[0], result.value[1]), remainder: result.remainder, failedExpectations: [], }; } else { return result; } } // addition:Addition tail:RelationTail? function item17(text) { const failedExpectations = []; let remainder = text; const result0 = item20(remainder); failedExpectations.push(...result0.failedExpectations); if (result0.success === false) { return { success: false, remainder: result0.remainder, failedExpectations, }; } else { remainder = result0.remainder; } const result1 = item551(remainder); failedExpectations.push(...result1.failedExpectations); if (result1.success === false) { return { success: false, remainder: result1.remainder, failedExpectations, }; } else { remainder = result1.remainder; } return { success: true, value: [result0.value, result1.value], remainder, failedExpectations, }; } // multiplication:Multiplication tail:AdditionTail? // { // /* : Expr */ // if (tail === null) { // return multiplication; // } else { // return tail.reduce((expr, op) => op(expr), multiplication); // } // } function item20(text) { const result = item21(text); if (result.success === true) { return { success: true, value: item549(() => runtime.getLocation(parse$source, input, text, result.remainder), () => runtime.getRange(parse$source, input, text, result.remainder), () => runtime.getText(text, result.remainder), () => input.length - text.length, (message, location = runtime.getLocation(parse$source, input, text, result.remainder), name) => { throw new ParseError(message, location, name); }, result.value[0], result.value[1]), remainder: result.remainder, failedExpectations: [], }; } else { return result; } } // multiplication:Multiplication tail:AdditionTail? function item21(text) { const failedExpectations = []; let remainder = text; const result0 = item24(remainder); failedExpectations.push(...result0.failedExpectations); if (result0.success === false) { return { success: false, remainder: result0.remainder, failedExpectations, }; } else { remainder = result0.remainder; } const result1 = item536(remainder); failedExpectations.push(...result1.failedExpectations); if (result1.success === false) { return { success: false, remainder: result1.remainder, failedExpectations, }; } else { remainder = result1.remainder; } return { success: true, value: [result0.value, result1.value], remainder, failedExpectations, }; } // unary:Unary tail:MultiplicationTail? // { // /* : Expr */ // if (tail === null) { // return unary; // } else { // return tail.reduce((expr, op) => op(expr), unary); // } // } function item24(text) { const result = item25(text); if (result.success === true) { return { success: true, value: item534(() => runtime.getLocation(parse$source, input, text, result.remainder), () => runtime.getRange(parse$source, input, text, result.remainder), () => runtime.getText(text, result.remainder), () => input.length - text.length, (message, location = runtime.getLocation(parse$source, input, text, result.remainder), name) => { throw new ParseError(message, location, name); }, result.value[0], result.value[1]), remainder: result.remainder, failedExpectations: [], }; } else { return result; } } // unary:Unary tail:MultiplicationTail? function item25(text) { const failedExpectations = []; let remainder = text; const result0 = item28(remainder); failedExpectations.push(...result0.failedExpectations); if (result0.success === false) { return { success: false, remainder: result0.remainder, failedExpectations, }; } else { remainder = result0.remainder; } const result1 = item519(remainder); failedExpectations.push(...result1.failedExpectations); if (result1.success === false) { return { success: false, remainder: result1.remainder, failedExpectations, }; } else { remainder = result1.remainder; } return { success: true, value: [result0.value, result1.value], remainder, failedExpectations, }; } // Member // / S ops:$( "!"+ / "-"+ ) expr:Member // { // /* : Expr */ // if (ops.length % 2 === 0) { // return expr; // } else if (expr.exprKind.case === "callExpr" && expr.exprKind.value.function === `${ops[0]}_`) { // return expr.exprKind.value.args[0]; // } else { // return builder.newCallExpr(offset(), `${ops[0]}_`, [expr]); // } // } function item28(text) { const choices = [item30, item505]; let failedExpectations = []; for (let func = choices.shift(); func !== undefined; func = choices.shift()) { const result = func(text); failedExpectations.push(...result.failedExpectations); if (result.success === true) { return { success: true, value: result.value, remainder: result.remainder, failedExpectations, }; } } return { success: false, remainder: text, failedExpectations, }; } // S primary:Primary tail:MemberTail // { // /* : Expr */ // if (tail.length === 0) { // return primary; // } else { // return tail.reduce((expr, op) => op(expr), primary); // } // } function item30(text) { const result = item31(text); if (result.success === true) { return { success: true, value: item504(() => runtime.getLocation(parse$source, input, text, result.remainder), () => runtime.getRange(parse$source, input, text, result.remainder), () => runtime.getText(text, result.remainder), () => input.length - text.length, (message, location = runtime.getLocation(parse$source, input, text, result.remainder), name) => { throw new ParseError(message, location, name); }, result.value[0], result.value[1]), remainder: result.remainder, failedExpectations: [], }; } else { return result; } } // S primary:Primary tail:MemberTail function item31(text) { const failedExpectations = []; let remainder = text; const result0 = remainder.match(/^(([\t\n\f\r ])+)?(\/\/([^\r\n])*([\r\n])+)?(([\t\n\f\r ])+)?/g); failedExpectations.push({ expectation: item41, remainder: remainder, }, { expectation: item58, remainder: remainder, }); if (result0?.length !== 1) { return { success: false, remainder, failedExpectations, }; } else { remainder = remainder.slice(result0[0].length); } const result1 = item63(remainder); failedExpectations.push(...result1.failedExpectations); if (result1.success === false) { return { success: false, remainder: result1.remainder, failedExpectations, }; } else { remainder = result1.remainder; } const result2 = item473(remainder); failedExpectations.push(...result2.failedExpectations); if (result2.success === false) { return { success: false, remainder: result2.remainder, failedExpectations, }; } else { remainder = result2.remainder; } return { success: true, value: [result1.value, result2.value], remainder, failedExpectations, }; } // WhiteSpace? Comment? WhiteSpace? function item33(text) { const failedExpectations = []; let remainder = text; const result0 = item34(remainder); failedExpectations.push(...result0.failedExpectations); if (result0.success === false) { return { success: false, remainder: result0.remainder, failedExpectations, }; } else { remainder = result0.remainder; } const result1 = item42(remainder); failedExpectations.push(...result1.failedExpectations); if (result1.success === false) { return { success: false, remainder: result1.remainder, failedExpectations, }; } else { remainder = result1.remainder; } const result2 = item59(remainder); failedExpectations.push(...result2.failedExpectations); if (result2.success === false) { return { success: false, remainder: result2.remainder, failedExpectations, }; } else { remainder = result2.remainder; } return { success: true, value: [result0.value, result1.value, result2.value], remainder, failedExpectations, }; } // WhiteSpace? function item34(text) { const result = item36(text); if (result.success === true) { return result; } else { return { success: true, value: null, remainder: text, failedExpectations: result.failedExpectations, }; } } // WhiteSpace "whitespace" // = $([\t\n\f\r ]+) // function item36(text) { const result = item37(text); if (result.success === true) { return result; } else { return { success: false, remainder: result.remainder, failedExpectations: [ { expectation: item41, remainder: result.remainder, }, ], }; } } // $([\t\n\f\r ]+) function item37(text) { const matches = text.match(/^([\t\n\f\r ])+/g); if (matches?.length === 1) { return { success: true, value: matches[0], remainder: text.slice(matches[0].length), failedExpectations: [], }; } else { return { success: false, remainder: text, failedExpectations: [ { expectation: item40, remainder: text, }, ], }; } } // Comment? function item42(text) { const result = item44(text); if (result.success === true) { return result; } else { return { success: true, value: null, remainder: text, failedExpectations: result.failedExpectations, }; } } // Comment "comment" // = '//' [^\r\n]* NewLine // function item44(text) { const result = item45(text); if (result.success === true) { return result; } else { return { success: false, remainder: result.remainder, failedExpectations: [ { expectation: item58, remainder: result.remainder, }, ], }; } } // '//' [^\r\n]* NewLine function item45(text) { const failedExpectations = []; let remainder = text; const result0 = item46(remainder); failedExpectations.push(...result0.failedExpectations); if (result0.success === false) { return { success: false, remainder: result0.remainder, failedExpectations, }; } else { remainder = result0.remainder; } const result1 = item48(remainder); failedExpectations.push(...result1.failedExpectations); if (result1.success === false) { return { success: false, remainder: result1.remainder, failedExpectations, }; } else { remainder = result1.remainder; } const result2 = item52(remainder); failedExpectations.push(...result2.failedExpectations); if (result2.success === false) { return { success: false, remainder: result2.remainder, failedExpectations, }; } else { remainder = result2.remainder; } return { success: true, value: [result0.value, result1.value, result2.value], remainder, failedExpectations, }; } // '//' function item46(text) { if (text.startsWith("//")) { return { success: true, value: "//", remainder: text.slice(2), failedExpectations: [], }; } else { return { success: false, remainder: text, failedExpectations: [ { expectation: item47, remainder: text, }, ], }; } } // [^\r\n]* function item48(text) { const values = []; const failedExpectations = []; let remainder = text; let result; do { let r = remainder; result = item49(r); failedExpectations.push(...result.failedExpectations); if (result.success === false) { break; } remainder = result.remainder; values.push(result.value); } while (true); return { success: true, value: values, remainder, failedExpectations }; } // [^\r\n] function item49(text) { if (/^[^\r\n]/g.test(text)) { return { success: true, value: text.slice(0, 1), remainder: text.slice(1), failedExpectations: [], }; } else { return { success: false, remainder: text, failedExpectations: [ { expectation: item50, remainder: text, }, ], }; } } // NewLine "new line" // = [\r\n]+ // function item52(text) { const result = item53(text); if (result.success === true) { return result; } else { return { success: false, remainder: result.remainder, failedExpectations: [ { expectation: item56, remainder: result.remainder, }, ], }; } } // [\r\n]+ function item53(text) { const values = []; const failedExpectations = []; let remainder = text; let result; do { let r = remainder; result = item54(r); failedExpectations.push(...result.failedExpectations); if (result.success === false) { break; } remainder = result.remainder; values.push(result.value); } while (true); if (values.length < 1 && result.success === false /* technically redundant */) { return { success: false, remainder: result.remainder, failedExpectations, }; } else { return { success: true, value: values, remainder, failedExpectations }; } } // [\r\n] function item54(text) { if (/^[\r\n]/g.test(text)) { return { success: true, value: text.slice(0, 1), remainder: text.slice(1), failedExpectations: [], }; } else { return { success: false, remainder: text, failedExpectations: [ { expectation: item55, remainder: text, }, ], }; } } // WhiteSpace? function item59(text) { const result = item36(text); if (result.success === true) { return result; } else { return { success: true, value: null, remainder: text, failedExpectations: result.failedExpectations, }; } } // Literal // / "."? S identifier:Identifier S "(" args:ExprList ")" // { return builder.newCallExpr(offset(), identifier, args) } // / dot:"."? S name:Identifier|1.., S "." S| S "{" entries:FieldInits (",")? S "}" // { return builder.newStructExpr(offset(), entries, (dot !== null ? dot : '') + name.join('.')) } // / "."? S name:Identifier // { return builder.newIdentExpr(offset(), name) } // / "(" @Expr ")" // / elements:("[" @ExprList (",")? S "]") // { return builder.newListExpr(offset(), elements) } // / entries:("{" @MapInits $((",")? S "}")) // { return builder.newStructExpr(offset(), entries) } function item63(text) { const choices = [ item65, item362, item391, item424, item431, item436, item448, ]; let failedExpectations = []; for (let func = choices.shift(); func !== undefined; func = choices.shift()) { const result = func(text); failedExpectations.push(...result.failedExpectations); if (result.success === true) { return { success: true, value: result.value, remainder: result.remainder, failedExpectations, }; } } return { success: false, remainder: text, failedExpectations, }; } // FloatLiteral / UnsignedIntLiteral / IntLiteral / StringLiteral / BytesLiteral / BooleanLiteral / NullLiteral function item65(text) { const choices = [ item67, item106, item129, item145, item330, item340, item351, ]; let failedExpectations = []; for (let func = choices.shift(); func !== undefined; func = choices.shift()) { const result = func(text); failedExpectations.push(...result.failedExpectations); if (result.success === true) { return { success: true, value: result.value, remainder: result.remainder, failedExpectations, }; } } return { success: false, remainder: text, failedExpectations, }; } // FloatLiteral "float literal" // = digits:$("-"? Digit* "." Digit+ Exponent? / "-"? Digit+ Exponent) // { return builder.newDoubleExpr(offset(), digits) } // function item67(text) { const result = item68(text); if (result.success === true) { return result; } else { return { success: false, remainder: result.remainder, failedExpectations: [ { expectation: item104, remainder: result.remainder, }, ], }; } } // digits:$("-"? Digit* "." Digit+ Exponent? / "-"? Digit+ Exponent) // { return builder.newDoubleExpr(offset(), digits) } function item68(text) { const result = item70(text); if (result.success === true) { return { success: true, value: item103(() => runtime.getLocation(parse$source, input, text, result.remainder), () => runtime.getRange(parse$source, input, text, result.remainder), () => runtime.getText(text, result.remainder), () => input.length - text.length, (message, location = runtime.getLocation(parse$source, input, text, result.remainder), name) => { throw new ParseError(message, location, name); }, result.value), remainder: result.remainder, failedExpectations: []