Skip to content

Commit

Permalink
Add qlik lexer
Browse files Browse the repository at this point in the history
  • Loading branch information
GDownAdviseInc committed Oct 21, 2021
1 parent a98980b commit 4afd47c
Show file tree
Hide file tree
Showing 4 changed files with 495 additions and 0 deletions.
1 change: 1 addition & 0 deletions pygments/lexers/_mapping.py
Expand Up @@ -378,6 +378,7 @@
'PythonTracebackLexer': ('pygments.lexers.python', 'Python Traceback', ('pytb', 'py3tb'), ('*.pytb', '*.py3tb'), ('text/x-python-traceback', 'text/x-python3-traceback')),
'QBasicLexer': ('pygments.lexers.basic', 'QBasic', ('qbasic', 'basic'), ('*.BAS', '*.bas'), ('text/basic',)),
'QVToLexer': ('pygments.lexers.qvt', 'QVTO', ('qvto', 'qvt'), ('*.qvto',), ()),
'QlikLexer': ('pygments.lexers.qlik', 'Qlik', ('qlik', 'qlikview', 'qliksense', 'qlikscript'), ('*.qvs', '*.qvw'), ()),
'QmlLexer': ('pygments.lexers.webmisc', 'QML', ('qml', 'qbs'), ('*.qml', '*.qbs'), ('application/x-qml', 'application/x-qt.qbs+qml')),
'RConsoleLexer': ('pygments.lexers.r', 'RConsole', ('rconsole', 'rout'), ('*.Rout',), ()),
'RNCCompactLexer': ('pygments.lexers.rnc', 'Relax-NG Compact', ('rng-compact', 'rnc'), ('*.rnc',), ()),
Expand Down
104 changes: 104 additions & 0 deletions pygments/lexers/qlik.py
@@ -0,0 +1,104 @@
"""
pygments.lexers.qlik
~~~~~~~~~~~~~~~~~~~~~
Lexer for the qlik scripting language
:copyright: Copyright 2006-2021 by the Pygments team, see AUTHORS.
:license: BSD, see LICENSE for details.
"""

import re

from pygments.lexer import RegexLexer, include, bygroups
from pygments.token import *

__all__ = ["QlikLexer"]


class QlikLexer(RegexLexer):
"""
Lexer for qlik code, including .qvs files
"""

name = "Qlik"
aliases = ["qlik", "qlikview", "qliksense", "qlikscript"]
filenames = ["*.qvs", "*.qvw"]

flags = re.IGNORECASE

tokens = {
"comment": [
(r"[^*/]", Comment.Multiline),
(r"/\*", Comment.Multiline, "#push"),
(r"\*/", Comment.Multiline, "#pop"),
(r"[*/]", Comment.Multiline),
],
"numerics": [
(r"\b[0-9][0-9]*\.[0-9]+([eE][0-9]+)?[fd]?\b", Number.Float),
(r"\b[0-9]+\b", Number.Integer),
],
"variables": [
(r"(\$\()(\w+)(\))", bygroups(Keyword, Name.Variable, Keyword)),
],
"root": [
(r"\s+", Text),
(r"/\*", Comment.Multiline, "comment"),
(r"//.*\n", Comment.Singleline),
(
r"\b(and|or|is|not)\b",
Operator.Word,
),
(
r"(for|in|while|do|break|return|continue|switch|case|default|"
r"if|else|endif|then|end|errormode|each|next)\b",
Keyword,
),
(r"(set|let|sub)\b", Keyword.Declaration),
(r"(num|date|dual|date#|text)\(", Name.Builtin),
(r"[a-z]\w*:", Keyword.Declaration),
(r"(?<![.$])(true|false|null)\b", Keyword.Constant),
(
r"(\bas|resident|mapping|distinct|load|join|(left|right|outer|full|"
r"inner) join|from|trace|execute|odbc|connect|add|"
r"alias|binary|buffer|bundle|concatenate|directory|"
r"intervalmatch|trace|unqualify|qualify|include|sql\sselect|"
r"inline|autogenerate|group\sby|order\sby|asc|desc|store|into|"
r"drop|table|field)\b",
Operator.Word,
),
(r"(noconcatenate|concatenate)\b", Operator.Word),
(
r"(ceil|floor|round|rangesum|rangeavg|rangestdev|len|trim|"
r"subfield|left|right|replace|exists|fieldindex|fieldvalue|"
r"peek|previous|next|lookup|recno|rowno|iterno|autonumberhash128|"
r"autonumberhash256|fieldvaluecount|sum|max|maxstring|min|"
r"avg|count|second|minute|hour|day|week|month|year|weekyear|"
r"weekday|now|today|localtime|makedate|makeweekdate|maketime|"
r"yeartodate|setdateyear|setdateyearmonth|yearstart|yearend|"
r"inyear|inyeartodate|inquarter|inquartertodate|addmonths|"
r"monthstart|monthend|inmonth|inmonthtodate|inmonths|"
r"inmonthstodate|inweek|inweektodate|inlunarweek|"
r"inlunarweektodate|timezone|gmt|utc|daylightsaving|filesize|"
r"documentname|filetime|isnull|crosstable|applymap|if|"
r"filename)\(",
Operator.Word,
),
include("variables"),
(r'"(\s|\w)+"', Keyword),
(
r"(\[.+)(\$\()(\w+)(\))(.+])",
bygroups(String, Keyword, Name.Variable, Keyword, String),
),
(r"\[[^\]]+\]", Keyword),
include("numerics"),
(
r"('.+)(\$\()(\w+)(\))(.+')",
bygroups(String, Keyword, Name.Variable, Keyword, String),
),
(r"<>|[\-\<\>\+\*\%\&\|\^\/=\)\(]", Operator),
(r"'.+'", String),
(r"\b\w+\b", Text),
(r"(\,|\;|\.|\(|\)|\\|\/)", Punctuation),
],
}
54 changes: 54 additions & 0 deletions tests/examplefiles/qlik/example.qvs
@@ -0,0 +1,54 @@
/* ---------------------------------
This is an example file containing qlik script
--------------------------------- */

LET vFilePath = '/home';

FOR EACH vFolder IN 'folder_1', 'folder_2'

// Check the file exists
IF FILESIZE('$(vFilePath)/$(vFolder)/file.qvd') >0 THEN

TABLE_NAME:
NoConcatenate
LOAD
[Field 1] AS FIELD_1,
"Field 2" AS FIELD_2,
FIELD_3,
FileName() AS SOURCE_FILE
FROM
[$(vFilePath)/$(vFolder)/file.qvd] (qvd);

MAP_FIELD_1:
MAPPING LOAD
FIELD_1,
SUM(FIELD_2)
RESIDENT
TABLE_NAME
GROUP BY
FIELD_1;

LEFT JOIN(TABLE_NAME)
LOAD DISTINCT
FIELD_1,
FIELD_2,
FIELD_3,
applymap('MAP_FIELD_1', FIELD_1, 0) AS FIELD_2_SUM,
'Token String' AS STRING_FIELD,
3.14159 AS NUMBER_FIELD
RESIDENT
TABLE_NAME
ORDER BY
FIELD_3 DESC;

STORE TABLE_NAME INTO [$(vFilePath)/$(vFolder)/file_output.qvd] (qvd);

DROP TABLE TABLE_NAME;

ELSE

TRACE No file found in $(vFilePath)/file.qvd;

END IF

NEXT vFolder

0 comments on commit 4afd47c

Please sign in to comment.