Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/pygments/lexers/minecraft.py: 100%
25 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-07-01 06:54 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-07-01 06:54 +0000
1"""
2 pygments.lexers.minecraft
3 ~~~~~~~~~~~~~~~~~~~~~~~~~
5 Lexers for Minecraft related languages.
7 SNBT. A data communication format used in Minecraft.
8 wiki: https://minecraft.fandom.com/wiki/NBT_format
10 MCFunction. The Function file for Minecraft Data packs and Add-ons.
11 official: https://learn.microsoft.com/en-us/minecraft/creator/documents/functionsintroduction
12 wiki: https://minecraft.fandom.com/wiki/Function
14 MCSchema. A kind of data Schema for Minecraft Add-on Development.
15 official: https://learn.microsoft.com/en-us/minecraft/creator/reference/content/schemasreference/
16 community example: https://www.mcbe-dev.net/addons/data-driven/manifest.html
18 :copyright: Copyright 2006-2023 by the Pygments team, see AUTHORS.
19 :license: BSD, see LICENSE for details.
20"""
22from pygments.lexer import RegexLexer, default, include, bygroups
23from pygments.token import Comment, Keyword, Literal, Name, Number, Operator, \
24 Punctuation, String, Text, Whitespace
26__all__ = ['SNBTLexer', 'MCFunctionLexer', 'MCSchemaLexer']
29class SNBTLexer(RegexLexer):
30 """Lexer for stringified NBT, a data format used in Minecraft
32 .. versionadded:: 2.12.0
33 """
35 name = "SNBT"
36 url = "https://minecraft.fandom.com/wiki/NBT_format"
37 aliases = ["snbt"]
38 filenames = ["*.snbt"]
39 mimetypes = ["text/snbt"]
41 tokens = {
42 "root": [
43 # We only look for the open bracket here since square bracket
44 # is only valid in NBT pathing (which is a mcfunction idea).
45 (r"\{", Punctuation, "compound"),
46 (r"[^\{]+", Text),
47 ],
49 "whitespace": [
50 (r"\s+", Whitespace),
51 ],
53 "operators": [
54 (r"[,:;]", Punctuation),
55 ],
57 "literals": [
58 (r"(true|false)", Keyword.Constant),
59 (r"-?\d+[eE]-?\d+", Number.Float),
60 (r"-?\d*\.\d+[fFdD]?", Number.Float),
61 (r"-?\d+[bBsSlLfFdD]?", Number.Integer),
63 # Separate states for both types of strings so they don't entangle
64 (r'"', String.Double, "literals.string_double"),
65 (r"'", String.Single, "literals.string_single"),
66 ],
67 "literals.string_double": [
68 (r"\\.", String.Escape),
69 (r'[^\\"\n]+', String.Double),
70 (r'"', String.Double, "#pop"),
71 ],
72 "literals.string_single": [
73 (r"\\.", String.Escape),
74 (r"[^\\'\n]+", String.Single),
75 (r"'", String.Single, "#pop"),
76 ],
78 "compound": [
79 # this handles the unquoted snbt keys
80 # note: stringified keys still work
81 (r"[A-Z_a-z]+", Name.Attribute),
82 include("operators"),
83 include("whitespace"),
84 include("literals"),
85 (r"\{", Punctuation, "#push"),
86 (r"\[", Punctuation, "list"),
87 (r"\}", Punctuation, "#pop"),
88 ],
90 "list": [
91 (r"[A-Z_a-z]+", Name.Attribute),
92 include("literals"),
93 include("operators"),
94 include("whitespace"),
95 (r"\[", Punctuation, "#push"),
96 (r"\{", Punctuation, "compound"),
97 (r"\]", Punctuation, "#pop"),
98 ],
99 }
102class MCFunctionLexer(RegexLexer):
103 """Lexer for the mcfunction scripting language used in Minecraft
104 Modelled somewhat after the `GitHub mcfunction grammar <https://github.com/Arcensoth/language-mcfunction>`_.
106 .. versionadded:: 2.12.0
107 """
109 name = "MCFunction"
110 url = "https://minecraft.fandom.com/wiki/Commands"
111 aliases = ["mcfunction", "mcf"]
112 filenames = ["*.mcfunction"]
113 mimetypes = ["text/mcfunction"]
115 # Used to denotate the start of a block comment, borrowed from Github's mcfunction
116 _block_comment_prefix = "[>!]"
118 tokens = {
119 "root": [
120 include("names"),
121 include("comments"),
122 include("literals"),
123 include("whitespace"),
124 include("property"),
125 include("operators"),
126 include("selectors"),
127 ],
129 "names": [
130 # The start of a command (either beginning of line OR after the run keyword)
131 # We don't encode a list of keywords since mods, plugins, or even pre-processors
132 # may add new commands, so we have a 'close-enough' regex which catches them.
133 (r"^(\s*)([a-z_]+)", bygroups(Whitespace, Name.Builtin)),
134 (r"(?<=run)\s+[a-z_]+", Name.Builtin),
136 # UUID
137 (r"\b[0-9a-fA-F]+(?:-[0-9a-fA-F]+){4}\b", Name.Variable),
138 include("resource-name"),
139 # normal command names and scoreboards
140 # there's no way to know the differences unfortuntely
141 (r"[A-Za-z_][\w.#%$]+", Keyword.Constant),
142 (r"[#%$][\w.#%$]+", Name.Variable.Magic),
143 ],
145 "resource-name": [
146 # resource names have to be lowercase
147 (r"#?[a-z_][a-z_.-]*:[a-z0-9_./-]+", Name.Function),
148 # similar to above except optional `:``
149 # a `/` must be present "somewhere"
150 (r"#?[a-z0-9_\.\-]+\/[a-z0-9_\.\-\/]+", Name.Function),
151 ],
153 "whitespace": [
154 (r"\s+", Whitespace),
155 ],
157 "comments": [
158 (rf"^\s*(#{_block_comment_prefix})", Comment.Multiline,
159 ("comments.block", "comments.block.emphasized")),
160 (r"#.*$", Comment.Single),
161 ],
162 "comments.block": [
163 (rf"^\s*#{_block_comment_prefix}", Comment.Multiline,
164 "comments.block.emphasized"),
165 (r"^\s*#", Comment.Multiline, "comments.block.normal"),
166 default("#pop"),
167 ],
168 "comments.block.normal": [
169 include("comments.block.special"),
170 (r"\S+", Comment.Multiline),
171 (r"\n", Text, "#pop"),
172 include("whitespace"),
173 ],
174 "comments.block.emphasized": [
175 include("comments.block.special"),
176 (r"\S+", String.Doc),
177 (r"\n", Text, "#pop"),
178 include("whitespace"),
179 ],
180 "comments.block.special": [
181 # Params
182 (r"@\S+", Name.Decorator),
184 include("resource-name"),
186 # Scoreboard player names
187 (r"[#%$][\w.#%$]+", Name.Variable.Magic),
188 ],
190 "operators": [
191 (r"[\-~%^?!+*<>\\/|&=.]", Operator),
192 ],
194 "literals": [
195 (r"\.\.", Literal),
196 (r"(true|false)", Keyword.Pseudo),
198 # these are like unquoted strings and appear in many places
199 (r"[A-Za-z_]+", Name.Variable.Class),
201 (r"[0-7]b", Number.Byte),
202 (r"[+-]?\d*\.?\d+([eE]?[+-]?\d+)?[df]?\b", Number.Float),
203 (r"[+-]?\d+\b", Number.Integer),
204 (r'"', String.Double, "literals.string-double"),
205 (r"'", String.Single, "literals.string-single"),
206 ],
207 "literals.string-double": [
208 (r"\\.", String.Escape),
209 (r'[^\\"\n]+', String.Double),
210 (r'"', String.Double, "#pop"),
211 ],
212 "literals.string-single": [
213 (r"\\.", String.Escape),
214 (r"[^\\'\n]+", String.Single),
215 (r"'", String.Single, "#pop"),
216 ],
218 "selectors": [
219 (r"@[a-z]", Name.Variable),
220 ],
223 ## Generic Property Container
224 # There are several, differing instances where the language accepts
225 # specific contained keys or contained key, value pairings.
226 #
227 # Property Maps:
228 # - Starts with either `[` or `{`
229 # - Key separated by `:` or `=`
230 # - Deliminated by `,`
231 #
232 # Property Lists:
233 # - Starts with `[`
234 # - Deliminated by `,`
235 #
236 # For simplicity, these patterns match a generic, nestable structure
237 # which follow a key, value pattern. For normal lists, there's only keys.
238 # This allow some "illegal" structures, but we'll accept those for
239 # sake of simplicity
240 #
241 # Examples:
242 # - `[facing=up, powered=true]` (blockstate)
243 # - `[name="hello world", nbt={key: 1b}]` (selector + nbt)
244 # - `[{"text": "value"}, "literal"]` (json)
245 ##
246 "property": [
247 # This state gets included in root and also several substates
248 # We do this to shortcut the starting of new properties
249 # within other properties. Lists can have sublists and compounds
250 # and values can start a new property (see the `difficult_1.txt`
251 # snippet).
252 (r"\{", Punctuation, ("property.curly", "property.key")),
253 (r"\[", Punctuation, ("property.square", "property.key")),
254 ],
255 "property.curly": [
256 include("whitespace"),
257 include("property"),
258 (r"\}", Punctuation, "#pop"),
259 ],
260 "property.square": [
261 include("whitespace"),
262 include("property"),
263 (r"\]", Punctuation, "#pop"),
265 # lists can have sequences of items
266 (r",", Punctuation),
267 ],
268 "property.key": [
269 include("whitespace"),
271 # resource names (for advancements)
272 # can omit `:` to default `minecraft:`
273 # must check if there is a future equals sign if `:` is in the name
274 (r"#?[a-z_][a-z_\.\-]*\:[a-z0-9_\.\-/]+(?=\s*\=)", Name.Attribute, "property.delimiter"),
275 (r"#?[a-z_][a-z0-9_\.\-/]+", Name.Attribute, "property.delimiter"),
277 # unquoted NBT key
278 (r"[A-Za-z_\-\+]+", Name.Attribute, "property.delimiter"),
280 # quoted JSON or NBT key
281 (r'"', Name.Attribute, "property.delimiter", "literals.string-double"),
282 (r"'", Name.Attribute, "property.delimiter", "literals.string-single"),
284 # index for a list
285 (r"-?\d+", Number.Integer, "property.delimiter"),
287 default("#pop"),
288 ],
289 "property.key.string-double": [
290 (r"\\.", String.Escape),
291 (r'[^\\"\n]+', Name.Attribute),
292 (r'"', Name.Attribute, "#pop"),
293 ],
294 "property.key.string-single": [
295 (r"\\.", String.Escape),
296 (r"[^\\'\n]+", Name.Attribute),
297 (r"'", Name.Attribute, "#pop"),
298 ],
299 "property.delimiter": [
300 include("whitespace"),
302 (r"[:=]!?", Punctuation, "property.value"),
303 (r",", Punctuation),
305 default("#pop"),
306 ],
307 "property.value": [
308 include("whitespace"),
310 # unquoted resource names are valid literals here
311 (r"#?[a-z_][a-z_\.\-]*\:[a-z0-9_\.\-/]+", Name.Tag),
312 (r"#?[a-z_][a-z0-9_\.\-/]+", Name.Tag),
314 include("literals"),
315 include("property"),
317 default("#pop"),
318 ],
319 }
322class MCSchemaLexer(RegexLexer):
323 """Lexer for Minecraft Add-ons data Schemas, an interface structure standard used in Minecraft
325 .. versionadded:: 2.14.0
326 """
328 name = 'MCSchema'
329 url = 'https://learn.microsoft.com/en-us/minecraft/creator/reference/content/schemasreference/'
330 aliases = ['mcschema']
331 filenames = ['*.mcschema']
332 mimetypes = ['text/mcschema']
334 tokens = {
335 'commentsandwhitespace': [
336 (r'\s+', Whitespace),
337 (r'//.*?$', Comment.Single),
338 (r'/\*.*?\*/', Comment.Multiline)
339 ],
340 'slashstartsregex': [
341 include('commentsandwhitespace'),
342 (r'/(\\.|[^[/\\\n]|\[(\\.|[^\]\\\n])*])+/'
343 r'([gimuysd]+\b|\B)', String.Regex, '#pop'),
344 (r'(?=/)', Text, ('#pop', 'badregex')),
345 default('#pop')
346 ],
347 'badregex': [
348 (r'\n', Whitespace, '#pop')
349 ],
350 'singlestring': [
351 (r'\\.', String.Escape),
352 (r"'", String.Single, '#pop'),
353 (r"[^\\']+", String.Single),
354 ],
355 'doublestring': [
356 (r'\\.', String.Escape),
357 (r'"', String.Double, '#pop'),
358 (r'[^\\"]+', String.Double),
359 ],
360 'root': [
361 (r'^(?=\s|/|<!--)', Text, 'slashstartsregex'),
362 include('commentsandwhitespace'),
364 # keywords for optional word and field types
365 (r'(?<=: )opt', Operator.Word),
366 (r'(?<=\s)[\w-]*(?=(\s+"|\n))', Keyword.Declaration),
368 # numeric literals
369 (r'0[bB][01]+', Number.Bin),
370 (r'0[oO]?[0-7]+', Number.Oct),
371 (r'0[xX][0-9a-fA-F]+', Number.Hex),
372 (r'\d+', Number.Integer),
373 (r'(\.\d+|\d+\.\d*|\d+)([eE][-+]?\d+)?', Number.Float),
375 # possible punctuations
376 (r'\.\.\.|=>', Punctuation),
377 (r'\+\+|--|~|\?\?=?|\?|:|\\(?=\n)|'
378 r'(<<|>>>?|==?|!=?|(?:\*\*|\|\||&&|[-<>+*%&|^/]))=?', Operator, 'slashstartsregex'),
379 (r'[{(\[;,]', Punctuation, 'slashstartsregex'),
380 (r'[})\].]', Punctuation),
382 # strings
383 (r"'", String.Single, 'singlestring'),
384 (r'"', String.Double, 'doublestring'),
386 # title line
387 (r'[\w-]*?(?=:\{?\n)', String.Symbol),
388 # title line with a version code, formatted
389 # `major.minor.patch-prerelease+buildmeta`
390 (r'([\w-]*?)(:)(\d+)(?:(\.)(\d+)(?:(\.)(\d+)(?:(\-)((?:[^\W_]|-)*(?:\.(?:[^\W_]|-)*)*))?(?:(\+)((?:[^\W_]|-)+(?:\.(?:[^\W_]|-)+)*))?)?)?(?=:\{?\n)', bygroups(String.Symbol, Operator, Number.Integer, Operator, Number.Integer, Operator, Number.Integer, Operator, String, Operator, String)),
392 (r'.*\n', Text),
393 ]
394 }