-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader.go
167 lines (156 loc) · 3.56 KB
/
reader.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
162
163
164
165
166
167
// Copyright 2012 Yuichi Araki. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package yall
import (
"bufio"
"bytes"
"io"
"strconv"
"strings"
)
type reader struct {
input *bufio.Reader
}
func (r *reader) setInput(input io.Reader) {
r.input = bufio.NewReader(input)
}
func (r *reader) nextString() (token string, size int, err error) {
buffer := bytes.NewBufferString("\"")
size = 1
escaped := false
for {
rune, s, err := r.input.ReadRune()
size += s
if err != nil {
break
}
switch rune {
case '"':
buffer.WriteRune('"')
if !escaped {
return buffer.String(), size, nil
}
case '\\':
escaped = true
buffer.WriteRune(rune)
default:
escaped = false
buffer.WriteRune(rune)
}
}
return "", 0, NewSyntaxError("Unexpected EOS in string")
}
func (r *reader) nextToken() (token string, size int, err error) {
buffer := new(bytes.Buffer)
size = 0
for {
rune, s, err := r.input.ReadRune()
size += s
if err != nil { // EOS
if 0 < buffer.Len() {
return buffer.String(), size, nil
}
return "", 0, err
}
switch rune {
case '(', ')', '[', ']', '\'', '`':
if 0 < buffer.Len() {
r.input.UnreadRune()
size -= s
return buffer.String(), size, nil
}
return string(rune), size, nil
case ' ', '\t', '\r', '\n':
if 0 < buffer.Len() {
return buffer.String(), size, nil
}
case ',':
if 0 < buffer.Len() {
r.input.UnreadRune()
size -= s
return buffer.String(), size, nil
}
maybeAt, as, aerr := r.input.ReadRune()
if maybeAt != '@' || aerr != nil {
r.input.UnreadRune()
return ",", size, nil
}
size += as
return ",@", size, nil
case '"':
if 0 < buffer.Len() {
r.input.UnreadRune()
size -= s
return buffer.String(), size, nil
}
return r.nextString()
default:
buffer.WriteRune(rune)
}
}
return "", 0, NewSyntaxError("?")
}
func (r *reader) readTokens(asList bool) (Expr, int, error) {
token, size, err := r.nextToken()
if nil != err {
return False, 0, err
}
var expr Expr
if "(" == token {
list, listSize, listErr := r.readTokens(true)
expr = list
size += listSize
err = listErr
} else if ")" == token {
if !asList {
panic(NewSyntaxError("Unexpected end of list"))
}
expr = Empty
asList = false
} else if "'" == token {
rest, restSize, restErr := r.readTokens(false)
expr = NewQuoted(rest)
size += restSize
err = restErr
} else if "`" == token {
rest, restSize, restErr := r.readTokens(false)
expr = NewQuasiquoted(rest)
size += restSize
err = restErr
} else if "," == token {
rest, restSize, restErr := r.readTokens(false)
expr = NewUnquoted(rest)
size += restSize
err = restErr
} else if ",@" == token {
list, listSize, listErr := r.readTokens(false)
expr = NewSplicingUnquoted(list)
size += listSize
err = listErr
} else if i, ierr := strconv.Atoi(token); ierr == nil {
expr = NewInteger(i)
} else if isString(token) {
expr = NewString(token[1 : len(token)-1])
} else {
expr = NewSymbol(token)
}
if asList {
cdr, cdrSize, cdrErr := r.readTokens(true)
return NewCell(expr, cdr.(*Cell)), size + cdrSize, cdrErr
}
return expr, size, nil
}
func (r *reader) read(input io.Reader) (Expr, int, error) {
r.setInput(input)
return r.readTokens(false)
}
func (r *reader) Read() (Expr, int, error) {
return r.readTokens(false)
}
func Read(input io.Reader) (expr Expr, size int, err error) {
return new(reader).read(input)
}
func ReadFromString(s string) (expr Expr, size int, err error) {
return Read(strings.NewReader(s))
}