Class Lexer


  • public final class Lexer
    extends Object
    Hand-written Java tokenizer with unbounded lookahead, backed by a buffer of already-scanned tokens.
    • Constructor Detail

      • Lexer

        public Lexer​(String source)
        Creates a lexer positioned at line 1, column 1 of the source.
        Parameters:
        source - Java source text to tokenize
    • Method Detail

      • nextToken

        public Token nextToken()
        Consumes the next token, taking it from the lookahead buffer if one is queued.
        Returns:
        the consumed token, EOF once the source is exhausted
      • peek

        public Token peek()
        Returns:
        the next token without consuming it
      • peekAhead

        public Token peekAhead​(int offset)
        Looks past the current token, scanning into the lookahead buffer as needed.
        Parameters:
        offset - how many tokens ahead to look, 0 being the next one
        Returns:
        the token at that distance
      • current

        public Token current()
        Returns:
        the token last returned by nextToken, or null before the first call
      • currentPosition

        public SourcePosition currentPosition()
        Returns:
        the line, column and offset of the scanner head, which is past any buffered lookahead