Skip to content

Commit

Permalink
Fix parsing of scientific numbers (fixes #399).
Browse files Browse the repository at this point in the history
  • Loading branch information
andialbrecht committed Jul 14, 2021
1 parent 23d2993 commit e660467
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Expand Up @@ -9,6 +9,7 @@ Enhancements
Bug Fixes

* Fix parsing of backticks (issue588).
* Fix parsing of scientific number (issue399).


Release 0.4.1 (Oct 08, 2020)
Expand Down
2 changes: 1 addition & 1 deletion sqlparse/keywords.py
Expand Up @@ -62,7 +62,7 @@ def is_keyword(value):
(r'(?<=\.)[A-ZÀ-Ü]\w*', tokens.Name), # .'Name'
(r'[A-ZÀ-Ü]\w*(?=\()', tokens.Name), # side effect: change kw to func
(r'-?0x[\dA-F]+', tokens.Number.Hexadecimal),
(r'-?\d*(\.\d+)?E-?\d+', tokens.Number.Float),
(r'-?\d+(\.\d+)?E-?\d+', tokens.Number.Float),
(r'(?![_A-ZÀ-Ü])-?(\d+(\.\d*)|\.\d+)(?![_A-ZÀ-Ü])',
tokens.Number.Float),
(r'(?![_A-ZÀ-Ü])-?\d+(?![_A-ZÀ-Ü])', tokens.Number.Integer),
Expand Down
14 changes: 10 additions & 4 deletions tests/test_parse.py
Expand Up @@ -188,11 +188,16 @@ def test_placeholder(ph):
assert p[0].ttype is T.Name.Placeholder


@pytest.mark.parametrize('num', ['6.67428E-8', '1.988e33', '1e-12'])
def test_scientific_numbers(num):
@pytest.mark.parametrize('num, expected', [
('6.67428E-8', T.Number.Float),
('1.988e33', T.Number.Float),
('1e-12', T.Number.Float),
('e1', None),
])
def test_scientific_numbers(num, expected):
p = sqlparse.parse(num)[0].tokens
assert len(p) == 1
assert p[0].ttype is T.Number.Float
assert p[0].ttype is expected


def test_single_quotes_are_strings():
Expand Down Expand Up @@ -336,7 +341,8 @@ def test_pprint():
"| | `- 0 Name 'd0'",
"| |- 10 Punctuation ','",
"| |- 11 Whitespace ' '",
"| `- 12 Float 'e0'",
"| `- 12 Identifier 'e0'",
"| `- 0 Name 'e0'",
"|- 3 Whitespace ' '",
"|- 4 Keyword 'from'",
"|- 5 Whitespace ' '",
Expand Down

0 comments on commit e660467

Please sign in to comment.