declaresub / abnf

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

Problem parsing IPv6 addresses #10

Closed vphpersson closed 2 years ago

vphpersson commented 2 years ago

Hi!

It does not seem to parse valid IPv6 addresses I'm providing:

>>> from abnf.grammars import rfc3986
>>> s = '2001:db8:cafe::17'
>>> rfc3986.Rule('IPv6address').parse_all(s)
Traceback (most recent call last):
  File "/home/vph/code/py/http_lib/venv/lib/python3.10/site-packages/abnf/parser.py", line 350, in parse
    node, new_start = self.definition.parse(source, start)
  File "/home/vph/code/py/http_lib/venv/lib/python3.10/site-packages/abnf/parser.py", line 82, in _parse_longest_match
    raise ParseError(self, start)
abnf.parser.ParseError: Alternation(Concatenation(Repetition(Repeat(6, 6), Concatenation(Rule('h16'), Literal(':'))), Rule('ls32')), Concatenation(Literal('::'), Repetition(Repeat(5, 5), Concatenation(Rule('h16'), Literal(':'))), Rule('ls32')), Concatenation(Option(Rule('h16')), Literal('::'), Repetition(Repeat(4, 4), Concatenation(Rule('h16'), Literal(':'))), Rule('ls32')), Concatenation(Option(Concatenation(Repetition(Repeat(0, 1), Concatenation(Rule('h16'), Literal(':'))), Rule('h16'))), Literal('::'), Repetition(Repeat(3, 3), Concatenation(Rule('h16'), Literal(':'))), Rule('ls32')), Concatenation(Option(Concatenation(Repetition(Repeat(0, 2), Concatenation(Rule('h16'), Literal(':'))), Rule('h16'))), Literal('::'), Repetition(Repeat(2, 2), Concatenation(Rule('h16'), Literal(':'))), Rule('ls32')), Concatenation(Option(Concatenation(Repetition(Repeat(0, 3), Concatenation(Rule('h16'), Literal(':'))), Rule('h16'))), Literal('::'), Rule('h16'), Literal(':'), Rule('ls32')), Concatenation(Option(Concatenation(Repetition(Repeat(0, 4), Concatenation(Rule('h16'), Literal(':'))), Rule('h16'))), Literal('::'), Rule('ls32')), Concatenation(Option(Concatenation(Repetition(Repeat(0, 5), Concatenation(Rule('h16'), Literal(':'))), Rule('h16'))), Literal('::'), Rule('h16')), Concatenation(Option(Concatenation(Repetition(Repeat(0, 6), Concatenation(Rule('h16'), Literal(':'))), Rule('h16'))), Literal('::'))): 0

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

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/vph/code/py/http_lib/venv/lib/python3.10/site-packages/abnf/parser.py", line 382, in parse_all
    node, start = self.parse(source, 0)
  File "/home/vph/code/py/http_lib/venv/lib/python3.10/site-packages/abnf/parser.py", line 362, in parse
    raise ParseError(self, start) from e
abnf.parser.ParseError: Rule('IPv6address'): 0
>>>
>>> s = '2001:0db8:0000:0000:0000:ff00:0042:8329'
>>> rfc3986.Rule('IPv6address').parse_all(s)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/vph/code/py/http_lib/venv/lib/python3.10/site-packages/abnf/parser.py", line 384, in parse_all
    raise ParseError(self, start)
abnf.parser.ParseError: Rule('IPv6address'): 24

Am I doing something wrong?

I'm guessing it is related to the missing support for backtracking?

declaresub commented 2 years ago

This is almost certainly due to a bug I introduced.

declaresub commented 2 years ago

As noted above, the fix is in. I will release a new version to pypi soon. Thanks for the bug..

vphpersson commented 2 years ago

Thank you for a nice library!

vphpersson commented 2 years ago

The 2001:db8:cafe::17 address is still not being accepted though.