-
-
Notifications
You must be signed in to change notification settings - Fork 190
/
grammar_test.go
63 lines (55 loc) · 1.75 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
51
52
53
54
55
56
57
58
59
60
61
62
63
package participle_test
import (
"testing"
require "github.com/alecthomas/assert/v2"
"github.com/alecthomas/participle/v2"
)
func TestBuild_Errors_Negation(t *testing.T) {
type grammar struct {
Whatever string `'a' | ! | 'b'`
}
_, err := participle.Build[grammar]()
require.EqualError(t, err, "Whatever: unexpected token |")
}
func TestBuild_Errors_Capture(t *testing.T) {
type grammar struct {
Whatever string `'a' | @ | 'b'`
}
_, err := participle.Build[grammar]()
require.EqualError(t, err, "Whatever: unexpected token |")
}
func TestBuild_Errors_UnclosedGroup(t *testing.T) {
type grammar struct {
Whatever string `'a' | ('b' | 'c'`
}
_, err := participle.Build[grammar]()
require.EqualError(t, err, `Whatever: expected ) but got "<EOF>"`)
}
func TestBuild_Errors_LookaheadGroup(t *testing.T) {
type grammar struct {
Whatever string `'a' | (?? 'what') | 'b'`
}
_, err := participle.Build[grammar]()
require.EqualError(t, err, `Whatever: expected = or ! but got "?"`)
}
func TestBuild_Colon_OK(t *testing.T) {
type grammar struct {
TokenTypeTest bool ` 'TokenTypeTest' : Ident`
DoubleCapture string `| 'DoubleCapture' ":" @Ident`
SinglePresent bool `| 'SinglePresent' ':' Ident`
SingleCapture string `| 'SingleCapture' ':' @Ident`
}
parser, err := participle.Build[grammar]()
require.NoError(t, err)
require.Equal(t, `Grammar = "TokenTypeTest"`+
` | ("DoubleCapture" ":" <ident>)`+
` | ("SinglePresent" ":" <ident>)`+
` | ("SingleCapture" ":" <ident>) .`, parser.String())
}
func TestBuild_Colon_MissingTokenType(t *testing.T) {
type grammar struct {
Key string `'name' : @Ident`
}
_, err := participle.Build[grammar]()
require.EqualError(t, err, `Key: expected identifier for literal type constraint but got "@"`)
}