-
Notifications
You must be signed in to change notification settings - Fork 0
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
Semicolon as statement ender #7
Conversation
src/parse.ts
Outdated
const list = [element()]; | ||
while (separator()) { | ||
while (lexer.token() !== Token.EOF) { |
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.
I think this function should be the one checking for semicolons, not parseStatement.
Although it would be best to pass in a predicate like separator
, except named terminator
or something.
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.
@sandersn Not sure if I got the exercise right this time. imteekay/mini-typescript@b74b6fc
The whole idea was
- Add a predicate function (
terminator
) to handle semicolons - after a semicolon token, we can have
- another semicolon: I considered that an empty statement (Allow var to have multiple declarations mini-typescript#2)
- just added a
peek
function to see the next token and if it's indeed a semicolon, call the parse statement to get theEmptyStatement
node
- just added a
- the rest of the tokens (string, number, eof): just parse a statement
- another semicolon: I considered that an empty statement (Allow var to have multiple declarations mini-typescript#2)
Does that make sense? Maybe it can be improved for this particular case.
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.
Here's the way Typescript does it, as I recall:
const list = []
while (peek()) {
list.push(element())
terminator()
}
where peek
returns true if the current token could be the start of a new Statement, and terminator
doesn't consume a token if terminators are optional. This relies on the first token of statements to be distinguishable from from non-statements, but the only non-statement that can come after a statement list is Token.EOF, so peek
is just going to be () => lexer.token !== Token.EOF
This way is a bit simpler than yours, and generalises to other kinds of lists, like parameter lists, arrays, etc.
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.
Simplified it, the implementation seems much better now! ✨ imteekay/mini-typescript@cad1fec
3582543
to
0bfa6da
Compare
Changes
Approach