@thinql/parse
Version:
Parse ThinQL queries to ASTs
165 lines (152 loc) • 8.78 kB
JavaScript
import Assertion from './types/Assertion.js'
import Call from './types/Call.js'
import Comparison from './types/Comparison.js'
import FullTextSearch from './types/FullTextSearch.js'
import lexer from './lexer.js'
import LogicalAndExpression from './types/LogicalAndExpression.js'
import LogicalOrExpression from './types/LogicalOrExpression.js'
import Property from './types/Property.js'
import Statement from './types/Statement.js'
import Value from './types/Value.js'
// Generated automatically by nearley, version 2.16.0
// http://github.com/Hardmath123/nearley
(function () {
function id(x) { return x[0]; }
var grammar = {
Lexer: lexer,
ParserRules: [
{"name": "main", "symbols": ["Statement"], "postprocess":
([statement]) => statement
},
{"name": "_$subexpression$1", "symbols": []},
{"name": "_$subexpression$1", "symbols": [(lexer.has("whitespace") ? {type: "whitespace"} : whitespace)]},
{"name": "_", "symbols": ["_$subexpression$1"], "postprocess":
() => null
},
{"name": "__", "symbols": [(lexer.has("whitespace") ? {type: "whitespace"} : whitespace)], "postprocess":
() => true
},
{"name": "callee", "symbols": [(lexer.has("unquotedString") ? {type: "unquotedString"} : unquotedString)], "postprocess":
([callee]) => callee
},
{"name": "comparisonOperator", "symbols": [(lexer.has("comparisonOperator") ? {type: "comparisonOperator"} : comparisonOperator)], "postprocess":
([comparisonOperator]) => comparisonOperator
},
{"name": "negator", "symbols": [(lexer.has("negator") ? {type: "negator"} : negator)], "postprocess":
([negator]) => negator
},
{"name": "string$subexpression$1", "symbols": [(lexer.has("quotedString") ? {type: "quotedString"} : quotedString)]},
{"name": "string$subexpression$1", "symbols": [(lexer.has("unquotedString") ? {type: "unquotedString"} : unquotedString)]},
{"name": "string", "symbols": ["string$subexpression$1"], "postprocess":
([[string]]) => string
},
{"name": "_Argument$subexpression$1", "symbols": ["Call"]},
{"name": "_Argument$subexpression$1", "symbols": ["Value"]},
{"name": "_Argument", "symbols": ["_Argument$subexpression$1"], "postprocess":
([[argument]]) => argument
},
{"name": "_LogicalExpression$subexpression$1", "symbols": ["LogicalAndExpression"]},
{"name": "_LogicalExpression$subexpression$1", "symbols": ["LogicalOrExpression"]},
{"name": "_LogicalExpression", "symbols": ["_LogicalExpression$subexpression$1"], "postprocess":
([[expression]]) => expression
},
{"name": "Assertion$ebnf$1", "symbols": ["negator"], "postprocess": id},
{"name": "Assertion$ebnf$1", "symbols": [], "postprocess": function(d) {return null;}},
{"name": "Assertion$subexpression$1", "symbols": ["Comparison"]},
{"name": "Assertion$subexpression$1$subexpression$1", "symbols": [(lexer.has("openBracket") ? {type: "openBracket"} : openBracket), "_", "_LogicalExpression", "_", (lexer.has("closeBracket") ? {type: "closeBracket"} : closeBracket)]},
{"name": "Assertion$subexpression$1", "symbols": ["Assertion$subexpression$1$subexpression$1"]},
{"name": "Assertion$subexpression$1", "symbols": ["Call"]},
{"name": "Assertion$subexpression$1", "symbols": ["FullTextSearch"]},
{"name": "Assertion", "symbols": ["Assertion$ebnf$1", "_", "Assertion$subexpression$1"], "postprocess":
([negator, , [assertionGroup]]) => {
let assertion = assertionGroup
let openBracket
if (Array.isArray(assertion)) {
([openBracket, , assertion] = assertionGroup)
}
return new Assertion(
{ assertion, negated: negator !== null },
negator || openBracket || assertion
)
}
},
{"name": "Call$ebnf$1$subexpression$1$ebnf$1", "symbols": []},
{"name": "Call$ebnf$1$subexpression$1$ebnf$1$subexpression$1", "symbols": ["__", "_Argument"]},
{"name": "Call$ebnf$1$subexpression$1$ebnf$1", "symbols": ["Call$ebnf$1$subexpression$1$ebnf$1", "Call$ebnf$1$subexpression$1$ebnf$1$subexpression$1"], "postprocess": function arrpush(d) {return d[0].concat([d[1]]);}},
{"name": "Call$ebnf$1$subexpression$1", "symbols": ["_Argument", "Call$ebnf$1$subexpression$1$ebnf$1"]},
{"name": "Call$ebnf$1", "symbols": ["Call$ebnf$1$subexpression$1"], "postprocess": id},
{"name": "Call$ebnf$1", "symbols": [], "postprocess": function(d) {return null;}},
{"name": "Call", "symbols": ["callee", (lexer.has("openBracket") ? {type: "openBracket"} : openBracket), "_", "Call$ebnf$1", "_", (lexer.has("closeBracket") ? {type: "closeBracket"} : closeBracket)], "postprocess":
([callee, , , argList]) => {
const args = []
if (argList) {
const [firstArgument, moreArguments] = argList
args.push(firstArgument)
if (moreArguments) {
for (const [, argument] of moreArguments) {
args.push(argument)
}
}
}
return new Call({ arguments: args, callee: callee.value }, callee)
}
},
{"name": "Comparison$subexpression$1", "symbols": ["Call"]},
{"name": "Comparison$subexpression$1", "symbols": ["Property"]},
{"name": "Comparison", "symbols": ["Comparison$subexpression$1", "_", "comparisonOperator", "_", "_Argument"], "postprocess":
([[left], , { value: comparisonOperator }, , right]) =>
new Comparison({ comparisonOperator, left, right }, left)
},
{"name": "FullTextSearch", "symbols": ["Value"], "postprocess":
([value]) => new FullTextSearch({ value }, value)
},
{"name": "LogicalAndExpression$ebnf$1$subexpression$1", "symbols": ["__", "Assertion"]},
{"name": "LogicalAndExpression$ebnf$1", "symbols": ["LogicalAndExpression$ebnf$1$subexpression$1"]},
{"name": "LogicalAndExpression$ebnf$1$subexpression$2", "symbols": ["__", "Assertion"]},
{"name": "LogicalAndExpression$ebnf$1", "symbols": ["LogicalAndExpression$ebnf$1", "LogicalAndExpression$ebnf$1$subexpression$2"], "postprocess": function arrpush(d) {return d[0].concat([d[1]]);}},
{"name": "LogicalAndExpression", "symbols": ["Assertion", "LogicalAndExpression$ebnf$1"], "postprocess":
([firstAssertion, moreAssertions]) => {
const assertions = [firstAssertion]
for (const [, assertion] of moreAssertions) {
assertions.push(assertion)
}
return new LogicalAndExpression({ assertions }, assertions[0])
}
},
{"name": "LogicalOrExpression$ebnf$1$subexpression$1", "symbols": ["__", (lexer.has("logicalOrOperator") ? {type: "logicalOrOperator"} : logicalOrOperator), "__", "Assertion"]},
{"name": "LogicalOrExpression$ebnf$1", "symbols": ["LogicalOrExpression$ebnf$1$subexpression$1"]},
{"name": "LogicalOrExpression$ebnf$1$subexpression$2", "symbols": ["__", (lexer.has("logicalOrOperator") ? {type: "logicalOrOperator"} : logicalOrOperator), "__", "Assertion"]},
{"name": "LogicalOrExpression$ebnf$1", "symbols": ["LogicalOrExpression$ebnf$1", "LogicalOrExpression$ebnf$1$subexpression$2"], "postprocess": function arrpush(d) {return d[0].concat([d[1]]);}},
{"name": "LogicalOrExpression", "symbols": ["Assertion", "LogicalOrExpression$ebnf$1"], "postprocess":
([firstAssertion, moreAssertions]) => {
const assertions = [firstAssertion]
for (const [, , , assertion] of moreAssertions) {
assertions.push(assertion)
}
return new LogicalOrExpression({ assertions }, assertions[0])
}
},
{"name": "Property", "symbols": ["string"], "postprocess":
([content]) => new Property({ content: content.value }, content)
},
{"name": "Statement$subexpression$1", "symbols": ["_LogicalExpression"]},
{"name": "Statement$subexpression$1", "symbols": ["Assertion"]},
{"name": "Statement", "symbols": ["_", "Statement$subexpression$1", "_"], "postprocess":
([, [expression]]) => new Statement({ expression }, expression)
},
{"name": "Value", "symbols": ["string"], "postprocess":
([content]) => {
const { type, value } = content
const literal = type === 'quotedString'
return new Value({ content: value, literal }, content)
}
}
]
, ParserStart: "main"
}
if (typeof module !== 'undefined'&& typeof module.exports !== 'undefined') {
module.exports = grammar;
} else {
window.grammar = grammar;
}
})();