aboutsummaryrefslogtreecommitdiff
path: root/spec/test/parser.go
blob: c30d45cf317b5d90d0df3c32bc1e08cc28426439 (plain) (blame)
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
//go:generate vartan compile tree.vartan -o tree.json
//go:generate vartan-go tree.json --package test

package test

import (
	"bufio"
	"bytes"
	"errors"
	"fmt"
	"io"
	"regexp"
	"strconv"
	"strings"
	"unicode/utf8"
)

type TreeDiff struct {
	ExpectedPath string
	ActualPath   string
	Message      string
}

func newTreeDiff(expected, actual *Tree, message string) *TreeDiff {
	return &TreeDiff{
		ExpectedPath: expected.path(),
		ActualPath:   actual.path(),
		Message:      message,
	}
}

type Tree struct {
	Parent   *Tree
	Offset   int
	Kind     string
	Children []*Tree
	Lexeme   string
}

func NewNonTerminalTree(kind string, children ...*Tree) *Tree {
	return &Tree{
		Kind:     kind,
		Children: children,
	}
}

func NewTerminalNode(kind string, lexeme string) *Tree {
	return &Tree{
		Kind:   kind,
		Lexeme: lexeme,
	}
}

func (t *Tree) Fill() *Tree {
	for i, c := range t.Children {
		c.Parent = t
		c.Offset = i
		c.Fill()
	}
	return t
}

func (t *Tree) path() string {
	if t.Parent == nil {
		return t.Kind
	}
	return fmt.Sprintf("%v.[%v]%v", t.Parent.path(), t.Offset, t.Kind)
}

func (t *Tree) Format() []byte {
	var b bytes.Buffer
	t.format(&b, 0)
	return b.Bytes()
}

func (t *Tree) format(buf *bytes.Buffer, depth int) {
	for i := 0; i < depth; i++ {
		buf.WriteString("    ")
	}
	buf.WriteString("(")
	if t.Kind == "" {
		buf.WriteString("<anonymous>")
	} else {
		buf.WriteString(t.Kind)
	}
	if len(t.Children) > 0 {
		buf.WriteString("\n")
		for i, c := range t.Children {
			c.format(buf, depth+1)
			if i < len(t.Children)-1 {
				buf.WriteString("\n")
			}
		}
	}
	buf.WriteString(")")
}

func DiffTree(expected, actual *Tree) []*TreeDiff {
	if expected == nil && actual == nil {
		return nil
	}
	if actual.Kind != expected.Kind {
		msg := fmt.Sprintf("unexpected kind: expected '%v' but got '%v'", expected.Kind, actual.Kind)
		return []*TreeDiff{
			newTreeDiff(expected, actual, msg),
		}
	}
	if expected.Lexeme != actual.Lexeme {
		msg := fmt.Sprintf("unexpected lexeme: expected '%v' but got '%v'", expected.Lexeme, actual.Lexeme)
		return []*TreeDiff{
			newTreeDiff(expected, actual, msg),
		}
	}
	if len(actual.Children) != len(expected.Children) {
		msg := fmt.Sprintf("unexpected node count: expected %v but got %v", len(expected.Children), len(actual.Children))
		return []*TreeDiff{
			newTreeDiff(expected, actual, msg),
		}
	}
	var diffs []*TreeDiff
	for i, exp := range expected.Children {
		if ds := DiffTree(exp, actual.Children[i]); len(ds) > 0 {
			diffs = append(diffs, ds...)
		}
	}
	return diffs
}

type TestCase struct {
	Description string
	Source      []byte
	Output      *Tree
}

func ParseTestCase(r io.Reader) (*TestCase, error) {
	parts, err := splitIntoParts(r)
	if err != nil {
		return nil, err
	}
	if len(parts) != 3 {
		return nil, fmt.Errorf("too many or too few part delimiters: a test case consists of just tree parts: %v parts found", len(parts))
	}

	tp := &treeParser{
		lineOffset: parts[0].lineCount + parts[1].lineCount + 2,
	}
	tree, err := tp.parseTree(bytes.NewReader(parts[2].buf))
	if err != nil {
		return nil, err
	}

	return &TestCase{
		Description: string(parts[0].buf),
		Source:      parts[1].buf,
		Output:      tree,
	}, nil
}

type testCasePart struct {
	buf       []byte
	lineCount int
}

func splitIntoParts(r io.Reader) ([]*testCasePart, error) {
	var bufs []*testCasePart
	s := bufio.NewScanner(r)
	for {
		buf, lineCount, err := readPart(s)
		if err != nil {
			return nil, err
		}
		if buf == nil {
			break
		}
		bufs = append(bufs, &testCasePart{
			buf:       buf,
			lineCount: lineCount,
		})
	}
	if err := s.Err(); err != nil {
		return nil, err
	}
	return bufs, nil
}

var reDelim = regexp.MustCompile(`^\s*---+\s*$`)

