-
Notifications
You must be signed in to change notification settings - Fork 0
/
sexp_parser.go
50 lines (41 loc) · 835 Bytes
/
sexp_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
package smt
import (
"errors"
"fmt"
"io"
)
var ParserEOF = errors.New("End-of-Input")
type Parser struct {
sexps chan Sexp
errs chan error
}
func (p *Parser) Read() (Sexp, error) {
select {
case s := <-p.sexps:
return s, nil
case err := <-p.errs:
return nil, err
}
}
func (p *Parser) emit(s Sexp) {
p.sexps <- s
}
func NewParser(r io.Reader) *Parser {
p := &Parser{
sexps: make(chan Sexp),
errs: make(chan error),
}
go p.streamingParse(r)
return p
}
func (p *Parser) streamingParse(r io.Reader) {
// this is weird, but without passing in a reference to this
// parser object through the lexer, there isn't another good
// way to keep the parser and lexer reentrant.
err := smtParse(newSmtLex(r, p))
if err != 0 {
p.errs <- fmt.Errorf("%d parse errors", err)
} else {
p.errs <- ParserEOF
}
}