diff --git a/src/lpython/parser/tokenizer.re b/src/lpython/parser/tokenizer.re index 64b0488b76..85d572752b 100644 --- a/src/lpython/parser/tokenizer.re +++ b/src/lpython/parser/tokenizer.re @@ -89,13 +89,17 @@ void lex_int(Allocator &al, const unsigned char *s, s = s + 2; uint64_t n = get_value((char*)s, 2, loc); u.from_smallint(n); - } else if ((std::tolower(s[1]) == 'o')) { + } else if (std::tolower(s[1]) == 'o') { // Oct s = s + 2; uint64_t n = get_value((char*)s, 8, loc); u.from_smallint(n); } else { lex_dec_int_large(al, s, e, u); + if (s[0] == '0' && u.n != 0) { + throw parser_local::TokenizerError( + "Leading zeros in decimal integer are not allowed", {loc}); + } } return; } diff --git a/tests/errors/test_literal.py b/tests/errors/test_literal.py new file mode 100644 index 0000000000..3800abf26a --- /dev/null +++ b/tests/errors/test_literal.py @@ -0,0 +1,2 @@ +def test_literal1(): + x: i32 = 0123 \ No newline at end of file diff --git a/tests/reference/tokens-test_literal-e20c024.json b/tests/reference/tokens-test_literal-e20c024.json new file mode 100644 index 0000000000..de80ac4c4e --- /dev/null +++ b/tests/reference/tokens-test_literal-e20c024.json @@ -0,0 +1,13 @@ +{ + "basename": "tokens-test_literal-e20c024", + "cmd": "lpython --no-color --show-tokens {infile} -o {outfile}", + "infile": "tests/errors/test_literal.py", + "infile_hash": "ac9e219faa40c6554983087e8ac198c323f2e5af284b8689f5608f76", + "outfile": null, + "outfile_hash": null, + "stdout": null, + "stdout_hash": null, + "stderr": "tokens-test_literal-e20c024.stderr", + "stderr_hash": "58fe8b74550bd6f81761b173626f3d45eaae346665862f1b085eebe8", + "returncode": 1 +} \ No newline at end of file diff --git a/tests/reference/tokens-test_literal-e20c024.stderr b/tests/reference/tokens-test_literal-e20c024.stderr new file mode 100644 index 0000000000..977e7c1fd1 --- /dev/null +++ b/tests/reference/tokens-test_literal-e20c024.stderr @@ -0,0 +1,5 @@ +tokenizer error: Leading zeros in decimal integer are not allowed + --> tests/errors/test_literal.py:2:14 + | +2 | x: i32 = 0123 + | ^^^^ diff --git a/tests/tests.toml b/tests/tests.toml index 972c72bf91..b5d59c07e1 100644 --- a/tests/tests.toml +++ b/tests/tests.toml @@ -1197,6 +1197,10 @@ tokens = true filename = "tokens/errors/indent3.py" tokens = true +[[test]] +filename = "errors/test_literal.py" +tokens = true + [[test]] filename = "errors/kwargs_01_error.py" asr = true