-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.go
99 lines (81 loc) · 1.87 KB
/
parse.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
//go:generate go get -u golang.org/x/tools/cmd/goyacc
//go:generate goyacc -o syntax.go syntax.y
package parser
import (
"github.com/BenLubar/bit/ast"
"github.com/BenLubar/bit/token"
)
func init() {
yyErrorVerbose = true
}
type parseError struct {
message string
}
func (err parseError) Error() string {
return err.message
}
func Parse(tokens []token.Token) (prog *ast.Program, err error) {
ch := make(chan token.Token, len(tokens))
for _, t := range tokens {
ch <- t
}
close(ch)
p := &parser{tokens: ch, prog: &ast.Program{}}
yyNewParser().Parse(p)
return p.prog, nil
}
func ParseChan(tokens <-chan token.Token, lines chan<- *ast.Line) (err error) {
defer close(lines)
defer func() {
if r := recover(); r != nil {
err = r.(parseError)
}
}()
p := &parser{tokens: tokens, lines: lines}
yyNewParser().Parse(p)
return nil
}
type parser struct {
tokens <-chan token.Token
lines chan<- *ast.Line
prog *ast.Program
}
var tokenMap = [...]int{
token.AddressOf: tAddressOf,
token.At: tAt,
token.Beyond: tBeyond,
token.Close: tClose,
token.Code: tCode,
token.Equals: tEquals,
token.Goto: tGoto,
token.If: tIf,
token.Is: tIs,
token.JumpRegister: tJumpRegister,
token.LineNumber: tLineNumber,
token.Nand: tNand,
token.One: tOne,
token.Open: tOpen,
token.Parenthesis: tParenthesis,
token.Print: tPrint,
token.Read: tRead,
token.The: tThe,
token.Value: tValue,
token.Variable: tVariable,
token.Zero: tZero,
}
func (p *parser) line(l *ast.Line) {
if p.prog != nil {
p.prog.Lines = append(p.prog.Lines, l)
} else {
p.lines <- l
}
}
func (p *parser) Lex(*yySymType) int {
if t, ok := <-p.tokens; ok {
return tokenMap[t]
}
return -1
}
func (p *parser) Error(message string) {
panic(parseError{message})
}