Commits

Daniele Varrazzo  committed 2fd3074

Better psql commands parser

Recognize strings and quoted variables. Handled as a separate state.

  • Participants
  • Parent commits 2ce0217

Comments (0)

Files changed (2)

File pygments/lexers/postgres.py

             (r'"(""|[^"])*"', String.Name), # quoted identifier
             (r'[a-zA-Z_][a-zA-Z0-9_]*', Name),
 
-            # psql backslash command and variable
-            # These actually belongs to the console lexer,
-            # but putting it here makes things easier.
-            (r'\\.*?\n', Name),             # TODO: what is a good token?
-            (r':[a-z][a-z0-9_]*\b', Name.Variable),
+            # TODO: consider splitting the regex parser
+            (r'\\[^\s]+', Keyword.Pseudo, 'psql-command'),
+            # psql variable in SQL
+            (r""":(['"]?)[a-z][a-z0-9_]*\b\1""", Name.Variable),
 
             (r'[;:()\[\],\.]', Punctuation),
         ],
             (r'\*/', Comment.Multiline, '#pop'),
             (r'[^/\*]+', Comment.Multiline),
             (r'[/*]', Comment.Multiline)
+        ],
+        'psql-command': [
+            (r'\n', Text, 'root'),
+            (r'\s+', Text),
+            (r""":(['"]?)[a-z][a-z0-9_]*\b\1""", Name.Variable),
+            (r"'(''|[^'])*'", String.Single),
+            (r"`([^`])*`", String.Backtick),
+            (r"[^\s]+", String.Symbol),
         ]
     }
 

File tests/examplefiles/psql_session.txt

   20 | 
 (2 rows)
 
+testdb=> \set foo 'my_table'
+testdb=> SELECT * FROM :"foo";
+
+testdb=> \set content `cat my_file.txt`
+testdb=> INSERT INTO my_table VALUES (:'content');
 
 regression=# select (
 regression(# 1);