aboutsummaryrefslogtreecommitdiff
path: root/driver
diff options
context:
space:
mode:
Diffstat (limited to 'driver')
-rw-r--r--driver/parser_test.go68
-rw-r--r--driver/semantic_action.go6
-rw-r--r--driver/syntax_error_test.go47
3 files changed, 68 insertions, 53 deletions
diff --git a/driver/parser_test.go b/driver/parser_test.go
index dd58dd5..da4f714 100644
--- a/driver/parser_test.go
+++ b/driver/parser_test.go
@@ -186,7 +186,9 @@ bar
: bar_text
|
;
-bar_text: "bar";
+
+bar_text
+ : "bar";
`,
src: `bar`,
cst: nonTermNode("s",
@@ -399,8 +401,10 @@ white_space #skip
s
: tagline
;
-tagline: "\f{words} IS OUT THERE.";
-fragment words: "[A-Za-z\u{0020}]+";
+tagline
+ : "\f{words} IS OUT THERE.";
+fragment words
+ : "[A-Za-z\u{0020}]+";
`,
src: `THE TRUTH IS OUT THERE.`,
},
@@ -410,15 +414,21 @@ fragment words: "[A-Za-z\u{0020}]+";
#name test;
list
- : '[' elems ']' #ast elems...
+ : l_bracket elems r_bracket #ast elems...
;
elems
- : elems ',' id #ast elems... id
+ : elems comma id #ast elems... id
| id
;
whitespace #skip
: "\u{0020}+";
+l_bracket
+ : '[';
+r_bracket
+ : ']';
+comma
+ : ',';
id
: "[A-Za-z]+";
`,
@@ -453,10 +463,12 @@ s
: a #ast a...
;
a
- : a ',' foo #ast a... foo
+ : a comma foo #ast a... foo
| foo
;
+comma
+ : ',';
foo
: 'foo';
`,
@@ -473,12 +485,14 @@ foo
#name test;
s
- : a ';' #ast a...
+ : a semi_colon #ast a...
;
-
a
:
;
+
+semi_colon
+ : ';';
`,
src: `;`,
ast: nonTermNode("s"),
@@ -497,9 +511,13 @@ expr
| expr@lhs sub expr@rhs #ast sub lhs rhs
| num
;
-add: '+';
-sub: '-';
-num: "0|[1-9][0-9]*";
+
+add
+ : '+';
+sub
+ : '-';
+num
+ : "0|[1-9][0-9]*";
`,
src: `1+2-3`,
ast: nonTermNode("expr",
@@ -524,9 +542,11 @@ num: "0|[1-9][0-9]*";
#name test;
s
- : foo@x ';' #ast foo
+ : foo@x semi_colon #ast foo
;
+semi_colon
+ : ';';
foo
: 'foo';
`,
@@ -648,12 +668,14 @@ div
#name test;
s
- : id id id ';'
- | error ';'
+ : id id id semi_colon
+ | error semi_colon
;
ws #skip
: "[\u{0009}\u{0020}]+";
+semi_colon
+ : ';';
id
: "[A-Za-z_]+";
`,
@@ -665,10 +687,12 @@ id
#name test;
s
- : foo ';'
- | error ';' #ast error
+ : foo semi_colon
+ | error semi_colon #ast error
;
+semi_colon
+ : ';';
foo
: 'foo';
`,
@@ -684,10 +708,12 @@ foo
#name test;
s
- : foo ';'
- | error@e ';' #ast e
+ : foo semi_colon
+ | error@e semi_colon #ast e
;
+semi_colon
+ : ';';
foo
: 'foo';
`,
@@ -707,12 +733,14 @@ seq
| elem
;
elem
- : id id id ';'
- | error ';' #recover
+ : id id id semi_colon
+ | error semi_colon #recover
;
ws #skip
: "[\u{0009}\u{0020}]+";
+semi_colon
+ : ';';
id
: "[A-Za-z_]+";
`,
diff --git a/driver/semantic_action.go b/driver/semantic_action.go
index 54d3291..7e5a773 100644
--- a/driver/semantic_action.go
+++ b/driver/semantic_action.go
@@ -340,11 +340,7 @@ func printTree(w io.Writer, node *Node, ruledLine string, childRuledLinePrefix s
case NodeTypeError:
fmt.Fprintf(w, "%v%v\n", ruledLine, node.KindName)
case NodeTypeTerminal:
- if node.KindName == "" {
- fmt.Fprintf(w, "%v<anonymous> %v\n", ruledLine, strconv.Quote(node.Text))
- } else {
- fmt.Fprintf(w, "%v%v %v\n", ruledLine, node.KindName, strconv.Quote(node.Text))
- }
+ fmt.Fprintf(w, "%v%v %v\n", ruledLine, node.KindName, strconv.Quote(node.Text))
case NodeTypeNonTerminal:
fmt.Fprintf(w, "%v%v\n", ruledLine, node.KindName)
diff --git a/driver/syntax_error_test.go b/driver/syntax_error_test.go
index ada1fb0..683e355 100644
--- a/driver/syntax_error_test.go
+++ b/driver/syntax_error_test.go
@@ -26,7 +26,8 @@ s
: foo
;
-foo: 'foo';
+foo
+ : 'foo';
`,
src: `bar`,
synErrCount: 1,
@@ -37,9 +38,9 @@ foo: 'foo';
#name test;
seq
- : seq elem ';'
- | elem ';'
- | error ';' #recover
+ : seq elem semi_colon
+ | elem semi_colon
+ | error semi_colon #recover
;
elem
: a b c
@@ -47,6 +48,8 @@ elem
ws #skip
: "[\u{0009}\u{0020}]+";
+semi_colon
+ : ';';
a
: 'a';
b
@@ -63,9 +66,9 @@ c
#name test;
seq
- : seq elem ';'
- | elem ';'
- | error ';' #recover
+ : seq elem semi_colon
+ | elem semi_colon
+ | error semi_colon #recover
;
elem
: a b c
@@ -73,6 +76,8 @@ elem
ws #skip
: "[\u{0009}\u{0020}]+";
+semi_colon
+ : ';';
a
: 'a';
b
@@ -91,9 +96,9 @@ c
#name test;
seq
- : seq elem ';'
- | elem ';'
- | error '*' '*' ';'
+ : seq elem semi_colon
+ | elem semi_colon
+ | error star star semi_colon
;
elem
: a b c
@@ -101,6 +106,10 @@ elem
ws #skip
: "[\u{0009}\u{0020}]+";
+semi_colon
+ : ';';
+star
+ : '*';
a
: 'a';
b
@@ -241,24 +250,6 @@ foo
"<eof>",
},
},
- {
- caption: "when an anonymous symbol is expected, an expected symbol list contains an auto-generated name with the prefix `x_`",
- specSrc: `
-#name test;
-
-s
- : foo 'bar'
- ;
-
-foo
- : 'foo';
-`,
- src: `foobaz`,
- cause: `baz`,
- expected: []string{
- "x_1",
- },
- },
}
for i, tt := range tests {
t.Run(fmt.Sprintf("#%v", i), func(t *testing.T) {