Daniele Varrazzo avatar Daniele Varrazzo committed 5f51139

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

Comments (0)

Files changed (1)

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 = []
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.