Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disallow spaces in between literals in pyparsing?

grammar = Literal("from") + Literal(":") + Word(alphas)

The grammar needs to reject from : mary and only accept from:mary i.e. without any interleaving spaces. How can I enforce this in pyparsing ? Thanks

like image 363
Frankie Ribery Avatar asked Apr 24 '11 04:04

Frankie Ribery


1 Answers

Can you use Combine?

grammar = Combine(Literal("from") + Literal(":") + Word(alphas))

So then:

EDIT in response to your comment.

Really?

>>> grammar = pyparsing.Combine(Literal("from") + Literal(":") + Word(pyparsing.alphas))
>>> grammar.parseString('from : mary')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/pymodules/python2.6/pyparsing.py", line 1076, in parseString
    raise exc
pyparsing.ParseException: Expected ":" (at char 4), (line:1, col:5)
>>> grammar.parseString('from:mary')
(['from:mary'], {})
like image 52
sjr Avatar answered Nov 15 '22 13:11

sjr