nested_comment = Delayed()
contents = (nested_comment | ~Lookahead(both) & Any())[:]
comment = start & contents & end > Comment
nested_comment += comment
more generally, it could be that it contains an error (in which case lepl is
backtracking like crazy because it is stuck) or some problem with the grammar
that i can't see, but which might be fixed by appropriate use of First().
andrew
> --
> You received this message because you are subscribed to the Google Groups "lepl" group.
> To post to this group, send email to le...@googlegroups.com.
> To unsubscribe from this group, send email to lepl+uns...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/lepl?hl=en.
>
[:] means repeat any number of times from 0 upwards.
that means that x[:] will successfully match an empty string.
that means that x[:][:] will sit for ever, repeatedly matching empty strings,
happy to be making progress.
now obviously you don't have anything that obviously wrong, but if
uncommenting a [:] leads to 100% cpu then it suggests that somwhere else that
thing (which can be empty) is being repeated, and the system is repetedly
matching the empty string.
the usual fix is to change [:] to [1:] (and perhaps that should be the default
when i next release a major release).
andrew
Ahh! *slap* that's it... so, the following works,
(and from here I should be able to fix the rest)
----
#! /usr/bin/env python
from lepl import *
start = Literal('(*')
end = Literal('*)')
# collect (as many as possible) symbols other than '(*' or '*)'
word = (~Lookahead(start|end)& Any()) #[1:,...]
parser = (start|end|word)[1:].get_parse()
print parser("foo(*bar(*baz*)meh*)teh")
----
Thanks Andrew!
Jelle.
So just for the record, should anyone be wondering, here is a parser
that deals with nested comments, including empty ones, and dropping the
delimiters.
Cheers!
#! /usr/bin/env python
from lepl import *
class Comment(Node): pass
start = Literal('(*')
end = Literal('*)')
# a word is any number of symbols not '(*' or '*)'
word = (~Lookahead(start|end) & Any())[1:,...]
nested_comment = Delayed()
contents = (nested_comment | word)[1:]
comment = ~start & (contents|Empty()) & ~end > Comment
nested_comment += comment
parser = nested_comment[1:].get_parse()
print parser("(*bar(*foo(*toz*)meh*)*)")[0]