From 7b59a2e3eae965594e907791fc6cbaa9e7e617b0 Mon Sep 17 00:00:00 2001 From: Dylan Baker Date: Thu, 24 Feb 2022 15:51:19 -0800 Subject: mparser: Use a literal for the ComparisonNode And fix a bug where `not in` is in the wrong order. --- mesonbuild/mparser.py | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) (limited to 'mesonbuild/mparser.py') diff --git a/mesonbuild/mparser.py b/mesonbuild/mparser.py index 6bb08a7..b9caf9c 100644 --- a/mesonbuild/mparser.py +++ b/mesonbuild/mparser.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +from __future__ import annotations from dataclasses import dataclass import re import codecs @@ -20,6 +21,8 @@ from .mesonlib import MesonException from . import mlog if T.TYPE_CHECKING: + from typing_extensions import Literal + from .ast import AstVisitor # This is the regex for the supported escape sequences of a regular string @@ -386,11 +389,11 @@ class AndNode(BaseNode): self.right = right # type: BaseNode class ComparisonNode(BaseNode): - def __init__(self, ctype: str, left: BaseNode, right: BaseNode): + def __init__(self, ctype: COMPARISONS, left: BaseNode, right: BaseNode): super().__init__(left.lineno, left.colno, left.filename) self.left = left # type: BaseNode self.right = right # type: BaseNode - self.ctype = ctype # type: str + self.ctype = ctype class ArithmeticNode(BaseNode): def __init__(self, operation: str, left: BaseNode, right: BaseNode): @@ -475,15 +478,19 @@ class TernaryNode(BaseNode): self.trueblock = trueblock # type: BaseNode self.falseblock = falseblock # type: BaseNode -comparison_map = {'equal': '==', - 'nequal': '!=', - 'lt': '<', - 'le': '<=', - 'gt': '>', - 'ge': '>=', - 'in': 'in', - 'notin': 'not in', - } +if T.TYPE_CHECKING: + COMPARISONS = Literal['==', '!=', '<', '<=', '>=', '>', 'in', 'notin'] + +comparison_map: T.Mapping[str, COMPARISONS] = { + 'equal': '==', + 'nequal': '!=', + 'lt': '<', + 'le': '<=', + 'gt': '>', + 'ge': '>=', + 'in': 'in', + 'not in': 'notin', +} # Recursive descent parser for Meson's definition language. # Very basic apart from the fact that we have many precedence -- cgit v1.1