xgo/compiler/ast/ident_list.go
Jacob R. McCollum adcf05d26f add variadic function parameters (#189)
* parsing variable function types

* finished variadic functions

* fix case where number of passed args to variadic function is 0

* remove extraneous OpVarArgs

* allow multiple variables in variadic function declaration

* fix IdentList string method to print multi-arg variadic functions correctly

* round 2 of fix IdentList string method to print multi-arg variadic functions correctly

round 2 of fix IdentList string method to print multi-arg variadic functions correctly

* clean up tasks in OpCall handling, add tests for variadic closures

* cleanup for pr

- add syntax documentation
- cleanup parseIdentList
- cleanup OpCall handling for functions and closures
- cleanup tests
2019-04-25 21:28:27 -07:00

63 lines
1.1 KiB
Go

package ast
import (
"strings"
"github.com/d5/tengo/compiler/source"
)
// IdentList represents a list of identifiers.
type IdentList struct {
LParen source.Pos
VarArgs bool
List []*Ident
RParen source.Pos
}
// Pos returns the position of first character belonging to the node.
func (n *IdentList) Pos() source.Pos {
if n.LParen.IsValid() {
return n.LParen
}
if len(n.List) > 0 {
return n.List[0].Pos()
}
return source.NoPos
}
// End returns the position of first character immediately after the node.
func (n *IdentList) End() source.Pos {
if n.RParen.IsValid() {
return n.RParen + 1
}
if l := len(n.List); l > 0 {
return n.List[l-1].End()
}
return source.NoPos
}
// NumFields returns the number of fields.
func (n *IdentList) NumFields() int {
if n == nil {
return 0
}
return len(n.List)
}
func (n *IdentList) String() string {
var list []string
for i, e := range n.List {
if n.VarArgs && i == len(n.List)-1 {
list = append(list, "..."+e.String())
} else {
list = append(list, e.String())
}
}
return "(" + strings.Join(list, ", ") + ")"
}