diff options
author | Filipe Brandenburger <filbranden@google.com> | 2018-03-06 22:10:01 -0800 |
---|---|---|
committer | Filipe Brandenburger <filbranden@google.com> | 2018-05-23 14:07:38 -0700 |
commit | 86d2f57e86b39db4322cfe49417ed64a62cfdf83 (patch) | |
tree | 7478c01ad3827c203ec46c1b100be12ed757501f | |
parent | ecbeae565b1597f91f37513c258c077b1f54ad21 (diff) | |
download | meson-86d2f57e86b39db4322cfe49417ed64a62cfdf83.zip meson-86d2f57e86b39db4322cfe49417ed64a62cfdf83.tar.gz meson-86d2f57e86b39db4322cfe49417ed64a62cfdf83.tar.bz2 |
Add support for octal and binary int literals.
Simplify support for alternate bases using int(..., base=0) which
auto-detects it using the standard Python syntax for numbers.
Octal numbers are useful to specify permission bits and umasks.
Binary numbers are not super useful... But considering we get them for
free, let's allow them here too.
v2: Tweak the regex so it doesn't accept a decimal number with a leading
zero, which is invalid for int(..., base=0) and would raise a ValueError
if passed around.
-rw-r--r-- | mesonbuild/mparser.py | 8 | ||||
-rw-r--r-- | test cases/common/68 number arithmetic/meson.build | 12 |
2 files changed, 14 insertions, 6 deletions
diff --git a/mesonbuild/mparser.py b/mesonbuild/mparser.py index 78683be..72cf143 100644 --- a/mesonbuild/mparser.py +++ b/mesonbuild/mparser.py @@ -94,8 +94,7 @@ class Lexer: # Need to be sorted longest to shortest. ('ignore', re.compile(r'[ \t]')), ('id', re.compile('[_a-zA-Z][_0-9a-zA-Z]*')), - ('hexnumber', re.compile('0[xX][0-9a-fA-F]+')), - ('number', re.compile(r'\d+')), + ('number', re.compile(r'0[bB][01]+|0[oO][0-7]+|0[xX][0-9a-fA-F]+|0|[1-9]\d*')), ('eol_cont', re.compile(r'\\\n')), ('eol', re.compile(r'\n')), ('multiline_string', re.compile(r"'''(.|\n)*?'''", re.M)), @@ -187,10 +186,7 @@ This will become a hard error in a future Meson release.""", self.getline(line_s lineno += len(lines) - 1 line_start = mo.end() - len(lines[-1]) elif tid == 'number': - value = int(match_text) - elif tid == 'hexnumber': - tid = 'number' - value = int(match_text, base=16) + value = int(match_text, base=0) elif tid == 'eol' or tid == 'eol_cont': lineno += 1 line_start = loc diff --git a/test cases/common/68 number arithmetic/meson.build b/test cases/common/68 number arithmetic/meson.build index f2e84a8..e31d7e4 100644 --- a/test cases/common/68 number arithmetic/meson.build +++ b/test cases/common/68 number arithmetic/meson.build @@ -62,3 +62,15 @@ hex2_255 = 0XFF assert(hex_255 == 255, 'Hex parsing is broken.') assert(hex2_255 == 255, 'Uppercase hex parsing is broken.') + +bin_123 = 0b1111011 +bin2_123 = 0B1111011 + +assert(bin_123 == 123, 'Bin number parsing is broken.') +assert(bin2_123 == 123, 'Uppercase bin number parsing is broken.') + +oct_493 = 0o755 +oct2_493 = 0O755 + +assert(oct_493 == 493, 'Oct number parsing is broken.') +assert(oct2_493 == 493, 'Uppercase oct number parsing is broken.') |