/**
   * Parse a term.
   *
   * @param token the initial token.
   * @return the root of the generated parse subtree.
   * @throws Exception if an error occurred.
   */
  private ICodeNode parseTerm(Token token) throws Exception {
    // Parse a factor and make its node the root node.
    ICodeNode rootNode = parseFactor(token);

    token = currentToken();
    TokenType tokenType = token.getType();

    // Loop over multiplicative operators.
    while (MULT_OPS.contains(tokenType)) {

      // Create a new operator node and adopt the current tree
      // as its first child.
      ICodeNodeType nodeType = MULT_OPS_OPS_MAP.get(tokenType);
      ICodeNode opNode = ICodeFactory.createICodeNode(nodeType);
      opNode.addChild(rootNode);

      token = nextToken(); // consume the operator

      // Parse another factor.  The operator node adopts
      // the term's tree as its second child.
      opNode.addChild(parseFactor(token));

      // The operator node becomes the new root node.
      rootNode = opNode;

      token = currentToken();
      tokenType = token.getType();
    }

    return rootNode;
  }
  public void parse(Token token) throws Exception {
    token = synchronize(IDENTIFIER_SET);

    // 遍历由分号分割的一系列变量申明
    while (token.getType() == IDENTIFIER) {

      // 解析每一个子表标识申明,比如 a,b,c : int
      parseIdentifierSublist(token);

      token = currentToken();
      TokenType tokenType = token.getType();

      // 一个定义由分号结束
      if (tokenType == SEMICOLON) {
        while (token.getType() == SEMICOLON) {
          token = nextToken(); // consume the ;
        }
      } else if (NEXT_START_SET.contains(tokenType)) {
        // 没有分号便开始下一个了。
        errorHandler.flag(token, MISSING_SEMICOLON, this);
      }

      token = synchronize(IDENTIFIER_SET);
    }
  }
  /**
   * Parse a simple expression.
   *
   * @param token the initial token.
   * @return the root of the generated parse subtree.
   * @throws Exception if an error occurred.
   */
  private ICodeNode parseSimpleExpression(Token token) throws Exception {
    TokenType signType = null; // type of leading sign (if any)

    // Look for a leading + or - sign.
    TokenType tokenType = token.getType();
    if ((tokenType == PLUS) || (tokenType == MINUS)) {
      signType = tokenType;
      token = nextToken(); // consume the + or -
    }

    // Parse a term and make the root of its tree the root node.
    ICodeNode rootNode = parseTerm(token);

    // Was there a leading - sign?
    if (signType == MINUS) {

      // Create a NEGATE node and adopt the current tree
      // as its child. The NEGATE node becomes the new root node.
      ICodeNode negateNode = ICodeFactory.createICodeNode(NEGATE);
      negateNode.addChild(rootNode);
      rootNode = negateNode;
    }

    token = currentToken();
    tokenType = token.getType();

    // Loop over additive operators.
    while (ADD_OPS.contains(tokenType)) {

      // Create a new operator node and adopt the current tree
      // as its first child.
      ICodeNodeType nodeType = ADD_OPS_OPS_MAP.get(tokenType);
      ICodeNode opNode = ICodeFactory.createICodeNode(nodeType);
      opNode.addChild(rootNode);

      token = nextToken(); // consume the operator

      // Parse another term.  The operator node adopts
      // the term's tree as its second child.
      opNode.addChild(parseTerm(token));

      // The operator node becomes the new root node.
      rootNode = opNode;

      token = currentToken();
      tokenType = token.getType();
    }

    return rootNode;
  }
  /**
   * Parse an expression.
   *
   * @param token the initial token.
   * @return the root of the generated parse subtree.
   * @throws Exception if an error occurred.
   */
  private ICodeNode parseExpression(Token token) throws Exception {
    // Parse a simple expression and make the root of its tree
    // the root node.
    ICodeNode rootNode = parseSimpleExpression(token);

    token = currentToken();
    TokenType tokenType = token.getType();

    // Look for a relational operator.
    if (REL_OPS.contains(tokenType)) {

      // Create a new operator node and adopt the current tree
      // as its first child.
      ICodeNodeType nodeType = REL_OPS_MAP.get(tokenType);
      ICodeNode opNode = ICodeFactory.createICodeNode(nodeType);
      opNode.addChild(rootNode);

      token = nextToken(); // consume the operator

      // Parse the second simple expression.  The operator node adopts
      // the simple expression's tree as its second child.
      opNode.addChild(parseSimpleExpression(token));

      // The operator node becomes the new root node.
      rootNode = opNode;
    }

    return rootNode;
  }
  /**
   * Parse a term.
   *
   * @param token the initial token.
   * @return the root of the generated parse subtree.
   * @throws Exception if an error occurred.
   */
  private ICodeNode parseTerm(Token token) throws Exception {
    // Parse a factor and make its node the root node.
    ICodeNode rootNode = parseFactor(token);

    token = currentToken();
    TokenType tokenType = token.getType();

    if (rootNode.getType() == ICodeNodeTypeImpl.SET
        && (tokenType == PascalTokenType.OR || tokenType == PascalTokenType.AND)) {
      errorHandler.flag(token, INVALID_OPERATOR, this);
    }

    // Loop over multiplicative operators.
    while (MULT_OPS.contains(tokenType)) {

      // Create a new operator node and adopt the current tree
      // as its first child.
      ICodeNodeType nodeType = MULT_OPS_OPS_MAP.get(tokenType);
      ICodeNode opNode = ICodeFactory.createICodeNode(nodeType);
      opNode.addChild(rootNode);

      switch ((ICodeNodeTypeImpl) opNode.getType()) {
        case INTEGER_DIVIDE:
        case FLOAT_DIVIDE:
          if (rootNode.getType() == ICodeNodeTypeImpl.SET) {
            errorHandler.flag(token, INVALID_OPERATOR, this);
          }
      }

      token = nextToken(); // consume the operator

      // Parse another factor.  The operator node adopts
      // the term's tree as its second child.
      opNode.addChild(parseFactor(token));

      // The operator node becomes the new root node.
      rootNode = opNode;

      token = currentToken();
      tokenType = token.getType();
    }

    return rootNode;
  }
  /**
   * 解析标识符
   *
   * @param token
   * @return 对应符号表项
   * @throws Exception
   */
  private SymTabEntry parseIdentifier(Token token) throws Exception {
    SymTabEntry id = null;

    if (token.getType() == IDENTIFIER) {
      String name = token.getText().toLowerCase();
      id = symTabStack.lookupLocal(name);

      // 申明必须得没有,否则重定义了
      if (id == null) {
        id = symTabStack.enterLocal(name);
        id.setDefinition(definition);
        id.appendLineNumber(token.getLineNumber());
      } else {
        errorHandler.flag(token, IDENTIFIER_REDEFINED, this);
      }

      token = nextToken();
    } else {
      errorHandler.flag(token, MISSING_IDENTIFIER, this);
    }

    return id;
  }
  /**
   * 解析标识符子表及类型说明
   *
   * @param token the current token.
   * @return 一个申明中的子表标识符列表
   * @throws Exception
   */
  protected ArrayList<SymTabEntry> parseIdentifierSublist(Token token) throws Exception {
    ArrayList<SymTabEntry> sublist = new ArrayList<SymTabEntry>();

    do {
      token = synchronize(IDENTIFIER_START_SET);
      SymTabEntry id = parseIdentifier(token);

      if (id != null) {
        sublist.add(id);
      }

      token = synchronize(COMMA_SET);
      TokenType tokenType = token.getType();

      // 找逗号
      if (tokenType == COMMA) {
        token = nextToken(); // 有逗号吞掉以便下一个

        if (IDENTIFIER_FOLLOW_SET.contains(token.getType())) {
          errorHandler.flag(token, MISSING_IDENTIFIER, this);
        }
      } else if (IDENTIFIER_START_SET.contains(tokenType)) {
        errorHandler.flag(token, MISSING_COMMA, this);
      }
    } while (!IDENTIFIER_FOLLOW_SET.contains(token.getType()));

    // 冒号后面的类型
    TypeSpec type = parseTypeSpec(token);

    // 将子表中的每个变量的类型设置上,比如a,b,c:int,先解析了a,b,c,然后解析了int类型
    // 那么这儿就会给a,b,c设置上int类型
    for (SymTabEntry variableId : sublist) {
      variableId.setTypeSpec(type);
    }

    return sublist;
  }
  /**
   * 解析变量申明里的类型
   *
   * @param token 当前token
   * @return 类型说明
   * @throws Exception
   */
  protected TypeSpec parseTypeSpec(Token token) throws Exception {
    // 同步在:处
    token = synchronize(COLON_SET);
    if (token.getType() == COLON) {
      token = nextToken(); // 吞掉 :
    } else {
      errorHandler.flag(token, MISSING_COLON, this);
    }

    // 解析类型说明
    TypeSpecificationParser typeSpecificationParser = new TypeSpecificationParser(this);
    TypeSpec type = typeSpecificationParser.parse(token);

    return type;
  }
  /**
   * Parse an expression.
   *
   * @param token the initial token.
   * @return the root of the generated parse subtree.
   * @throws Exception if an error occurred.
   */
  private ICodeNode parseExpression(Token token) throws Exception {
    // Parse a simple expression and make the root of its tree
    // the root node.
    ICodeNode rootNode = parseSimpleExpression(token);

    token = currentToken();
    TokenType tokenType = token.getType();

    // Look for a relational operator.
    if (REL_OPS.contains(tokenType)) {

      // Create a new operator node and adopt the current tree
      // as its first child.
      ICodeNodeType nodeType = REL_OPS_MAP.get(tokenType);
      ICodeNode opNode = ICodeFactory.createICodeNode(nodeType);
      opNode.addChild(rootNode);

      ICodeNodeTypeImpl type = (ICodeNodeTypeImpl) opNode.getType();
      if (type == LT || type == GT || type == IN_SET) {
        if (rootNode.getType() == ICodeNodeTypeImpl.SET) {
          errorHandler.flag(token, INVALID_OPERATOR, this);
        }
      }

      Token previousToken = token;
      token = nextToken(); // consume the operator

      // Parse the second simple expression.  The operator node adopts
      // the simple expression's tree as its second child.
      ICodeNode rightNode = parseSimpleExpression(token);
      opNode.addChild(rightNode);

      if (opNode.getType() == IN_SET && rightNode.getType() == INTEGER_CONSTANT) {
        errorHandler.flag(previousToken, INVALID_OPERATOR, this);
      }

      // The operator node becomes the new root node.
      rootNode = opNode;
    }

    return rootNode;
  }
  /**
   * Parse a set.
   *
   * @param token the initial token.
   * @return the root of the generated parse subtree.
   * @throws Exception if an error occurred.
   */
  private ICodeNode parseSet(Token token) throws Exception {
    ICodeNode rootNode = ICodeFactory.createICodeNode(SETS);
    int tempInteger = 0;
    token = nextToken(); // consume left bracket

    while (token.getType() != RIGHT_BRACKET) {

      if (token.getType() == INTEGER) {
        tempInteger = (int) token.getValue();
        rootNode.addChild(parseTerm(token));
        token = currentToken();
      } else if (token.getType() == IDENTIFIER) {
        ICodeNode var = parseFactor(token);
        rootNode.addChild(var);
        token = currentToken();
      }
      if (token.getType() == COMMA) {
        token = nextToken(); // consume next token
        if (token.getType() == COMMA) {
          errorHandler.flag(token, EXTRA_COMMA, this);
        }
      } else if (token.getType() == RIGHT_BRACKET) { // do nothing
      } else if (token.getType() == SEMICOLON) {
        errorHandler.flag(token, MISSING_CLOSE_SQUARE_BRACKET, this);
        break;
      } else if (token.getType() == DOT_DOT) {
        token = nextToken(); // consume the integer.
        // Create a STRING_CONSTANT node as the root node.
        ICodeNode node = ICodeFactory.createICodeNode(SUBRANGE);
        // denotes the maximum value of the range.
        if (token.getType() != INTEGER) {
          errorHandler.flag(token, MISSING_MAX_VAL_SUBRANGE, this);
        } else {
          int value = (int) token.getValue();
          node.setAttribute(VALUE, value);
        }
        rootNode.addChild(node);
        token = nextToken();

      } else {
        errorHandler.flag(token, MISSING_COMMA, this);
      }
    }
    if (token.getType() != SEMICOLON) {
      token = nextToken(); // consume right bracket
    }
    return rootNode;
  }
  /**
   * Parse a factor.
   *
   * @param token the initial token.
   * @return the root of the generated parse subtree.
   * @throws Exception if an error occurred.
   */
  private ICodeNode parseFactor(Token token) throws Exception {
    TokenType tokenType = token.getType();
    ICodeNode rootNode = null;

    switch ((PascalTokenType) tokenType) {
      case IDENTIFIER:
        {
          // Look up the identifier in the symbol table stack.
          // Flag the identifier as undefined if it's not found.
          String name = token.getText().toLowerCase();
          SymTabEntry id = symTabStack.lookup(name);
          if (id == null) {
            errorHandler.flag(token, IDENTIFIER_UNDEFINED, this);
            id = symTabStack.enterLocal(name);
          }

          rootNode = ICodeFactory.createICodeNode(VARIABLE);
          rootNode.setAttribute(ID, id);
          id.appendLineNumber(token.getLineNumber());

          token = nextToken(); // consume the identifier
          break;
        }

      case INTEGER:
        {
          // Create an INTEGER_CONSTANT node as the root node.
          rootNode = ICodeFactory.createICodeNode(INTEGER_CONSTANT);
          rootNode.setAttribute(VALUE, token.getValue());

          token = nextToken(); // consume the number
          break;
        }

      case REAL:
        {
          // Create an REAL_CONSTANT node as the root node.
          rootNode = ICodeFactory.createICodeNode(REAL_CONSTANT);
          rootNode.setAttribute(VALUE, token.getValue());

          token = nextToken(); // consume the number
          break;
        }

      case STRING:
        {
          String value = (String) token.getValue();

          // Create a STRING_CONSTANT node as the root node.
          rootNode = ICodeFactory.createICodeNode(STRING_CONSTANT);
          rootNode.setAttribute(VALUE, value);

          token = nextToken(); // consume the string
          break;
        }

      case NOT:
        {
          token = nextToken(); // consume the NOT

          // Create a NOT node as the root node.
          rootNode = ICodeFactory.createICodeNode(ICodeNodeTypeImpl.NOT);

          // Parse the factor.  The NOT node adopts the
          // factor node as its child.
          rootNode.addChild(parseFactor(token));

          break;
        }

      case LEFT_PAREN:
        {
          token = nextToken(); // consume the (

          // Parse an expression and make its node the root node.
          rootNode = parseExpression(token);

          // Look for the matching ) token.
          token = currentToken();
          if (token.getType() == RIGHT_PAREN) {
            token = nextToken(); // consume the )
          } else {
            errorHandler.flag(token, MISSING_RIGHT_PAREN, this);
          }

          break;
        }
      case LEFT_BRACKET:
        {
          rootNode = parseSet(token);

          break;
        }

      default:
        {
          errorHandler.flag(token, UNEXPECTED_TOKEN, this);
          break;
        }
    }

    return rootNode;
  }
  /**
   * Parse type definitions.
   *
   * @param token the initial token.
   * @throws Exception if an error occurred.
   */
  public void parse(Token token) throws Exception {
    token = synchronize(IDENTIFIER_SET);

    // Loop to parse a sequence of type definitions
    // separated by semicolons.
    while (token.getType() == IDENTIFIER) {
      String name = token.getText().toLowerCase();
      SymTabEntry typeId = symTabStack.lookupLocal(name);

      // Enter the new identifier into the symbol table
      // but don't set how it's defined yet.
      if (typeId == null) {
        typeId = symTabStack.enterLocal(name);
        typeId.appendLineNumber(token.getLineNumber());
      } else {
        errorHandler.flag(token, IDENTIFIER_REDEFINED, this);
        typeId = null;
      }

      token = nextToken(); // consume the identifier token

      // Synchronize on the = token.
      token = synchronize(EQUALS_SET);
      if (token.getType() == EQUALS) {
        token = nextToken(); // consume the =
      } else {
        errorHandler.flag(token, MISSING_EQUALS, this);
      }

      // Parse the type specification.
      TypeSpecificationParser typeSpecificationParser = new TypeSpecificationParser(this);
      TypeSpec type = typeSpecificationParser.parse(token);

      // Set identifier to be a type and set its type specificationt.
      if (typeId != null) {
        typeId.setDefinition(TYPE);
      }

      // Cross-link the type identifier and the type specification.
      if (type != null && typeId != null) {
        if (type.getIdentifier() == null) {
          type.setIdentifier(typeId);
        }
        typeId.setTypeSpec(type);
      } else {
        token = synchronize(FOLLOW_SET);
      }

      token = currentToken();
      TokenType tokenType = token.getType();

      // Look for one or more semicolons after a definition.
      if (tokenType == SEMICOLON) {
        while (token.getType() == SEMICOLON) {
          token = nextToken(); // consume the ;
        }
      }

      // If at the start of the next definition or declaration,
      // then missing a semicolon.
      else if (NEXT_START_SET.contains(tokenType)) {
        errorHandler.flag(token, MISSING_SEMICOLON, this);
      }

      token = synchronize(IDENTIFIER_SET);
    }
  }
  /**
   * Parse a set.
   *
   * @param token the initial token.
   * @return the root of the generated parse subtree.
   * @throws Exception if an error occurred.
   */
  private ICodeNode parseSet(Token token) throws Exception {
    ICodeNode rootNode = ICodeFactory.createICodeNode(ICodeNodeTypeImpl.SET);
    HashSet<Integer> values = new HashSet<>();
    rootNode.setAttribute(VALUE, new HashSet<Integer>());
    boolean isFinished = false;

    while (token.getType() != RIGHT_BRACKET && token.getType() != ERROR && !isFinished) {
      ICodeNode leftNode = parseSimpleExpression(token);

      if (leftNode.getType() == INTEGER_CONSTANT
          && !values.add((Integer) leftNode.getAttribute(VALUE))) {
        errorHandler.flag(token, NON_UNIQUE_MEMBERS, this);
      }

      token = currentToken();

      switch ((PascalTokenType) token.getType()) {
        case RIGHT_BRACKET:
          rootNode.addChild(leftNode);
          break;
        case COMMA:
          rootNode.addChild(leftNode);
          token = nextToken(); // Consume the ,
          if (token.getType() == COMMA) {
            errorHandler.flag(token, EXTRA_COMMAS, this);
            token = nextToken(); // Consume the extra ,
          }
          break;
        case DOT_DOT:
          token = nextToken(); // Consume the ..
          if (token.getType() == COMMA) {
            errorHandler.flag(token, INVALID_SUBRANGE, this);
            token = nextToken(); // Consume the ,
            rootNode.addChild(leftNode);
          } else {
            ICodeNode rightNode = parseSimpleExpression(token);
            ICodeNode subrangeNode = ICodeFactory.createICodeNode(SUBRANGE);
            subrangeNode.addChild(leftNode);
            subrangeNode.addChild(rightNode);
            rootNode.addChild(subrangeNode);

            if (leftNode.getType() == INTEGER_CONSTANT && rightNode.getType() == INTEGER_CONSTANT) {
              boolean duplicateFound = false;
              Integer leftRange = (Integer) leftNode.getAttribute(VALUE) + 1;
              Integer rightRange = (Integer) rightNode.getAttribute(VALUE);

              while (leftRange <= rightRange) {
                if (!values.add(leftRange++) && !duplicateFound) {
                  errorHandler.flag(token, NON_UNIQUE_MEMBERS, this);
                  duplicateFound = true;
                }
              }
            }

            token = currentToken();
            if (token.getType() == COMMA) {
              token = nextToken(); // Consume the ,
            } else if (token.getType() != RIGHT_BRACKET) {
              errorHandler.flag(token, MISSING_COMMA, this);
            }
          }
          break;

        case INTEGER:
          errorHandler.flag(token, MISSING_COMMA, this);
          break;

        case SEMICOLON:
          isFinished = true;
          break;

        default:
          errorHandler.flag(token, UNEXPECTED_TOKEN, this);
          break;
      }
    }

    return rootNode;
  }