-
Notifications
You must be signed in to change notification settings - Fork 0
/
token.go
72 lines (54 loc) · 1.37 KB
/
token.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Copyright (c) 2016 David R. Jenni. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package token defines constants representing the lexical tokens for pg.
package token
import "strconv"
// Type is the set of lexical tokens of pg.
type Type int
// Token constants
const (
// Special tokens
ILLEGAL Type = iota
EOF
// Identifiers and literals
literalBeg
IDENT // Foo
STRING // "abc"
literalEnd
// Operators and delimiters
operatorBeg
ARROW // -> or →
PERIOD // .
PIPE // |
operatorEnd
// Keyword
EPSILON // e or ε
)
var tokens = [...]string{
ILLEGAL: "ILLEGAL",
EOF: "EOF",
IDENT: "IDENT",
STRING: "STRING",
ARROW: "ARROW",
PERIOD: "PERIOD",
PIPE: "PIPE",
EPSILON: "EPSILON",
}
// String returns the string corresponding to the token.
func (t Type) String() string {
s := ""
if 0 <= t && t < Type(len(tokens)) {
s = tokens[t]
}
if s == "" {
s = "token(" + strconv.Itoa(int(t)) + ")"
}
return s
}
// IsLiteral returns true for tokens corresponding to literals; it
// returns false otherwise.
func (t Type) IsLiteral() bool { return literalBeg < t && t < literalEnd }
// IsOperator returns true for tokens corresponding to operators and
// delimiters; it returns false otherwise.
func (t Type) IsOperator() bool { return operatorBeg < t && t < operatorEnd }