You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
2.0 KiB

  1. // Copyright 2009 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // Package token defines constants representing the lexical tokens of the gcfg
  5. // configuration syntax and basic operations on tokens (printing, predicates).
  6. //
  7. // Note that the API for the token package may change to accommodate new
  8. // features or implementation changes in gcfg.
  9. //
  10. package token
  11. import "strconv"
  12. // Token is the set of lexical tokens of the gcfg configuration syntax.
  13. type Token int
  14. // The list of tokens.
  15. const (
  16. // Special tokens
  17. ILLEGAL Token = iota
  18. EOF
  19. COMMENT
  20. literal_beg
  21. // Identifiers and basic type literals
  22. // (these tokens stand for classes of literals)
  23. IDENT // section-name, variable-name
  24. STRING // "subsection-name", variable value
  25. literal_end
  26. operator_beg
  27. // Operators and delimiters
  28. ASSIGN // =
  29. LBRACK // [
  30. RBRACK // ]
  31. EOL // \n
  32. operator_end
  33. )
  34. var tokens = [...]string{
  35. ILLEGAL: "ILLEGAL",
  36. EOF: "EOF",
  37. COMMENT: "COMMENT",
  38. IDENT: "IDENT",
  39. STRING: "STRING",
  40. ASSIGN: "=",
  41. LBRACK: "[",
  42. RBRACK: "]",
  43. EOL: "\n",
  44. }
  45. // String returns the string corresponding to the token tok.
  46. // For operators and delimiters, the string is the actual token character
  47. // sequence (e.g., for the token ASSIGN, the string is "="). For all other
  48. // tokens the string corresponds to the token constant name (e.g. for the
  49. // token IDENT, the string is "IDENT").
  50. //
  51. func (tok Token) String() string {
  52. s := ""
  53. if 0 <= tok && tok < Token(len(tokens)) {
  54. s = tokens[tok]
  55. }
  56. if s == "" {
  57. s = "token(" + strconv.Itoa(int(tok)) + ")"
  58. }
  59. return s
  60. }
  61. // Predicates
  62. // IsLiteral returns true for tokens corresponding to identifiers
  63. // and basic type literals; it returns false otherwise.
  64. //
  65. func (tok Token) IsLiteral() bool { return literal_beg < tok && tok < literal_end }
  66. // IsOperator returns true for tokens corresponding to operators and
  67. // delimiters; it returns false otherwise.
  68. //
  69. func (tok Token) IsOperator() bool { return operator_beg < tok && tok < operator_end }