[#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 <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2022-06-28 13:42:35 +03:00 committed by fyrchik
parent dea3d9c419
commit ab4d1e34a8
2 changed files with 13 additions and 3 deletions

View file

@ -551,16 +551,17 @@ func writeFilterStringTo(w io.StringWriter, f netmap.Filter) error {
// DecodeString decodes PlacementPolicy from the string composed using // DecodeString decodes PlacementPolicy from the string composed using
// WriteStringTo. Returns error if s is malformed. // WriteStringTo. Returns error if s is malformed.
func (p *PlacementPolicy) DecodeString(s string) error { func (p *PlacementPolicy) DecodeString(s string) error {
var v policyVisitor
input := antlr.NewInputStream(s) input := antlr.NewInputStream(s)
lexer := parser.NewQueryLexer(input) lexer := parser.NewQueryLexer(input)
lexer.RemoveErrorListeners() lexer.RemoveErrorListeners()
lexer.AddErrorListener(&v)
stream := antlr.NewCommonTokenStream(lexer, 0) stream := antlr.NewCommonTokenStream(lexer, 0)
pp := parser.NewQuery(stream) pp := parser.NewQuery(stream)
pp.BuildParseTrees = true pp.BuildParseTrees = true
var v policyVisitor
pp.RemoveErrorListeners() pp.RemoveErrorListeners()
pp.AddErrorListener(&v) pp.AddErrorListener(&v)
pl := pp.Policy().Accept(&v) pl := pp.Policy().Accept(&v)

View file

@ -25,9 +25,10 @@ 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`, FILTER City EQ SPB AND SSD EQ true OR City EQ SPB AND Rating GE 5 AS SPBSSD`,
} }
for _, testCase := range testCases {
var p PlacementPolicy var p PlacementPolicy
for _, testCase := range testCases {
require.NoError(t, p.DecodeString(testCase)) require.NoError(t, p.DecodeString(testCase))
var b strings.Builder var b strings.Builder
@ -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()) 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) { func TestPlacementPolicyEncoding(t *testing.T) {