-
Notifications
You must be signed in to change notification settings - Fork 0
/
expectations.go
288 lines (246 loc) · 6.99 KB
/
expectations.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package parser
import (
"fmt"
"github.com/dnsge/orange/asm/lexer"
"strings"
)
// Expect returns an ExpectationEntry that captures an expected TokenKind
func Expect(kind lexer.TokenKind) ExpectationEntry {
return &singleExpectationEntry{
kind: kind,
keep: true,
}
}
// ExpectIgnore returns an ExpectationEntry that expects but ignores a TokenKind
func ExpectIgnore(kind lexer.TokenKind) ExpectationEntry {
return &singleExpectationEntry{
kind: kind,
keep: false,
}
}
// ExpectAny returns an ExpectationEntry that captures one of any given TokenKind
func ExpectAny(kinds ...lexer.TokenKind) ExpectationEntry {
return &multipleExpectationEntry{
kinds: kinds,
keep: true,
}
}
// ExpectAnyIgnore returns an ExpectationEntry that expects but ignores one of any given TokenKind
func ExpectAnyIgnore(kinds ...lexer.TokenKind) ExpectationEntry {
return &multipleExpectationEntry{
kinds: kinds,
keep: false,
}
}
type singleExpectationEntry struct {
kind lexer.TokenKind
keep bool
}
func (s *singleExpectationEntry) Matches(kind lexer.TokenKind) bool {
return s.kind == kind
}
func (s *singleExpectationEntry) Keep() bool {
return s.keep
}
func (s *singleExpectationEntry) Describe() string {
return lexer.DescribeTokenKind(s.kind)
}
type multipleExpectationEntry struct {
kinds []lexer.TokenKind
keep bool
}
func (m *multipleExpectationEntry) Matches(kind lexer.TokenKind) bool {
for _, k := range m.kinds {
if k == kind {
return true
}
}
return false
}
func (m *multipleExpectationEntry) Keep() bool {
return m.keep
}
func (m *multipleExpectationEntry) Describe() string {
if len(m.kinds) == 1 {
return lexer.DescribeTokenKind(m.kinds[0])
}
res := "one of "
for i := range m.kinds {
res += lexer.DescribeTokenKind(m.kinds[i])
if i != len(m.kinds)-1 {
res += ", "
}
}
return res
}
// ExpectationEntry describes a possible token expectation that can be matched
// within stream of many Tokens.
type ExpectationEntry interface {
Matches(kind lexer.TokenKind) bool
Keep() bool
Describe() string
}
// Expectation is an aggregate of ExpectationEntries that matches multiple
// expected Tokens.
type Expectation struct {
keepCount int
entries []ExpectationEntry
description string
}
func NewExpectation(description string, entries ...ExpectationEntry) *Expectation {
keepCount := 0
for _, entry := range entries {
if entry.Keep() {
keepCount++
}
}
return &Expectation{
keepCount: keepCount,
entries: entries,
description: description,
}
}
// ExtractionCount returns the number of captured (e.g. not ignored) Tokens
// that its pattern describes
func (e *Expectation) ExtractionCount() int {
return e.keepCount
}
// ExtractExpectedStructure attempts to extract a subset of Tokens from a
// TokenStream, storing the results in dest. dest must have a capacity large
// enough to store all the expected tokens.
//
// Because append is used, dest MUST NOT need to grow.
func ExtractExpectedStructure(stream *lexer.TokenStream, dest *[]*lexer.Token, exp *Expectation) error {
for _, e := range exp.entries {
if !stream.HasNext() {
// Only report EOF errors if we cared about capturing the last token
if e.Keep() {
return &ExtractionError{
expectations: []*Expectation{exp},
parseMessages: []string{fmt.Sprintf("expected token %s but got EOF", e.Describe())},
}
} else {
continue
}
}
actual := stream.Pop()
if e.Matches(actual.Kind) {
if e.Keep() {
*dest = append(*dest, actual)
}
} else {
return &ExtractionError{
expectations: []*Expectation{exp},
parseMessages: []string{fmt.Sprintf("unexpected token %s at %d:%d (expected %s)", lexer.DescribeToken(actual), actual.Row, actual.Column, e.Describe())},
}
}
}
return nil
}
type OneOfExpectations struct {
expectations []*Expectation
}
func OneOf(expectations ...*Expectation) *OneOfExpectations {
return &OneOfExpectations{
expectations: expectations,
}
}
// ExtractOneOfExpectedStructure functions similar to ExtractExpectedStructure,
// but instead extracts the first matching expectation from OneOfExpectations.
func ExtractOneOfExpectedStructure(stream *lexer.TokenStream, dest *[]*lexer.Token, exps *OneOfExpectations) error {
var errorMessages []string
origDest := make([]*lexer.Token, len(*dest), cap(*dest))
copy(origDest, *dest)
startStreamPos := stream.Pos()
outer:
for _, exp := range exps.expectations {
*dest = origDest
stream.Jump(startStreamPos)
tokenProgress := 0 // TODO: Use tokenProgress to select most likely error
for _, e := range exp.entries {
if !stream.HasNext() {
// Only report EOF errors if we cared about capturing the last token
if e.Keep() {
errorMessages = append(errorMessages, fmt.Sprintf("expected token %s but got EOF", e.Describe()))
continue outer
} else {
continue
}
}
actual := stream.Pop()
if e.Matches(actual.Kind) {
tokenProgress++
if e.Keep() {
*dest = append(*dest, actual)
}
} else {
errorMessages = append(errorMessages, fmt.Sprintf("unexpected token %s at %d:%d (expected %s)", lexer.DescribeToken(actual), actual.Row, actual.Column, e.Describe()))
continue outer
}
}
// If we get to this point, we've successfully matched one of the expectations.
return nil
}
return &ExtractionError{
expectations: exps.expectations,
parseMessages: errorMessages,
}
}
type Extractable interface {
Extract(stream *lexer.TokenStream, dest *[]*lexer.Token) error
ExtractionCount() int
Description() string
}
func (e *Expectation) Extract(stream *lexer.TokenStream, dest *[]*lexer.Token) error {
return ExtractExpectedStructure(stream, dest, e)
}
func (e *Expectation) Description() string {
return e.description
}
func (o *OneOfExpectations) Extract(stream *lexer.TokenStream, dest *[]*lexer.Token) error {
return ExtractOneOfExpectedStructure(stream, dest, o)
}
func (o *OneOfExpectations) ExtractionCount() int {
max := 0
for _, exp := range o.expectations {
if exp.ExtractionCount() > max {
max = exp.ExtractionCount()
}
}
return max
}
func (o *OneOfExpectations) Description() string {
if len(o.expectations) == 1 {
return o.expectations[0].Description()
}
allDescriptions := make([]string, len(o.expectations))
for i := range o.expectations {
allDescriptions[i] = fmt.Sprintf("%q", o.expectations[i].Description())
}
return fmt.Sprintf("one of [%s]", strings.Join(allDescriptions, ", "))
}
type ExtractionError struct {
expectations []*Expectation
parseMessages []string
}
func (ee *ExtractionError) Error() string {
builder := new(strings.Builder)
multiple := len(ee.expectations) > 1
builder.WriteString("failed to parse statement: ")
if multiple {
builder.WriteString("no match among multiple = [\n")
} else {
builder.WriteRune('\n')
}
for i := range ee.expectations {
builder.WriteRune('\t')
builder.WriteString(ee.expectations[i].Description())
builder.WriteString(" ==> ")
builder.WriteString(ee.parseMessages[i])
builder.WriteRune('\n')
}
if multiple {
builder.WriteRune(']')
}
return builder.String()
}