token.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Code generated by gocc; DO NOT EDIT.
  2. package token
  3. import (
  4. "fmt"
  5. )
  6. type Token struct {
  7. Type
  8. Lit []byte
  9. Pos
  10. }
  11. type Type int
  12. const (
  13. INVALID Type = iota
  14. EOF
  15. )
  16. type Pos struct {
  17. Offset int
  18. Line int
  19. Column int
  20. }
  21. func (p Pos) String() string {
  22. return fmt.Sprintf("Pos(offset=%d, line=%d, column=%d)", p.Offset, p.Line, p.Column)
  23. }
  24. type TokenMap struct {
  25. typeMap []string
  26. idMap map[string]Type
  27. }
  28. func (m TokenMap) Id(tok Type) string {
  29. if int(tok) < len(m.typeMap) {
  30. return m.typeMap[tok]
  31. }
  32. return "unknown"
  33. }
  34. func (m TokenMap) Type(tok string) Type {
  35. if typ, exist := m.idMap[tok]; exist {
  36. return typ
  37. }
  38. return INVALID
  39. }
  40. func (m TokenMap) TokenString(tok *Token) string {
  41. //TODO: refactor to print pos & token string properly
  42. return fmt.Sprintf("%s(%d,%s)", m.Id(tok.Type), tok.Type, tok.Lit)
  43. }
  44. func (m TokenMap) StringType(typ Type) string {
  45. return fmt.Sprintf("%s(%d)", m.Id(typ), typ)
  46. }
  47. var TokMap = TokenMap{
  48. typeMap: []string{
  49. "INVALID",
  50. "$",
  51. "graphx",
  52. "{",
  53. "}",
  54. "strict",
  55. "digraph",
  56. ";",
  57. "=",
  58. "node",
  59. "edge",
  60. "[",
  61. "]",
  62. ",",
  63. ":",
  64. "subgraph",
  65. "->",
  66. "--",
  67. "id",
  68. },
  69. idMap: map[string]Type{
  70. "INVALID": 0,
  71. "$": 1,
  72. "graphx": 2,
  73. "{": 3,
  74. "}": 4,
  75. "strict": 5,
  76. "digraph": 6,
  77. ";": 7,
  78. "=": 8,
  79. "node": 9,
  80. "edge": 10,
  81. "[": 11,
  82. "]": 12,
  83. ",": 13,
  84. ":": 14,
  85. "subgraph": 15,
  86. "->": 16,
  87. "--": 17,
  88. "id": 18,
  89. },
  90. }