From 9190a74541cffb66a24143f8276428125dbfda9c Mon Sep 17 00:00:00 2001 From: apstndb <803393+apstndb@users.noreply.github.com> Date: Tue, 1 Oct 2024 12:55:25 +0900 Subject: [PATCH] Fix to use NamedType --- ast/ast.go | 14 +++++++------- ast/sql.go | 4 ++-- parser.go | 24 +++++++++++++----------- 3 files changed, 22 insertions(+), 20 deletions(-) diff --git a/ast/ast.go b/ast/ast.go index 78d8848a..8b8b56e9 100644 --- a/ast/ast.go +++ b/ast/ast.go @@ -1355,13 +1355,13 @@ type NewConstructorArg struct { // NewConstructor represents NEW operator which creates a protocol buffer using a parenthesized list of arguments. // -// NEW {{TypeName | sql}} ({{Args | sqlJoin ", "}}) +// NEW {{.Type | sql}} ({{.Args | sqlJoin ", "}}) type NewConstructor struct { // pos = New // end = Rparen + 1 - New token.Pos - TypeName *Path + New token.Pos + Type *NamedType Args []*NewConstructorArg @@ -1370,14 +1370,14 @@ type NewConstructor struct { // BracedNewConstructor represents NEW operator which creates a protocol buffer using a map constructor. // -// NEW {{.TypeName | sql}} {{"{"}}{{"}"}} +// NEW {{.Type | sql}} {{"{"}}{{"}"}} type BracedNewConstructor struct { // pos = New // end = Rbrace + 1 - New token.Pos - TypeName *Path - Body *BracedConstructor + New token.Pos + Type *NamedType + Body *BracedConstructor } // BracedConstructor represents a single map constructor which is used in BracedNewConstructor. diff --git a/ast/sql.go b/ast/sql.go index bb8ddeda..ea22e2aa 100644 --- a/ast/sql.go +++ b/ast/sql.go @@ -694,7 +694,7 @@ func (n *NewConstructorArg) SQL() string { func (n *NewConstructor) SQL() string { var sql string - sql += "NEW " + n.TypeName.SQL() + "(" + sql += "NEW " + n.Type.SQL() + "(" for i, arg := range n.Args { if i > 0 { sql += ", " @@ -706,7 +706,7 @@ func (n *NewConstructor) SQL() string { } func (b *BracedNewConstructor) SQL() string { - return "NEW " + b.TypeName.SQL() + " " + b.Body.SQL() + return "NEW " + b.Type.SQL() + " " + b.Body.SQL() } func (b *BracedConstructor) SQL() string { diff --git a/parser.go b/parser.go index c10aafbd..557a7d59 100644 --- a/parser.go +++ b/parser.go @@ -2008,7 +2008,7 @@ func (p *Parser) parseStructTypeFields() (fields []*ast.StructField, gt token.Po return } -func (p *Parser) parseNewConstructor(newPos token.Pos, path []*ast.Ident) *ast.NewConstructor { +func (p *Parser) parseNewConstructor(newPos token.Pos, namedType *ast.NamedType) *ast.NewConstructor { p.expect("(") var args []*ast.NewConstructorArg @@ -2031,10 +2031,10 @@ func (p *Parser) parseNewConstructor(newPos token.Pos, path []*ast.Ident) *ast.N } rparen := p.expect(")").Pos return &ast.NewConstructor{ - New: newPos, - TypeName: &ast.Path{Idents: path}, - Args: args, - Rparen: rparen, + New: newPos, + Type: namedType, + Args: args, + Rparen: rparen, } } @@ -2074,23 +2074,25 @@ func (p *Parser) parseBracedConstructor() *ast.BracedConstructor { Fields: fields, } } -func (p *Parser) parseBracedNewConstructor(newPos token.Pos, path []*ast.Ident) *ast.BracedNewConstructor { + +func (p *Parser) parseBracedNewConstructor(newPos token.Pos, namedType *ast.NamedType) *ast.BracedNewConstructor { body := p.parseBracedConstructor() return &ast.BracedNewConstructor{ - New: newPos, - TypeName: &ast.Path{Idents: path}, - Body: body, + New: newPos, + Type: namedType, + Body: body, } } func (p *Parser) parseNewConstructors() ast.Expr { newPos := p.expect("NEW").Pos path := p.parseIdentOrPath() + namedType := &ast.NamedType{Path: path} switch p.Token.Kind { case "(": - return p.parseNewConstructor(newPos, path) + return p.parseNewConstructor(newPos, namedType) case "{": - return p.parseBracedNewConstructor(newPos, path) + return p.parseBracedNewConstructor(newPos, namedType) default: p.panicfAtToken(&p.Token, `expect '{' or '(', but %v`, p.Token.Kind) }