func readPart(s *bufio.Scanner) ([]byte, int, error) {
	if !s.Scan() {
		return nil, 0, s.Err()
	}
	buf := &bytes.Buffer{}
	line := s.Bytes()
	if reDelim.Match(line) {
		// Return an empty slice because (*bytes.Buffer).Bytes() returns nil if we have never written data.
		return []byte{}, 0, nil
	}
	_, err := buf.Write(line)
	if err != nil {
		return nil, 0, err
	}
	lineCount := 1
	for s.Scan() {
		line := s.Bytes()
		if reDelim.Match(line) {
			return buf.Bytes(), lineCount, nil
		}
		_, err := buf.Write([]byte("\n"))
		if err != nil {
			return nil, 0, err
		}
		_, err = buf.Write(line)
		if err != nil {
			return nil, 0, err
		}
		lineCount++
	}
	if err := s.Err(); err != nil {
		return nil, 0, err
	}
	return buf.Bytes(), lineCount, nil
}

type treeParser struct {
	lineOffset int
}

func (tp *treeParser) parseTree(src io.Reader) (*Tree, error) {
	toks, err := NewTokenStream(src)
	if err != nil {
		return nil, err
	}
	gram := NewGrammar()
	tb := NewDefaultSyntaxTreeBuilder()
	p, err := NewParser(toks, gram, SemanticAction(NewASTActionSet(gram, tb)))
	if err != nil {
		return nil, err
	}
	err = p.Parse()
	if err != nil {
		return nil, err
	}
	synErrs := p.SyntaxErrors()
	if len(synErrs) > 0 {
		var b strings.Builder
		b.Write(formatSyntaxError(synErrs[0], gram, tp.lineOffset))
		for _, synErr := range synErrs[1:] {
			b.WriteRune('\n')
			b.Write(formatSyntaxError(synErr, gram, tp.lineOffset))
		}
		return nil, errors.New(b.String())
	}
	t, err := tp.genTree(tb.Tree())
	if err != nil {
		return nil, err
	}
	return t.Fill(), nil
}

func formatSyntaxError(synErr *SyntaxError, gram Grammar, lineOffset int) []byte {
	var b bytes.Buffer

	b.WriteString(fmt.Sprintf("%v:%v: %v: ", lineOffset+synErr.Row+1, synErr.Col+1, synErr.Message))

	tok := synErr.Token
	switch {
	case tok.EOF():
		b.WriteString("<eof>")
	case tok.Invalid():
		b.WriteString(fmt.Sprintf("'%v' (<invalid>)", string(tok.Lexeme())))
	default:
		if term := gram.Terminal(tok.TerminalID()); term != "" {
			b.WriteString(fmt.Sprintf("'%v' (%v)", string(tok.Lexeme()), term))
		} else {
			b.WriteString(fmt.Sprintf("'%v'", string(tok.Lexeme())))
		}
	}
	b.WriteString(fmt.Sprintf(": expected: %v", synErr.ExpectedTerminals[0]))
	for _, t := range synErr.ExpectedTerminals[1:] {
		b.WriteString(fmt.Sprintf(", %v", t))
	}

	return b.Bytes()
}

func (tp *treeParser) genTree(node *Node) (*Tree, error) {
	// A node labeled 'error' cannot have children. It always must be (error).
	if sym := node.Children[0]; sym.Text == "error" {
		if len(node.Children) > 1 {
			return nil, fmt.Errorf("%v:%v: error node cannot take children", tp.lineOffset+sym.Row+1, sym.Col+1)
		}
		return NewTerminalNode(sym.Text, ""), nil
	}

	if len(node.Children) == 2 && node.Children[1].KindName == "string" {
		var text string
		str := node.Children[1].Children[0]
		switch str.KindName {
		case "raw_string":
			text = str.Children[0].Text
		case "interpreted_string":
			var b strings.Builder
			for _, c := range str.Children {
				switch c.KindName {
				case "escaped_seq":
					b.WriteString(strings.TrimPrefix(`\`, c.Text))
				case "escape_char":
					return nil, fmt.Errorf("%v:%v: incomplete escape sequence", tp.lineOffset+c.Row+1, c.Col+1)
				case "codepoint_expr":
					cp := c.Children[0]
					n, err := strconv.ParseInt(cp.Text, 16, 64)
					if err != nil {
						return nil, fmt.Errorf("%v:%v: %v", tp.lineOffset+cp.Row+1, cp.Col+1, err)
					}
					if !utf8.ValidRune(rune(n)) {
						return nil, fmt.Errorf("%v:%v: invalid code point: %v", tp.lineOffset+cp.Row+1, cp.Col+1, cp.Text)
					}
					b.WriteRune(rune(n))
				default:
					b.WriteString(c.Text)
				}
			}
			text = b.String()
		}
		return NewTerminalNode(node.Children[0].Text, text), nil
	}

	var children []*Tree
	if len(node.Children) > 1 {
		children = make([]*Tree, len(node.Children)-1)
		for i, c := range node.Children[1:] {
			var err error
			children[i], err = tp.genTree(c)
			if err != nil {
				return nil, err
			}
		}
	}
	return NewNonTerminalTree(node.Children[0].Text, children...), nil
}