From d14fe1b0332ef2a973f0d16dd370c457f2da0589 Mon Sep 17 00:00:00 2001 From: TSUYUSATO Kitsune Date: Wed, 18 Dec 2024 16:25:21 +0900 Subject: [PATCH] Use direct returns instead https://github.com/cloudspannerecosystem/memefish/pull/189#discussion_r1835347015 --- parser.go | 84 +++++++++++++++++++------------------------------------ 1 file changed, 28 insertions(+), 56 deletions(-) diff --git a/parser.go b/parser.go index 11cb2f0d..1dfdd4c6 100644 --- a/parser.go +++ b/parser.go @@ -160,15 +160,13 @@ func (p *Parser) parseStatement() (stmt ast.Statement) { switch { case p.Token.Kind == "SELECT" || p.Token.Kind == "@" || p.Token.Kind == "WITH" || p.Token.Kind == "(": - stmt = p.parseQueryStatement() - return + return p.parseQueryStatement() case p.Token.Kind == "CREATE" || p.Token.IsKeywordLike("ALTER") || p.Token.IsKeywordLike("DROP") || p.Token.IsKeywordLike("RENAME") || p.Token.IsKeywordLike("GRANT") || p.Token.IsKeywordLike("REVOKE") || p.Token.IsKeywordLike("ANALYZE"): return p.parseDDL() case p.Token.IsKeywordLike("INSERT") || p.Token.IsKeywordLike("DELETE") || p.Token.IsKeywordLike("UPDATE"): - stmt = p.parseDML() - return + return p.parseDML() } panic(p.errorfAtToken(&p.Token, "unexpected token: %s", p.Token.Kind)) @@ -213,12 +211,11 @@ func (p *Parser) parseQueryStatement() (stmt *ast.QueryStatement) { with := p.tryParseWith() query := p.parseQueryExpr() - stmt = &ast.QueryStatement{ + return &ast.QueryStatement{ Hint: hint, With: with, Query: query, } - return } func (p *Parser) tryParseHint() *ast.Hint { @@ -2182,17 +2179,13 @@ func (p *Parser) parseType() (t ast.Type) { switch p.Token.Kind { case token.TokenIdent: if !p.lookaheadSimpleType() { - t = p.parseNamedType() - return + return p.parseNamedType() } - t = p.parseSimpleType() - return + return p.parseSimpleType() case "ARRAY": - t = p.parseArrayType() - return + return p.parseArrayType() case "STRUCT": - t = p.parseStructType() - return + return p.parseStructType() } panic(p.errorfAtToken(&p.Token, "expected token: , ARRAY, STRUCT, but: %s", p.Token.Kind)) @@ -2462,17 +2455,13 @@ func (p *Parser) parseDDL() (ddl ast.DDL) { case p.Token.IsKeywordLike("VIEW"): return p.parseCreateView(pos, false) case p.Token.IsKeywordLike("INDEX") || p.Token.IsKeywordLike("UNIQUE") || p.Token.IsKeywordLike("NULL_FILTERED"): - ddl = p.parseCreateIndex(pos) - return + return p.parseCreateIndex(pos) case p.Token.IsKeywordLike("VECTOR"): - ddl = p.parseCreateVectorIndex(pos) - return + return p.parseCreateVectorIndex(pos) case p.Token.IsKeywordLike("SEARCH"): - ddl = p.parseCreateSearchIndex(pos) - return + return p.parseCreateSearchIndex(pos) case p.Token.IsKeywordLike("ROLE"): - ddl = p.parseCreateRole(pos) - return + return p.parseCreateRole(pos) case p.Token.IsKeywordLike("CHANGE"): return p.parseCreateChangeStream(pos) case p.Token.IsKeywordLike("MODEL"): @@ -2492,24 +2481,19 @@ func (p *Parser) parseDDL() (ddl ast.DDL) { p.nextToken() switch { case p.Token.IsKeywordLike("TABLE"): - ddl = p.parseAlterTable(pos) - return + return p.parseAlterTable(pos) case p.Token.IsKeywordLike("DATABASE"): return p.parseAlterDatabase(pos) case p.Token.Kind == "PROTO": return p.parseAlterProtoBundle(pos) case p.Token.IsKeywordLike("INDEX"): - ddl = p.parseAlterIndex(pos) - return + return p.parseAlterIndex(pos) case p.Token.IsKeywordLike("SEARCH"): - ddl = p.parseAlterSearchIndex(pos) - return + return p.parseAlterSearchIndex(pos) case p.Token.IsKeywordLike("SEQUENCE"): - ddl = p.parseAlterSequence(pos) - return + return p.parseAlterSequence(pos) case p.Token.IsKeywordLike("CHANGE"): - ddl = p.parseAlterChangeStream(pos) - return + return p.parseAlterChangeStream(pos) case p.Token.IsKeywordLike("STATISTICS"): return p.parseAlterStatistics(pos) case p.Token.IsKeywordLike("MODEL"): @@ -2524,26 +2508,19 @@ func (p *Parser) parseDDL() (ddl ast.DDL) { case p.Token.Kind == "PROTO": return p.parseDropProtoBundle(pos) case p.Token.IsKeywordLike("TABLE"): - ddl = p.parseDropTable(pos) - return + return p.parseDropTable(pos) case p.Token.IsKeywordLike("INDEX"): - ddl = p.parseDropIndex(pos) - return + return p.parseDropIndex(pos) case p.Token.IsKeywordLike("SEARCH"): - ddl = p.parseDropSearchIndex(pos) - return + return p.parseDropSearchIndex(pos) case p.Token.IsKeywordLike("VECTOR"): - ddl = p.parseDropVectorIndex(pos) - return + return p.parseDropVectorIndex(pos) case p.Token.IsKeywordLike("SEQUENCE"): - ddl = p.parseDropSequence(pos) - return + return p.parseDropSequence(pos) case p.Token.IsKeywordLike("VIEW"): - ddl = p.parseDropView(pos) - return + return p.parseDropView(pos) case p.Token.IsKeywordLike("ROLE"): - ddl = p.parseDropRole(pos) - return + return p.parseDropRole(pos) case p.Token.IsKeywordLike("CHANGE"): return p.parseDropChangeStream(pos) case p.Token.IsKeywordLike("MODEL"): @@ -2552,12 +2529,10 @@ func (p *Parser) parseDDL() (ddl ast.DDL) { p.panicfAtToken(&p.Token, "expected pseudo keyword: TABLE, INDEX, ROLE, CHANGE, MODEL, but: %s", p.Token.AsString) case p.Token.IsKeywordLike("RENAME"): p.nextToken() - ddl = p.parseRenameTable(pos) - return + return p.parseRenameTable(pos) case p.Token.IsKeywordLike("GRANT"): p.nextToken() - ddl = p.parseGrant(pos) - return + return p.parseGrant(pos) case p.Token.IsKeywordLike("REVOKE"): p.nextToken() return p.parseRevoke(pos) @@ -4169,14 +4144,11 @@ func (p *Parser) parseDML() (dml ast.DML) { pos := id.Pos switch { case id.IsKeywordLike("INSERT"): - dml = p.parseInsert(pos) - return + return p.parseInsert(pos) case id.IsKeywordLike("DELETE"): - dml = p.parseDelete(pos) - return + return p.parseDelete(pos) case id.IsKeywordLike("UPDATE"): - dml = p.parseUpdate(pos) - return + return p.parseUpdate(pos) } panic(p.errorfAtToken(id, "expect pseudo keyword: INSERT, DELETE, UPDATE but: %s", id.AsString))