Newer
Older
zweic / sources / zweic / Parser-partial.scala
/*  zweic -- a compiler for Zwei
 *
 *  Stephane Micheloud & LAMP
 *
 *  $Id$
 */

package zweic;


/**
 * This class implements the parser of the zwei compiler.
 */
class Parser(in: java.io.InputStream) extends Scanner(in) {
  import Tokens._;

  /**
   * Reports an error that a specific token was expected but
   * a different token found.
   */
  private def error(expected: Token): Unit =
    error("token of class " + expected);


  /**
   * Reports an error that a specific token was expected but
   * a different token found.
   */
  private def error(expected: String): Unit = {
    val message = "syntax error\n" +
      "expected: " + expected + "\n" +
      "found   : " + representation;
    Report.fail(start, message);
  }

  /**
   * If the current token corresponds to the token class 'expected'
   * then skip it, otherwise signal an error.
   */
  private def accept(expected: Token): Unit =
    if (token == expected)
      nextToken;
    else
      error(expected);

  /**
   * If the current token corresponds to the token class 'expected'
   * then skip it and return success.
   */
  private def check(expected: Token): Boolean =
    if (token == expected) {
      nextToken;
      true
    } else
      false;


  /**
   * Parse the input and return an abstract syntax tree.
   */
  def parse: Program = {
    val result = program;
    accept(EOF);
    result
  }

  /**
   * Program = { ClassDecl } Expression
   */
  private def program: Program = {
    // ... à compléter ...
  }

  // ... à compléter ...
}