qlik.py 3.6 KB

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