Skip to content

Commit c49d52b

Browse files
kofukalecthomas
authored andcommitted
Add mcfunction lexer
1 parent 59126c5 commit c49d52b

File tree

3 files changed

+198
-0
lines changed

3 files changed

+198
-0
lines changed

Diff for: lexers/m/mcfunction.go

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package m
2+
3+
import (
4+
. "github.com/alecthomas/chroma" // nolint
5+
"github.com/alecthomas/chroma/lexers/internal"
6+
)
7+
8+
// mcfunction lexer.
9+
var MCFunction = internal.Register(MustNewLexer(
10+
&Config{
11+
Name: "mcfunction",
12+
Aliases: []string{"mcfunction"},
13+
Filenames: []string{"*.mcfunction"},
14+
MimeTypes: []string{},
15+
NotMultiline: true,
16+
DotAll: true,
17+
},
18+
Rules{
19+
"simplevalue": {
20+
{`(true|false)`, KeywordConstant, nil},
21+
{`[01]b`, LiteralNumber, nil},
22+
{`-?(0|[1-9]\d*)(\.\d+[eE](\+|-)?\d+|[eE](\+|-)?\d+|\.\d+)`, LiteralNumberFloat, nil},
23+
{`(-?\d+)(\.\.)(-?\d+)`, ByGroups(LiteralNumberInteger, Punctuation, LiteralNumberInteger), nil},
24+
{`-?(0|[1-9]\d*)`, LiteralNumberInteger, nil},
25+
{`"(\\\\|\\"|[^"])*"`, LiteralStringDouble, nil},
26+
{`'[^']+'`, LiteralStringSingle, nil},
27+
{`([!#]?)(\w+)`, ByGroups(Punctuation, Text), nil},
28+
},
29+
"nbtobjectattribute": {
30+
Include("nbtvalue"),
31+
{`:`, Punctuation, nil},
32+
{`,`, Punctuation, Pop(1)},
33+
{`\}`, Punctuation, Pop(2)},
34+
},
35+
"nbtobjectvalue": {
36+
{`("(\\\\|\\"|[^"])*"|[a-zA-Z0-9_]+)`, NameTag, Push("nbtobjectattribute")},
37+
{`\}`, Punctuation, Pop(1)},
38+
},
39+
"nbtarrayvalue": {
40+
Include("nbtvalue"),
41+
{`,`, Punctuation, nil},
42+
{`\]`, Punctuation, Pop(1)},
43+
},
44+
"nbtvalue": {
45+
Include("simplevalue"),
46+
{`\{`, Punctuation, Push("nbtobjectvalue")},
47+
{`\[`, Punctuation, Push("nbtarrayvalue")},
48+
},
49+
"argumentvalue": {
50+
Include("simplevalue"),
51+
{`,`, Punctuation, Pop(1)},
52+
{`[}\]]`, Punctuation, Pop(2)},
53+
},
54+
"argumentlist": {
55+
{`(nbt)(={)`, ByGroups(NameAttribute, Punctuation), Push("nbtobjectvalue")},
56+
{`([A-Za-z0-9/_!]+)(={)`, ByGroups(NameAttribute, Punctuation), Push("argumentlist")},
57+
{`([A-Za-z0-9/_!]+)(=)`, ByGroups(NameAttribute, Punctuation), Push("argumentvalue")},
58+
Include("simplevalue"),
59+
{`,`, Punctuation, nil},
60+
{`[}\]]`, Punctuation, Pop(1)},
61+
},
62+
"root": {
63+
{`#.*?\n`, CommentSingle, nil},
64+
{Words(`/?`, `\b`, `ability`, `attributes`, `advancement`,
65+
`ban`, `ban-ip`, `banlist`, `bossbar`,
66+
`camerashake`, `classroommode`, `clear`,
67+
`clearspawnpoint`, `clone`, `code`, `collect`,
68+
`createagent`, `data`, `datapack`, `debug`,
69+
`defaultgamemode`, `deop`, `destroy`, `detect`,
70+
`detectredstone`, `difficulty`, `dropall`,
71+
`effect`, `enchant`, `event`, `execute`,
72+
`experience`, `fill`, `flog`, `forceload`,
73+
`function`, `gamemode`, `gamerule`,
74+
`geteduclientinfo`, `give`, `help`, `item`,
75+
`immutableworld`, `kick`, `kill`, `list`,
76+
`locate`, `locatebiome`, `loot`, `me`, `mixer`,
77+
`mobevent`, `move`, `msg`, `music`, `op`,
78+
`pardon`, `particle`, `playanimation`,
79+
`playsound`, `position`, `publish`,
80+
`raytracefog`, `recipe`, `reload`, `remove`,
81+
`replaceitem`, `ride`, `save`, `save-all`,
82+
`save-off`, `save-on`, `say`, `schedule`,
83+
`scoreboard`, `seed`, `setblock`,
84+
`setidletimeout`, `setmaxplayers`,
85+
`setworldspawn`, `spawnpoint`, `spectate`,
86+
`spreadplayers`, `stop`, `stopsound`,
87+
`structure`, `summon`, `tag`, `team`, `teammsg`,
88+
`teleport`, `tell`, `tellraw`, `testfor`,
89+
`testforblock`, `testforblocks`, `tickingarea`,
90+
`time`, `title`, `toggledownfall`, `tp`,
91+
`tpagent`, `transfer`, `transferserver`,
92+
`trigger`, `turn`, `w`, `weather`, `whitelist`,
93+
`worldborder`, `worldbuilder`, `wsserver`, `xp`,
94+
), KeywordReserved, nil},
95+
{Words(``, ``, `@p`, `@r`, `@a`, `@e`, `@s`, `@c`, `@v`),
96+
KeywordConstant, nil},
97+
{`\[`, Punctuation, Push("argumentlist")},
98+
{`{`, Punctuation, Push("nbtobjectvalue")},
99+
{`~`, NameBuiltin, nil},
100+
{`([a-zA-Z_]+:)?[a-zA-Z_]+\b`, Text, nil},
101+
{`([a-z]+)(\.)([0-9]+)\b`, ByGroups(Text, Punctuation, LiteralNumber), nil},
102+
{`([<>=]|<=|>=)`, Punctuation, nil},
103+
Include("simplevalue"),
104+
{`\s+`, TextWhitespace, nil},
105+
},
106+
},
107+
))

