working version

This commit is contained in:
Daniel Bulant 2024-02-18 12:17:09 +01:00
parent bd8728abdd
commit c1f38bace8
2 changed files with 33 additions and 19 deletions

View file

@ -125,7 +125,7 @@ const kwmap = new Map([
"outside", "outside",
"intersects", "intersects",
"<", ">", "<=", ">=", "<", ">", "<=", ">=",
"@@", "@" "@@"
], Comparison], ], Comparison],
[["&&", "and"], And], [["&&", "and"], And],
[["||", "or"], Or], [["||", "or"], Or],
@ -205,7 +205,7 @@ function isAlphaNum(ch: number) {
return ch >= Ch.A && ch <= Ch.Z || ch >= Ch.a && ch <= Ch.z || ch >= Ch._0 && ch <= Ch._9 || ch == Ch.Underscore return ch >= Ch.A && ch <= Ch.Z || ch >= Ch.a && ch <= Ch.z || ch >= Ch._0 && ch <= Ch._9 || ch == Ch.Underscore
} }
function readWord(input: InputStream, result?: string) { function readWord(input: InputStream, result: string) {
for (;;) { for (;;) {
if (input.next != Ch.Underscore && !isAlphaNum(input.next)) break if (input.next != Ch.Underscore && !isAlphaNum(input.next)) break
if (result != null) result += String.fromCharCode(input.next) if (result != null) result += String.fromCharCode(input.next)
@ -224,23 +224,37 @@ export const tokens = new ExternalTokenizer((input, stack) => {
let {next} = input; let {next} = input;
if(isAlpha(next)) { if(isAlpha(next)) {
input.advance() input.advance()
let word = readWord(input, String.fromCharCode(next)) let word = readWord(input, String.fromCharCode(next)).toLowerCase()
let word2: string | undefined = undefined let word2;
if (word != null) { if(["is", "not"].includes(word)) {
word = word.toLowerCase() // needs another word
if(["is", "not"].includes(word)) { skipSpaces(input)
// needs another word word2 = readWord(input, "")
skipSpaces(input) if(!word2) return;
word2 = readWord(input) word = word + " " + word2.toLowerCase()
if(!word2) return; }
word = word + " " + word2.toLowerCase() for(let [kws, token] of kwmap) {
if(kws.includes(word)) {
input.acceptToken(token)
return
} }
for(let [kws, token] of kwmap) { }
if(kws.includes(word)) { } else {
input.acceptToken(token) // no idea why this doesn't work generally, it fails the parser weirdly.
return // so this is used for special character operators only
let str = String.fromCharCode(next).toLowerCase();
while(allkws.find(kw => kw.startsWith(str))) {
if(allkws.includes(str)) {
for(let [kws, token] of kwmap) {
if(kws.includes(str)) {
input.advance()
input.acceptToken(token)
return
}
} }
} }
input.advance()
str += String.fromCharCode(input.next).toLowerCase()
} }
} }
}, {contextual: false}) }, {contextual: false})

View file

@ -1,4 +1,4 @@
import {baseParser} from "./dist/index.js" import {baseParser} from "./dist/index.js"
// console.log(baseParser.parse('select field from type::table($var) where $var > 1 and field2 @@ field1 timeout 10s').toString()) console.log(baseParser.parse('select field from type::table($var) where $var > 1 and field2 @1@ field1 timeout 10s').toString())
// console.log(baseParser.parse('select * from documents where contents @@ "test"').toString()) console.log(baseParser.parse('select * from documents where contents @@ "test"').toString())
console.log(baseParser.parse('select * from documents').toString()) console.log(baseParser.parse('select * from documents where test > 1').toString())