/// <summary> /// ClassStmt /// CLASS ID LBRACES DeclarationStmt* RBRACES /// </summary> /// <returns></returns> private ClassStmt ParseClassStmt() { Consume(TokenType.CLASS); ClassStmt ret = new ClassStmt(Consume(TokenType.ID).Literal); FuncStmt fs = null; FuncStmt fcur = null; VarStmt vs = null; VarStmt vcur = null; Consume(TokenType.LBRACES); while (!Check(TokenType.RBRACES)) { DeclarationStmt stmt = ParseDeclOrDefStmt(); if (stmt is FuncStmt) { AppendASTLinkedList(ref fs, ref fcur, (FuncStmt)stmt); } else { AppendASTLinkedList(ref vs, ref vcur, (VarStmt)stmt); } } Consume(TokenType.RBRACES); ret.Methods = fs; ret.Fields = vs; return(ret); }
/// <summary> /// 这里用了LookAhead /// 此外不允许不带定义的声明 /// DeclarationStmt /// ACCESS_FLAG* TypeExpr FuncDeclarator BlockStmt /// ACCESS_FLAG* TypeExpr VarDeclarator SEMICOLON /// </summary> /// <returns></returns> private DeclarationStmt ParseDeclOrDefStmt() { AccessFlag flag = ParserAccessFlag(); TypeExpr type = ParseTypeExpr(); if (Check(TokenType.LPAREN) || CheckAt(1, TokenType.LPAREN)) { FuncStmt ret = ParseFuncDeclarator(type, flag); ret.Body = ParseBlockStmt(); return(ret); } else { VarStmt ret = ParseVarDeclarator(type, flag); Consume(TokenType.SEMICOLON); return(ret); } }