Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

URI Parse: IndexError: list index out of range #31

Closed
ixemad opened this issue Nov 14, 2016 · 3 comments
Closed

URI Parse: IndexError: list index out of range #31

ixemad opened this issue Nov 14, 2016 · 3 comments

Comments

@ixemad
Copy link

ixemad commented Nov 14, 2016

I'm trying to implement (pastebin LhSuFXhL) the URI grammar (RFC 3986) but I get an IndexError exception whether I code the host rule as indicated in the RFC.

As far as I know it could be solved whether I introduce the following changes in the grammar but I would like to be as accurate as possible.

def host():
    return Optional([ ip_literal, ipv4address, reg_name ])

def reg_name():
    return OneOrMore([ unreserved, pct_encoded, sub_delims])
@igordejanovic
Copy link
Member

You've hit an interesting case of nested empty parses. See unit test in the commit. There was a subtle bug triggered if nothing is matched in nested parses consisting of OrderedChoice and ZeroOrMore.

The authority rule if completely optional (indirectly), meaning that it will match empty strings. I'm not sure is this what you really want, or what RFC suggest?

Get the current master version. It parses now but you will get empty parse tree for illegal inputs. That is not what you want I guess. It is usually a good idea to terminate your grammar root rule with EOF match. That means that you must be at the end of your input when done parsing. If that is not the case than your have superfluous characters in the input and NoMatch should be raised.

def authority():
    return Optional(userinfo, '@'), host, Optional(':', port), EOF

@ixemad
Copy link
Author

ixemad commented Nov 15, 2016

I want to implement the full grammar of several RFCs so it's not possible to append EOF to authority. I also wondered how authority [1], host [2] and reg-name [3] are defined. Anyway, glad to see it's solved in the current master.

Kind regards!

[1] https://tools.ietf.org/html/rfc3986#section-3.2
[2] https://tools.ietf.org/html/rfc3986#section-3.2.2
[3] https://tools.ietf.org/html/rfc3986#page-21

@igordejanovic
Copy link
Member

Ah, right. authority is not your grammar root rule. Anyway, when you finish your grammar your top level rule should be something like:

def grammar(): return URI, EOF
def URI(): return ....

Now, your real RFC grammar starts from URI but your root rule used in ParserPython will include EOF at the end to signify that you are interested in parsing the whole input always. This will prevent successful parses for URIs that have superfluous characters at the end.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants