This repository has been archived by the owner on Jul 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
273 lines (245 loc) · 6.51 KB
/
parser.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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
//-----------------------------------------------------------------------------
// Copyright (c) 2022 Detlef Stern
//
// This file is part of sxpf.
//
// sxpf is licensed under the latest version of the EUPL // (European Union
// Public License). Please see file LICENSE.txt for your rights and obligations
// under this license.
//-----------------------------------------------------------------------------
package sxpf
import (
"bytes"
"errors"
"io"
"strings"
)
// ErrMissingOpenBracket is raised if there is one additional closing bracket.
var ErrMissingOpenBracket = errors.New("missing opening bracket")
// ErrMissingCloseBracket raised if there is one additonal opening bracket.
var ErrMissingCloseBracket = errors.New("missing closing bracket")
// ErrMissingOpenParenthesis is raised if there is one additional closing parenthesis.
var ErrMissingOpenParenthesis = errors.New("missing opening parenthesis")
// ErrMissingCloseParenthesis raised if there is one additonal opening parenthesis.
var ErrMissingCloseParenthesis = errors.New("missing closing parenthesis")
// ErrMissingOpenCurly is raised if there is one additional closing curly bracket.
var ErrMissingOpenCurly = errors.New("missing opening opening curly brackets")
// ErrMissingCloseCurly raised if there is one additonal opening curly bracket.
var ErrMissingCloseCurly = errors.New("missing closing curly brackets")
// ErrNestedTooDeeply is raised if brackets / parentheses were nested too deeply.
var ErrNestedTooDeeply = errors.New("exceeded max depth")
// ErrMissingQuote is raised if there is no closing quote character.
var ErrMissingQuote = errors.New("missing quote character")
// ErrMissing EOF is raised if there is additional input after an expression.
var ErrMissingEOF = errors.New("missing end of input")
// ErrUnknownToken is raised if an unexpected token occured.
var ErrUnknownToken = errors.New("unknown token")
func ParseString(smk SymbolMaker, src string) (Value, error) {
return consumeReader(smk, strings.NewReader(src))
}
func ParseBytes(smk SymbolMaker, src []byte) (Value, error) {
return consumeReader(smk, bytes.NewBuffer(src))
}
func consumeReader(smk SymbolMaker, r RuneReader) (Value, error) {
val, err := ParseValue(smk, r)
if err != nil {
return val, err
}
_, err = ParseValue(smk, r)
if err == io.EOF {
return val, nil
}
return val, ErrMissingEOF
}
type RuneReader interface {
ReadRune() (r rune, size int, err error)
UnreadRune() error
}
func ParseValue(smk SymbolMaker, rr RuneReader) (Value, error) {
pa := NewParser(smk, rr)
return pa.Parse()
}
type Parser struct {
smk SymbolMaker
sc *Scanner
tbuf []*Token
maxNesting uint
}
func NewParser(smk SymbolMaker, rr RuneReader) *Parser {
return &Parser{
smk: smk,
sc: NewScanner(rr),
tbuf: nil,
maxNesting: 10000,
}
}
func (pa *Parser) SetMaxNesting(n uint) uint {
prevN := pa.maxNesting
pa.maxNesting = n
return prevN
}
func (pa *Parser) Parse() (Value, error) {
return pa.parseValue(pa.next())
}
func (pa *Parser) next() Token {
if tb := pa.tbuf; len(tb) > 0 {
result := tb[0]
tb[0] = nil
if len(tb) > 1 {
pa.tbuf = tb[1:]
} else {
pa.tbuf = nil
}
return *result
}
tok := pa.sc.Next()
switch tok.Typ {
case TokLeftBrack:
// Fill buffer until right bracket
return pa.fillBuffer(&tok, TokRightBrack, ErrMissingCloseBracket)
case TokLeftParen:
// Fill buffer until right parenthesis
return pa.fillBuffer(&tok, TokRightParen, ErrMissingCloseParenthesis)
case TokLeftCurly:
// Fill buffer until right curly bracket
return pa.fillBuffer(&tok, TokRightCurly, ErrMissingCloseCurly)
}
return tok
}
func (pa *Parser) fillBuffer(token *Token, etyp TokenType, errEOF error) Token {
typ := token.Typ
nesting := uint(0)
for {
tok := pa.sc.Next()
switch tok.Typ {
case TokEOF:
pa.sc.err = errEOF
return Token{Typ: TokErr}
case TokErr:
return tok
case TokLeftBrack, TokLeftParen, TokLeftCurly:
pa.tbuf = append(pa.tbuf, &tok)
nesting++
if nesting >= pa.maxNesting {
pa.sc.err = ErrNestedTooDeeply
return Token{Typ: TokErr}
}
case TokRightBrack, TokRightParen, TokRightCurly:
pa.tbuf = append(pa.tbuf, &tok)
if nesting == 0 {
if tok.Typ == etyp {
return *token
}
switch typ {
case TokRightBrack:
pa.sc.err = ErrMissingCloseBracket
case TokRightParen:
pa.sc.err = ErrMissingCloseParenthesis
case TokRightCurly:
pa.sc.err = ErrMissingCloseCurly
}
return Token{Typ: TokErr}
}
nesting--
default:
pa.tbuf = append(pa.tbuf, &tok)
}
}
}
func (pa *Parser) err() error { return pa.sc.Err() }
func (pa *Parser) parseValue(tok Token) (Value, error) {
switch tok.Typ {
case TokEOF:
return nil, io.EOF
case TokErr:
return nil, pa.err()
case TokLeftParen:
return pa.parseList()
case TokLeftBrack:
return pa.parseVector()
case TokString:
return NewString(tok.Val), nil
case TokRightParen, TokPeriod:
return nil, ErrMissingOpenParenthesis
case TokRightBrack:
return nil, ErrMissingOpenBracket
case TokRightCurly:
return nil, ErrMissingOpenCurly
case TokSymbol:
return pa.smk.MakeSymbol(tok.Val), nil
default:
return nil, ErrUnknownToken
}
}
func (pa *Parser) parseVector() (Value, error) {
elems := []Value{}
for {
tok := pa.next()
switch tok.Typ {
case TokEOF:
return nil, ErrMissingCloseBracket
case TokErr:
return nil, pa.err()
case TokRightBrack:
return NewVector(elems...), nil
}
val, err := pa.parseValue(tok)
if err != nil {
return nil, err
}
elems = append(elems, val)
}
}
func (pa *Parser) parseList() (Value, error) {
elems := []Value{}
loop:
for {
tok := pa.next()
switch tok.Typ {
case TokEOF:
return nil, ErrMissingCloseParenthesis
case TokErr:
return nil, pa.err()
case TokRightParen:
p := Nil()
for i := len(elems) - 1; i >= 0; i-- {
p = NewPair(elems[i], p)
}
return p, nil
case TokPeriod:
if len(elems) == 0 {
return nil, ErrMissingCloseParenthesis
}
break loop
}
val, err := pa.parseValue(tok)
if err != nil {
return nil, err
}
elems = append(elems, val)
}
tok := pa.next()
switch tok.Typ {
case TokEOF:
return nil, ErrMissingCloseParenthesis
case TokErr:
return nil, pa.err()
}
val, err := pa.parseValue(tok)
if err != nil {
return nil, err
}
tok = pa.next()
switch tok.Typ {
case TokErr:
return nil, pa.err()
case TokRightParen:
default:
return nil, ErrMissingCloseParenthesis
}
p := NewPair(elems[len(elems)-1], val)
for i := len(elems) - 2; i >= 0; i-- {
p = NewPair(elems[i], p)
}
return p, nil
}