http urls monitor.

token.go 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package token
  2. import (
  3. "fmt"
  4. "strconv"
  5. hcltoken "github.com/hashicorp/hcl/hcl/token"
  6. )
  7. // Token defines a single HCL token which can be obtained via the Scanner
  8. type Token struct {
  9. Type Type
  10. Pos Pos
  11. Text string
  12. }
  13. // Type is the set of lexical tokens of the HCL (HashiCorp Configuration Language)
  14. type Type int
  15. const (
  16. // Special tokens
  17. ILLEGAL Type = iota
  18. EOF
  19. identifier_beg
  20. literal_beg
  21. NUMBER // 12345
  22. FLOAT // 123.45
  23. BOOL // true,false
  24. STRING // "abc"
  25. NULL // null
  26. literal_end
  27. identifier_end
  28. operator_beg
  29. LBRACK // [
  30. LBRACE // {
  31. COMMA // ,
  32. PERIOD // .
  33. COLON // :
  34. RBRACK // ]
  35. RBRACE // }
  36. operator_end
  37. )
  38. var tokens = [...]string{
  39. ILLEGAL: "ILLEGAL",
  40. EOF: "EOF",
  41. NUMBER: "NUMBER",
  42. FLOAT: "FLOAT",
  43. BOOL: "BOOL",
  44. STRING: "STRING",
  45. NULL: "NULL",
  46. LBRACK: "LBRACK",
  47. LBRACE: "LBRACE",
  48. COMMA: "COMMA",
  49. PERIOD: "PERIOD",
  50. COLON: "COLON",
  51. RBRACK: "RBRACK",
  52. RBRACE: "RBRACE",
  53. }
  54. // String returns the string corresponding to the token tok.
  55. func (t Type) String() string {
  56. s := ""
  57. if 0 <= t && t < Type(len(tokens)) {
  58. s = tokens[t]
  59. }
  60. if s == "" {
  61. s = "token(" + strconv.Itoa(int(t)) + ")"
  62. }
  63. return s
  64. }
  65. // IsIdentifier returns true for tokens corresponding to identifiers and basic
  66. // type literals; it returns false otherwise.
  67. func (t Type) IsIdentifier() bool { return identifier_beg < t && t < identifier_end }
  68. // IsLiteral returns true for tokens corresponding to basic type literals; it
  69. // returns false otherwise.
  70. func (t Type) IsLiteral() bool { return literal_beg < t && t < literal_end }
  71. // IsOperator returns true for tokens corresponding to operators and
  72. // delimiters; it returns false otherwise.
  73. func (t Type) IsOperator() bool { return operator_beg < t && t < operator_end }
  74. // String returns the token's literal text. Note that this is only
  75. // applicable for certain token types, such as token.IDENT,
  76. // token.STRING, etc..
  77. func (t Token) String() string {
  78. return fmt.Sprintf("%s %s %s", t.Pos.String(), t.Type.String(), t.Text)
  79. }
  80. // HCLToken converts this token to an HCL token.
  81. //
  82. // The token type must be a literal type or this will panic.
  83. func (t Token) HCLToken() hcltoken.Token {
  84. switch t.Type {
  85. case BOOL:
  86. return hcltoken.Token{Type: hcltoken.BOOL, Text: t.Text}
  87. case FLOAT:
  88. return hcltoken.Token{Type: hcltoken.FLOAT, Text: t.Text}
  89. case NULL:
  90. return hcltoken.Token{Type: hcltoken.STRING, Text: ""}
  91. case NUMBER:
  92. return hcltoken.Token{Type: hcltoken.NUMBER, Text: t.Text}
  93. case STRING:
  94. return hcltoken.Token{Type: hcltoken.STRING, Text: t.Text, JSON: true}
  95. default:
  96. panic(fmt.Sprintf("unimplemented HCLToken for type: %s", t.Type))
  97. }
  98. }