-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrammar_test.go
50 lines (43 loc) · 1.26 KB
/
grammar_test.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
package parsimonious
import (
"testing"
"github.com/stretchr/testify/assert"
)
func Test_Grammar_ParseErrors(t *testing.T) {
t.Run("leftovers", func(t *testing.T) {
grammar, err := NewGrammar(`seq = "a" (" " "b")+`)
assert.NoError(t, err)
tree, err := grammar.Parse("a bb")
assert.Nil(t, tree)
assert.Error(t, err)
assert.IsType(t, &ErrIncompleteParseFailed{}, err)
assert.Equal(
t, err.Error(),
`rule "seq" matched in its entirely, but it didn't consume all the text. `+
`The non-matching portion of the text begins with "" (line 1, column 4)`,
)
})
t.Run("left recursion", func(t *testing.T) {
grammar, err := NewGrammar(`
expression = operator_expression / non_operator_expression
non_operator_expression = number_expression
operator_expression = expression "+" non_operator_expression
number_expression = ~"[0-9]+"
`,
)
assert.NoError(t, err)
tree, err := grammar.ParseWithRule(
"operator_expression",
"1+2",
ParseWithDebug(true),
)
assert.Nil(t, tree)
assert.Error(t, err)
assert.IsType(t, &ErrLeftRecursion{}, err)
assert.Equal(
t, err.Error(),
`left recursion in rule "operator_expression" at "1+" (line 1, column 1). `+
`Please rewrite your grammar into a rule that does not use left recursion.`,
)
})
}