Skip to content

Commit 8ad6975

Browse files
authored
Merge pull request #2568 from hankluo6/leading_zero
Handling leading zeros in integer literals
2 parents 77baa1f + 0df7765 commit 8ad6975

File tree

5 files changed

+29
-1
lines changed

5 files changed

+29
-1
lines changed

src/lpython/parser/tokenizer.re

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,17 @@ void lex_int(Allocator &al, const unsigned char *s,
8989
s = s + 2;
9090
uint64_t n = get_value((char*)s, 2, loc);
9191
u.from_smallint(n);
92-
} else if ((std::tolower(s[1]) == 'o')) {
92+
} else if (std::tolower(s[1]) == 'o') {
9393
// Oct
9494
s = s + 2;
9595
uint64_t n = get_value((char*)s, 8, loc);
9696
u.from_smallint(n);
9797
} else {
9898
lex_dec_int_large(al, s, e, u);
99+
if (s[0] == '0' && u.n != 0) {
100+
throw parser_local::TokenizerError(
101+
"Leading zeros in decimal integer are not allowed", {loc});
102+
}
99103
}
100104
return;
101105
}

tests/errors/test_literal.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def test_literal1():
2+
x: i32 = 0123
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"basename": "tokens-test_literal-e20c024",
3+
"cmd": "lpython --no-color --show-tokens {infile} -o {outfile}",
4+
"infile": "tests/errors/test_literal.py",
5+
"infile_hash": "ac9e219faa40c6554983087e8ac198c323f2e5af284b8689f5608f76",
6+
"outfile": null,
7+
"outfile_hash": null,
8+
"stdout": null,
9+
"stdout_hash": null,
10+
"stderr": "tokens-test_literal-e20c024.stderr",
11+
"stderr_hash": "58fe8b74550bd6f81761b173626f3d45eaae346665862f1b085eebe8",
12+
"returncode": 1
13+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
tokenizer error: Leading zeros in decimal integer are not allowed
2+
--> tests/errors/test_literal.py:2:14
3+
|
4+
2 | x: i32 = 0123
5+
| ^^^^

tests/tests.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1197,6 +1197,10 @@ tokens = true
11971197
filename = "tokens/errors/indent3.py"
11981198
tokens = true
11991199

1200+
[[test]]
1201+
filename = "errors/test_literal.py"
1202+
tokens = true
1203+
12001204
[[test]]
12011205
filename = "errors/kwargs_01_error.py"
12021206
asr = true

0 commit comments

Comments
 (0)