From 3e2e5cb17decfa8bea927503c3a548460b18adb3 Mon Sep 17 00:00:00 2001 From: hankluo6 Date: Thu, 7 Mar 2024 18:14:50 -0600 Subject: [PATCH 1/3] Handling leading zeros in integer literals --- src/lpython/parser/tokenizer.re | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/lpython/parser/tokenizer.re b/src/lpython/parser/tokenizer.re index 64b0488b76..894e253e6e 100644 --- a/src/lpython/parser/tokenizer.re +++ b/src/lpython/parser/tokenizer.re @@ -96,6 +96,10 @@ void lex_int(Allocator &al, const unsigned char *s, 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; } From 197d6cac59c4acfab57a7e98a323b933637214f9 Mon Sep 17 00:00:00 2001 From: hankluo6 Date: Thu, 7 Mar 2024 18:16:35 -0600 Subject: [PATCH 2/3] Remove parentheses for consistency --- src/lpython/parser/tokenizer.re | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lpython/parser/tokenizer.re b/src/lpython/parser/tokenizer.re index 894e253e6e..85d572752b 100644 --- a/src/lpython/parser/tokenizer.re +++ b/src/lpython/parser/tokenizer.re @@ -89,7 +89,7 @@ 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); From 0df77658f705cd71293182b39e2358c92c145449 Mon Sep 17 00:00:00 2001 From: hankluo6 Date: Thu, 7 Mar 2024 18:18:57 -0600 Subject: [PATCH 3/3] Add test --- tests/errors/test_literal.py | 2 ++ tests/reference/tokens-test_literal-e20c024.json | 13 +++++++++++++ tests/reference/tokens-test_literal-e20c024.stderr | 5 +++++ tests/tests.toml | 4 ++++ 4 files changed, 24 insertions(+) create mode 100644 tests/errors/test_literal.py create mode 100644 tests/reference/tokens-test_literal-e20c024.json create mode 100644 tests/reference/tokens-test_literal-e20c024.stderr 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