-
Notifications
You must be signed in to change notification settings - Fork 0
/
result.go
222 lines (198 loc) · 5.34 KB
/
result.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
// Copyright (C) 2017, Pablo Lalloni <[email protected]>.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
package seared
import (
"strings"
"github.com/lalloni/seared/buffer"
"github.com/lalloni/seared/node"
)
type Result struct {
Expression Expression
Success bool
Input buffer.Buffer
Start int
End int
// Parent is the parent PEG expression Result
Parent *Result
// Results are the children PEG expressions Results
Results []*Result
// Nodes are the parse trees produced
Nodes []*node.Node
}
func (r *Result) Match() string {
return r.Input.String(r.Start, r.End)
}
func (r *Result) Length() int {
return r.End - r.Start
}
func (r *Result) Error() string {
return "Invalid input '" + r.Input.String(r.Start, r.Start+1) + "' at " + r.Input.Location(r.Start).String() + ", expected " + r.Expression.Expectation()
}
func (r *Result) WithNodes(nodes ...*node.Node) *Result {
for _, node := range nodes {
r.Nodes = append(r.Nodes, node)
}
return r
}
func (r *Result) WithResults(results ...*Result) *Result {
for _, result := range results {
r.Results = append(r.Results, result)
}
return r
}
func (r *Result) HasChildren() bool {
return len(r.Results) > 0
}
func (r *Result) ChildlessResults() []*Result {
if !r.HasChildren() {
return []*Result{r}
}
rs := []*Result{}
for _, child := range r.Results {
rs = append(rs, child.ChildlessResults()...)
}
return rs
}
func (r *Result) FailedChildlessResults() []*Result {
cls := r.ChildlessResults()
rs := []*Result{}
for _, cl := range cls {
if !cl.Success {
rs = append(rs, cl)
}
}
return rs
}
func formatResultTree(d int, r *Result) string {
s := strings.Repeat(" ", 2*d) + r.Expression.Name()
if r.Success {
s += ": matched '" + r.Match() + "'"
} else {
s += ": " + r.Error()
}
s += "\n"
for _, child := range r.Results {
s += formatResultTree(d+1, child)
}
return s
}
func (r *Result) FormatResultTree() string {
return formatResultTree(0, r)
}
func (r *Result) FormatNodeTree() string {
ss := []string{}
for _, node := range r.Nodes {
ss = append(ss, node.Format())
}
return strings.Join(ss, "\n")
}
func (r *Result) FirstRuleAncestor() *Result {
if r.Expression == nil {
return nil
}
if _, ok := r.Expression.(*rule); ok {
return r
}
if r.Parent == nil {
return nil
}
return r.Parent.FirstRuleAncestor()
}
func deepestFunc(r *Result, f func(r *Result) bool) (result *Result, depth int) {
result = r
depth = 0
for _, child := range r.Results {
if f(child) {
rr, dd := deepestFunc(child, f)
if depth < dd+1 {
result = rr
depth = dd + 1
}
}
}
return
}
func (r *Result) DeepestFailedResult() (result *Result) {
result, _ = deepestFunc(r, func(r *Result) bool { return r.HasChildren() || !r.Success })
return
}
func (r *Result) FarthestFailedResult() (result *Result) {
p := 0
for _, f := range r.FailedChildlessResults() {
if f.Start > p {
p = f.Start
result = f
}
}
return
}
func (r *Result) BetterError() string {
ffr := r.FarthestFailedResult()
if ffr == nil {
return ""
}
frs := make([]*Result, 0)
for _, fr := range r.FailedChildlessResults() {
if fr.Start == ffr.Start {
frs = append(frs, fr)
}
}
ss := make([]string, 0)
for _, fr := range frs {
ss = append(ss, fr.Expression.Expectation())
}
return "Invalid input '" + r.Input.String(ffr.Start, ffr.Start+1) + "' at " + r.Input.Location(ffr.Start).String() + ", expected " + strings.Join(ss, " or ")
}
func (r *Result) Depth() int {
if r.Parent == nil {
return 0
}
return 1 + r.Parent.Depth()
}
func Success(expression Expression, input buffer.Buffer, start, end int) *Result {
return &Result{
Expression: expression,
Success: true,
Input: input,
Start: start,
End: end,
}
}
func Failure(expression Expression, input buffer.Buffer, start, end int) *Result {
return &Result{
Expression: expression,
Success: false,
Input: input,
Start: start,
End: end,
}
}
func ResultsNodes(results []*Result) []*node.Node {
nodes := []*node.Node{}
for _, result := range results {
nodes = append(nodes, result.Nodes...)
}
return nodes
}