Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/pygments/lexers/graphql.py: 100%
13 statements
« prev ^ index » next coverage.py v7.3.1, created at 2023-09-18 06:13 +0000
« prev ^ index » next coverage.py v7.3.1, created at 2023-09-18 06:13 +0000
1"""
2 pygments.lexers.graphql
3 ~~~~~~~~~~~~~~~~~~~~~~~
5 Lexer for GraphQL, an open-source data query and manipulation
6 language for APIs.
8 More information:
9 https://graphql.org/
11 :copyright: Copyright 2006-2023 by the Pygments team, see AUTHORS.
12 :license: BSD, see LICENSE for details.
13"""
15from pygments.lexer import RegexLexer, words, include, bygroups, default
16from pygments.token import (Comment, Keyword, Name, Number, Punctuation, String,
17 Whitespace)
20__all__ = ["GraphQLLexer"]
22OPERATION_TYPES = ("query", "mutation", "subscription")
23BUILTIN_TYPES = ("Int", "Float", "String", "Boolean", "ID")
24BOOLEAN_VALUES = ("true", "false", "null")
25KEYWORDS = (
26 "type",
27 "schema",
28 "extend",
29 "enum",
30 "scalar",
31 "implements",
32 "interface",
33 "union",
34 "input",
35 "directive",
36 "QUERY",
37 "MUTATION",
38 "SUBSCRIPTION",
39 "FIELD",
40 "FRAGMENT_DEFINITION",
41 "FRAGMENT_SPREAD",
42 "INLINE_FRAGMENT",
43 "SCHEMA",
44 "SCALAR",
45 "OBJECT",
46 "FIELD_DEFINITION",
47 "ARGUMENT_DEFINITION",
48 "INTERFACE",
49 "UNION",
50 "ENUM",
51 "ENUM_VALUE",
52 "INPUT_OBJECT",
53 "INPUT_FIELD_DEFINITION",
54)
57class GraphQLLexer(RegexLexer):
58 """
59 Lexer for GraphQL syntax
61 .. versionadded:: 2.16
62 """
63 name = "GraphQL"
64 aliases = ["graphql"]
65 filenames = ["*.graphql"]
66 url = "https://graphql.org"
68 tokens = {
69 "ignored_tokens": [
70 (r"\s+", Whitespace), # Whitespaces
71 (r"#.*$", Comment),
72 (",", Punctuation), # Insignificant commas
73 ],
74 "value": [
75 include("ignored_tokens"),
76 (r"-?\d+(?![.eE])", Number.Integer, "#pop"),
77 (
78 r"-?\d+(\.\d+)?([eE][+-]?\d+)?",
79 Number.Float,
80 "#pop",
81 ),
82 (r'"', String, ("#pop", "string")),
83 (words(BOOLEAN_VALUES, suffix=r"\b"), Name.Builtin, "#pop"),
84 (r"\$[a-zA-Z_]\w*", Name.Variable, "#pop"),
85 (r"[a-zA-Z_]\w*", Name.Constant, "#pop"),
86 (r"\[", Punctuation, ("#pop", "list_value")),
87 (r"\{", Punctuation, ("#pop", "object_value")),
88 ],
89 "list_value": [
90 include("ignored_tokens"),
91 ("]", Punctuation, "#pop"),
92 default("value"),
93 ],
94 "object_value": [
95 include("ignored_tokens"),
96 (r"[a-zA-Z_]\w*", Name),
97 (r":", Punctuation, "value"),
98 (r"\}", Punctuation, "#pop"),
99 ],
100 "string": [
101 (r'\\(["\\/bfnrt]|u[a-fA-F0-9]{4})', String.Escape),
102 (r'[^\\"\n]+', String), # all other characters
103 (r'"', String, "#pop"),
104 ],
105 "root": [
106 include("ignored_tokens"),
107 (words(OPERATION_TYPES, suffix=r"\b"), Keyword, "operation"),
108 (words(KEYWORDS, suffix=r"\b"), Keyword),
109 (r"\{", Punctuation, "selection_set"),
110 (r"fragment\b", Keyword, "fragment_definition"),
111 ],
112 "operation": [
113 include("ignored_tokens"),
114 (r"[a-zA-Z_]\w*", Name.Function),
115 (r"\(", Punctuation, "variable_definition"),
116 (r"\{", Punctuation, ("#pop", "selection_set")),
117 ],
118 "variable_definition": [
119 include("ignored_tokens"),
120 (r"\$[a-zA-Z_]\w*", Name.Variable),
121 (r"[\]!]", Punctuation),
122 (r":", Punctuation, "type"),
123 (r"=", Punctuation, "value"),
124 (r"\)", Punctuation, "#pop"),
125 ],
126 "type": [
127 include("ignored_tokens"),
128 (r"\[", Punctuation),
129 (words(BUILTIN_TYPES, suffix=r"\b"), Name.Builtin, "#pop"),
130 (r"[a-zA-Z_]\w*", Name.Class, "#pop"),
131 ],
132 "selection_set": [
133 include("ignored_tokens"),
134 (r"([a-zA-Z_]\w*)(\s*)(:)", bygroups(Name.Label, Whitespace, Punctuation)),
135 (r"[a-zA-Z_]\w*", Name), # Field
136 (
137 r"(\.\.\.)(\s+)(on)\b",
138 bygroups(Punctuation, Whitespace, Keyword),
139 "inline_fragment",
140 ),
141 (r"\.\.\.", Punctuation, "fragment_spread"),
142 (r"\(", Punctuation, "arguments"),
143 (r"@[a-zA-Z_]\w*", Name.Decorator, "directive"),
144 (r"\{", Punctuation, "selection_set"),
145 (r"\}", Punctuation, "#pop"),
146 ],
147 "directive": [
148 include("ignored_tokens"),
149 (r"\(", Punctuation, ("#pop", "arguments")),
150 ],
151 "arguments": [
152 include("ignored_tokens"),
153 (r"[a-zA-Z_]\w*", Name),
154 (r":", Punctuation, "value"),
155 (r"\)", Punctuation, "#pop"),
156 ],
157 # Fragments
158 "fragment_definition": [
159 include("ignored_tokens"),
160 (r"[\]!]", Punctuation), # For NamedType
161 (r"on\b", Keyword, "type"),
162 (r"[a-zA-Z_]\w*", Name.Function),
163 (r"@[a-zA-Z_]\w*", Name.Decorator, "directive"),
164 (r"\{", Punctuation, ("#pop", "selection_set")),
165 ],
166 "fragment_spread": [
167 include("ignored_tokens"),
168 (r"@[a-zA-Z_]\w*", Name.Decorator, "directive"),
169 (r"[a-zA-Z_]\w*", Name, "#pop"), # Fragment name
170 ],
171 "inline_fragment": [
172 include("ignored_tokens"),
173 (r"[a-zA-Z_]\w*", Name.Class), # Type condition
174 (r"@[a-zA-Z_]\w*", Name.Decorator, "directive"),
175 (r"\{", Punctuation, ("#pop", "selection_set")),
176 ],
177 }