token.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // Code generated by gocc; DO NOT EDIT.
  2. package token
  3. import (
  4. "fmt"
  5. "strconv"
  6. "unicode/utf8"
  7. )
  8. type Token struct {
  9. Type
  10. Lit []byte
  11. Pos
  12. }
  13. type Type int
  14. const (
  15. INVALID Type = iota
  16. EOF
  17. )
  18. type Pos struct {
  19. Offset int
  20. Line int
  21. Column int
  22. }
  23. func (p Pos) String() string {
  24. return fmt.Sprintf("Pos(offset=%d, line=%d, column=%d)", p.Offset, p.Line, p.Column)
  25. }
  26. type TokenMap struct {
  27. typeMap []string
  28. idMap map[string]Type
  29. }
  30. func (m TokenMap) Id(tok Type) string {
  31. if int(tok) < len(m.typeMap) {
  32. return m.typeMap[tok]
  33. }
  34. return "unknown"
  35. }
  36. func (m TokenMap) Type(tok string) Type {
  37. if typ, exist := m.idMap[tok]; exist {
  38. return typ
  39. }
  40. return INVALID
  41. }
  42. func (m TokenMap) TokenString(tok *Token) string {
  43. //TODO: refactor to print pos & token string properly
  44. return fmt.Sprintf("%s(%d,%s)", m.Id(tok.Type), tok.Type, tok.Lit)
  45. }
  46. func (m TokenMap) StringType(typ Type) string {
  47. return fmt.Sprintf("%s(%d)", m.Id(typ), typ)
  48. }
  49. // CharLiteralValue returns the string value of the char literal.
  50. func (t *Token) CharLiteralValue() string {
  51. return string(t.Lit[1 : len(t.Lit)-1])
  52. }
  53. // Float32Value returns the float32 value of the token or an error if the token literal does not
  54. // denote a valid float32.
  55. func (t *Token) Float32Value() (float32, error) {
  56. if v, err := strconv.ParseFloat(string(t.Lit), 32); err != nil {
  57. return 0, err
  58. } else {
  59. return float32(v), nil
  60. }
  61. }
  62. // Float64Value returns the float64 value of the token or an error if the token literal does not
  63. // denote a valid float64.
  64. func (t *Token) Float64Value() (float64, error) {
  65. return strconv.ParseFloat(string(t.Lit), 64)
  66. }
  67. // IDValue returns the string representation of an identifier token.
  68. func (t *Token) IDValue() string {
  69. return string(t.Lit)
  70. }
  71. // Int32Value returns the int32 value of the token or an error if the token literal does not
  72. // denote a valid float64.
  73. func (t *Token) Int32Value() (int32, error) {
  74. if v, err := strconv.ParseInt(string(t.Lit), 10, 64); err != nil {
  75. return 0, err
  76. } else {
  77. return int32(v), nil
  78. }
  79. }
  80. // Int64Value returns the int64 value of the token or an error if the token literal does not
  81. // denote a valid float64.
  82. func (t *Token) Int64Value() (int64, error) {
  83. return strconv.ParseInt(string(t.Lit), 10, 64)
  84. }
  85. // UTF8Rune decodes the UTF8 rune in the token literal. It returns utf8.RuneError if
  86. // the token literal contains an invalid rune.
  87. func (t *Token) UTF8Rune() (rune, error) {
  88. r, _ := utf8.DecodeRune(t.Lit)
  89. if r == utf8.RuneError {
  90. err := fmt.Errorf("Invalid rune")
  91. return r, err
  92. }
  93. return r, nil
  94. }
  95. // StringValue returns the string value of the token literal.
  96. func (t *Token) StringValue() string {
  97. return string(t.Lit[1 : len(t.Lit)-1])
  98. }
  99. var TokMap = TokenMap{
  100. typeMap: []string{
  101. "INVALID",
  102. "$",
  103. "graphx",
  104. "{",
  105. "}",
  106. "strict",
  107. "digraph",
  108. ";",
  109. "=",
  110. "node",
  111. "edge",
  112. "[",
  113. "]",
  114. ",",
  115. ":",
  116. "subgraph",
  117. "->",
  118. "--",
  119. "id",
  120. },
  121. idMap: map[string]Type{
  122. "INVALID": 0,
  123. "$": 1,
  124. "graphx": 2,
  125. "{": 3,
  126. "}": 4,
  127. "strict": 5,
  128. "digraph": 6,
  129. ";": 7,
  130. "=": 8,
  131. "node": 9,
  132. "edge": 10,
  133. "[": 11,
  134. "]": 12,
  135. ",": 13,
  136. ":": 14,
  137. "subgraph": 15,
  138. "->": 16,
  139. "--": 17,
  140. "id": 18,
  141. },
  142. }