-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
78 lines (69 loc) · 1.43 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
package lr0
import (
"io"
"github.com/pkg/errors"
)
func newParser(g *grammar) Parser {
return &parser{
g: g,
t: newTable(g),
}
}
type parser struct {
g *grammar
t *table
}
func (p *parser) Parse(input *State) (result any, err error) {
st := newStack(p.t)
var (
next = input
ok bool
to tableStateIndex
)
Goal:
for {
at := next
for st.Current().IsReduceOnly() {
ok, err = st.Reduce()
if err != nil {
return nil, WithSource(err, at)
}
if !ok {
// if this happens ever?
// REFACT: looks like this will never happen now
// no reduce rule - unexpected input
//st.Current().TerminalsSet()
return nil, WithSource(NewParseError("unexpected input 1"), at)
}
}
var m *Match
if !next.IsEOF() {
next, m, err = p.g.Match(next, st.Current().TerminalsSet())
if err != nil && err != io.EOF {
return nil, errors.Wrap(err, "unexpected input")
}
}
for {
if m != nil {
if to, ok = st.Current().TerminalAction(m.Term); ok {
st.Shift(to, m.Term, m.Value)
break
}
}
if st.Current().AcceptEof() {
if m == nil {
break Goal
}
return nil, WithSource(NewParseError("unexpected input instead of EOF"), at)
}
ok, err = st.Reduce()
if err != nil {
return nil, WithSource(err, at)
}
if !ok {
return nil, WithSource(p.g.ExpectationError(st.Current().TerminalsSet(), "unexpected input"), at)
}
}
}
return st.Done(), nil
}