package minijava.parser import minijava.lexer.Lexer import java.io.{InputStream,IOException} /** LL(2) parser for the MiniJava+ grammar. */ trait Parser extends Lexer { self: Reporter => import Trees._ import minijava.lexer.Tokens._ def parseInputStream(in: InputStream): Tree = { setInputStream(in) // from Lexer /* ... */ val tree: Tree = parseGoal tree } /** Store the currentToken (first lookahead), and upcomingToken (second lookahead), as read from the lexer. */ private var currentToken: Token = Token(BAD) private var upcomingToken: Token = Token(BAD) def readToken: Unit = { /* update currentToken and upcomingToken accordingly, using nextToken from the Lexer */ } /** ''Eats'' the expected token, or terminates with an error. */ private def eat(tokenClass: TokenClass): Unit = { // ... } /** Complains that what was found was not expected. The method accepts arbitrarily many arguments of type TokenClass. */ private def expected(tokenClass: TokenClass, more: TokenClass*): Nothing = { fatalError("expected: " + (tokenClass::more.toList).mkString(" or ") + ", found: " + currentToken, currentToken) } private def parseGoal: Tree = { // ... } /* etc. */ /* One example: */ private def parseIdentifier: Identifier = currentToken.info match { case ID(value) => { val ret = Identifier(value).setPos(currentToken); readToken; ret } case _ => expected(IDCLASS) } }