1

I'm using the bychan library and this question mainly surrounds it.

I just checked out the minilang example and read through the code. When I saw the StatementNode, I was struggling to find a way to add an ending semicolon for conventional purpose. In that example, the ; is used to separate statements, but there isn't a way to end a script with the last statement ending with a ; because the parser isn't able to parse the end and would result in:

'Premature end reached' @  position ..., current lexeme is END ...

Is there a way to check whether the next token is the END or is what I'm thinking incorrect and looking ahead isn't the right thing to do and that there are other ways?

I did try parser.nextIs(EndToken.get()) before, but the IDE simply says no instance(s) of type variable(s) N exist so that EndToken<N> conforms to TokenKey

I may not be that advance in Java to figure out a solution. :(

from: https://github.com/atorstling/bychan/issues/37

Daniel Cheung
  • 4,779
  • 1
  • 30
  • 63

1 Answers1

0

This was an omission. The core parser PrattParserhas a peek() function to get the next token and you can compare it to any token type. You would use that as peek().getToken().equals(EndToken.get()).

The parser interface for the dynamic package, UserParserCallback, has another interface for the same thing: nextIs(TokenKey). The idea was to create a more typesafe equality check than javas builtin equals. The equality type was called TokenKey.

Now you ran into the problem that the static tokens, such as EndToken, didn't have any TokenKeys. The fix for issue 37 resolved this.

So now you can call parser.nextIs(EndToken.get().getKey()). For an example, see ReadmeExamples::statements.

`

Alexander Torstling
  • 18,552
  • 7
  • 62
  • 74