parsing escaped characters

52 views
Skip to first unread message

Dan S

unread,
Sep 8, 2012, 2:00:46 PM9/8/12
to modgr...@googlegroups.com
Hi - first steps with this module, and I'm trying to work out how to parse doublequoted strings, where a doublequote can be included if escaped with a backslash.
"This is an \"example\" of something that should parse OK"

The module has NOT_FOLLOWED_BY, but it seems like NOT_PRECEDED_BY would be good for this case...? Trying to think of a neat way to express this in modgrammar. So far all I have is:

class StringLiteral(Grammar):
    # TODO: escaped double-quotes would break this.
    grammar = ('"', OPTIONAL(WORD('^"')), '"')


Dan S

unread,
Sep 8, 2012, 4:22:15 PM9/8/12
to modgr...@googlegroups.com
Found one way:

grammar = ('"', ZERO_OR_MORE(WORD('^"') | WORD('^\\', '"', count=2)), '"')

iris...@gmail.com

unread,
Dec 7, 2012, 9:46:54 AM12/7/12
to modgr...@googlegroups.com

There is a QuotedString class in the extras module that may do what you would like.
Reply all
Reply to author
Forward
0 new messages