From 7271e46bbcb11acf860c91eddfe12dd7eed5ccad Mon Sep 17 00:00:00 2001 From: Ryo Nihei Date: Thu, 26 Aug 2021 23:16:09 +0900 Subject: Add error symbol and #recover directive to recover from an error state --- driver/parser_test.go | 92 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) (limited to 'driver/parser_test.go') diff --git a/driver/parser_test.go b/driver/parser_test.go index 9467d6c..224f12e 100644 --- a/driver/parser_test.go +++ b/driver/parser_test.go @@ -444,6 +444,94 @@ s ; a: 'a'; +`, + specErr: true, + }, + // The grammar can contain the 'error' symbol. + { + specSrc: ` +s + : id id id ';' + | error ';' + ; + +ws: "[\u{0009}\u{0020}]+" #skip; +id: "[A-Za-z_]+"; +`, + src: `foo bar baz ;`, + }, + // The grammar can contain the 'recover' directive. + { + specSrc: ` +seq + : seq elem + | elem + ; +elem + : id id id ';' + | error ';' #recover + ; + +ws: "[\u{0009}\u{0020}]+" #skip; +id: "[A-Za-z_]+"; +`, + src: `a b c ; d e f ;`, + }, + // The 'recover' directive cannot take a parameter. + { + specSrc: ` +seq + : seq elem + | elem + ; +elem + : id id id ';' + | error ';' #recover foo + ; + +ws: "[\u{0009}\u{0020}]+" #skip; +id: "[A-Za-z_]+"; +`, + src: `a b c ; d e f ;`, + specErr: true, + }, + // You cannot use the error symbol as a non-terminal symbol. + { + specSrc: ` +s + : foo + ; +error + : bar + ; + +foo: 'foo'; +bar: 'bar'; +`, + specErr: true, + }, + // You cannot use the error symbol as a terminal symbol. + { + specSrc: ` +s + : foo + | error + ; + +foo: 'foo'; +error: 'error'; +`, + specErr: true, + }, + // You cannot use the error symbol as a terminal symbol, even if given the skip directive. + { + specSrc: ` +s + : foo + ; + +foo: 'foo'; +error: 'error' #skip; `, specErr: true, }, @@ -492,6 +580,10 @@ a: 'a'; t.Fatal(err) } + if len(p.SyntaxErrors()) > 0 { + t.Fatalf("unexpected syntax errors occurred: %+v", p.SyntaxErrors()) + } + if tt.cst != nil { testTree(t, p.CST(), tt.cst) } -- cgit v1.2.3