Daniele Varrazzo avatar Daniele Varrazzo committed b7c2458

Adding a Pl/pgSQL lexer

Comments (0)

Files changed (2)

pygments/lexers/_postgres_builtins.py

     'void', 'opaque',
     ]
 
+PLPGSQL_KEYWORDS = [
+    'ALIAS', 'CONSTANT', 'DIAGNOSTICS', 'ELSIF', 'EXCEPTION', 'EXIT',
+    'FOREACH', 'GET', 'LOOP', 'NOTICE', 'OPEN', 'PERFORM', 'QUERY', 'RAISE',
+    'RETURN', 'REVERSE', 'SQLSTATE', 'WHILE',
+    ]
+
 if __name__ == '__main__':
     update_myself()
 

pygments/lexers/postgres.py

 from pygments.lexers import get_lexer_by_name, ClassNotFound
 
 from pygments.lexers._postgres_builtins import (
-    KEYWORDS, DATATYPES, PSEUDO_TYPES)
+    KEYWORDS, DATATYPES, PSEUDO_TYPES, PLPGSQL_KEYWORDS)
 
 
-__all__ = [ 'PostgresLexer', 'PostgresConsoleLexer' ]
+__all__ = [ 'PostgresLexer', 'PlPgsqlLexer', 'PostgresConsoleLexer' ]
 
 line_re  = re.compile('.*?\n')
 
     }
 
 
+class PlPgsqlLexer(PostgresLexer):
+    """
+    Handle the extra syntax in Pl/pgSQL language.
+    """
+    name = 'PL/pgSQL'
+    aliases = ['plpgsql']
+    mimetypes = ['text/x-plpgsql']
+    tokens = deepcopy(PostgresLexer.tokens)
+
+    # extend the keywords list
+    for i, pattern in enumerate(tokens['root']):
+        if pattern[1] == Keyword:
+            tokens['root'][i] = (
+                r'(' + '|'.join(KEYWORDS + PLPGSQL_KEYWORDS) + r')\b',
+                Keyword)
+            del i
+            break
+    else:
+        assert 0, "SQL keywords not found"
+
+    tokens['root'][:0] = [
+        (r'\%[a-z][a-z0-9_]*\b', Name.Builtin),     # actually, a datatype
+        (r':=', Operator),
+        (r'\<\<[a-z][a-z0-9_]*\>\>', Name.Label),
+        (r'\#[a-z][a-z0-9_]*\b', Keyword.Pseudo),   # #variable_conflict
+    ]
+
+
 class PsqlRegexLexer(PostgresLexer):
     """
     Extend the PostgresLexer adding support specific for psql commands.
         (r"[^\s]+", String.Symbol),
     ]
 
-
 re_prompt = re.compile(r'^.*?[=\-\(][#>]')
 re_psql_command = re.compile(r'\s*\\')
 re_end_command = re.compile(r';\s*(--.*?)?$')
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.