From 92751889a1de0f84296dad47dfaeddb1285782bc Mon Sep 17 00:00:00 2001 From: HeyJavaBean Date: Fri, 17 Nov 2023 16:30:28 +0800 Subject: [PATCH] optimize: optimize parser to support empty annotation --- parser/parser_test.go | 6 +++++- parser/thrift.peg | 2 +- parser/thrift.peg.go | 5 +---- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/parser/parser_test.go b/parser/parser_test.go index 17393eba..cfa95f08 100644 --- a/parser/parser_test.go +++ b/parser/parser_test.go @@ -38,6 +38,8 @@ struct s { 2: string f2 ( a = "a", b = "" ); 3: string (str = "str") f3; 4: string f4; + 5: string f5 (); + 6: string f6 ( ); } ( xxx = "", yyy = "y", @@ -104,7 +106,7 @@ func TestAnnotation(t *testing.T) { test.Assert(t, has(ast.Structs[0].Annotations, "xxx", "")) test.Assert(t, has(ast.Structs[0].Annotations, "yyy", "y")) test.Assert(t, has(ast.Structs[0].Annotations, "zzz", "zzz")) - test.Assert(t, len(ast.Structs[0].Fields) == 4) + test.Assert(t, len(ast.Structs[0].Fields) == 6) test.Assert(t, len(ast.Structs[0].Fields[0].Annotations) == 1) test.Assert(t, len(ast.Structs[0].Fields[1].Annotations) == 2) test.Assert(t, len(ast.Structs[0].Fields[2].Annotations) == 0) @@ -113,6 +115,8 @@ func TestAnnotation(t *testing.T) { test.Assert(t, has(ast.Structs[0].Fields[1].Annotations, "a", "a")) test.Assert(t, has(ast.Structs[0].Fields[1].Annotations, "b", "")) test.Assert(t, has(ast.Structs[0].Fields[2].Type.Annotations, "str", "str")) + test.Assert(t, ast.Structs[0].Fields[4].Annotations == nil) + test.Assert(t, ast.Structs[0].Fields[5].Annotations == nil) test.Assert(t, len(ast.Exceptions) == 1) test.Assert(t, len(ast.Exceptions[0].Annotations) == 1) diff --git a/parser/thrift.peg b/parser/thrift.peg index 45963d9f..d0c2b487 100644 --- a/parser/thrift.peg +++ b/parser/thrift.peg @@ -72,7 +72,7 @@ DoubleConstant <- Skip <[+\-]? ( Exponent <- ('e' / 'E') IntConstant -Annotations <- LPAR Annotation+ RPAR +Annotations <- LPAR Annotation* RPAR Annotation <- Identifier EQUAL Literal ListSeparator? diff --git a/parser/thrift.peg.go b/parser/thrift.peg.go index dd18089b..97060272 100644 --- a/parser/thrift.peg.go +++ b/parser/thrift.peg.go @@ -2008,7 +2008,7 @@ func (p *ThriftIDL) Init(options ...func(*ThriftIDL) error) error { position, tokenIndex = position203, tokenIndex203 return false }, - /* 31 Annotations <- <(LPAR Annotation+ RPAR)> */ + /* 31 Annotations <- <(LPAR Annotation* RPAR)> */ func() bool { position207, tokenIndex207 := position, tokenIndex { @@ -2016,9 +2016,6 @@ func (p *ThriftIDL) Init(options ...func(*ThriftIDL) error) error { if !_rules[ruleLPAR]() { goto l207 } - if !_rules[ruleAnnotation]() { - goto l207 - } l209: { position210, tokenIndex210 := position, tokenIndex