Skip to content

Commit

Permalink
ast.CallExpr: NoParen => NoParenEnd
Browse files Browse the repository at this point in the history
  • Loading branch information
xushiwei committed Sep 27, 2021
1 parent fce9ae1 commit ea555ec
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 14 deletions.
19 changes: 12 additions & 7 deletions ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,12 +319,12 @@ type (

// A CallExpr node represents an expression followed by an argument list.
CallExpr struct {
Fun Expr // function expression
Lparen token.Pos // position of "("
Args []Expr // function arguments; or nil
Ellipsis token.Pos // position of "..." (token.NoPos if there is no "...")
Rparen token.Pos // position of ")"
NoParen bool
Fun Expr // function expression
Lparen token.Pos // position of "("
Args []Expr // function arguments; or nil
Ellipsis token.Pos // position of "..." (token.NoPos if there is no "...")
Rparen token.Pos // position of ")"
NoParenEnd token.Pos
}

// A StarExpr node represents an expression of the form "*" Expression.
Expand Down Expand Up @@ -537,7 +537,12 @@ func (x *SliceExpr) End() token.Pos { return x.Rbrack + 1 }
func (x *TypeAssertExpr) End() token.Pos { return x.Rparen + 1 }

// End returns position of first character immediately after the node.
func (x *CallExpr) End() token.Pos { return x.Rparen + 1 }
func (x *CallExpr) End() token.Pos {
if x.NoParenEnd != token.NoPos {
return x.NoParenEnd
}
return x.Rparen + 1
}

// End returns position of first character immediately after the node.
func (x *StarExpr) End() token.Pos { return x.X.End() }
Expand Down
2 changes: 1 addition & 1 deletion cl/class_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type gmxInfo struct {

var (
gmxTypes = map[string]gmxInfo{
".gmx": {".spx", []string{"github.com/goplus/spx"}},
".gmx": {".spx", []string{"github.com/goplus/spx", "math"}},
}
)

Expand Down
3 changes: 3 additions & 0 deletions cl/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,9 @@ func (p *nodeInterp) LoadExpr(node ast.Node) (src string, pos token.Position) {
f := p.files[pos.Filename]
n := int(node.End() - start)
pos.Filename = relFile(p.workingDir, pos.Filename)
if pos.Offset+n < 0 {
log.Println("LoadExpr:", node, pos.Filename, pos.Line, pos.Offset, node.Pos(), node.End(), n)
}
src = string(f.Code[pos.Offset : pos.Offset+n])
return
}
Expand Down
2 changes: 1 addition & 1 deletion parser/_testdata/cmdlinestyle/cmd.gop
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ func main() {
changeYpos - 0.7
changeYpos-0.7
x[1]
x []
x []...
}
20 changes: 17 additions & 3 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,12 @@ func (p *parser) atComma(context string, follow token.Token) bool {
if p.tok == token.SEMICOLON && p.lit == "\n" {
msg += " before newline"
}
p.error(p.pos, msg+" in "+context)
msgctx := msg + " in " + context
p.error(p.pos, msgctx)
if debugParseError {
log.Std.Output("", log.Linfo, 2, msgctx)
panic(msgctx)
}
return true // "insert" comma and continue
}
return false
Expand Down Expand Up @@ -1553,18 +1558,27 @@ func (p *parser) parseCallOrConversion(fun ast.Expr, isCmd bool) *ast.CallExpr {
if p.tok == token.ELLIPSIS {
ellipsis = p.pos
p.next()
if p.tok != token.RPAREN {
break
}
}
if !p.atComma("argument list", endTok) {
break
}
p.next()
}
p.exprLev--
if !isCmd {
var noParenEnd token.Pos
if isCmd {
noParenEnd = p.pos
} else {
rparen = p.expectClosing(token.RPAREN, "argument list")
}
if debugParseOutput {
log.Printf("ast.CallExpr{Fun: %v, Ellipsis: %v, isCmd: %v}\n", fun, ellipsis != 0, isCmd)
}
return &ast.CallExpr{
Fun: fun, Lparen: lparen, Args: list, Ellipsis: ellipsis, Rparen: rparen, NoParen: isCmd}
Fun: fun, Lparen: lparen, Args: list, Ellipsis: ellipsis, Rparen: rparen, NoParenEnd: noParenEnd}
}

func (p *parser) parseValue(keyOk bool) ast.Expr {
Expand Down
5 changes: 3 additions & 2 deletions printer/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -920,8 +920,9 @@ func (p *printer) expr1(expr ast.Expr, prec1, depth int) {
} else {
wasIndented = p.possibleSelectorExpr(x.Fun, token.HighestPrec, depth)
}
if x.NoParen {
if x.NoParenEnd != token.NoPos {
p.print(blank)
depth++
} else {
p.print(x.Lparen, token.LPAREN)
}
Expand All @@ -934,7 +935,7 @@ func (p *printer) expr1(expr ast.Expr, prec1, depth int) {
} else {
p.exprList(x.Lparen, x.Args, depth, commaTerm, x.Rparen, false)
}
if !x.NoParen {
if x.NoParenEnd == token.NoPos {
p.print(x.Rparen, token.RPAREN)
}
if wasIndented {
Expand Down

0 comments on commit ea555ec

Please sign in to comment.