2020-01-09 20:52:19 +00:00
|
|
|
package parser
|
2019-02-23 15:39:44 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
2020-01-09 20:52:19 +00:00
|
|
|
)
|
2019-02-23 15:39:44 +00:00
|
|
|
|
2020-01-09 20:52:19 +00:00
|
|
|
const (
|
|
|
|
nullRep = "<null>"
|
2019-02-23 15:39:44 +00:00
|
|
|
)
|
|
|
|
|
2020-01-09 20:52:19 +00:00
|
|
|
// Node represents a node in the AST.
|
|
|
|
type Node interface {
|
|
|
|
// Pos returns the position of first character belonging to the node.
|
|
|
|
Pos() Pos
|
|
|
|
// End returns the position of first character immediately after the node.
|
|
|
|
End() Pos
|
|
|
|
// String returns a string representation of the node.
|
|
|
|
String() string
|
|
|
|
}
|
|
|
|
|
2019-02-23 15:39:44 +00:00
|
|
|
// IdentList represents a list of identifiers.
|
|
|
|
type IdentList struct {
|
2020-01-09 20:52:19 +00:00
|
|
|
LParen Pos
|
2019-06-16 21:33:25 +00:00
|
|
|
VarArgs bool
|
|
|
|
List []*Ident
|
2020-01-09 20:52:19 +00:00
|
|
|
RParen Pos
|
2019-02-23 15:39:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Pos returns the position of first character belonging to the node.
|
2020-01-09 20:52:19 +00:00
|
|
|
func (n *IdentList) Pos() Pos {
|
2019-02-23 15:39:44 +00:00
|
|
|
if n.LParen.IsValid() {
|
|
|
|
return n.LParen
|
|
|
|
}
|
|
|
|
if len(n.List) > 0 {
|
|
|
|
return n.List[0].Pos()
|
|
|
|
}
|
2020-01-09 20:52:19 +00:00
|
|
|
return NoPos
|
2019-02-23 15:39:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// End returns the position of first character immediately after the node.
|
2020-01-09 20:52:19 +00:00
|
|
|
func (n *IdentList) End() Pos {
|
2019-02-23 15:39:44 +00:00
|
|
|
if n.RParen.IsValid() {
|
|
|
|
return n.RParen + 1
|
|
|
|
}
|
|
|
|
if l := len(n.List); l > 0 {
|
|
|
|
return n.List[l-1].End()
|
|
|
|
}
|
2020-01-09 20:52:19 +00:00
|
|
|
return NoPos
|
2019-02-23 15:39:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2019-06-16 21:33:25 +00:00
|
|
|
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())
|
|
|
|
}
|
2019-02-23 15:39:44 +00:00
|
|
|
}
|
|
|
|
return "(" + strings.Join(list, ", ") + ")"
|
|
|
|
}
|