Created
June 6, 2023 22:50
-
-
Save chadulous/3d4e66ba785bc015e6e2692e1f4b706e to your computer and use it in GitHub Desktop.
Function Query language
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
function buildQuery(table, modifiers) { | |
const query = modifiers.shift() | |
let amount; | |
if(query.name === 'select') { | |
amount = modifiers.shift(); | |
checkview: if(options.viewsEnabled) { | |
if(!query.arguments?.view) { | |
query.arguments.view = 'default' | |
break checkview; | |
} | |
if(typeof query.arguments?.view !== 'string') return error('view parameter must be of type string.') | |
} | |
}; | |
const queryObject = { | |
type: 'Query', | |
of: table, | |
queryType: query.name, | |
selectAmount: amount?.name, | |
selectView: query.arguments?.view, | |
modifiers, | |
}; | |
return queryObject; | |
} | |
function buildFunction(name, args) { | |
return { | |
type: 'Call', | |
name, | |
arguments: args | |
} | |
} | |
function buildArgs(values) { | |
return values.reduce((a, v) => { | |
return { ...a, [v.name]: v.value } | |
}, {}) | |
} | |
function buildVariable(name) { | |
return { | |
type: 'Variable', | |
name, | |
} | |
} | |
} | |
query = table:identifier '.' modifiers:(func)|.., '.'| semicolon { return buildQuery(table, modifiers) } | |
func 'expression call' = name:identifier "("_ args:args _")" { return buildFunction(name, args) } | |
args 'argument list' = args:(name:identifier _ ':' _ value:(variable / str / num) { return { | |
name, | |
value | |
} })|.., _ ',' _| { return buildArgs(args) } | |
variable 'variable getter' = 'var' "(" _ name:identifier _ ")" { return buildVariable(name) } | |
str 'string literal' = "'" value:($("\\'" / [^'])*) "'" { return value } | |
num 'number literal' = value:($[0-9]+) { return parseInt(value) } | |
identifier 'identifier' = value:($valid+) { return value } | |
valid "identifier" = [^\\\/\?\&( \t\r)\.\:] | |
semicolon 'semicolon' = ';' { return /* Should never evaluate to anything. */ } | |
_ 'whitespace' = [ ]* { return /* Should never evaluate to anything. */ } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment