ast constructed
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
import express from 'express'
|
import express from 'express'
|
||||||
import { json } from 'body-parser'
|
import { json } from 'body-parser'
|
||||||
import { Sequelize } from 'sequelize'
|
import { Sequelize, Op } from 'sequelize'
|
||||||
import { database, Character, Game, Pick, App } from './db'
|
import { database, Character, Game, Pick, App } from './db'
|
||||||
import { lexr, parseOrderByString, FilterParser } from './tokenizer'
|
import { lexr, parseOrderByString, FilterParser } from './tokenizer'
|
||||||
|
|
||||||
@@ -46,7 +46,10 @@ app.post('/api/game', jsonParser, async (req, res) => {
|
|||||||
const gameData = await Game.findAll({
|
const gameData = await Game.findAll({
|
||||||
offset: page * count,
|
offset: page * count,
|
||||||
limit: count,
|
limit: count,
|
||||||
order: orderBy
|
order: orderBy,
|
||||||
|
where: {
|
||||||
|
id: { [Op.eq]: 2 }
|
||||||
|
}
|
||||||
})
|
})
|
||||||
const pageCount = Math.ceil((await Character.count()) / count)
|
const pageCount = Math.ceil((await Character.count()) / count)
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import Tokenizr from 'tokenizr'
|
import Tokenizr from 'tokenizr'
|
||||||
import ASTY from 'asty'
|
import ASTY from 'asty'
|
||||||
|
import { Op } from 'sequelize'
|
||||||
|
|
||||||
export const lexr = new Tokenizr()
|
export const lexr = new Tokenizr()
|
||||||
|
|
||||||
@@ -55,6 +56,14 @@ const conjunctinoRegex = /AND|OR/
|
|||||||
const equalityRegex = /([a-zA-Z]+)\s?(=|!=|<|>|<=|>=|:)\s?([a-zA-Z\d"']+)/
|
const equalityRegex = /([a-zA-Z]+)\s?(=|!=|<|>|<=|>=|:)\s?([a-zA-Z\d"']+)/
|
||||||
const spacerRegex = /\s/
|
const spacerRegex = /\s/
|
||||||
|
|
||||||
|
const opperatorMap = {
|
||||||
|
'=': Op.eq,
|
||||||
|
'!=': Op.ne,
|
||||||
|
'<=': Op.lte,
|
||||||
|
'>=': Op.gte,
|
||||||
|
':': Op.like
|
||||||
|
}
|
||||||
|
|
||||||
export class FilterParser {
|
export class FilterParser {
|
||||||
asty: ASTY = new ASTY()
|
asty: ASTY = new ASTY()
|
||||||
lexer: Tokenizr = this.createLexer()
|
lexer: Tokenizr = this.createLexer()
|
||||||
@@ -91,37 +100,61 @@ export class FilterParser {
|
|||||||
|
|
||||||
parseFilter(filter: string) {
|
parseFilter(filter: string) {
|
||||||
this.lexer.input(filter)
|
this.lexer.input(filter)
|
||||||
let block = this.parseBlock()
|
console.log(`parsing ${filter}`)
|
||||||
|
this.lexer.begin()
|
||||||
|
this.lexer.tokens().forEach((token) => {
|
||||||
|
console.log(token)
|
||||||
|
})
|
||||||
|
this.lexer.rollback()
|
||||||
|
let block = this.parseBlock('AND')
|
||||||
this.lexer.consume('EOF')
|
this.lexer.consume('EOF')
|
||||||
this.lexer.reset()
|
this.lexer.reset()
|
||||||
|
|
||||||
return block
|
return block
|
||||||
}
|
}
|
||||||
|
|
||||||
private parseBlock() {
|
private parseBlock(conjunciton: string) {
|
||||||
let items = []
|
let items = []
|
||||||
|
let activeCon = conjunciton
|
||||||
|
|
||||||
for(;;) {
|
for (;;) {
|
||||||
let nextItem = this.lexer.alternatives(
|
let nextItem = this.lexer.alternatives(
|
||||||
() => this.parseEquality(),
|
() => this.parseEquality(),
|
||||||
() => this.parseGroup(),
|
() => this.parseGroup(),
|
||||||
() => this.parseConjunction())
|
() => {
|
||||||
|
|
||||||
if (nextItem===undefined) {
|
console.log("Conjunct")
|
||||||
break;
|
|
||||||
|
let conToken = this.lexer.consume('conjunction')
|
||||||
|
|
||||||
|
console.log("potato")
|
||||||
|
if (items.length === 1) {
|
||||||
|
activeCon = conToken.value
|
||||||
|
}
|
||||||
|
|
||||||
|
if (conToken.value === activeCon) {
|
||||||
|
return this.parseEquality()
|
||||||
|
} else {
|
||||||
|
return this.parseBlock(conToken.value)
|
||||||
|
}
|
||||||
|
}, () => {}
|
||||||
|
)
|
||||||
|
|
||||||
|
if (nextItem === undefined) {
|
||||||
|
console.log("breaking")
|
||||||
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
items.push(nextItem)
|
items.push(nextItem)
|
||||||
}
|
}
|
||||||
|
|
||||||
return items
|
return this.asty.create('conjunction').set('type', activeCon).add(items)
|
||||||
}
|
}
|
||||||
|
|
||||||
private parseConjunction() {
|
private parseConjunction() {}
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
private parseEquality() {
|
private parseEquality() {
|
||||||
|
console.log("doing an equality")
|
||||||
let columnToken = this.lexer.consume('column')
|
let columnToken = this.lexer.consume('column')
|
||||||
let opperatorToken = this.lexer.consume('opperator')
|
let opperatorToken = this.lexer.consume('opperator')
|
||||||
let valueToken = this.lexer.consume('value')
|
let valueToken = this.lexer.consume('value')
|
||||||
@@ -133,17 +166,20 @@ export class FilterParser {
|
|||||||
node.set(columnToken.type, columnToken.value)
|
node.set(columnToken.type, columnToken.value)
|
||||||
node.set(opperatorToken.type, opperatorToken.value)
|
node.set(opperatorToken.type, opperatorToken.value)
|
||||||
node.set(valueToken.type, valueToken.value)
|
node.set(valueToken.type, valueToken.value)
|
||||||
|
node.set('expression', {
|
||||||
|
[columnToken.value]: {
|
||||||
|
[opperatorMap[opperatorToken.value]]: valueToken.value
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
return node
|
return node
|
||||||
}
|
}
|
||||||
|
|
||||||
private parseGroup() {
|
private parseGroup() {
|
||||||
this.lexer.consume('opengroup')
|
this.lexer.consume('opengroup')
|
||||||
let block = this.parseBlock()
|
let block = this.parseBlock('AND')
|
||||||
this.lexer.consume('closegroup')
|
this.lexer.consume('closegroup')
|
||||||
|
|
||||||
let node = this.asty.create("group").set({ns: ""}).add(block)
|
return block
|
||||||
|
|
||||||
return node
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user