Diff for: lexers/testdata/mcfunction.actual

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
fill 29 4 26 ~ ~ ~ minecraft:air replace minecraft:water
2+
3+
# NBT (JSON-like) value.
4+
summon zombie ~ ~ ~ {NoAI:1b}
5+
6+
# Target selector
7+
gamemode creative @a
8+
9+
# With argument
10+
kill @e[type=chicken,nested={a0=0,a1=1}]
11+
12+
# Ragne
13+
kill @e[y_rotation=0..180]

Diff for: lexers/testdata/mcfunction.expected

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
[
2+
{"type":"KeywordReserved","value":"fill"},
3+
{"type":"TextWhitespace","value":" "},
4+
{"type":"LiteralNumberInteger","value":"29"},
5+
{"type":"TextWhitespace","value":" "},
6+
{"type":"LiteralNumberInteger","value":"4"},
7+
{"type":"TextWhitespace","value":" "},
8+
{"type":"LiteralNumberInteger","value":"26"},
9+
{"type":"TextWhitespace","value":" "},
10+
{"type":"NameBuiltin","value":"~"},
11+
{"type":"TextWhitespace","value":" "},
12+
{"type":"NameBuiltin","value":"~"},
13+
{"type":"TextWhitespace","value":" "},
14+
{"type":"NameBuiltin","value":"~"},
15+
{"type":"TextWhitespace","value":" "},
16+
{"type":"Text","value":"minecraft:air"},
17+
{"type":"TextWhitespace","value":" "},
18+
{"type":"Text","value":"replace"},
19+
{"type":"TextWhitespace","value":" "},
20+
{"type":"Text","value":"minecraft:water"},
21+
{"type":"TextWhitespace","value":"\n\n"},
22+
{"type":"CommentSingle","value":"# NBT (JSON-like) value.\n"},
23+
{"type":"KeywordReserved","value":"summon"},
24+
{"type":"TextWhitespace","value":" "},
25+
{"type":"Text","value":"zombie"},
26+
{"type":"TextWhitespace","value":" "},
27+
{"type":"NameBuiltin","value":"~"},
28+
{"type":"TextWhitespace","value":" "},
29+
{"type":"NameBuiltin","value":"~"},
30+
{"type":"TextWhitespace","value":" "},
31+
{"type":"NameBuiltin","value":"~"},
32+
{"type":"TextWhitespace","value":" "},
33+
{"type":"Punctuation","value":"{"},
34+
{"type":"NameTag","value":"NoAI"},
35+
{"type":"Punctuation","value":":"},
36+
{"type":"LiteralNumber","value":"1b"},
37+
{"type":"Punctuation","value":"}"},
38+
{"type":"TextWhitespace","value":"\n\n"},
39+
{"type":"CommentSingle","value":"# Target selector\n"},
40+
{"type":"KeywordReserved","value":"gamemode"},
41+
{"type":"TextWhitespace","value":" "},
42+
{"type":"Text","value":"creative"},
43+
{"type":"TextWhitespace","value":" "},
44+
{"type":"KeywordConstant","value":"@a"},
45+
{"type":"TextWhitespace","value":"\n\n"},
46+
{"type":"CommentSingle","value":"# With argument\n"},
47+
{"type":"KeywordReserved","value":"kill"},
48+
{"type":"TextWhitespace","value":" "},
49+
{"type":"KeywordConstant","value":"@e"},
50+
{"type":"Punctuation","value":"["},
51+
{"type":"NameAttribute","value":"type"},
52+
{"type":"Punctuation","value":"="},
53+
{"type":"Text","value":"chicken"},
54+
{"type":"Punctuation","value":","},
55+
{"type":"NameAttribute","value":"nested"},
56+
{"type":"Punctuation","value":"={"},
57+
{"type":"NameAttribute","value":"a0"},
58+
{"type":"Punctuation","value":"="},
59+
{"type":"LiteralNumberInteger","value":"0"},
60+
{"type":"Punctuation","value":","},
61+
{"type":"NameAttribute","value":"a1"},
62+
{"type":"Punctuation","value":"="},
63+
{"type":"LiteralNumberInteger","value":"1"},
64+
{"type":"Punctuation","value":"}]"},
65+
{"type":"TextWhitespace","value":"\n\n"},
66+
{"type":"CommentSingle","value":"# Ragne\n"},
67+
{"type":"KeywordReserved","value":"kill"},
68+
{"type":"TextWhitespace","value":" "},
69+
{"type":"KeywordConstant","value":"@e"},
70+
{"type":"Punctuation","value":"["},
71+
{"type":"NameAttribute","value":"y_rotation"},
72+
{"type":"Punctuation","value":"="},
73+
{"type":"LiteralNumberInteger","value":"0"},
74+
{"type":"Punctuation","value":".."},
75+
{"type":"LiteralNumberInteger","value":"180"},
76+
{"type":"Punctuation","value":"]"},
77+
{"type":"TextWhitespace","value":"\n"}
78+
]

0 commit comments

Comments
 (0)