-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlexer.go
More file actions
202 lines (166 loc) · 3.68 KB
/
Copy pathlexer.go
File metadata and controls
202 lines (166 loc) · 3.68 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package sexpr
import (
"bytes"
"errors"
"fmt"
"io"
"math/big"
"strings"
"unicode"
)
// ErrLexer is the error value returned by the Lexer if the contains
// an invalid token.
// See also https://golang.org/pkg/errors/#New
// and // https://golang.org/pkg/builtin/#error
var ErrLexer = errors.New("lexer error")
// tokenType enumerates all types to tokens
// See also https://stackoverflow.com/questions/14426366/what-is-an-idiomatic-way-of-representing-enums-in-go
type tokenType int
const (
tokenEOF tokenType = iota
tokenSymbol
tokenNumber
tokenLpar
tokenRpar
tokenDot
tokenQuote
)
// A token is a Lisp atom, including a number.
type token struct {
typ tokenType
literal string
// `num` is (a pointer to) an __unbounded__ integer
// See also https://golang.org/pkg/math/big/
num *big.Int
}
func equalToken(tok1, tok2 *token) bool {
return tok1 != nil && tok2 != nil &&
tok1.typ == tok2.typ &&
tok1.literal == tok2.literal &&
(tok1.num == tok2.num ||
tok1.num != nil && tok2.num != nil && tok1.num.Cmp(tok2.num) == 0)
}
func (tok *token) String() string {
if tok.typ == tokenNumber {
return fmt.Sprintf("%d", tok.num)
}
return fmt.Sprintf("%s", tok.literal)
}
type lexer struct {
rd io.RuneReader
peeking bool
peekRune rune
last rune
buf bytes.Buffer
}
func newLexer(input string) *lexer {
return &lexer{
rd: strings.NewReader(input),
}
}
var tokens = make(map[string]*token)
func mkToken(typ tokenType, literal string) *token {
tok := tokens[literal]
if tok == nil {
if typ == tokenNumber {
// This error is checked in call-sites
num, _ := new(big.Int).SetString(literal, 10)
tok = &token{typ: typ, num: num}
} else {
tok = &token{typ: typ, literal: literal}
}
}
return tok
}
func mkTokenEOF() *token {
return mkToken(tokenEOF, "")
}
func mkTokenLpar() *token {
return mkToken(tokenLpar, "(")
}
func mkTokenRpar() *token {
return mkToken(tokenRpar, ")")
}
func mkTokenDot() *token {
return mkToken(tokenDot, ".")
}
func mkTokenQuote() *token {
return mkToken(tokenQuote, "QUOTE")
}
func mkTokenNumber(literal string) *token {
return mkToken(tokenNumber, literal)
}
func mkTokenSymbol(literal string) *token {
return mkToken(tokenSymbol, literal)
}
func (l *lexer) next() (*token, error) {
for {
r := l.read()
switch {
case isSpace(r):
case r == eofRune:
return mkTokenEOF(), nil
case r == '(':
return mkTokenLpar(), nil
case r == ')':
return mkTokenRpar(), nil
case r == '.':
return mkTokenDot(), nil
case r == '\'':
return mkTokenQuote(), nil
default:
// try to tokenize a symbol or a number
l.accum(r, isSymbolOrNumber)
literal := l.buf.String()
// try to tokenize a number
if _, ok := new(big.Int).SetString(literal, 10); ok {
return mkTokenNumber(literal), nil
}
// error if neither symbol or number
if !isSymbolOrNumber(r) {
return nil, ErrLexer
}
return mkTokenSymbol(literal), nil
}
}
}
const eofRune rune = -1
func (l *lexer) read() rune {
if l.peeking {
l.peeking = false
return l.peekRune
}
r, _, err := l.rd.ReadRune()
if err == io.EOF {
r = eofRune
}
l.last = r
return r
}
func (l *lexer) accum(r rune, valid func(rune) bool) {
l.buf.Reset()
for {
l.buf.WriteRune(rune(unicode.ToUpper(r)))
r = l.read()
if r == eofRune {
return
}
if !valid(r) {
l.back(r)
return
}
}
}
func (l *lexer) back(r rune) {
l.peeking = true
l.peekRune = r
}
func isSpace(r rune) bool {
return r == ' ' || r == '\t' || r == '\n' || r == '\r'
}
func isSymbolOrNumber(r rune) bool {
return r == '_' || r == '-' || r == '+' || r == '*' || unicode.IsLetter(r) || isNumber(r)
}
func isNumber(r rune) bool {
return '0' <= r && r <= '9'
}