13
13
pp .ParserElement .enablePackrat ()
14
14
15
15
16
+ class ParseError (ValueError ):
17
+ def __init__ (self , msg ):
18
+ super ().__init__ (f"Filter expression parse error: { msg } " )
19
+
20
+
16
21
class UnsupportedFilteringFeatureError (ValueError ):
17
22
def __init__ (self ):
18
23
super ().__init__ (
@@ -27,6 +32,11 @@ class UnsupportedRegexError(UnsupportedFilteringFeatureError):
27
32
feature = "Regular expressions"
28
33
29
34
35
+ class UnsupportedArraySubscriptError (UnsupportedFilteringFeatureError ):
36
+ issue = "167"
37
+ feature = "Array subscripts"
38
+
39
+
30
40
# The parser and evaluation model here are based on the eval_arith example
31
41
# in the pyparsing docs:
32
42
# https://github.com/pyparsing/pyparsing/blob/master/examples/eval_arith.py
@@ -176,6 +186,17 @@ def make_bcftools_filter_parser(all_fields=None, map_vcf_identifiers=True):
176
186
vcf_prefixes = pp .Literal ("INFO/" ) | pp .Literal ("FORMAT/" ) | pp .Literal ("FMT/" )
177
187
vcf_identifier = pp .Combine (vcf_prefixes + identifier ) | identifier
178
188
189
+ # indexed_identifier = pp.Forward()
190
+ # indexed_identifier <<= identifier + (
191
+ # pp.Literal("[") + pp.common.integer + pp.Literal("]"))
192
+ # fn_call = (ident + lpar - Group(expr_list) + rpar).setParseAction(
193
+ # insert_fn_argcount_tuple
194
+ # )
195
+
196
+ # lbrack, rbrack = map(pp.Suppress, "[]")
197
+ # indexed_identifier = identifier + lbrack - pp.Group(pp.common.integer) + rbrack
198
+ # print(indexed_identifier)
199
+
179
200
name_mapper = _identity
180
201
if map_vcf_identifiers :
181
202
name_mapper = functools .partial (vcf_name_to_vcz_name , all_fields )
@@ -223,7 +244,10 @@ def __init__(self, *, field_names=None, include=None, exclude=None):
223
244
224
245
if expr is not None :
225
246
parser = make_bcftools_filter_parser (field_names )
226
- self .parse_result = parser .parse_string (expr , parse_all = True )
247
+ try :
248
+ self .parse_result = parser .parse_string (expr , parse_all = True )
249
+ except pp .ParseException as e :
250
+ raise ParseError (e ) from None
227
251
# This isn't a very good pattern, fix
228
252
self .referenced_fields = self .parse_result [0 ].referenced_fields ()
229
253
0 commit comments