Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tokenizer doesn't handle numeric literals if they start with 0 #331

Closed
Y-Nak opened this issue Mar 23, 2021 · 0 comments · Fixed by #334
Closed

tokenizer doesn't handle numeric literals if they start with 0 #331

Y-Nak opened this issue Mar 23, 2021 · 0 comments · Fixed by #334

Comments

@Y-Nak
Copy link
Member

Y-Nak commented Mar 23, 2021

What is wrong?

Tokenizer doesn't tokenize correctly if numeric literals start with 0 followed by non zero value.
For example,

02

is tokenized into

    [
        Token {
            typ: NUMBER,
            string: "0",
            span: Span {
                start: 0,
                end: 1,
            },
            line: "02\n",
        },
        Token {
            typ: NUMBER,
            string: "2",
            span: Span {
                start: 1,
                end: 2,
            },
            line: "02\n",
        },
        Token {
            typ: NEWLINE,
            string: "\n",
            span: Span {
                start: 2,
                end: 3,
            },
            line: "02\n",
        },
        Token {
            typ: ENDMARKER,
            string: "",
            span: Span {
                start: 3,
                end: 3,
            },
            line: "",
        },
    ],

Also, it would be better to discuss spec of number literals(sorry if it has been discussed already).
i.e. currently, tokenizer allows hex/bin/octal/decimal numbers while analyzer allows only decimal numbers. This seems inconsistent between them.

EDIT: opened #333 for further discussion of numeric literal representations.

How can it be fixed

It would be fixed by tweaking the code below

pub const HEXNUMBER: &str = r"0[xX](?:_?[0-9a-fA-F])+";
pub const BINNUMBER: &str = r"0[bB](?:_?[01])+";
pub const OCTNUMBER: &str = r"0[oO](?:_?[0-7])+";
pub const DECNUMBER: &str = r"(?:0(?:_?0)*|[1-9](?:_?[0-9])*)";
.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant