-
Notifications
You must be signed in to change notification settings - Fork 7
/
lexer.go
161 lines (145 loc) · 3 KB
/
lexer.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package goscheme
import (
"bufio"
"io"
"strings"
"unicode"
)
// Tokenize return the scheme tokens of input string
func Tokenize(inputScript string) []string {
t := NewTokenizerFromString(inputScript)
return t.Tokens()
}
// Tokenizer wraps the input to generate tokens.
type Tokenizer struct {
Source *bufio.Reader
EOF bool
currentCh rune
currentToken string
}
// NewTokenizerFromString construct *Tokenizer from string
func NewTokenizerFromString(input string) *Tokenizer {
return &Tokenizer{Source: bufio.NewReader(strings.NewReader(input)), currentCh: -1}
}
// NewTokenizerFromReader construct *Tokenizer from io.Reader
func NewTokenizerFromReader(input io.Reader) *Tokenizer {
return &Tokenizer{Source: bufio.NewReader(input), currentCh: -1}
}
func (t *Tokenizer) readAhead() {
if t.EOF {
return
}
r, _, err := t.Source.ReadRune()
if err == io.EOF {
t.EOF = true
return
}
t.currentCh = r
}
func (t *Tokenizer) readString() (string, bool) {
buf := make([]rune, 0, 10)
buf = append(buf, '"')
t.readAhead()
for !t.EOF && t.currentCh != '"' {
if t.currentCh == '\\' {
t.readAhead()
if t.currentCh == 'n' {
buf = append(buf, '\n')
} else if t.currentCh == 't' {
buf = append(buf, '\t')
} else {
buf = append(buf, t.currentCh)
}
t.readAhead()
continue
}
buf = append(buf, t.currentCh)
t.readAhead()
}
if t.EOF {
return "", !t.EOF
}
buf = append(buf, '"')
t.readAhead()
return string(buf), true
}
func (t *Tokenizer) readSymbol() (string, bool) {
buf := make([]rune, 0, 1)
if t.EOF {
return "", false
}
for !t.EOF && isSymbolCh(t.currentCh) {
buf = append(buf, t.currentCh)
t.readAhead()
}
return string(buf), true
}
func isSymbolCh(r rune) bool {
return !unicode.IsSpace(r) && !strings.ContainsRune("()'", r)
}
func (t *Tokenizer) skipComment() {
for t.currentCh == ';' {
for t.currentCh != '\n' {
t.readAhead()
if t.EOF {
return
}
}
t.readAhead()
}
}
func (t *Tokenizer) readNextToken() (string, bool) {
if t.EOF {
t.currentCh = 0
t.currentToken = ""
return "", false
}
for t.currentCh == -1 || unicode.IsSpace(t.currentCh) {
t.readAhead()
if t.EOF {
t.currentToken = ""
t.currentCh = 0
t.EOF = true
return "", false
}
}
if t.currentCh == ';' {
t.skipComment()
return t.readNextToken()
}
if t.currentCh == '"' {
return t.readString()
}
if t.currentCh == '(' {
t.readAhead()
return "(", true
}
if t.currentCh == ')' {
t.readAhead()
return ")", true
}
if isSymbolCh(t.currentCh) {
return t.readSymbol()
}
if t.currentCh == '\'' {
t.readAhead()
return "'", true
}
return "", false
}
// NextToken read ahead and returns the next valid token.
func (t *Tokenizer) NextToken() (string, bool) {
token, ok := t.readNextToken()
t.currentToken = token
return t.currentToken, ok
}
// Tokens returns all the tokens
func (t *Tokenizer) Tokens() []string {
var ret []string
token, ok := t.NextToken()
for ok {
ret = append(ret, token)
token, ok = t.NextToken()
}
return ret
}