-
Notifications
You must be signed in to change notification settings - Fork 1
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
Missing closing bracket leads to crash of js sql-parse #66
Comments
Interestingly, in this situation when collecting errors, antrl4 gives us an None def syntaxError(self, recognizer, offendingSymbol, line, column, msg, e):
error = ParsingException(
msg=msg,
offending_token=offendingSymbol,
e=e, # <--------------------
query=e.ctx.parser.getTokenStream().getText(e.ctx.start, e.offendingToken.tokenIndex)
)
raise error
We still have the error message and could just compose the exception ourselves, the only issue is that we currently use the |
Potential fix: def syntaxError(self, recognizer, offendingSymbol, line, column, msg, e):
if e:
query = recognizer.getTokenStream().getText(e.ctx.start, offendingSymbol.tokenIndex)
else:
# If antlr4 doesn't give us an error object, we heuristically create a query, or a piece of it
# so we increase the chances of it being correctly assigned.
# It means that theoretically if you input two wrong queries that antlr4 manages
# to distinguish as two different statements (which is hard already), and both are similar
# the errors could be matched wrongly. Still pretty rare, and it is very hard to come up with
# an actual query that does it.
# The newly generated query will be either the offendingToken + one token to the left
# or offendingToken + two tokens to the left, if the second is possible it takes precedence.
min_token_to_check = max(1, offendingSymbol.tokenIndex - 2)
tokens = recognizer.getTokenStream().tokens[
min_token_to_check: offendingSymbol.tokenIndex:offendingSymbol.tokenIndex + 1]
query = "".join(map(lambda x: x.text, tokens))
error = ParsingException(
msg=msg,
offending_token=offendingSymbol,
e=e if e else type('NotViableInput', (Exception,), {})(),
query=query
)
self.errors.append(error) |
1 task
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example:
Leads to:
The text was updated successfully, but these errors were encountered: