Skip to content

Commit

Permalink
Fix to use NamedType
Browse files Browse the repository at this point in the history
  • Loading branch information
apstndb committed Oct 1, 2024
1 parent 872d311 commit 9190a74
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 20 deletions.
14 changes: 7 additions & 7 deletions ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions ast/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 += ", "
Expand All @@ -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 {
Expand Down
24 changes: 13 additions & 11 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
}
}

Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit 9190a74

Please sign in to comment.