-
Notifications
You must be signed in to change notification settings - Fork 1
/
tokenizer.go
61 lines (53 loc) · 1.1 KB
/
tokenizer.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
package token
import (
"github.com/akm/tparser/runes"
)
type TokeninzerFlag uint
const (
LoadSpace TokeninzerFlag = 1 << iota
LoadComment
)
type Tokenizer struct {
*runes.Cursor
loadSpace bool
loadComment bool
}
func NewTokenizer(text *[]rune, flags TokeninzerFlag) *Tokenizer {
return &Tokenizer{
Cursor: runes.NewCursor(text),
loadSpace: flags&LoadSpace == LoadSpace,
loadComment: flags&LoadComment == LoadComment,
}
}
func (t *Tokenizer) Clone() *Tokenizer {
return &Tokenizer{
Cursor: t.Cursor.Clone(),
loadSpace: t.loadSpace,
loadComment: t.loadComment,
}
}
var processors = []func(*runes.Cursor) *Token{
ProcessEof,
ProcessComment,
ProcessString,
ProcessNumeral,
ProcessDoubleSpecialSymbol,
ProcessSingleSpecialSymbol,
ProcessWord,
ProcessSpace,
}
func (t *Tokenizer) GetNext() *Token {
for _, proc := range processors {
token := proc(t.Cursor)
if token != nil {
if !t.loadSpace && token.Type == Space {
return t.GetNext()
} else if !t.loadComment && token.Type == Comment {
return t.GetNext()
} else {
return token
}
}
}
return nil
}