I need to parse expressions based on following rules:
- An expression can contain a filter object represented as
name:value
- An expression can contain a string expression
- An expression can contain Booleans OR,AND
- Everything inside can be quoted
So a typical expression looks like
filter1:45 hello world filter:5454
filter1:45 'hello world' filter:5454
hello world
'hello world' OR filter:43
Here's what I've tried so far:
class BooleanLiteral(Keyword):
grammar = Enum(K("OR"), K("AND"))
class LineFilter(Namespace):
grammar = flag('inverted', "-"), name(), ":", attr('value', word)
class LineExpression(List):
grammar = csl(LineFilter, separator=blank)
With this grammar, I can parse strings like
filter2:32 filter1:3243
From what I understood I can provide csl
function with a list of objects, and the grammar needs to be in that order. However what if I want to parse an object like
filter34:43 hello filter32:3232
OR
filter34:43 OR filter32:3232
How can I say that there are multiple types of objects (filters, expressions, booleans) in an expression? Is that possible with peg?
hello world
two tokens (hello
andworld
), or one? How do you answer the same question for"hello world"
? – Ellipsoidfilter OR filter
,expression OR expression
orexpression OR filter
. Otherwise there is any number of filters and expressions, and there is no order specified. 2) string literals and booleans can be quoted, not filters 3) Space is a normal separator. 4) quoted string = one token. sohello world
ishello
world
while"hello world"
ishello world
. Did I answer all of your questions? – Lafrance