/** * 解析变量申明里的类型 * * @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 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); } }