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 fails on expression ending in integer '0' #146

Open
benmaier opened this issue Mar 28, 2024 · 0 comments
Open

Tokenizer fails on expression ending in integer '0' #146

benmaier opened this issue Mar 28, 2024 · 0 comments

Comments

@benmaier
Copy link

Minimal example reproducing bug

In [1]: from javalang.tokenizer import tokenize

In [2]: list(tokenize("a == 0"))
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[3], line 1
----> 1 list(tokenize("a == 0"))

File .\env\Lib\site-packages\javalang\tokenizer.py:538, in JavaTokenizer.tokenize(self)
    535     self.read_string()
    537 elif c in '0123456789':
--> 538     token_type = self.read_integer_or_float(c, c_next)
    540 elif self.is_java_identifier_start(c):
    541     token_type = self.read_identifier()

File .\env\Lib\site-packages\javalang\tokenizer.py:370, in JavaTokenizer.read_integer_or_float(self, c, c_next)
    369 def read_integer_or_float(self, c, c_next):
--> 370     if c == '0' and c_next in 'xX':
    371         return self.read_hex_integer_or_float()
    372     elif c == '0' and c_next in 'bB':

TypeError: 'in <string>' requires string as left operand, not NoneType

Expected behavior

In [1]: from javalang.tokenizer import tokenize

In [2]: list(tokenize("a == 0"))
Out[2]: 
[Identifier "a" line 1, position 1,
 Operator "==" line 1, position 3,
 DecimalInteger "0" line 1, position 6]

Source of problem

Lies in the expectation that an integer of '0' would always be followed by another token (this is done to check for possible hexadecimal or binary notation), see https://github.com/c2nes/javalang/blob/master/javalang/tokenizer.py#L370 . But this does not have to be the case.

Suggested fix

catch None for c_next in read_integer_or_float

    def read_integer_or_float(self, c, c_next):
        if c == '0' and c_next is None:
            return self.read_decimal_float_or_integer()
        elif c == '0' and c_next in 'xX':
            return self.read_hex_integer_or_float()
        elif c == '0' and c_next in 'bB':
            self.read_bin_integer()
            return BinaryInteger
        elif c == '0' and c_next in '01234567':
            self.read_octal_integer()
            return OctalInteger
        else:
            return self.read_decimal_float_or_integer()
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

No branches or pull requests

1 participant