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