kusto.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. """
  2. pygments.lexers.kusto
  3. ~~~~~~~~~~~~~~~~~~~~~
  4. Lexers for Kusto Query Language (KQL).
  5. :copyright: Copyright 2006-2024 by the Pygments team, see AUTHORS.
  6. :license: BSD, see LICENSE for details.
  7. """
  8. from pygments.lexer import RegexLexer, words
  9. from pygments.token import (Comment, Keyword, Name, Number, Punctuation,
  10. String, Whitespace)
  11. __all__ = ["KustoLexer"]
  12. # Although these all seem to be keywords
  13. # https://github.com/microsoft/Kusto-Query-Language/blob/master/src/Kusto.Language/Syntax/SyntaxFacts.cs
  14. # it appears that only the ones with tags here
  15. # https://github.com/microsoft/Kusto-Query-Language/blob/master/src/Kusto.Language/Parser/QueryGrammar.cs
  16. # are highlighted in the Azure portal log query editor.
  17. KUSTO_KEYWORDS = [
  18. 'and', 'as', 'between', 'by', 'consume', 'contains', 'containscs', 'count',
  19. 'distinct', 'evaluate', 'extend', 'facet', 'filter', 'find', 'fork',
  20. 'getschema', 'has', 'invoke', 'join', 'limit', 'lookup', 'make-series',
  21. 'matches regex', 'mv-apply', 'mv-expand', 'notcontains', 'notcontainscs',
  22. '!contains', '!has', '!startswith', 'on', 'or', 'order', 'parse', 'parse-where',
  23. 'parse-kv', 'partition', 'print', 'project', 'project-away', 'project-keep',
  24. 'project-rename', 'project-reorder', 'range', 'reduce', 'regex', 'render',
  25. 'sample', 'sample-distinct', 'scan', 'search', 'serialize', 'sort', 'startswith',
  26. 'summarize', 'take', 'top', 'top-hitters', 'top-nested', 'typeof', 'union',
  27. 'where', 'bool', 'date', 'datetime', 'int', 'long', 'real', 'string', 'time'
  28. ]
  29. # From
  30. # https://github.com/microsoft/Kusto-Query-Language/blob/master/src/Kusto.Language/Syntax/SyntaxFacts.cs
  31. KUSTO_PUNCTUATION = [
  32. "(", ")", "[", "]", "{", "}", "|", "<|", "+", "-", "*", "/",
  33. "%", ".." "!", "<", "<=", ">", ">=", "=", "==", "!=", "<>",
  34. ":", ";", ",", "=~", "!~", "?", "=>",
  35. ]
  36. class KustoLexer(RegexLexer):
  37. """For Kusto Query Language source code.
  38. """
  39. name = "Kusto"
  40. aliases = ["kql", "kusto"]
  41. filenames = ["*.kql", "*.kusto", ".csl"]
  42. url = "https://learn.microsoft.com/en-us/azure/data-explorer/kusto/query"
  43. version_added = '2.17'
  44. tokens = {
  45. "root": [
  46. (r"\s+", Whitespace),
  47. (words(KUSTO_KEYWORDS, suffix=r"\b"), Keyword),
  48. (r"//.*", Comment),
  49. (words(KUSTO_PUNCTUATION), Punctuation),
  50. (r"[^\W\d]\w*", Name),
  51. # Numbers can take the form 1, .1, 1., 1.1, 1.1111, etc.
  52. (r"\d+[.]\d*|[.]\d+", Number.Float),
  53. (r"\d+", Number.Integer),
  54. (r"'", String, "single_string"),
  55. (r'"', String, "double_string"),
  56. (r"@'", String, "single_verbatim"),
  57. (r'@"', String, "double_verbatim"),
  58. (r"```", String, "multi_string"),
  59. ],
  60. "single_string": [
  61. (r"'", String, "#pop"),
  62. (r"\\.", String.Escape),
  63. (r"[^'\\]+", String),
  64. ],
  65. "double_string": [
  66. (r'"', String, "#pop"),
  67. (r"\\.", String.Escape),
  68. (r'[^"\\]+', String),
  69. ],
  70. "single_verbatim": [
  71. (r"'", String, "#pop"),
  72. (r"[^']+", String),
  73. ],
  74. "double_verbatim": [
  75. (r'"', String, "#pop"),
  76. (r'[^"]+', String),
  77. ],
  78. "multi_string": [
  79. (r"[^`]+", String),
  80. (r"```", String, "#pop"),
  81. (r"`", String),
  82. ],
  83. }