From 15b0cb9e75ca378e94b55b7f1ff23108f0899cde Mon Sep 17 00:00:00 2001 From: Andi Albrecht Date: Sun, 1 Mar 2015 11:52:34 +0100 Subject: Allow identifiers to start with an underscore (fixes #175). --- CHANGES | 1 + sqlparse/lexer.py | 3 +-- tests/test_parse.py | 9 +++++++++ 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/CHANGES b/CHANGES index 24f3858..36842bc 100644 --- a/CHANGES +++ b/CHANGES @@ -7,6 +7,7 @@ Bug Fixes * Add missing SQL types (issue154, issue155, issue156, by jukebox). * Fix parsing of multi-line comments (issue172, by JacekPliszka). * Fix parsing of escaped backslashes (issue174, by caseyching). +* Fix parsing of identifiers starting with underscore (issue175). Enhancements * Improve formatting of HAVING statements. diff --git a/sqlparse/lexer.py b/sqlparse/lexer.py index 5282ad3..999eb2c 100644 --- a/sqlparse/lexer.py +++ b/sqlparse/lexer.py @@ -202,7 +202,7 @@ class Lexer(object): (r'CREATE(\s+OR\s+REPLACE)?\b', tokens.Keyword.DDL), (r'DOUBLE\s+PRECISION\b', tokens.Name.Builtin), (r'(?<=\.)[^\W\d_]\w*', tokens.Name), - (r'[^\W\d_]\w*', is_keyword), + (r'[^\W\d]\w*', is_keyword), (r'[;:()\[\],\.]', tokens.Punctuation), (r'[<>=~!]+', tokens.Operator.Comparison), (r'[+/@#%^&|`?^-]+', tokens.Operator), @@ -292,7 +292,6 @@ class Lexer(object): for rexmatch, action, new_state in statetokens: m = rexmatch(text, pos) if m: - # print rex.pattern value = m.group() if value in known_names: yield pos, known_names[value], value diff --git a/tests/test_parse.py b/tests/test_parse.py index 24eea2d..ad5d2db 100644 --- a/tests/test_parse.py +++ b/tests/test_parse.py @@ -145,6 +145,15 @@ def test_quoted_identifier(): assert t[2].get_real_name() == 'y' +@pytest.mark.parametrize('name', [ + 'foo', + '_foo', +]) +def test_valid_identifier_names(name): # issue175 + t = sqlparse.parse(name)[0].tokens + assert isinstance(t[0], sqlparse.sql.Identifier) + + def test_psql_quotation_marks(): # issue83 # regression: make sure plain $$ work t = sqlparse.split(""" -- cgit v1.2.1