Jason Corbett - 2013-09-16

I've tested most of the grammar, the problem seems to occur with the use of Forward for logical_operation in the following grammar. Am I using it wrong.

Here is the failure:

query.parseString('and(eq(a.b, 6),le(f,"foo"))')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/jcorbett/Development/slick/3.0.0.0.0/vpy/lib/python2.7/site-packages/pyparsing.py", line 1041, in parseString
raise exc
pyparsing.ParseException: Expected ")" (at char 14), (line:1, col:15)

Here is the grammar:

import pyparsing

comparison_method_name = pyparsing.oneOf(['eq','ne','lt','lte','gt','gte','in','nin','all','size','exists'])
string_comparison_method_name = pyparsing.oneOf(['exact','iexact','contains','icontains','startswith','istartswith','endswith','iendswith'])
logical_method_name = pyparsing.oneOf(['and','or','not'])

identifier = pyparsing.Word(pyparsing.alphas, pyparsing.alphanums + '_')
complex_identifier = identifier + pyparsing.ZeroOrMore(pyparsing.Literal('.') + identifier)

open_paren = pyparsing.Suppress('(')
close_paren = pyparsing.Suppress(')')

integer_value = pyparsing.Word(pyparsing.nums)
float_value = pyparsing.Regex(r'\d+\.\d+')
simple_value = (pyparsing.dblQuotedString | float_value | integer_value)

list_value = open_paren + simple_value + pyparsing.ZeroOrMore(pyparsing.Suppress(',') + simple_value) + close_paren

method_value = simple_value | list_value

string_comparison = pyparsing.Group(string_comparison_method_name + open_paren + complex_identifier + pyparsing.OneOrMore(pyparsing.Suppress(',') + pyparsing.dblQuotedString) + close_paren)
simple_comparison = pyparsing.Group(comparison_method_name + open_paren + complex_identifier + pyparsing.OneOrMore(pyparsing.Suppress(',') + method_value) + close_paren)

logical_operation = pyparsing.Forward()
operation = (simple_comparison | string_comparison | logical_operation)
logical_operation << (logical_method_name + open_paren + operation + pyparsing.ZeroOrMore(pyparsing.Suppress(',') + operation) + close_paren)

query = operation
 
Last edit: Jason Corbett 2013-09-16