From ab4d1e34a8ac8f56c0768984a43399614e360d4a Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Tue, 28 Jun 2022 13:42:35 +0300 Subject: [PATCH] [#280] policy: Use `policyVisitor` as `ErrorListener` for `QueryLexer` Share `policyVisitor` instance as an `ErrorListener` provider for both `QueryLexer` and `Query`. Signed-off-by: Leonard Lyubich --- netmap/policy.go | 5 +++-- netmap/policy_test.go | 11 ++++++++++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/netmap/policy.go b/netmap/policy.go index 28b53900..2884c3fa 100644 --- a/netmap/policy.go +++ b/netmap/policy.go @@ -551,16 +551,17 @@ func writeFilterStringTo(w io.StringWriter, f netmap.Filter) error { // DecodeString decodes PlacementPolicy from the string composed using // WriteStringTo. Returns error if s is malformed. func (p *PlacementPolicy) DecodeString(s string) error { + var v policyVisitor + input := antlr.NewInputStream(s) lexer := parser.NewQueryLexer(input) lexer.RemoveErrorListeners() + lexer.AddErrorListener(&v) stream := antlr.NewCommonTokenStream(lexer, 0) pp := parser.NewQuery(stream) pp.BuildParseTrees = true - var v policyVisitor - pp.RemoveErrorListeners() pp.AddErrorListener(&v) pl := pp.Policy().Accept(&v) diff --git a/netmap/policy_test.go b/netmap/policy_test.go index 0c2782c5..2d04313f 100644 --- a/netmap/policy_test.go +++ b/netmap/policy_test.go @@ -25,8 +25,9 @@ SELECT 1 IN City FROM SPBSSD AS SPB FILTER City EQ SPB AND SSD EQ true OR City EQ SPB AND Rating GE 5 AS SPBSSD`, } + var p PlacementPolicy + for _, testCase := range testCases { - var p PlacementPolicy require.NoError(t, p.DecodeString(testCase)) @@ -35,6 +36,14 @@ FILTER City EQ SPB AND SSD EQ true OR City EQ SPB AND Rating GE 5 AS SPBSSD`, require.Equal(t, testCase, b.String()) } + + invalidTestCases := []string{ + `?REP 1`, + } + + for i := range invalidTestCases { + require.Error(t, p.DecodeString(invalidTestCases[i]), "#%d", i) + } } func TestPlacementPolicyEncoding(t *testing.T) {