From 3aeeafe79eceb85c47086c75c62cc99029f4c323 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Fri, 21 May 2021 18:12:32 +0300 Subject: [PATCH 1/5] [#3] policy: use ANTLRv4 parser generator Signed-off-by: Evgenii Stratonikov --- .gitattributes | 4 + go.mod | 2 +- go.sum | 5 +- pkg/policy/doc.go | 2 +- pkg/policy/grammar.ebnf | 55 ---- pkg/policy/grammar.go | 60 ---- pkg/policy/parser/Query.g4 | 45 +++ pkg/policy/parser/Query.interp | Bin 0 -> 4174 bytes pkg/policy/parser/Query.tokens | Bin 0 -> 331 bytes pkg/policy/parser/QueryLexer.g4 | 41 +++ pkg/policy/parser/QueryLexer.interp | Bin 0 -> 7060 bytes pkg/policy/parser/QueryLexer.tokens | Bin 0 -> 331 bytes pkg/policy/parser/generate.go | 3 + pkg/policy/parser/query_base_listener.go | Bin 0 -> 4408 bytes pkg/policy/parser/query_base_visitor.go | Bin 0 -> 1712 bytes pkg/policy/parser/query_lexer.go | Bin 0 -> 9729 bytes pkg/policy/parser/query_listener.go | Bin 0 -> 3058 bytes pkg/policy/parser/query_parser.go | Bin 0 -> 59362 bytes pkg/policy/parser/query_visitor.go | Bin 0 -> 1645 bytes pkg/policy/query.go | 363 +++++++++++++++-------- pkg/policy/query_test.go | 35 ++- 21 files changed, 374 insertions(+), 241 deletions(-) create mode 100644 .gitattributes delete mode 100644 pkg/policy/grammar.ebnf delete mode 100644 pkg/policy/grammar.go create mode 100644 pkg/policy/parser/Query.g4 create mode 100644 pkg/policy/parser/Query.interp create mode 100644 pkg/policy/parser/Query.tokens create mode 100644 pkg/policy/parser/QueryLexer.g4 create mode 100644 pkg/policy/parser/QueryLexer.interp create mode 100644 pkg/policy/parser/QueryLexer.tokens create mode 100644 pkg/policy/parser/generate.go create mode 100644 pkg/policy/parser/query_base_listener.go create mode 100644 pkg/policy/parser/query_base_visitor.go create mode 100644 pkg/policy/parser/query_lexer.go create mode 100644 pkg/policy/parser/query_listener.go create mode 100644 pkg/policy/parser/query_parser.go create mode 100644 pkg/policy/parser/query_visitor.go diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..b371347 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,4 @@ +/pkg/policy/parser/*.go -diff +/pkg/policy/parser/generate.go diff +**/*.interp -diff +**/*.tokens -diff diff --git a/go.mod b/go.mod index f7f1829..25c51c6 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/nspcc-dev/neofs-sdk-go go 1.16 require ( - github.com/alecthomas/participle v0.7.1 + github.com/antlr/antlr4/runtime/Go/antlr v0.0.0-20210521073959-f0d4d129b7f1 github.com/nspcc-dev/neofs-api-go v1.27.0 github.com/stretchr/testify v1.6.1 go.uber.org/zap v1.10.0 diff --git a/go.sum b/go.sum index 799b7f7..ed17a36 100644 --- a/go.sum +++ b/go.sum @@ -10,15 +10,14 @@ github.com/Workiva/go-datastructures v1.0.50/go.mod h1:Z+F2Rca0qCsVYDS8z7bAGm8f3 github.com/abiosoft/ishell v2.0.0+incompatible/go.mod h1:HQR9AqF2R3P4XXpMpI0NAzgHf/aS6+zVXRj14cVk9qg= github.com/abiosoft/readline v0.0.0-20180607040430-155bce2042db/go.mod h1:rB3B4rKii8V21ydCbIzH5hZiCQE7f5E9SzUb/ZZx530= github.com/aead/siphash v1.0.1/go.mod h1:Nywa3cDsYNNK3gaciGTWPwHt0wlpNV15vwmswBAUSII= -github.com/alecthomas/participle v0.7.1 h1:2bN7reTw//5f0cugJcTOnY/NYZcWQOaajW+BwZB5xWs= -github.com/alecthomas/participle v0.7.1/go.mod h1:HfdmEuwvr12HXQN44HPWXR0lHmVolVYe4dyL6lQ3duY= -github.com/alecthomas/repr v0.0.0-20181024024818-d37bc2a10ba1/go.mod h1:xTS7Pm1pD1mvyM075QCDSRqH6qRLXylzS24ZTpRiSzQ= github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= github.com/alicebob/gopher-json v0.0.0-20180125190556-5a6b3ba71ee6/go.mod h1:SGnFV6hVsYE877CKEZ6tDNTjaSXYUk6QqoIK6PrAtcc= github.com/alicebob/miniredis v2.5.0+incompatible/go.mod h1:8HZjEj4yU0dwhYHky+DxYx+6BMjkBbe5ONFIF1MXffk= +github.com/antlr/antlr4/runtime/Go/antlr v0.0.0-20210521073959-f0d4d129b7f1 h1:zFRi26YWd7NIorBXe8UkevRl0dIvk/AnXHWaAiZG+Yk= +github.com/antlr/antlr4/runtime/Go/antlr v0.0.0-20210521073959-f0d4d129b7f1/go.mod h1:F7bn7fEU90QkQ3tnmaTx3LTKLEDqnwWODIYppRQ5hnY= github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8= github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q= github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8= diff --git a/pkg/policy/doc.go b/pkg/policy/doc.go index e2f94bb..10394fa 100644 --- a/pkg/policy/doc.go +++ b/pkg/policy/doc.go @@ -1,5 +1,5 @@ // Package policy provides facilities for creating policy from SQL-like language. -// eBNF grammar is provided in `grammar.ebnf` for illustration. +// ANTLRv4 grammar is provided in `parser/Query.g4` and `parser/QueryLexer.g4`. // // Current limitations: // 1. Grouping filter expressions in parenthesis is not supported right now. diff --git a/pkg/policy/grammar.ebnf b/pkg/policy/grammar.ebnf deleted file mode 100644 index 2bcd3a2..0000000 --- a/pkg/policy/grammar.ebnf +++ /dev/null @@ -1,55 +0,0 @@ -Policy ::= - RepStmt, [RepStmt], - CbtStmt?, - [SelectStmt], - [FilterStmt], -; - -RepStmt ::= - 'REP', Number1, (* number of object replicas *) - ('AS', Ident)? (* optional selector name *) -; - -CbtStmt ::= 'CBF', Number1 (* container backup factor *) -; - -SelectStmt ::= - 'SELECT', Number1, (* number of nodes to select without container backup factor *) - ('IN', Clause?, Ident)?, (* bucket name *) - FROM, (Ident | '*'), (* filter reference or whole netmap *) - ('AS', Ident)? (* optional selector name *) -; - -Clause ::= - 'SAME' (* nodes from the same bucket *) - | 'DISTINCT' (* nodes from distinct buckets *) -; - -FilterStmt ::= - 'FILTER', AndChain, ['OR', AndChain], - 'AS', Ident (* obligatory filter name *) -; - -AndChain ::= - Expr, ['AND', Expr] -; - -Expr ::= - '@' Ident (* filter reference *) - | Key, Op, Value (* attribute filter *) -; - -Op ::= 'EQ' | 'NE' | 'GE' | 'GT' | 'LT' | 'LE' -; - -Key ::= Ident | String -; - -Value ::= Ident | Number | String -; - -Number1 ::= Digit1 [Digit]; -Number ::= Digit [Digit]; - -Digit1 ::= '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' ; -Digit ::= '0' | Digit1; diff --git a/pkg/policy/grammar.go b/pkg/policy/grammar.go deleted file mode 100644 index 6fd228e..0000000 --- a/pkg/policy/grammar.go +++ /dev/null @@ -1,60 +0,0 @@ -//nolint:govet,golint // fails on struct tags here, but participle needs this syntax -package policy - -import ( - "github.com/alecthomas/participle" -) - -var parser *participle.Parser - -func init() { - p, err := participle.Build(&query{}) - if err != nil { - panic(err) - } - parser = p -} - -type query struct { - Replicas []*replicaStmt `@@+` - CBF uint32 `("CBF" @Int)?` - Selectors []*selectorStmt `@@*` - Filters []*filterStmt `@@*` -} - -type replicaStmt struct { - Count int `"REP" @Int` - Selector string `("IN" @Ident)?` -} - -type selectorStmt struct { - Count uint32 `"SELECT" @Int` - Bucket []string `("IN" @(("SAME" | "DISTINCT")? Ident))?` - Filter string `"FROM" @(Ident | "*")` - Name string `("AS" @Ident)?` -} - -type filterStmt struct { - Value *orChain `"FILTER" @@` - Name string `"AS" @Ident` -} - -type filterOrExpr struct { - Reference string `"@"@Ident` - Expr *simpleExpr `| @@` -} - -type orChain struct { - Clauses []*andChain `@@ ("OR" @@)*` -} - -type andChain struct { - Clauses []*filterOrExpr `@@ ("AND" @@)*` -} - -type simpleExpr struct { - Key string `@(Ident | String)` - // We don't use literals here to improve error messages. - Op string `@Ident` - Value string `@(Ident | String | Int)` -} diff --git a/pkg/policy/parser/Query.g4 b/pkg/policy/parser/Query.g4 new file mode 100644 index 0000000..0cad576 --- /dev/null +++ b/pkg/policy/parser/Query.g4 @@ -0,0 +1,45 @@ +parser grammar Query; + +options { + tokenVocab = QueryLexer; +} + +policy: repStmt+ cbfStmt? selectStmt* filterStmt*; + +repStmt: + REP Count = NUMBER1 // number of object replicas + (IN Selector = ident)?; // optional selector name + +cbfStmt: CBF BackupFactor = NUMBER1; // container backup factor + +selectStmt: + SELECT Count = NUMBER1 // number of nodes to select without container backup factor *) + (IN clause? Bucket = ident)? // bucket name + FROM Filter = identWC // filter reference or whole netmap + (AS Name = ident)? // optional selector name + ; + +clause: CLAUSE_SAME | CLAUSE_DISTINCT; // nodes from distinct buckets + +filterExpr: + F1 = filterExpr Op = AND_OP F2 = filterExpr + | F1 = filterExpr Op = OR_OP F2 = filterExpr + | expr + ; + +filterStmt: + FILTER Expr = filterExpr + AS Name = ident // obligatory filter name + ; + +expr: + AT Filter = ident // reference to named filter + | Key = filterKey SIMPLE_OP Value = filterValue // attribute comparison + ; + +filterKey : ident | STRING; +filterValue : ident | number | STRING; +number : ZERO | NUMBER1; +keyword : REP | IN | AS | SELECT | FROM | FILTER; +ident : keyword | IDENT; +identWC : ident | WILDCARD; diff --git a/pkg/policy/parser/Query.interp b/pkg/policy/parser/Query.interp new file mode 100644 index 0000000000000000000000000000000000000000..63f74d9a5f7db8596c48f2d0c52c4bffcbc36599 GIT binary patch literal 4174 zcmb7`TW=dT5QX3QEA-~J@WWDf<*ACT6hdv;kdzu|Q5d$f0gT8O$TC|0`+jGJS}o}y zK!FuGb9iRv47u9f-P5=Fv9r0~)!Um*=keyDe)$j{UpJd@G+mq?g`B)3?8cxop zpI5VM3ESIyb>Zy#a=M%?!ZOX%b(o*d7VEJ1eExB^jN#AO@*=F(%lYEBaJ33y`?{(3 z@}8yjEo|%O)$U;z?ry)@{8De~yPZRuo7a~*e7)bW*|UG2x5>1{`r!XwzonHwZZ@wq zyZ>-oZ^O6x?VqRZmvH~3e%$$ZbrM3j**$&;fAx+#-To*JjywJCXwpCK^m^4u-e@ox z9e3hzGz5%;UJtNW4JKm(y5q5!So~sFbnN5K#71AjW)YhU+YOl5&L5i1$P$&n8L=sV z&FF#HT-dQPOOw3l9+NyTu1Ow+iP0pFpFAdcEK)Ht`y&gC9MjzG4Uz;eZHhNd_vw|E`7kH48D$7jP{sVhBr0_mz^k~C7fDo zU@{gqF4u?!mG(tC!S|62*#B#U%r#E?fvH2(L^E1Q`+-Sj?us9bP+9%Kh+w6wbUf&H zX4=HmkfvBAHTxybgTy+PG?7SNv_TPR$xm4wh{&%s`QfN7MKi@{V%!nY_fwM?`q}?W z`43Bn5)qYnWWr!)Ogez&RtI2Rg1cZ{SY7dj?N4sc_2goSO)DaoSw*%CH09kYz%J+m zB59$4C^g{MhNwL79dB4tJR-VHv*Ry9)zPG=pzdQ;C{fF?I`rd;h>Yndo6_2pm=5Xy zQYWfdTacQLN}Pczq#SLqCy8LxZ*tgP71G5yJXgHB`9(K|HJK7mez86FXyy+>T~;6{ zzc|H480LQ@PbMCIzpP7hdY}=G#VDSRuGj=|^n7dYf#oWWSy*2=S~rONexXIVN-cfb z>C+1;bTBATAf6VoglfGktmfcmJYi)c?q@VomHKp(59oyq6hjA>`~gzH?BC;{7RUxD z+%bGTv$^b!B1SBx0du>wVTI}xnbEW>UIDGC2>yU(R28uabRdf*Vh91A{us^^FeGUf zJfy!Np@72e8Zzk}f<{IAUWlZHb{WGUW*Gjk=uZBS^h~Y2krtKDqJ_cCF#KV0qVk6{ z$&#MUNw)w@m0?{ERlJe*0S;6^XPQrqT##bLZWlKUAj5Ddl|gi^&njTApj7zj#c{y2 z|E@&RT_Dy7X`-|2DFlpJ$cnHuMxP#QQf`-kvA5T7hVfg8#>gtqFJ;QDD^s1P6%WB3i0TWE> zq@HmEObAu6hKR2ZVTi4pQAm0Vdp<=%na~f>_O7)I0_+Q;h=-(aXO&kQNK^*Q2_3K~ zx?}jUVqFMRx_=F_#O+q?4JaM&L2~9G>Ek;Pihb^=n3Ve|$4c$$Gl&5DelThFYK;{! zd1itwB1e_3IO$lC&%T<Q@uUD8o{K52uv?qbe Q>VwViB0DVWwcGvsKMaQe`v3p{ literal 0 HcmV?d00001 diff --git a/pkg/policy/parser/Query.tokens b/pkg/policy/parser/Query.tokens new file mode 100644 index 0000000000000000000000000000000000000000..7f5aee960e912a0eb93d1254eb0be4dcdb88ee23 GIT binary patch literal 331 zcmXw!!D_=m3`Fnx5w$&8cACToK{sBxEUb5p)*;Y~|Nk#KCipZXghp>diF;jDsQRHj zwY03+4nL6FsuxN{zbQ1m()5`1M$NKKo8D>my68xA%A2`LN3WFQ6g;X3O*uT9?JX>( z(w}2YZA%5$GEm;v;8s*Foe6_^{9=khQYl{@{JSCx_4=BfN@kAF>jAr2)n z8gO++0R}Y#n^R$cb7%~720H_u-}DT8KKL00_}u^*9q|_gSqC>9;!b2t1Mvq0*`E+( L|3Hx4K}Pxq{%=e0 literal 0 HcmV?d00001 diff --git a/pkg/policy/parser/QueryLexer.g4 b/pkg/policy/parser/QueryLexer.g4 new file mode 100644 index 0000000..6c245b6 --- /dev/null +++ b/pkg/policy/parser/QueryLexer.g4 @@ -0,0 +1,41 @@ +lexer grammar QueryLexer; + +AND_OP : 'AND'; +OR_OP : 'OR'; +SIMPLE_OP : 'EQ' | 'NE' | 'GE' | 'GT' | 'LT' | 'LE'; + +REP : 'REP'; +IN : 'IN'; +AS : 'AS'; +CBF : 'CBF'; +SELECT : 'SELECT'; +FROM : 'FROM'; +FILTER : 'FILTER'; +WILDCARD : '*'; + +CLAUSE_SAME : 'SAME'; +CLAUSE_DISTINCT : 'DISTINCT'; + +L_PAREN : '('; +R_PAREN : ')'; +AT : '@'; + +IDENT : Nondigit (Digit | Nondigit)* ; +fragment Digit : [0-9] ; +fragment Nondigit : [a-zA-Z_] ; + +NUMBER1 : [1-9] Digit* ; +ZERO : '0' ; + +// Taken from antlr4 json grammar with minor corrections. +// https://github.com/antlr/grammars-v4/blob/master/json/JSON.g4 +STRING : '"' (ESC | SAFECODEPOINTDOUBLE)* '"' + | '\'' (ESC | SAFECODEPOINTSINGLE)* '\'' ; + +fragment ESC : '\\' (['"\\/bfnrt] | UNICODE) ; +fragment UNICODE : 'u' HEX HEX HEX HEX ; +fragment HEX : [0-9a-fA-F] ; +fragment SAFECODEPOINTSINGLE : ~ ['\\\u0000-\u001F] ; +fragment SAFECODEPOINTDOUBLE : ~ ["\\\u0000-\u001F] ; + +WS : [ \t\n\r] + -> skip ; diff --git a/pkg/policy/parser/QueryLexer.interp b/pkg/policy/parser/QueryLexer.interp new file mode 100644 index 0000000000000000000000000000000000000000..d453ff6e05930c0ed8b54aaca3ea4cceb2712edd GIT binary patch literal 7060 zcmc(jO>ZMb5QgvbD|`!}99;1?C%o}2YvI_D?Fa-y(XvFaBAb;U9QgOVPgT!H_bdyD z1JZc9>z(SVx2k)(YyABFmxrg(G#LS>SDd$TvQi_<3*B> z^{wRXUbpp0x9b-#bwAa+`ueQS%j4mOX1hDr8gua(57#%fn$7lf-tJxN6N#Vd_eHWA zuEu?-dUj5~{qgRH_m6MiWTeFD{%~6zj@(b%o7-JYPt>aIzFMCuk*b1H1)(bFR9|m* zo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N;}+vfhOTeCD#NImgGl`twXo^Sz0S1~Hvp_A=L>hP}q9Pv{C(@QT3?&oO z0MoMpCY}SV^dTaJ3uc|k6U}bA0G9aI)y@q7lY*wYaRGEkXTro}nKGs(f$A+ukeYT9 z0XTj7yjk^2;*RB!Mhjr&^Q@c^eq5$AJTIh6Bn_0@03eI8hTwI7QO@ zz>IJ@-_%a42&c;s05t+v=SLEswSa9d0XV}2_Gl6h1%gmA6O^J_kbXZ&zSr7V8klYA zJ7VLAs+&$nyj;L?OTrNwM`*0k#vvINjohIK85Kd{P@;`fc#QMQ5gSKSCx((71 zuf%xLCCNW{C_{jIoY9b>%pu7%>JxO;Pe}qXA_4ArfEdT5;2u3cIEe+g%bkBXP(v3+ zh!G-a_goSq1&^+7;W=V_A)wu}Eu{Hw=vXU4<10YA(})DZ{#_t63p@_yh|nu^w_+0^ z{%ef70gW_DPpEYqK>U~K65UkZ)cS`&L}=8w{)q}MJIs&UrjKH&uK;~e&e~B8$x-4a z;0%R?O*CMLmW7`=Acnar{IHUvl8kj60Z7Ny$i={y=R#(s+S;(GfT2~=KxrXWnQE*O zE+5}T(!4O4mhSv3ol2zj1v9T#7%)UK26^<_1}nL$SQ-_Mh~qdh1|d1QE_0ts z8P-9gOUzU`j8UKWMUQMo)CuI?VpVR8cG`Ou);r=wEIQF6qo zlc8{MGeeXOGDpb<^;eEUq^x&bt%diF;jDsQRHj zwY03+4nL6FsuxN{zbQ1m()5`1M$NKKo8D>my68xA%A2`LN3WFQ6g;X3O*uT9?JX>( z(w}2YZA%5$GEm;v;8s*Foe6_^{9=khQYl{@{JSCx_4=BfN@kAF>jAr2)n z8gO++0R}Y#n^R$cb7%~720H_u-}DT8KKL00_}u^*9q|_gSqC>9;!b2t1Mvq0*`E+( L|3Hx4K}Pxq{%=e0 literal 0 HcmV?d00001 diff --git a/pkg/policy/parser/generate.go b/pkg/policy/parser/generate.go new file mode 100644 index 0000000..850f362 --- /dev/null +++ b/pkg/policy/parser/generate.go @@ -0,0 +1,3 @@ +package parser + +//go:generate antlr4 -Dlanguage=Go -visitor QueryLexer.g4 Query.g4 diff --git a/pkg/policy/parser/query_base_listener.go b/pkg/policy/parser/query_base_listener.go new file mode 100644 index 0000000000000000000000000000000000000000..d872ae1da61b8c0f331f306fba2f297e90dca5f2 GIT binary patch literal 4408 zcmb7{KX2PG5XE^g0p{VC@N)>)cd)!$ z!Q%R66$U|_q_4@2L7ni1afl*37z9~SS6slwE)y^9I!vn~N=lJ){G3O;Ek#za=x2pH z7eOEoe@_~wm2a~~C_|7nkbpd0=S(oj`|WMT<-S%0!I?C`t4*6q_tGzV55%Enj*gAs zZ7Tl$3xcu$mI@Dlb5ul+z&6Je?AjHCy*o08AFL4L zu&upD^hD%I+njs@4!#3YQ$P}&nIJpB0^ zfNzhA=8L6&JDe&_4!bGmC@(Z7qLm9 zkc_BMJTt<&5uyj78e#}UbfO` zg4U1~1}^Mw+Ak1wBQ_nE5~3f)u8b8{2^b$?`g@59^rJSqlHxoT<^)q$Ct4XinF+{S zO9dBrxpoktv83P__4AU0ta3ut&7AxN+G4XawQ-UUfv6-a2Fiy()D9~YX3&Nl2uvz& zTXd)B>Oj!5tZuJ=acan6LkvC-xJAa(iA`e@7hHcLX9= pw$9LB5?2R;Zio1#DRv12|z zo56H3xiy{EdF!NQhBGsrEzD>*UbGsGr{(Im891?aV&e z?_i#j9%WgYHNGx0b8jvT^t5iWILRLh$mwj2catd3X&0GcV7%PK|Blw;+#pXd`{SoQ zrrO?r8V9FMbJFSCZqsy5`sbadIX!LnIqG-M`%Pne{T@KuJv{|@+U}m8VZzDT8Nd^K zIKAC&HVyz=%o6lmu9Vd@=;Mk$KIm%)qR^+7rs+u^SM>W31OoJ80Ey`1iat8}LEpAz zz=k7e1&Iwuu!*A+2;xu;V8ao+cI%umkOu=z$p2BS1|Oic2W4RDU^ zsjiF;;DLZJMOy-h00wH5Mwf{VY0T)L(MU0|A)Afuf`PG?0Ypj5gi(crFYSIvFVdV}ezBxs@dbn5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Z_&}mPcFj&HlZas5>3zw1%ARj$G=Rxg&^i(oUEhoO|#xnMus(s)VNos z@CNdpds)!)QAB*gQC9>2=+SzIWv6f``W+D~Z@Q4PxD$0f=v@o0M;ZQo0@sjr!k8np z^LV@8Eb}xwLe_ZqT(5Le0#F!qlSS)z#iYJWA^m%Pxmd)G)NtDtlL~;;+NmrIAx571i6=}IkX#?K<#xNwu3|kU5T3&_FY!+=_`en2*Rn-z zTHK`ksw~pqq67dOMr8L{Vm5B)Dh1#k2h*2u736M69?a|HUP#8lU!h-^en{4WpyaqPXCaw~W=W)J z=M}P)KqUjh(hljqqVOD`SK@YARvE$qdL?j=p&d w>#qb)%9wx!@+;AE)lI=W=tA-TqMPDO%|16i!E4jFT zTtz}OX?9HaAe&U3K*>0^g+kgUy=^nxIquc&Tj)yzcI@0L4QiKWApeM& z*-P|7yI!HiRV2g=cd;PlOI_sIh4zr8MS=cC-X7RgHUc3#7!Qa#Ti)$ zM~e0dF|$Sixl#D`rjpFf zAY{Exrp-|kuwN%VV4ZwGoO2xrVO$x_;J%W~*$75OD1is&d;q&zvgh*`HKF(&`19%O zYRR7uU(~py^e$k42uAOX1A&~Me-DA+9dOv(#r28UMTPzR*>oEC+i%*aZeOJv2kOK5 z`F+?mHgY`{lej%=GH_8j)e-n{+f`8)R2|wse(YmcZjXvYfs?`|cThM9armV54vGuf z{-pjRXHgr7*#U}8*}jt6VM7)b*dUv+1M?#rv96ZvI~)J3_<$X_bNPzPqw#uOXVUef Rvlp(emiBEhgKYgP{sGV$F9!es literal 0 HcmV?d00001 diff --git a/pkg/policy/parser/query_parser.go b/pkg/policy/parser/query_parser.go new file mode 100644 index 0000000000000000000000000000000000000000..fb94f3a2573c6d6ebacf2689b0f89022a44144b7 GIT binary patch literal 59362 zcmeHwYjYbplI>^dSJe2!4CU3ZMOiP;tRt>kmU|iNmVG7lIAS|G95hvGb4`&9lJa9` z`oG_EG67TqReaN;?}j_vVpRf2WF`_oX5vvl{E(gXZ}RN-yq6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Drgb@?lPWDg5aT?nz-7z z{h6qkab+U-TD_PEGaZ-+kec05;@5RsdUESQrN*uy9aAJ1n%@jAcrp^#t#lKWCO;U%{wOFm2qPPUzWyDXkQwCS6(K z{EZ~Fhx!)yyo7DYY%a)K$zpbBB4b%$lSA`S`Wj5+{pf0?o(w~nG-Ol5_sg~+32d^- z^6uJC4K*5aRA-6Xw%z!e-n7l3SvDJSGZ03}WWUmbU$2eTqfh<*R>8ms0LKoGzpamC5~ds5(6cgmb%W!gGVt3XXNX~K z-xW@&Uh8EJDe@JJ@D$a?t?-nf4VdAMgbF*nMBGD1vgaplL#DVPa3x#ZiXR4TG-SKToc4H)-@n=U^IvOwpBuOThI>nS4a^?z z2(8r`|NigyxJXrU?EG2YW#2ny#hrgKRo?LN&Ef@LQ!HNG_ecwXd|A@G>g58c6W53C zeM=_4J=V8O95^ojQ09JA>)gh^EoDr73tp?CZxPeXx9_RlYSX^1iQnNK<0^YJqL1di zr71D)qolQ&_7>}O0d3fu57Z_cVrI5pq`mBA7YkSbfaNL;Tq9PrK(*0*30L!MOv z^)_+fWi_kVpb?Ehb0l{neXHjs69v!SG+#bHJH33qs8Q_8m9}-H6|8C*8&Q87wy_~- zQ`YgDvq|=`C2k`Y@?4~C(n?kWqj`SOEz3^D(PhHdVO;k&;;*)qLxy!&%hB1$4z1-k zXW#ARW$fjB?yC4E>}7lN$#na6g6%BV%7PbBQW#lk)fQLD_ueC`oiuL$> zO%Gu|-`@AyS*|rm??-!`u>sqtCca1sQOyd#K-jp^I@bpAc@wH3(F2TmI&i zfxA+NIo^84#)fFu!&~>=v9zIlCF;?Id_wQ0)5fQvGBENUyn)%jWskCd%Wm(+^{c^< z%M1K`y6~&`8!K)jdL{{#CX9$EkZ literal 0 HcmV?d00001 diff --git a/pkg/policy/parser/query_visitor.go b/pkg/policy/parser/query_visitor.go new file mode 100644 index 0000000000000000000000000000000000000000..b6128a568b159afe615ceacbf2a9247a419c1433 GIT binary patch literal 1645 zcmb`H!EVAZ42I9-DJ*u`c4(!E%Z{k932g!fXq-dpvWT{+LaI?A-hFA@mL4WeQ#iEp zZzca{yJ^A@A{8U-F-Oe}#*k>0!AFU@3icz|RWP1!-d8XRo`a_VCJUG^HZYycHUVP= z7YDvaD7ZeNhA^a!jLED}+Q4ux%~!b#gvvsmn^e<#6zVcJGQ;pyQD(@P-8=@nut?D$ ze9EIVN<(5FPX1stT9j6CDQum#GOCi9sz8d{T{1VQ6D|v6Vr??h`mab5pb?$~u}k`dh$Imj z;Ym1Rio*0E>n=l_tM){Rl$S?rrxiKXwBlk<`$VSp>`YIEZV@kChA>xs6I6d_WL;^X^8O)q>nzcnj}s)K>2-w^7f38~sYSupdo8KNtW2 literal 0 HcmV?d00001 diff --git a/pkg/policy/query.go b/pkg/policy/query.go index 1b6447e..7fbdae5 100644 --- a/pkg/policy/query.go +++ b/pkg/policy/query.go @@ -3,9 +3,12 @@ package policy import ( "errors" "fmt" + "strconv" "strings" + "github.com/antlr/antlr4/runtime/Go/antlr" "github.com/nspcc-dev/neofs-api-go/pkg/netmap" + "github.com/nspcc-dev/neofs-sdk-go/pkg/policy/parser" ) var ( @@ -19,83 +22,252 @@ var ( ErrUnknownFilter = errors.New("policy: filter not found") // ErrUnknownSelector is returned when a value of IN is unknown. ErrUnknownSelector = errors.New("policy: selector not found") + // ErrSyntaxError is returned for errors found by ANTLR parser. + ErrSyntaxError = errors.New("policy: syntax error") ) -func parse(s string) (*query, error) { - q := new(query) - err := parser.Parse(strings.NewReader(s), q) - if err != nil { - return nil, err - } - return q, nil +type policyVisitor struct { + errors []error + parser.BaseQueryVisitor + antlr.DefaultErrorListener } // Parse parses s into a placement policy. func Parse(s string) (*netmap.PlacementPolicy, error) { - q, err := parse(s) - if err != nil { + return parse(s) +} + +func newPolicyVisitor() *policyVisitor { + return &policyVisitor{} +} + +func parse(s string) (*netmap.PlacementPolicy, error) { + input := antlr.NewInputStream(s) + lexer := parser.NewQueryLexer(input) + stream := antlr.NewCommonTokenStream(lexer, 0) + + p := parser.NewQuery(stream) + p.BuildParseTrees = true + + v := newPolicyVisitor() + p.RemoveErrorListeners() + p.AddErrorListener(v) + pl := p.Policy().Accept(v) + + if len(v.errors) != 0 { + return nil, v.errors[0] + } + if err := validatePolicy(pl.(*netmap.PlacementPolicy)); err != nil { return nil, err } + return pl.(*netmap.PlacementPolicy), nil +} - seenFilters := map[string]bool{} - fs := make([]*netmap.Filter, 0, len(q.Filters)) - for _, qf := range q.Filters { - f, err := filterFromOrChain(qf.Value, seenFilters) - if err != nil { - return nil, err +func (p *policyVisitor) SyntaxError(recognizer antlr.Recognizer, offendingSymbol interface{}, line, column int, msg string, e antlr.RecognitionException) { + p.reportError(fmt.Errorf("%w: line %d:%d %s", ErrSyntaxError, line, column, msg)) +} + +func (p *policyVisitor) reportError(err error) interface{} { + p.errors = append(p.errors, err) + return nil +} + +// VisitPolicy implements parser.QueryVisitor interface. +func (p *policyVisitor) VisitPolicy(ctx *parser.PolicyContext) interface{} { + if len(p.errors) != 0 { + return nil + } + + pl := new(netmap.PlacementPolicy) + + repStmts := ctx.AllRepStmt() + rs := make([]*netmap.Replica, 0, len(repStmts)) + for _, r := range repStmts { + res, ok := r.Accept(p).(*netmap.Replica) + if !ok { + return nil } - f.SetName(qf.Name) - fs = append(fs, f) - seenFilters[qf.Name] = true + + rs = append(rs, res) + } + pl.SetReplicas(rs...) + + if cbfStmt := ctx.CbfStmt(); cbfStmt != nil { + cbf, ok := cbfStmt.(*parser.CbfStmtContext).Accept(p).(uint32) + if !ok { + return nil + } + pl.SetContainerBackupFactor(cbf) + } + + selStmts := ctx.AllSelectStmt() + ss := make([]*netmap.Selector, 0, len(selStmts)) + for _, s := range selStmts { + res, ok := s.Accept(p).(*netmap.Selector) + if !ok { + return nil + } + + ss = append(ss, res) + } + pl.SetSelectors(ss...) + + filtStmts := ctx.AllFilterStmt() + fs := make([]*netmap.Filter, 0, len(filtStmts)) + for _, f := range filtStmts { + fs = append(fs, f.Accept(p).(*netmap.Filter)) + } + pl.SetFilters(fs...) + + return pl +} + +func (p *policyVisitor) VisitCbfStmt(ctx *parser.CbfStmtContext) interface{} { + cbf, err := strconv.ParseUint(ctx.GetBackupFactor().GetText(), 10, 32) + if err != nil { + return p.reportError(ErrInvalidNumber) + } + + return uint32(cbf) +} + +// VisitRepStmt implements parser.QueryVisitor interface. +func (p *policyVisitor) VisitRepStmt(ctx *parser.RepStmtContext) interface{} { + num, err := strconv.ParseUint(ctx.GetCount().GetText(), 10, 32) + if err != nil { + return p.reportError(ErrInvalidNumber) + } + + rs := new(netmap.Replica) + rs.SetCount(uint32(num)) + + if sel := ctx.GetSelector(); sel != nil { + rs.SetSelector(sel.GetText()) + } + + return rs +} + +// VisitSelectStmt implements parser.QueryVisitor interface. +func (p *policyVisitor) VisitSelectStmt(ctx *parser.SelectStmtContext) interface{} { + res, err := strconv.ParseUint(ctx.GetCount().GetText(), 10, 32) + if err != nil { + return p.reportError(ErrInvalidNumber) + } + + s := new(netmap.Selector) + s.SetCount(uint32(res)) + + if clStmt := ctx.Clause(); clStmt != nil { + s.SetClause(clauseFromString(clStmt.GetText())) + } + + if bStmt := ctx.GetBucket(); bStmt != nil { + s.SetAttribute(ctx.GetBucket().GetText()) + } + + s.SetFilter(ctx.GetFilter().GetText()) // either ident or wildcard + + if ctx.AS() != nil { + s.SetName(ctx.GetName().GetText()) + } + return s +} + +// VisitFilterStmt implements parser.QueryVisitor interface. +func (p *policyVisitor) VisitFilterStmt(ctx *parser.FilterStmtContext) interface{} { + f := p.VisitFilterExpr(ctx.GetExpr().(*parser.FilterExprContext)).(*netmap.Filter) + f.SetName(ctx.GetName().GetText()) + return f +} + +func (p *policyVisitor) VisitFilterExpr(ctx *parser.FilterExprContext) interface{} { + if eCtx := ctx.Expr(); eCtx != nil { + return eCtx.Accept(p) + } + + f := new(netmap.Filter) + op := operationFromString(ctx.GetOp().GetText()) + f.SetOperation(op) + + f1 := ctx.GetF1().Accept(p).(*netmap.Filter) + f2 := ctx.GetF2().Accept(p).(*netmap.Filter) + + // Consider f1=(.. AND ..) AND f2. This can be merged because our AND operation + // is of arbitrary arity. ANTLR generates left-associative parse-tree by default. + if f1.Operation() == op { + f.SetInnerFilters(append(f1.InnerFilters(), f2)...) + return f + } + + f.SetInnerFilters(f1, f2) + return f +} + +// VisitFilterKey implements parser.QueryVisitor interface. +func (p *policyVisitor) VisitFilterKey(ctx *parser.FilterKeyContext) interface{} { + if id := ctx.Ident(); id != nil { + return id.GetText() + } + + str := ctx.STRING().GetText() + return str[1 : len(str)-1] +} + +func (p *policyVisitor) VisitFilterValue(ctx *parser.FilterValueContext) interface{} { + if id := ctx.Ident(); id != nil { + return id.GetText() + } + + if num := ctx.Number(); num != nil { + return num.GetText() + } + + str := ctx.STRING().GetText() + return str[1 : len(str)-1] +} + +// VisitExpr implements parser.QueryVisitor interface. +func (p *policyVisitor) VisitExpr(ctx *parser.ExprContext) interface{} { + f := new(netmap.Filter) + if flt := ctx.GetFilter(); flt != nil { + f.SetName(flt.GetText()) + return f + } + + key := ctx.GetKey().Accept(p) + opStr := ctx.SIMPLE_OP().GetText() + value := ctx.GetValue().Accept(p) + + f.SetKey(key.(string)) + f.SetOperation(operationFromString(opStr)) + f.SetValue(value.(string)) + return f +} + +// validatePolicy checks high-level constraints such as filter link in SELECT +// being actually defined in FILTER section. +func validatePolicy(p *netmap.PlacementPolicy) error { + seenFilters := map[string]bool{} + for _, f := range p.Filters() { + seenFilters[f.Name()] = true } seenSelectors := map[string]bool{} - ss := make([]*netmap.Selector, 0, len(q.Selectors)) - for _, qs := range q.Selectors { - if qs.Filter != netmap.MainFilterName && !seenFilters[qs.Filter] { - return nil, fmt.Errorf("%w: '%s'", ErrUnknownFilter, qs.Filter) + for _, s := range p.Selectors() { + if flt := s.Filter(); flt != netmap.MainFilterName && !seenFilters[flt] { + return fmt.Errorf("%w: '%s'", ErrUnknownFilter, flt) } - s := netmap.NewSelector() - switch len(qs.Bucket) { - case 1: // only bucket - s.SetAttribute(qs.Bucket[0]) - case 2: // clause + bucket - s.SetClause(clauseFromString(qs.Bucket[0])) - s.SetAttribute(qs.Bucket[1]) - } - s.SetName(qs.Name) - seenSelectors[qs.Name] = true - s.SetFilter(qs.Filter) - if qs.Count == 0 { - return nil, fmt.Errorf("%w: SELECT", ErrInvalidNumber) - } - s.SetCount(qs.Count) - ss = append(ss, s) + seenSelectors[s.Name()] = true } - rs := make([]*netmap.Replica, 0, len(q.Replicas)) - for _, qr := range q.Replicas { - r := netmap.NewReplica() - if qr.Selector != "" { - if !seenSelectors[qr.Selector] { - return nil, fmt.Errorf("%w: '%s'", ErrUnknownSelector, qr.Selector) - } - r.SetSelector(qr.Selector) + for _, r := range p.Replicas() { + if sel := r.Selector(); sel != "" && !seenSelectors[sel] { + return fmt.Errorf("%w: '%s'", ErrUnknownSelector, sel) } - if qr.Count == 0 { - return nil, fmt.Errorf("%w: REP", ErrInvalidNumber) - } - r.SetCount(uint32(qr.Count)) - rs = append(rs, r) } - p := new(netmap.PlacementPolicy) - p.SetFilters(fs...) - p.SetSelectors(ss...) - p.SetReplicas(rs...) - p.SetContainerBackupFactor(q.CBF) - - return p, nil + return nil } func clauseFromString(s string) netmap.Clause { @@ -105,74 +277,31 @@ func clauseFromString(s string) netmap.Clause { case "DISTINCT": return netmap.ClauseDistinct default: - return 0 + // Such errors should be handled by ANTLR code thus this panic. + panic(fmt.Errorf("BUG: invalid clause: %s", s)) } } -func filterFromOrChain(expr *orChain, seen map[string]bool) (*netmap.Filter, error) { - var fs []*netmap.Filter - for _, ac := range expr.Clauses { - f, err := filterFromAndChain(ac, seen) - if err != nil { - return nil, err - } - fs = append(fs, f) - } - if len(fs) == 1 { - return fs[0], nil - } - - f := netmap.NewFilter() - f.SetOperation(netmap.OpOR) - f.SetInnerFilters(fs...) - return f, nil -} - -func filterFromAndChain(expr *andChain, seen map[string]bool) (*netmap.Filter, error) { - var fs []*netmap.Filter - for _, fe := range expr.Clauses { - var f *netmap.Filter - var err error - if fe.Expr != nil { - f, err = filterFromSimpleExpr(fe.Expr, seen) - } else { - f = netmap.NewFilter() - f.SetName(fe.Reference) - } - if err != nil { - return nil, err - } - fs = append(fs, f) - } - if len(fs) == 1 { - return fs[0], nil - } - - f := netmap.NewFilter() - f.SetOperation(netmap.OpAND) - f.SetInnerFilters(fs...) - return f, nil -} - -func filterFromSimpleExpr(se *simpleExpr, seen map[string]bool) (*netmap.Filter, error) { - f := netmap.NewFilter() - f.SetKey(se.Key) - switch se.Op { +func operationFromString(op string) netmap.Operation { + switch strings.ToUpper(op) { + case "AND": + return netmap.OpAND + case "OR": + return netmap.OpOR case "EQ": - f.SetOperation(netmap.OpEQ) + return netmap.OpEQ case "NE": - f.SetOperation(netmap.OpNE) + return netmap.OpNE case "GE": - f.SetOperation(netmap.OpGE) + return netmap.OpGE case "GT": - f.SetOperation(netmap.OpGT) + return netmap.OpGT case "LE": - f.SetOperation(netmap.OpLE) + return netmap.OpLE case "LT": - f.SetOperation(netmap.OpLT) + return netmap.OpLT default: - return nil, fmt.Errorf("%w: '%s'", ErrUnknownOp, se.Op) + // Such errors should be handled by ANTLR code thus this panic. + panic(fmt.Errorf("BUG: invalid operation: %s", op)) } - f.SetValue(se.Value) - return f, nil } diff --git a/pkg/policy/query_test.go b/pkg/policy/query_test.go index ebe2be2..2493edc 100644 --- a/pkg/policy/query_test.go +++ b/pkg/policy/query_test.go @@ -2,6 +2,7 @@ package policy import ( "errors" + "fmt" "testing" "github.com/nspcc-dev/neofs-api-go/v2/netmap" @@ -80,6 +81,32 @@ func TestFromSelectNoAttribute(t *testing.T) { }) } +func TestString(t *testing.T) { + qTemplate := `REP 1 +SELECT 1 IN City FROM Filt +FILTER Property EQ %s AND Something NE 7 AS Filt` + + testCases := []string{ + `"double-quoted"`, + `"with ' single"`, + `'single-quoted'`, + `'with " double'`, + } + + for _, s := range testCases { + t.Run(s, func(t *testing.T) { + q := fmt.Sprintf(qTemplate, s) + r, err := Parse(q) + require.NoError(t, err) + + expected := newFilter("Filt", "", "", netmap.AND, + newFilter("", "Property", s[1:len(s)-1], netmap.EQ), + newFilter("", "Something", "7", netmap.NE)) + require.EqualValues(t, []*netmap.Filter{expected}, r.Filters()) + }) + } +} + func TestFromSelectClause(t *testing.T) { q := `REP 4 SELECT 3 IN Country FROM * @@ -205,12 +232,12 @@ func TestValidation(t *testing.T) { SELECT 1 IN City FROM F FILTER Country KEK RU AS F` _, err := Parse(q) - require.True(t, errors.Is(err, ErrUnknownOp), "got: %v", err) + require.True(t, errors.Is(err, ErrSyntaxError), "got: %v", err) }) t.Run("TypoInREP", func(t *testing.T) { q := `REK 3` _, err := Parse(q) - require.Error(t, err) + require.True(t, errors.Is(err, ErrSyntaxError)) }) t.Run("InvalidFilterName", func(t *testing.T) { q := `REP 3 @@ -223,13 +250,13 @@ func TestValidation(t *testing.T) { t.Run("InvalidNumberInREP", func(t *testing.T) { q := `REP 0` _, err := Parse(q) - require.True(t, errors.Is(err, ErrInvalidNumber), "got: %v", err) + require.True(t, errors.Is(err, ErrSyntaxError), "got: %v", err) }) t.Run("InvalidNumberInREP", func(t *testing.T) { q := `REP 1 IN Good SELECT 0 IN City FROM *` _, err := Parse(q) - require.True(t, errors.Is(err, ErrInvalidNumber), "got: %v", err) + require.True(t, errors.Is(err, ErrSyntaxError), "got: %v", err) }) } From 0f5ce2e8bb3debc613dd190dee7438b589ccf5a9 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Fri, 21 May 2021 18:12:27 +0300 Subject: [PATCH 2/5] [#3] policy: allow to use brackets in filters Signed-off-by: Evgenii Stratonikov --- pkg/policy/doc.go | 5 +---- pkg/policy/parser/Query.g4 | 1 + pkg/policy/parser/Query.interp | Bin 4174 -> 4346 bytes pkg/policy/parser/query_parser.go | Bin 59362 -> 60564 bytes pkg/policy/query.go | 4 ++++ pkg/policy/query_test.go | 24 ++++++++++++++++++++++++ 6 files changed, 30 insertions(+), 4 deletions(-) diff --git a/pkg/policy/doc.go b/pkg/policy/doc.go index 10394fa..4e10a1b 100644 --- a/pkg/policy/doc.go +++ b/pkg/policy/doc.go @@ -2,10 +2,7 @@ // ANTLRv4 grammar is provided in `parser/Query.g4` and `parser/QueryLexer.g4`. // // Current limitations: -// 1. Grouping filter expressions in parenthesis is not supported right now. -// Requiring this will make query too verbose, making it optional makes -// our grammar not LL(1). This can be supported in future. -// 2. Filters must be defined before they are used. +// 1. Filters must be defined before they are used. // This requirement may be relaxed in future. // // Example query: diff --git a/pkg/policy/parser/Query.g4 b/pkg/policy/parser/Query.g4 index 0cad576..fc74c6f 100644 --- a/pkg/policy/parser/Query.g4 +++ b/pkg/policy/parser/Query.g4 @@ -24,6 +24,7 @@ clause: CLAUSE_SAME | CLAUSE_DISTINCT; // nodes from distinct buckets filterExpr: F1 = filterExpr Op = AND_OP F2 = filterExpr | F1 = filterExpr Op = OR_OP F2 = filterExpr + | '(' Inner = filterExpr ')' | expr ; diff --git a/pkg/policy/parser/Query.interp b/pkg/policy/parser/Query.interp index 63f74d9a5f7db8596c48f2d0c52c4bffcbc36599..9512ecb628462c54d2fb7c893a4d88015cbb1ffc 100644 GIT binary patch literal 4346 zcmaKv-A)@x5QXpiDSXH7ii>I60xC!Tgoq?Pbn>&5xy*<356g~c+=)&Wt$B?SozE+3q2f%a7;p=c^e0p06&#db3(A{|Hy>5FVbk z&0gLQX?+e4&5!l&ZWlg(`fBrIvu!@_9NOMIJvQO%?UrWG|NrrjmbTd(>_405wDZ@^ z_Nk%!cb}Sv@U40N@B71-aQmgX-}!iT5<DhonTW}XUmR4>Ss(YNHu@H8Hd%AA_60FmEBtY9WNSv& zWCCYoO#;@8tXcTQI+6xMldR|#lPpiJNfwF4^!yP-7L)c|f+n`%BY<(hPmwh_F1|Lf z9Kb`in&L!E-x>aUNb3z?<1B)cT6n9+cgU= zyQ8?4a4M~ZiCC;LxyEcLwQo`hwvR+${l+M7Zf*+j2C*9k8rPJrC@V|dXHC?r4JNgW zC0;x@Z;W)8-8V)A>JC&2PcmnwKBt;BMJ2Qa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OE<~}t zpv!=diAMV#rhGW#r9`lUwZj`DezeEy!TimKf+?9yR^xLpLPQxas1yHH#OG~*pa&q{ z;`w0m@nZ{h70zZHV*?f)M?QHCpsa<187tSdq3NU0SRlTm!&hrE%W&mQ-QZ&pM0InG z(ZfK1S145r=Pof4K!oM$`V0ioa`Or0v&~@`aGGP50(=Ju@j%6AqbkKocBIuv0FeMe z?0`#wOQywZfFdyI=6iw2;usM&A`t9g;HwJ3fasTWXPIkhvLbB3^)$6Y+7PQi6ew`H zN%jX73Y$xmmmV_sZipUS9*iRi4?}4=RN*3BN_QNnEoyCPgbfN@nyWRq^cv13)ik)` z>WvKPI*+_WZI8s-PO{goFIPQrgba5eQMDDhVnV_Dq-IGrjsXY1cqrE^Z)n6ut1Oeb z>CNrs1Be?QJCtWRF`^ROU3vBM7&I7{g(531DN%&RQRq8y5ar2*XVa`$U8V!FWvE&3 z1WVS5a1k?%-BO1nAX~MnO`PsDwuocFN6)usaq-FH%kS3>Sw#|b}be`_xd zkG~&U2NfmUGVyVO)=i$GeC$nX+UYT+K%brHr?+SNHQ&pb^y216S^meFXV_F|y|of&pX*xgui5j@{+`_0DlxSh0 zpax#JvT)2q^xROEPW}0fY75FP?o*ub*y3<2jhy>gaRFp$X?ZtwEgGa<7@?z!ABMkQ z@C?mHJL%$3Z@BHsc&p~rzOMdy*i=PHa_+Bu2^aS&`&8l5<) Date: Fri, 21 May 2021 18:21:11 +0300 Subject: [PATCH 3/5] [#3] policy/test: remove debug print --- pkg/policy/encode_test.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/pkg/policy/encode_test.go b/pkg/policy/encode_test.go index 10de5fe..d2bb716 100644 --- a/pkg/policy/encode_test.go +++ b/pkg/policy/encode_test.go @@ -1,7 +1,6 @@ package policy_test import ( - "fmt" "strings" "testing" @@ -30,7 +29,6 @@ FILTER City EQ SPB AND SSD EQ true OR City EQ SPB AND Rating GE 5 AS SPBSSD`, require.NoError(t, err) got := policy.Encode(q) - fmt.Println(strings.Join(got, "\n")) require.Equal(t, testCase, strings.Join(got, "\n")) } } From f5413e94b56e71801d03d88bb1502ac3cccb232d Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Fri, 21 May 2021 18:29:44 +0300 Subject: [PATCH 4/5] [#3] policy/test: increase test coverage Signed-off-by: Evgenii Stratonikov --- pkg/policy/query_test.go | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/pkg/policy/query_test.go b/pkg/policy/query_test.go index b7278d4..9e27e67 100644 --- a/pkg/policy/query_test.go +++ b/pkg/policy/query_test.go @@ -3,6 +3,7 @@ package policy import ( "errors" "fmt" + "math" "testing" "github.com/nspcc-dev/neofs-api-go/v2/netmap" @@ -271,17 +272,28 @@ func TestValidation(t *testing.T) { _, err := Parse(q) require.Error(t, err) }) - t.Run("InvalidNumberInREP", func(t *testing.T) { - q := `REP 0` - _, err := Parse(q) - require.True(t, errors.Is(err, ErrSyntaxError), "got: %v", err) - }) - t.Run("InvalidNumberInREP", func(t *testing.T) { - q := `REP 1 IN Good - SELECT 0 IN City FROM *` - _, err := Parse(q) - require.True(t, errors.Is(err, ErrSyntaxError), "got: %v", err) - }) +} + +// Checks that an error is returned in cases when positive 32-bit integer is expected. +func TestInvalidNumbers(t *testing.T) { + tmpls := []string{ + "REP %d", + "REP 1 CBF %d", + "REP 1 SELECT %d FROM *", + } + for i := range tmpls { + zero := fmt.Sprintf(tmpls[i], 0) + t.Run(zero, func(t *testing.T) { + _, err := Parse(zero) + require.Error(t, err) + }) + + big := fmt.Sprintf(tmpls[i], int64(math.MaxUint32)+1) + t.Run(big, func(t *testing.T) { + _, err := Parse(big) + require.Error(t, err) + }) + } } func TestFilterStringSymbols(t *testing.T) { From 4a0ed2f6ea1c629e518cccee19b01a2962820234 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Fri, 21 May 2021 19:04:15 +0300 Subject: [PATCH 5/5] [#3] policy: update doc.go Signed-off-by: Evgenii Stratonikov --- pkg/policy/doc.go | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/pkg/policy/doc.go b/pkg/policy/doc.go index 4e10a1b..cceebca 100644 --- a/pkg/policy/doc.go +++ b/pkg/policy/doc.go @@ -3,15 +3,17 @@ // // Current limitations: // 1. Filters must be defined before they are used. -// This requirement may be relaxed in future. +// This requirement may be relaxed in future. +// 2. Keywords are key-sensitive. This can be changed if necessary +// https://github.com/antlr/antlr4/blob/master/doc/case-insensitive-lexing.md . // // Example query: -// REP 1 in SPB -// REP 2 in Americas +// REP 1 IN SPB +// REP 2 IN Americas // CBF 4 -// SELECT 1 Node IN City FROM SPBSSD AS SPB -// SELECT 2 Node IN SAME City FROM Americas AS Americas +// SELECT 1 IN City FROM SPBSSD AS SPB +// SELECT 2 IN SAME City FROM Americas AS Americas // FILTER SSD EQ true AS IsSSD -// FILTER @IsSSD AND Country eq "RU" AND City eq "St.Petersburg" AS SPBSSD -// FILTER 'Continent' == 'North America' OR Continent == 'South America' AS Americas +// FILTER @IsSSD AND Country EQ "RU" AND City EQ "St.Petersburg" AS SPBSSD +// FILTER 'Continent' EQ 'North America' OR Continent EQ 'South America' AS Americas package policy