thinql-parse
Version:
Parse ThinQL queries
144 lines (133 loc) • 9.21 kB
JavaScript
// Generated automatically by nearley, version 2.16.0
// http://github.com/Hardmath123/nearley
(function () {
function id(x) { return x[0]; }
const { lexer } = require('./lexer')
const {
Call,
ComparisonOperator,
Condition,
FullTextSearch,
Group,
LogicalExpression,
LogicalOperator,
Property,
Value,
} = require('./types')
const extractExpression = ([firstAssertion, moreAssertions]) => {
if (moreAssertions.length === 0) {
return firstAssertion
}
const buildLogicalExpression = () => {
const [, operator, assertion] = moreAssertions.pop()
return new LogicalExpression({
operator: operator
? operator[0]
: new LogicalOperator({ kind: LogicalOperator.symbolFor('and') }, null),
left: moreAssertions.length > 0 ? buildLogicalExpression() : firstAssertion,
right: assertion,
}, assertion.token)
}
return buildLogicalExpression()
}
const extractComparisonOperator = ([token]) => (
new ComparisonOperator({ kind: token.type }, token)
)
const extractLogicalOperator = ([token]) => (
new LogicalOperator({ kind: token.type }, token)
)
var grammar = {
Lexer: lexer,
ParserRules: [
{"name": "main", "symbols": ["_", "MainExpression"], "postprocess": ([, expression]) => expression},
{"name": "MainExpression$ebnf$1", "symbols": []},
{"name": "MainExpression$ebnf$1$subexpression$1$ebnf$1$subexpression$1", "symbols": ["LogicalOperator", "Space"]},
{"name": "MainExpression$ebnf$1$subexpression$1$ebnf$1", "symbols": ["MainExpression$ebnf$1$subexpression$1$ebnf$1$subexpression$1"], "postprocess": id},
{"name": "MainExpression$ebnf$1$subexpression$1$ebnf$1", "symbols": [], "postprocess": function(d) {return null;}},
{"name": "MainExpression$ebnf$1$subexpression$1", "symbols": ["Space", "MainExpression$ebnf$1$subexpression$1$ebnf$1", "Assertion"]},
{"name": "MainExpression$ebnf$1", "symbols": ["MainExpression$ebnf$1", "MainExpression$ebnf$1$subexpression$1"], "postprocess": function arrpush(d) {return d[0].concat([d[1]]);}},
{"name": "MainExpression", "symbols": ["Assertion", "MainExpression$ebnf$1", (lexer.has("eof") ? {type: "eof"} : eof)], "postprocess": extractExpression},
{"name": "Assertion", "symbols": ["Condition"], "postprocess": id},
{"name": "Assertion", "symbols": ["FullTextSearch"], "postprocess": id},
{"name": "Assertion", "symbols": ["Group"], "postprocess": id},
{"name": "Assertion", "symbols": ["StandaloneCall"], "postprocess": id},
{"name": "Group$subexpression$1$ebnf$1", "symbols": []},
{"name": "Group$subexpression$1$ebnf$1$subexpression$1$ebnf$1$subexpression$1", "symbols": ["LogicalOperator", "Space"]},
{"name": "Group$subexpression$1$ebnf$1$subexpression$1$ebnf$1", "symbols": ["Group$subexpression$1$ebnf$1$subexpression$1$ebnf$1$subexpression$1"], "postprocess": id},
{"name": "Group$subexpression$1$ebnf$1$subexpression$1$ebnf$1", "symbols": [], "postprocess": function(d) {return null;}},
{"name": "Group$subexpression$1$ebnf$1$subexpression$1", "symbols": ["Space", "Group$subexpression$1$ebnf$1$subexpression$1$ebnf$1", "Assertion"]},
{"name": "Group$subexpression$1$ebnf$1", "symbols": ["Group$subexpression$1$ebnf$1", "Group$subexpression$1$ebnf$1$subexpression$1"], "postprocess": function arrpush(d) {return d[0].concat([d[1]]);}},
{"name": "Group$subexpression$1", "symbols": ["Assertion", "Group$subexpression$1$ebnf$1"]},
{"name": "Group", "symbols": [(lexer.has("openGroup") ? {type: "openGroup"} : openGroup), "_", "Group$subexpression$1", "_", (lexer.has("closeGroup") ? {type: "closeGroup"} : closeGroup)], "postprocess": ([openGroup, , rawExpression]) => new Group({
expression: extractExpression(rawExpression),
negate: openGroup.value.negate,
}, openGroup) },
{"name": "Condition$subexpression$1", "symbols": ["Property"]},
{"name": "Condition$subexpression$1", "symbols": ["ComparisonCall"]},
{"name": "Condition", "symbols": ["Condition$subexpression$1", "_", "ComparisonOperator", "_", "ConditionValue"], "postprocess": ([[property], , operator, , value]) => (
new Condition({ property, operator, value }, property.token)
) },
{"name": "Property", "symbols": ["StringWithoutFlags"], "postprocess": ([string]) => new Property({ name: string.value }, string)},
{"name": "ConditionValue", "symbols": ["Value"], "postprocess": id},
{"name": "ConditionValue", "symbols": ["ComparisonCall"], "postprocess": id},
{"name": "Value", "symbols": ["StringWithFlags"], "postprocess": ([string]) => (
new Value({ value: string.value.value, flags: string.value.flags }, string)
) },
{"name": "FullTextSearch", "symbols": ["Value"], "postprocess": ([value]) => new FullTextSearch({ value, negate: false }, value.token)},
{"name": "FullTextSearch", "symbols": [(lexer.has("negatedFullTextSearch") ? {type: "negatedFullTextSearch"} : negatedFullTextSearch)], "postprocess": ([string]) => new FullTextSearch({
value: new Value({ value: string.value.value, flags: string.value.flags }, string),
negate: true,
}, string.token) },
{"name": "StringWithoutFlags", "symbols": [(lexer.has("quotedStringWithoutFlags") ? {type: "quotedStringWithoutFlags"} : quotedStringWithoutFlags)], "postprocess": id},
{"name": "StringWithoutFlags", "symbols": [(lexer.has("unquotedString") ? {type: "unquotedString"} : unquotedString)], "postprocess": id},
{"name": "StringWithFlags", "symbols": ["StringWithoutFlags"], "postprocess": ([string]) => ({ ...string, value: { value: string.value, flags: '' } })},
{"name": "StringWithFlags", "symbols": [(lexer.has("quotedStringWithFlags") ? {type: "quotedStringWithFlags"} : quotedStringWithFlags)], "postprocess": id},
{"name": "StandaloneCall", "symbols": [(lexer.has("negatedCallee") ? {type: "negatedCallee"} : negatedCallee), "CallArgs"], "postprocess": ([string, args]) => new Call({
callee: string.value,
negate: true,
arguments: args,
}, string) },
{"name": "StandaloneCall", "symbols": ["ComparisonCall"], "postprocess": id},
{"name": "ComparisonCall", "symbols": [(lexer.has("unquotedString") ? {type: "unquotedString"} : unquotedString), "CallArgs"], "postprocess": ([string, args]) => new Call({
callee: string.value,
negate: null,
arguments: args,
}, string) },
{"name": "CallArgs$ebnf$1$subexpression$1$ebnf$1", "symbols": []},
{"name": "CallArgs$ebnf$1$subexpression$1$ebnf$1$subexpression$1", "symbols": ["Space", "ConditionValue"]},
{"name": "CallArgs$ebnf$1$subexpression$1$ebnf$1", "symbols": ["CallArgs$ebnf$1$subexpression$1$ebnf$1", "CallArgs$ebnf$1$subexpression$1$ebnf$1$subexpression$1"], "postprocess": function arrpush(d) {return d[0].concat([d[1]]);}},
{"name": "CallArgs$ebnf$1$subexpression$1", "symbols": ["ConditionValue", "CallArgs$ebnf$1$subexpression$1$ebnf$1"]},
{"name": "CallArgs$ebnf$1", "symbols": ["CallArgs$ebnf$1$subexpression$1"], "postprocess": id},
{"name": "CallArgs$ebnf$1", "symbols": [], "postprocess": function(d) {return null;}},
{"name": "CallArgs", "symbols": [{"literal":"("}, "_", "CallArgs$ebnf$1", "_", {"literal":")"}], "postprocess": ([, , possibleArgs]) => {
let args = []
if (possibleArgs) {
const [firstArg, moreArgs] = possibleArgs
args = [
firstArg,
...moreArgs.map(([, value]) => value),
]
}
return args
} },
{"name": "ComparisonOperator", "symbols": [(lexer.has("eq") ? {type: "eq"} : eq)], "postprocess": extractComparisonOperator},
{"name": "ComparisonOperator", "symbols": [(lexer.has("ne") ? {type: "ne"} : ne)], "postprocess": extractComparisonOperator},
{"name": "ComparisonOperator", "symbols": [(lexer.has("lt") ? {type: "lt"} : lt)], "postprocess": extractComparisonOperator},
{"name": "ComparisonOperator", "symbols": [(lexer.has("lte") ? {type: "lte"} : lte)], "postprocess": extractComparisonOperator},
{"name": "ComparisonOperator", "symbols": [(lexer.has("gt") ? {type: "gt"} : gt)], "postprocess": extractComparisonOperator},
{"name": "ComparisonOperator", "symbols": [(lexer.has("gte") ? {type: "gte"} : gte)], "postprocess": extractComparisonOperator},
{"name": "ComparisonOperator", "symbols": [(lexer.has("match") ? {type: "match"} : match)], "postprocess": extractComparisonOperator},
{"name": "LogicalOperator", "symbols": [(lexer.has("and") ? {type: "and"} : and)], "postprocess": extractLogicalOperator},
{"name": "LogicalOperator", "symbols": [(lexer.has("or") ? {type: "or"} : or)], "postprocess": extractLogicalOperator},
{"name": "_", "symbols": []},
{"name": "_", "symbols": ["Space"]},
{"name": "Space", "symbols": [(lexer.has("space") ? {type: "space"} : space)], "postprocess": () => null}
]
, ParserStart: "main"
}
if (typeof module !== 'undefined'&& typeof module.exports !== 'undefined') {
module.exports = grammar;
} else {
window.grammar = grammar;
}
})();