-
Notifications
You must be signed in to change notification settings - Fork 1
/
token.go
executable file
·132 lines (110 loc) · 2.24 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Package token is generated by GoGLL. Do not edit
package token
import(
"fmt"
)
// Token is returned by the lexer for every scanned lexical token
type Token struct {
typ Type
lext, rext int
input []rune
}
/*
New returns a new token.
lext is the left extent and rext the right extent of the token in the input.
input is the input slice scanned by the lexer.
*/
func New(t Type, lext, rext int, input []rune) *Token {
return &Token{
typ: t,
lext: lext,
rext: rext,
input: input,
}
}
// GetLineColumn returns the line and column of the left extent of t
func (t *Token) GetLineColumn() (line, col int) {
line, col = 1, 1
for j := 0; j < t.lext; j++ {
switch t.input[j] {
case '\n':
line++
col = 1
case '\t':
col += 4
default:
col++
}
}
return
}
// GetInput returns the input from which t was parsed.
func (t *Token) GetInput() []rune {
return t.input
}
// Lext returns the left extent of t
func (t *Token) Lext() int {
return t.lext
}
// Literal returs the literal runes of t scanned by the lexer
func (t *Token) Literal() []rune {
return t.input[t.lext:t.rext]
}
// LiteralString returns string(t.Literal())
func (t *Token) LiteralString() string {
return string(t.Literal())
}
// Rext returns the right extent of t in the input
func (t *Token) Rext() int {
return t.rext
}
func (t *Token) String() string {
return fmt.Sprintf("%s (%d,%d) %s",
t.TypeID(), t.lext, t.rext, t.LiteralString())
}
// Type returns the token Type of t
func (t *Token) Type() Type {
return t.typ
}
// TypeID returns the token Type ID of t.
// This may be different from the literal of token t.
func (t *Token) TypeID() string {
return t.Type().ID()
}
// Type is the token type
type Type int
func (t Type) String() string {
return TypeToString[t]
}
// ID returns the token type ID of token Type t
func (t Type) ID() string {
return TypeToID[t]
}
const(
Error Type = iota // Error
EOF // EOF
Type0 // &
Type1 // id
Type2 // |
)
var TypeToString = []string{
"Error",
"EOF",
"Type0",
"Type1",
"Type2",
}
var StringToType = map[string] Type {
"Error" : Error,
"EOF" : EOF,
"Type0" : Type0,
"Type1" : Type1,
"Type2" : Type2,
}
var TypeToID = []string {
"Error",
"EOF",
"&",
"id",
"|",
}