-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexer.go
175 lines (161 loc) · 3.59 KB
/
lexer.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
package lr0
import (
"io"
"github.com/pkg/errors"
)
//// Lexer does search predefined Terminals in an input stream.
//type Lexer interface {
// NamedHiddenRegistry
// // IsTerminal returns true if the given Id is one of defined Terminal
// IsTerminal(id Id) bool
// // GetTerminalIdsSet returns new set of all defined Id
// GetTerminalIdsSet() idSet
// // Match tries to parse one of expected Terminals in the given State.
// //
// // Order in `expected` does not matter. Only definition order ot Terminals
// // does matter.
// //
// // If none of expected Terminals matched, it will try to match first of the
// // rest unexpected Terminals.
// //
// // At EOF the final eof State and io.EOF will be returned.
// //
// // When nothing matched, a ErrParse wrapped error will be returned.
// Match(state *State, expected readonlyIdSet) (next *State, m *Match, err error)
// ExpectationError(expected readonlyIdSet, pre string) error
//}
type termMap = map[Id]Terminal
type lexer struct {
list []Terminal
terminals termMap
internalTerms map[Id][]Terminal
}
// newLexer creates a new empty Configurable
func newLexer(t ...Terminal) *lexer {
l := &lexer{
list: make([]Terminal, 0, len(t)),
terminals: make(termMap),
internalTerms: make(map[Id][]Terminal),
}
for _, ti := range t {
id := ti.Id()
if id == InvalidId {
panic(errors.Wrap(ErrDefine, "zero id"))
}
if id < 0 {
prev, _ := l.internalTerms[id]
l.internalTerms[id] = append(prev, ti)
continue
}
if prev, exists := l.terminals[id]; exists {
if prev == ti {
continue
}
panic(errors.Wrapf(ErrDefine, "redefine terminal %v with %v", dumpSymbol(prev), dumpSymbol(ti)))
}
l.list = append(l.list, ti)
l.terminals[id] = ti
}
return l
}
func (l *lexer) SymbolName(id Id) string {
if s, ok := l.terminals[id]; ok {
return s.Name()
}
return ""
}
func (l *lexer) IsTerminal(id Id) bool {
_, ok := l.terminals[id]
return ok
}
func (l *lexer) IsHidden(id Id) bool {
t, ok := l.terminals[id]
return ok && t.IsHidden()
}
func (l *lexer) GetTerminalIdsSet() idSet {
m := newIdSet()
for id := range l.terminals {
m.Add(id)
}
return m
}
func (l *lexer) Match(state *State, expected readonlyIdSet) (*State, *Match, error) {
state = l.skipWhitespaces(state)
if state.IsEOF() {
return state, nil, io.EOF
}
var (
altNext *State
altM *Match
)
for _, t := range l.list {
nextS, v := t.Match(state)
if v != nil {
if err, ok := v.(error); ok {
return nil, nil, err
}
}
if nextS != nil {
m2 := &Match{
Term: t.Id(),
Value: v,
}
if expected.Has(t.Id()) {
return nextS, m2, nil
}
if altNext == nil {
altNext = nextS
altM = m2
}
}
}
if altNext != nil {
return altNext, altM, nil
}
return nil, nil, WithSource(l.ExpectationError(expected, ""), state)
}
func (l *lexer) ExpectationError(expected readonlyIdSet, pre string) error {
s := pre
if s != "" {
s += ": "
}
s += "expected "
i, last := 0, expected.Count()-1
for _, t := range l.list {
if !expected.Has(t.Id()) {
continue
}
if i > 0 {
if i < last {
s += ", "
} else {
s += " or "
}
}
i++
s += dumpSymbol(t)
}
return NewParseError(s)
}
func (l *lexer) skipWhitespaces(state *State) (next *State) {
next = state
wsList, ok := l.internalTerms[tWhitespace]
if !ok {
return
}
WsType:
for !next.IsEOF() {
for _, ws := range wsList {
to, _ := ws.Match(next)
// is this ws type matched, move further and retry if we will
// find another ws type
if to != nil {
next = to
continue WsType
}
}
// no ws here, done
break
}
return
}