cfg: fix compatibility with pyparsing 2.4.7

I was developing this with 3.0.1. Seems to work fine after some simple
adjustments.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2022-07-12 11:26:45 +02:00
parent bca49d9143
commit d231aa7d0b
3 changed files with 9 additions and 6 deletions

View file

@ -1,3 +1,4 @@
jinja2
pyparsing
requests
tqdm

View file

@ -5,9 +5,9 @@ import platform
import sys
import pyparsing as pp
from pyparsing.exceptions import ParseException
from pyparsing import ParseException
pp.ParserElement.enable_packrat()
pp.ParserElement.enablePackrat()
# ConfigurationPredicate :
# ConfigurationOption
@ -33,14 +33,14 @@ pp.ParserElement.enable_packrat()
IDENT_CHARS = pp.alphas + '_', pp.alphanums + '_'
def _call(word, arg):
return pp.Group(pp.Literal(word) + pp.Suppress('(') + arg + pp.Suppress(')'), aslist=True)
return pp.Group(pp.Literal(word) + pp.Suppress('(') + arg + pp.Suppress(')'))
@functools.cache
def cfg_grammar():
pred = pp.Forward()
ident = pp.Word(IDENT_CHARS[0], IDENT_CHARS[1])
option = pp.Group(ident + pp.Optional(pp.Suppress('=') + pp.quotedString), aslist=True)
option = pp.Group(ident + pp.Optional(pp.Suppress('=') + pp.quotedString))
not_ = _call('not', pred)
@ -96,6 +96,8 @@ def evaluate_variable(name):
def evaluate(expr, nested=False):
# print(f'evaluate: {expr}')
if hasattr(expr, 'asList'):
expr = expr.asList() # compat with pyparsing 2.7.x
match expr:
case ['cfg', expr] if not nested:
return evaluate(expr, True)
@ -116,5 +118,5 @@ def evaluate(expr, nested=False):
raise ValueError
def parse_and_evaluate(expr):
parsed = cfg_grammar().parse_string(expr)
parsed = cfg_grammar().parseString(expr)
return evaluate(parsed[0])

View file

@ -5,7 +5,7 @@ from rust2rpm import cfg
def test_pyparsing_run_tests():
g = cfg.cfg_grammar()
g.run_tests("""\
g.runTests("""\
cfg(target_os = "macos")
cfg(any(foo, bar))
cfg(all(unix, target_pointer_width = "32"))