-
Notifications
You must be signed in to change notification settings - Fork 66
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
formatError for undefined tokens #114
Conversation
I don't really have a problem with for (let tok; tok = lexer.next();) {
try {
parser.eat(tok)
} catch (err) {
throw new Error(lexer.formatError(tok, "Syntax error"))
}
} which makes using it outside of the loop unintuitive and odd. I think it makes more sense to write the second call to lexer.formatError(null, "Unexpected EOF") or simply lexer.formatError("Unexpected EOF") Additionally, perhaps such a call should use the current lexer position rather than always use EOF; it would be odd to call formatError in the middle of the token stream and have the result point to its end. |
Agreed on all points! Thanks 😊 Sent with GitHawk |
moo.js
Outdated
@@ -544,7 +544,24 @@ | |||
} | |||
} | |||
|
|||
Lexer.prototype.makeEOF = function(type) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is makeEOF
useful for something other than formatError
(to which the user can just pass null
or undefined
)? It seems more like an implementation detail.
Furthermore, aren't line
and col
inconsistent with offset
when the lexer isn't actually at EOF? Should you be allowed to call makeEOF
in that scenario?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
aren't
line
andcol
inconsistent withoffset
when the lexer isn't actually at EOF?
That's a great point; I'm not sure what I was thinking.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are some styles of parser where it's useful to have an EOF token; I think that was the idea here. However I haven't yet needed makeEOF()
in practice, so I'm happy to remove it.
Sounds good. I think my comment above may still be relevant:
|
Awesome, thanks! LGTM |
It's fairly natural to write code of the form:
The second
formatError
call is not valid, becausetok
will beundefined
here. Moo usesundefined
to indicate that there are no more tokens, i.e. we've reached the end of the buffer.There's no way to get Moo to format an error at the end of the file, after the last token, without manually constructing an EOF token. I propose letting
formatError
acceptundefined
, and silently interpret it as an EOF token.Alternatively, we could introduce a
lexer.makeEOF()
method which returns this end-of-file token directly.