qlik.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. """
  2. pygments.lexers.qlik
  3. ~~~~~~~~~~~~~~~~~~~~
  4. Lexer for the qlik scripting language
  5. :copyright: Copyright 2006-2024 by the Pygments team, see AUTHORS.
  6. :license: BSD, see LICENSE for details.
  7. """
  8. import re
  9. from pygments.lexer import RegexLexer, include, bygroups, words
  10. from pygments.token import Comment, Keyword, Name, Number, Operator, \
  11. Punctuation, String, Text
  12. from pygments.lexers._qlik_builtins import OPERATORS_LIST, STATEMENT_LIST, \
  13. SCRIPT_FUNCTIONS, CONSTANT_LIST
  14. __all__ = ["QlikLexer"]
  15. class QlikLexer(RegexLexer):
  16. """
  17. Lexer for qlik code, including .qvs files
  18. """
  19. name = "Qlik"
  20. aliases = ["qlik", "qlikview", "qliksense", "qlikscript"]
  21. filenames = ["*.qvs", "*.qvw"]
  22. url = "https://qlik.com"
  23. version_added = '2.12'
  24. flags = re.IGNORECASE
  25. tokens = {
  26. # Handle multi-line comments
  27. "comment": [
  28. (r"\*/", Comment.Multiline, "#pop"),
  29. (r"[^*]+", Comment.Multiline),
  30. ],
  31. # Handle numbers
  32. "numerics": [
  33. (r"\b\d+\.\d+(e\d+)?[fd]?\b", Number.Float),
  34. (r"\b\d+\b", Number.Integer),
  35. ],
  36. # Handle variable names in things
  37. "interp": [
  38. (
  39. r"(\$\()(\w+)(\))",
  40. bygroups(String.Interpol, Name.Variable, String.Interpol),
  41. ),
  42. ],
  43. # Handle strings
  44. "string": [
  45. (r"'", String, "#pop"),
  46. include("interp"),
  47. (r"[^'$]+", String),
  48. (r"\$", String),
  49. ],
  50. #
  51. "assignment": [
  52. (r";", Punctuation, "#pop"),
  53. include("root"),
  54. ],
  55. "field_name_quote": [
  56. (r'"', String.Symbol, "#pop"),
  57. include("interp"),
  58. (r"[^\"$]+", String.Symbol),
  59. (r"\$", String.Symbol),
  60. ],
  61. "field_name_bracket": [
  62. (r"\]", String.Symbol, "#pop"),
  63. include("interp"),
  64. (r"[^\]$]+", String.Symbol),
  65. (r"\$", String.Symbol),
  66. ],
  67. "function": [(r"\)", Punctuation, "#pop"), include("root")],
  68. "root": [
  69. # Whitespace and comments
  70. (r"\s+", Text.Whitespace),
  71. (r"/\*", Comment.Multiline, "comment"),
  72. (r"//.*\n", Comment.Single),
  73. # variable assignment
  74. (r"(let|set)(\s+)", bygroups(Keyword.Declaration, Text.Whitespace),
  75. "assignment"),
  76. # Word operators
  77. (words(OPERATORS_LIST["words"], prefix=r"\b", suffix=r"\b"),
  78. Operator.Word),
  79. # Statements
  80. (words(STATEMENT_LIST, suffix=r"\b"), Keyword),
  81. # Table names
  82. (r"[a-z]\w*:", Keyword.Declaration),
  83. # Constants
  84. (words(CONSTANT_LIST, suffix=r"\b"), Keyword.Constant),
  85. # Functions
  86. (words(SCRIPT_FUNCTIONS, suffix=r"(?=\s*\()"), Name.Builtin,
  87. "function"),
  88. # interpolation - e.g. $(variableName)
  89. include("interp"),
  90. # Quotes denote a field/file name
  91. (r'"', String.Symbol, "field_name_quote"),
  92. # Square brackets denote a field/file name
  93. (r"\[", String.Symbol, "field_name_bracket"),
  94. # Strings
  95. (r"'", String, "string"),
  96. # Numbers
  97. include("numerics"),
  98. # Operator symbols
  99. (words(OPERATORS_LIST["symbols"]), Operator),
  100. # Strings denoted by single quotes
  101. (r"'.+?'", String),
  102. # Words as text
  103. (r"\b\w+\b", Text),
  104. # Basic punctuation
  105. (r"[,;.()\\/]", Punctuation),
  106. ],
  107. }