aboutsummaryrefslogtreecommitdiff
path: root/spec/parser_test.go
diff options
context:
space:
mode:
authorRyo Nihei <nihei.dev@gmail.com>2022-03-28 22:31:30 +0900
committerRyo Nihei <nihei.dev@gmail.com>2022-03-29 01:45:05 +0900
commited43562cf58e8c0f9390421848879308fdfc60cb (patch)
tree16beff7d233b95ae53d2c8019bc47be378f304b8 /spec/parser_test.go
parentSimplify the syntax of #ast directive (diff)
downloadurubu-ed43562cf58e8c0f9390421848879308fdfc60cb.tar.gz
urubu-ed43562cf58e8c0f9390421848879308fdfc60cb.tar.xz
Add label notation
Diffstat (limited to 'spec/parser_test.go')
-rw-r--r--spec/parser_test.go104
1 files changed, 104 insertions, 0 deletions
diff --git a/spec/parser_test.go b/spec/parser_test.go
index 154aea0..e81f95e 100644
--- a/spec/parser_test.go
+++ b/spec/parser_test.go
@@ -91,6 +91,19 @@ func TestParse(t *testing.T) {
Pattern: p,
}
}
+ label := func(name string) *LabelNode {
+ return &LabelNode{
+ Name: name,
+ }
+ }
+ withLabelPos := func(label *LabelNode, pos Position) *LabelNode {
+ label.Pos = pos
+ return label
+ }
+ withLabel := func(elem *ElementNode, label *LabelNode) *ElementNode {
+ elem.Label = label
+ return elem
+ }
withElemPos := func(elem *ElementNode, pos Position) *ElementNode {
elem.Pos = pos
return elem
@@ -536,6 +549,97 @@ fragment number: "[0-9]";
},
},
{
+ caption: "a symbol can have a label",
+ src: `
+expr
+ : term@lhs add term@rhs
+ ;
+`,
+ ast: &RootNode{
+ Productions: []*ProductionNode{
+ withProdPos(
+ prod("expr",
+ withAltPos(
+ alt(
+ withElemPos(
+ withLabel(
+ id("term"),
+ withLabelPos(
+ label("lhs"),
+ newPos(3),
+ ),
+ ),
+ newPos(3),
+ ),
+ withElemPos(
+ id("add"),
+ newPos(3),
+ ),
+ withElemPos(
+ withLabel(
+ id("term"),
+ withLabelPos(
+ label("rhs"),
+ newPos(3),
+ ),
+ ),
+ newPos(3),
+ ),
+ ),
+ newPos(3),
+ ),
+ ),
+ newPos(2),
+ ),
+ },
+ },
+ },
+ {
+ caption: "a label must be an identifier, not a string",
+ src: `
+foo
+ : bar@'baz'
+ ;
+`,
+ synErr: synErrNoLabel,
+ },
+ {
+ caption: "a label must be an identifier, not a pattern",
+ src: `
+foo
+ : bar@"baz"
+ ;
+`,
+ synErr: synErrNoLabel,
+ },
+ {
+ caption: "the symbol marker @ must be followed by an identifier",
+ src: `
+foo
+ : bar@
+ ;
+`,
+ synErr: synErrNoLabel,
+ },
+ {
+ caption: "a symbol cannot have more than or equal to two labels",
+ src: `
+foo
+ : bar@baz@bra
+ ;
+`,
+ synErr: synErrLabelWithNoSymbol,
+ },
+ {
+ caption: "a label must follow a symbol",
+ src: `
+foo
+ : @baz
+ ;
+`,
+ synErr: synErrLabelWithNoSymbol,
+ },
+ {
caption: "a grammar can contain left and right associativities",
src: `
%left l1 l2