ast.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package parser
  2. import (
  3. "strings"
  4. )
  5. const (
  6. nullRep = "<null>"
  7. )
  8. // Node represents a node in the AST.
  9. type Node interface {
  10. // Pos returns the position of first character belonging to the node.
  11. Pos() Pos
  12. // End returns the position of first character immediately after the node.
  13. End() Pos
  14. // String returns a string representation of the node.
  15. String() string
  16. }
  17. // IdentList represents a list of identifiers.
  18. type IdentList struct {
  19. LParen Pos
  20. VarArgs bool
  21. List []*Ident
  22. RParen Pos
  23. }
  24. // Pos returns the position of first character belonging to the node.
  25. func (n *IdentList) Pos() Pos {
  26. if n.LParen.IsValid() {
  27. return n.LParen
  28. }
  29. if len(n.List) > 0 {
  30. return n.List[0].Pos()
  31. }
  32. return NoPos
  33. }
  34. // End returns the position of first character immediately after the node.
  35. func (n *IdentList) End() Pos {
  36. if n.RParen.IsValid() {
  37. return n.RParen + 1
  38. }
  39. if l := len(n.List); l > 0 {
  40. return n.List[l-1].End()
  41. }
  42. return NoPos
  43. }
  44. // NumFields returns the number of fields.
  45. func (n *IdentList) NumFields() int {
  46. if n == nil {
  47. return 0
  48. }
  49. return len(n.List)
  50. }
  51. func (n *IdentList) String() string {
  52. var list []string
  53. for i, e := range n.List {
  54. if n.VarArgs && i == len(n.List)-1 {
  55. list = append(list, "..."+e.String())
  56. } else {
  57. list = append(list, e.String())
  58. }
  59. }
  60. return "(" + strings.Join(list, ", ") + ")"
  61. }