aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/python/Pygments/py3/pygments/lexers/kusto.py
blob: 7b146861cf5576f9a29fc27cc6c24ada15d6ef1d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
"""
    pygments.lexers.kusto
    ~~~~~~~~~~~~~~~~~~~~~

    Lexers for Kusto Query Language (KQL).

    :copyright: Copyright 2006-2024 by the Pygments team, see AUTHORS.
    :license: BSD, see LICENSE for details.
"""

from pygments.lexer import RegexLexer, words
from pygments.token import (Comment, Keyword, Name, Number, Punctuation,
                            String, Whitespace)

__all__ = ["KustoLexer"]

# Although these all seem to be keywords
# https://github.com/microsoft/Kusto-Query-Language/blob/master/src/Kusto.Language/Syntax/SyntaxFacts.cs
# it appears that only the ones with tags here
# https://github.com/microsoft/Kusto-Query-Language/blob/master/src/Kusto.Language/Parser/QueryGrammar.cs
# are highlighted in the Azure portal log query editor.
KUSTO_KEYWORDS = [
    'and', 'as', 'between', 'by', 'consume', 'contains', 'containscs', 'count',
    'distinct', 'evaluate', 'extend', 'facet', 'filter', 'find', 'fork',
    'getschema', 'has', 'invoke', 'join', 'limit', 'lookup', 'make-series',
    'matches regex', 'mv-apply', 'mv-expand', 'notcontains', 'notcontainscs',
    '!contains', '!has', '!startswith', 'on', 'or', 'order', 'parse', 'parse-where',
    'parse-kv', 'partition', 'print', 'project', 'project-away', 'project-keep',
    'project-rename', 'project-reorder', 'range', 'reduce', 'regex', 'render',
    'sample', 'sample-distinct', 'scan', 'search', 'serialize', 'sort', 'startswith',
    'summarize', 'take', 'top', 'top-hitters', 'top-nested', 'typeof', 'union',
    'where', 'bool', 'date', 'datetime', 'int', 'long', 'real', 'string', 'time'
]

# From
# https://github.com/microsoft/Kusto-Query-Language/blob/master/src/Kusto.Language/Syntax/SyntaxFacts.cs
KUSTO_PUNCTUATION = [
    "(", ")", "[", "]", "{", "}", "|", "<|", "+", "-", "*", "/",
    "%", ".." "!", "<", "<=", ">", ">=", "=", "==", "!=", "<>",
    ":", ";", ",", "=~", "!~", "?", "=>",
]


class KustoLexer(RegexLexer):
    """For Kusto Query Language source code.
    """

    name = "Kusto"
    aliases = ["kql", "kusto"]
    filenames = ["*.kql", "*.kusto", ".csl"]
    url = "https://learn.microsoft.com/en-us/azure/data-explorer/kusto/query"
    version_added = '2.17'

    tokens = {
        "root": [
            (r"\s+", Whitespace),
            (words(KUSTO_KEYWORDS, suffix=r"\b"), Keyword),
            (r"//.*", Comment),
            (words(KUSTO_PUNCTUATION), Punctuation),
            (r"[^\W\d]\w*", Name),
            # Numbers can take the form 1, .1, 1., 1.1, 1.1111, etc.
            (r"\d+[.]\d*|[.]\d+", Number.Float),
            (r"\d+", Number.Integer),
            (r"'", String, "single_string"),
            (r'"', String, "double_string"),
            (r"@'", String, "single_verbatim"),
            (r'@"', String, "double_verbatim"),
            (r"```", String, "multi_string"),
        ],
        "single_string": [
            (r"'", String, "#pop"),
            (r"\\.", String.Escape),
            (r"[^'\\]+", String),
        ],
        "double_string": [
            (r'"', String, "#pop"),
            (r"\\.", String.Escape),
            (r'[^"\\]+', String),
        ],
        "single_verbatim": [
            (r"'", String, "#pop"),
            (r"[^']+", String),
        ],
        "double_verbatim": [
            (r'"', String, "#pop"),
            (r'[^"]+', String),
        ],
        "multi_string": [
            (r"[^`]+", String),
            (r"```", String, "#pop"),
            (r"`", String),
        ],
    }