add pygments lexer to colorize query strings (closes #34252)
authorNicolas Chauvat <nicolas.chauvat@logilab.fr>
Mon, 16 Dec 2013 15:31:56 +0100
changeset 725 0c8840b3be04
parent 724 3d5a9d57cf95
child 729 17e72a50aca2
add pygments lexer to colorize query strings (closes #34252)
debian/control
pygments_ext.py
--- a/debian/control	Mon Oct 21 18:23:31 2013 +0200
+++ b/debian/control	Mon Dec 16 15:31:56 2013 +0100
@@ -22,6 +22,7 @@
          python-logilab-database (>= 1.6.0)
 Conflicts: cubicweb-common (<= 3.13.9)
 Provides: ${python:Provides}
+Suggests: python-pygments
 Description: relationship query language (RQL) utilities
  A library providing the base utilities to handle RQL queries,
  such as a parser, a type inferencer.
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pygments_ext.py	Mon Dec 16 15:31:56 2013 +0100
@@ -0,0 +1,47 @@
+# -*- coding: utf-8 -*-
+"""
+    pygments.lexers.rql
+    ~~~~~~~~~~~~~~~~~~~
+
+    Lexer for RQL the relation query language
+
+    http://www.logilab.org/project/rql
+"""
+
+import re
+
+from pygments.lexer import RegexLexer, _mapping
+from pygments.token import Punctuation, \
+     Text, Comment, Operator, Keyword, Name, String, Number
+
+__all__ = ['RqlLexer']
+
+class RqlLexer(RegexLexer):
+    """
+    Lexer for Relation Query Language.
+    """
+
+    name = 'RQL'
+    aliases = ['rql']
+    filenames = ['*.rql']
+    mimetypes = ['text/x-rql']
+
+    flags = re.IGNORECASE
+    tokens = {
+        'root': [
+            (r'\s+', Text),
+            (r'(DELETE|SET|INSERT|UNION|DISTINCT|WITH|WHERE|BEING|OR'
+             r'|AND|NOT|GROUPBY|HAVING|ORDERBY|ASC|DESC|LIMIT|OFFSET'
+             r'|TODAY|NOW|TRUE|FALSE|NULL|EXISTS)\b', Keyword),
+            (r'[+*/<>=%-]', Operator),
+            (r'(Any|is|instance_of)\b', Name.Builtin),
+            (r'[0-9]+', Number.Integer),
+            (r'[A-Z_][A-Z0-9_]*\??', Name),
+            (r"'(''|[^'])*'", String.Single),
+            (r'"(""|[^"])*"', String.Single),
+            (r'[;:()\[\],\.]', Punctuation)
+        ],
+    }
+
+_mapping.LEXERS['RqlLexer'] = ('rql.pygments_ext', 'RQL', ('rql',), ('*.rql',), ('text/x-rql',))
+