You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a grammar where I want to be able to parse an empty line as a "null statement", like Python's pass. It's probably not strictly necessary for my language but it will be helpful during development.
Problem is, the two approaches I have tried don't work.
This code below ends up in an infinite loop:
importunittestclassPyParsingTests(unittest.TestCase):
deftest_compound_statements(self):
importpyparsingaspp# No warning is emittedpp.enable_all_warnings()
# doesn't matter whether I remove newline from the set of skippable whitespace characters, or not# pp.ParserElement.set_default_whitespace_chars(' \t')empty_line=pp.rest_of_linenull_statement=empty_line# Doesn't matter which of the two formulations below I use - same result in each case#compound_statement = pp.OneOrMore(null_statement)compound_statement=null_statement+null_statement[...]
# I know this is deprecated, but using here just in case. No RecursiveGrammarException is raised#compound_statement.validate()# Expected result here - parses 3 'empty_line' elements.# Observed result - seems to loop forevercompound_statement.parse_string("\n\n\n", parse_all=True)
# Same happens even without the parse_all#compound_statement.parse_string("\n\n\n")# And with whitespace in each line#compound_statement.parse_string(" \n \n \n")if__name__=='__main__':
unittest.main()
I guessed that this is because pp.rest_of_line does not consume the end of line character, meaning the parser would never make progress. This makes sense but I can't imagine the infinite loop is desired.
If I amend the empty_line definition to this: empty_line = pp.rest_of_line + "\n", then I get the following exceptions:
I am not sure how I would avoid the second exception (which seems to be complaining that it can't parse a 4th line, even though it only wants "one or more", and the first exception being unhandled before the second is thrown just looks like a bug.
The text was updated successfully, but these errors were encountered:
Looking at this this weekend. There is an odd behavior in the Python re module (which I use in the Regex class, and rest_of_line is implemented as Regex(".*"):
I have a grammar where I want to be able to parse an empty line as a "null statement", like Python's
pass
. It's probably not strictly necessary for my language but it will be helpful during development.Problem is, the two approaches I have tried don't work.
This code below ends up in an infinite loop:
I guessed that this is because
pp.rest_of_line
does not consume the end of line character, meaning the parser would never make progress. This makes sense but I can't imagine the infinite loop is desired.If I amend the
empty_line
definition to this:empty_line = pp.rest_of_line + "\n"
, then I get the following exceptions:I am not sure how I would avoid the second exception (which seems to be complaining that it can't parse a 4th line, even though it only wants "one or more", and the first exception being unhandled before the second is thrown just looks like a bug.
The text was updated successfully, but these errors were encountered: