xgo/parser/ast.go

70 lines
1.3 KiB
Go
Raw Normal View History

package parser
2019-01-09 10:17:42 +03:00
import (
"strings"
2019-12-20 22:40:38 +03:00
)
2019-01-09 10:17:42 +03:00
2019-12-20 22:40:38 +03:00
const (
nullRep = "<null>"
2019-01-09 10:17:42 +03:00
)
2019-12-20 22:40:38 +03: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-01-17 12:56:05 +03:00
// IdentList represents a list of identifiers.
2019-01-09 10:17:42 +03:00
type IdentList struct {
2019-12-20 22:40:38 +03:00
LParen Pos
VarArgs bool
List []*Ident
2019-12-20 22:40:38 +03:00
RParen Pos
2019-01-09 10:17:42 +03:00
}
// Pos returns the position of first character belonging to the node.
2019-12-20 22:40:38 +03:00
func (n *IdentList) Pos() Pos {
2019-01-09 10:17:42 +03:00
if n.LParen.IsValid() {
return n.LParen
}
if len(n.List) > 0 {
return n.List[0].Pos()
}
2019-12-20 22:40:38 +03:00
return NoPos
2019-01-09 10:17:42 +03:00
}
// End returns the position of first character immediately after the node.
2019-12-20 22:40:38 +03:00
func (n *IdentList) End() Pos {
2019-01-09 10:17:42 +03:00
if n.RParen.IsValid() {
return n.RParen + 1
}
if l := len(n.List); l > 0 {
return n.List[l-1].End()
}
2019-12-20 22:40:38 +03:00
return NoPos
2019-01-09 10:17:42 +03:00
}
// NumFields returns the number of fields.
2019-01-09 10:17:42 +03:00
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())
}
2019-01-09 10:17:42 +03:00
}
return "(" + strings.Join(list, ", ") + ")"
}