diff options
author | ptmcg <ptmcg@austin.rr.com> | 2020-07-07 00:17:41 -0500 |
---|---|---|
committer | ptmcg <ptmcg@austin.rr.com> | 2020-07-07 00:17:41 -0500 |
commit | 5c0607027846ba830eb72fb32c82e9152bf35295 (patch) | |
tree | a8b7635d71b6ac6c62fd893b5bb6601e82d0add4 | |
parent | c104123d984ae1f7ac81046f78e2bf6421e30e56 (diff) | |
download | pyparsing-git-5c0607027846ba830eb72fb32c82e9152bf35295.tar.gz |
infixNotation unit tests require infixNotation bug fixes!
-rw-r--r-- | pyparsing/helpers.py | 25 |
1 files changed, 11 insertions, 14 deletions
diff --git a/pyparsing/helpers.py b/pyparsing/helpers.py index a4c042c..04b1899 100644 --- a/pyparsing/helpers.py +++ b/pyparsing/helpers.py @@ -687,15 +687,22 @@ def infixNotation(baseExpr, opList, lpar=Suppress("("), rpar=Suppress(")")): lastExpr = baseExpr | (lpar + ret + rpar) for i, operDef in enumerate(opList): opExpr, arity, rightLeftAssoc, pa = (operDef + (None,))[:4] - termName = "%s term" % opExpr if arity < 3 else "%s%s term" % opExpr if arity == 3: - if opExpr is None or len(opExpr) != 2: + if not isinstance(opExpr, (tuple, list)) or len(opExpr) != 2: raise ValueError( "if numterms=3, opExpr must be a tuple or list of two expressions" ) opExpr1, opExpr2 = opExpr + + if not 1 <= arity <= 3: + raise ValueError("operator must be unary (1), binary (2), or ternary (3)") + + if rightLeftAssoc not in (opAssoc.LEFT, opAssoc.RIGHT): + raise ValueError("operator must indicate right or left associativity") + + termName = "%s term" % opExpr if arity < 3 else "%s%s term" % opExpr thisExpr = Forward().setName(termName) - if rightLeftAssoc == opAssoc.LEFT: + if rightLeftAssoc is opAssoc.LEFT: if arity == 1: matchExpr = _FB(lastExpr + opExpr) + Group(lastExpr + OneOrMore(opExpr)) elif arity == 2: @@ -711,11 +718,7 @@ def infixNotation(baseExpr, opList, lpar=Suppress("("), rpar=Suppress(")")): matchExpr = _FB( lastExpr + opExpr1 + lastExpr + opExpr2 + lastExpr ) + Group(lastExpr + OneOrMore(opExpr1 + lastExpr + opExpr2 + lastExpr)) - else: - raise ValueError( - "operator must be unary (1), binary (2), or ternary (3)" - ) - elif rightLeftAssoc == opAssoc.RIGHT: + elif rightLeftAssoc is opAssoc.RIGHT: if arity == 1: # try to avoid LR with this extra test if not isinstance(opExpr, Optional): @@ -734,12 +737,6 @@ def infixNotation(baseExpr, opList, lpar=Suppress("("), rpar=Suppress(")")): matchExpr = _FB( lastExpr + opExpr1 + thisExpr + opExpr2 + thisExpr ) + Group(lastExpr + opExpr1 + thisExpr + opExpr2 + thisExpr) - else: - raise ValueError( - "operator must be unary (1), binary (2), or ternary (3)" - ) - else: - raise ValueError("operator must indicate right or left associativity") if pa: if isinstance(pa, (tuple, list)): matchExpr.setParseAction(*pa) |