Skip to content

Commit

Permalink
Merge pull request #2568 from hankluo6/leading_zero
Browse files Browse the repository at this point in the history
Handling leading zeros in integer literals
  • Loading branch information
Shaikh-Ubaid authored Mar 8, 2024
2 parents 77baa1f + 0df7765 commit 8ad6975
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/lpython/parser/tokenizer.re
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 2 additions & 0 deletions tests/errors/test_literal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def test_literal1():
x: i32 = 0123
13 changes: 13 additions & 0 deletions tests/reference/tokens-test_literal-e20c024.json
Original file line number Diff line number Diff line change
@@ -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
}
5 changes: 5 additions & 0 deletions tests/reference/tokens-test_literal-e20c024.stderr
Original file line number Diff line number Diff line change
@@ -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
| ^^^^
4 changes: 4 additions & 0 deletions tests/tests.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8ad6975

Please sign in to comment.