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