Public
Edited
Mar 8, 2020
Importers
1 star
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
readEx = regex => str => {
const match = str.match(new RegExp(regex))
if (!match) return {error: true, expecting: regex}
const rest = str.substr(match[0].length)
return {error: false, read: [match[0]], rest}
}
Insert cell
Insert cell
readVar = readEx(`^${IDENTIFIER}`)
Insert cell
readOp = readEx(`^ ?${OP} ?`)
Insert cell
readBool = readEx(`^ (and|or) `)
Insert cell
Insert cell
compose2 = (f, g) => str => {
let a = f(str)
if (a.error) return a
let b = g(a.rest)
if (b.error) return {after: a, ...b}
return {error: false, read: [...a.read, ...b.read], rest: b.rest}
}
Insert cell
Insert cell
compose = (...fs) => fs.reduce(compose2)
Insert cell
Insert cell
readRelation = str => {
const res = compose(readVar, readOp, readVar)(str)
if (res.error) return res
return {...res, read: [res.read]}
}
Insert cell
readRelation('x.top = 10').read
Insert cell
Insert cell
Insert cell
interleave = (sepBy, terms) => str => {
const firstRead = terms(str)
if (firstRead.error) return firstRead
let rest = firstRead.rest
let read = firstRead.read
while(rest !== '') {
const a = sepBy(rest)
if (a.error) return {error: false, read: read, rest}
const b = terms(a.rest)
if (b.error) return b
rest = b.rest
read = [...read, ...a.read, ...b.read]
}
return {error: false, read, rest}
}
Insert cell
Insert cell
readRelations = interleave(readBool, readRelation)
Insert cell
readRelations('a.time = 1 and b.user = 2').read
Insert cell
Insert cell
dropChar = c => str => {
if (str[0] !== c) return {error: true, expecting: c}
return {error: false, read: [], rest: str.substr(1)}
}
Insert cell
readGroups = {
const readGroup = str => {
const res = compose(dropChar('('), readRelations, dropChar(')'))(str)
if (res.error) return res
return {...res, read: [res.read]}
}
return interleave(readBool, readGroup)
}
Insert cell
readGroups('(a.time = 1 and b.user = 2) and (b.user = 3)').read
Insert cell
readGroups(exampleStr).read
Insert cell
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more