From 5707d390174b7e8db431bc9cee2257043ea59f84 Mon Sep 17 00:00:00 2001 From: Charles Brunet Date: Wed, 23 Aug 2023 09:32:21 -0400 Subject: [PATCH] parser: preserve number base --- mesonbuild/cargo/builder.py | 2 +- mesonbuild/cmake/interpreter.py | 2 +- mesonbuild/mparser.py | 12 ++++++++++-- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/mesonbuild/cargo/builder.py b/mesonbuild/cargo/builder.py index fb086d164..2442fbfa7 100644 --- a/mesonbuild/cargo/builder.py +++ b/mesonbuild/cargo/builder.py @@ -45,7 +45,7 @@ def number(value: int, filename: str) -> mparser.NumberNode: :param filename: the file that the value came from :return: A NumberNode """ - return mparser.NumberNode(_token('number', filename, value)) + return mparser.NumberNode(_token('number', filename, str(value))) def bool(value: builtins.bool, filename: str) -> mparser.BooleanNode: diff --git a/mesonbuild/cmake/interpreter.py b/mesonbuild/cmake/interpreter.py index f88d091ab..f577ba774 100644 --- a/mesonbuild/cmake/interpreter.py +++ b/mesonbuild/cmake/interpreter.py @@ -966,7 +966,7 @@ class CMakeInterpreter: return IdNode(token(val=value)) def number(value: int) -> NumberNode: - return NumberNode(token(val=value)) + return NumberNode(token(val=str(value))) def nodeify(value: TYPE_mixed_list) -> BaseNode: if isinstance(value, str): diff --git a/mesonbuild/mparser.py b/mesonbuild/mparser.py index 9867c7164..5ab76e7dc 100644 --- a/mesonbuild/mparser.py +++ b/mesonbuild/mparser.py @@ -217,7 +217,7 @@ class Lexer: lineno += len(lines) - 1 line_start = mo.end() - len(lines[-1]) elif tid == 'number': - value = int(match_text, base=0) + value = match_text elif tid == 'eol_cont': lineno += 1 line_start = loc @@ -285,8 +285,16 @@ class IdNode(ElementaryNode[str]): def __str__(self) -> str: return "Id node: '%s' (%d, %d)." % (self.value, self.lineno, self.colno) +@dataclass(unsafe_hash=True) class NumberNode(ElementaryNode[int]): - pass + + raw_value: str = field(hash=False) + + def __init__(self, token: Token[str]): + BaseNode.__init__(self, token.lineno, token.colno, token.filename) + self.raw_value = token.value + self.value = int(token.value, base=0) + self.bytespan = token.bytespan class StringNode(ElementaryNode[str]): def __str__(self) -> str: