Commits

Daniele Varrazzo  committed 5f51139

Regex part of the psql lexer split from the Postgres SQL lexer

  • Participants
  • Parent commits 4c6fe77

Comments (0)

Files changed (1)

File pygments/lexers/postgres.py

 
 import re
 import sys
+from copy import deepcopy
 
 from pygments.lexer import Lexer, RegexLexer, include, bygroups, using, \
      this, do_insertions
             (r'(?ms)(\$[^\$]*\$)(.*?)(\1)', language_callback),
             (r'[a-zA-Z_][a-zA-Z0-9_]*', Name),
 
-            # TODO: consider splitting the psql parser
-            (r'\\[^\s]+', Keyword.Pseudo, 'psql-command'),
             # psql variable in SQL
             (r""":(['"]?)[a-z][a-z0-9_]*\b\1""", Name.Variable),
 
             (r'[^/\*]+', Comment.Multiline),
             (r'[/*]', Comment.Multiline)
         ],
-        'psql-command': [
-            (r'\n', Text, 'root'),
-            (r'\s+', Text),
-            (r'\\[^\s]+', Keyword.Pseudo),
-            (r""":(['"]?)[a-z][a-z0-9_]*\b\1""", Name.Variable),
-            (r"'(''|[^'])*'", String.Single),
-            (r"`([^`])*`", String.Backtick),
-            (r"[^\s]+", String.Symbol),
-        ]
     }
 
+
+class PsqlRegexLexer(PostgresLexer):
+    """
+    Extend the PostgresLexer adding support specific for psql commands.
+
+    This is not a complete psql lexer yet as it lacks prompt support
+    and output rendering.
+    """
+    name = 'PostgreSQL console - regexp based lexer'
+    aliases = []    # not public
+    tokens = deepcopy(PostgresLexer.tokens)
+    tokens['root'].append(
+        (r'\\[^\s]+', Keyword.Pseudo, 'psql-command'))
+    tokens['psql-command'] = [
+        (r'\n', Text, 'root'),
+        (r'\s+', Text),
+        (r'\\[^\s]+', Keyword.Pseudo),
+        (r""":(['"]?)[a-z][a-z0-9_]*\b\1""", Name.Variable),
+        (r"'(''|[^'])*'", String.Single),
+        (r"`([^`])*`", String.Backtick),
+        (r"[^\s]+", String.Symbol),
+    ]
+
+
 re_prompt = re.compile(r'^.*?[=\-\(][#>]')
 
 re_psql_command = re.compile(r'(\s*)(\\.+?)(\s+)$')
     mimetypes = ['text/x-postgresql-psql']
 
     def get_tokens_unprocessed(self, data):
-        sql = PostgresLexer(**self.options)
+        sql = PsqlRegexLexer(**self.options)
 
         curcode = ''
         insertions = []