declaresub / abnf

Python parser generator for ABNF grammars
MIT License
41 stars 9 forks source link

Bug: from_file breaking #12

Closed midirhee12 closed 2 years ago

midirhee12 commented 2 years ago

After attempting to parse the following abnf:

lowercase   = %x61-7A

and running the following code:

from abnf.parser import Rule

class FileRule(Rule):
    pass

FileRule.from_file('file.abnf')

I got the following error:

Traceback (most recent call last):
  File "/usr/lib/python3.10/site-packages/abnf/parser.py", line 354, in parse
    node, new_start = self.definition.parse(source, start)
  File "/usr/lib/python3.10/site-packages/abnf/parser.py", line 283, in parse
    raise ParseError(self, start)
abnf.parser.ParseError: Repetition(Repeat(1, None), Alternation(ABNFGrammarRule('rule'), Concatenation(Repetition(Repeat(0, None), ABNFGrammarRule('c-wsp')), ABNFGrammarRule('c-nl')))): 0

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.10/site-packages/abnf/parser.py", line 426, in from_file
    node = ABNFGrammarRule("rulelist").parse_all(src)
  File "/usr/lib/python3.10/site-packages/abnf/parser.py", line 386, in parse_all
    node, start = self.parse(source, 0)
  File "/usr/lib/python3.10/site-packages/abnf/parser.py", line 366, in parse
    raise ParseError(self, start) from e
abnf.parser.ParseError: ABNFGrammarRule('rulelist'): 0
declaresub commented 2 years ago

if you are loading the rule from file.abnf, make sure that the rule is terminated with a CRLF. Rule.from_file expects the file contents to be a rulelist as defined in RFC 5234.

midirhee12 commented 2 years ago

Hmm, I am on linux, so that could be possible. I thought I had converted LF to CRLF properly. Let me check.

midirhee12 commented 2 years ago

Ah, thanks. Yeah, I had my editor (neovim) rewriting it after save. I should have had looked at that first.

For other vim or neovim users, just set ff=dos, and you'll be good to go.