Skip to content

Commit f09329a

Browse files
kofukalecthomas
authored andcommitted
Add lexer for Meson build system
Ported pygments' lexer using pygments2chroma.py.
1 parent a0e9618 commit f09329a

File tree

3 files changed

+129
-0
lines changed

3 files changed

+129
-0
lines changed

lexers/m/meson.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package m
2+
3+
import (
4+
. "github.com/alecthomas/chroma" // nolint
5+
"github.com/alecthomas/chroma/lexers/internal"
6+
)
7+
8+
// Meson lexer.
9+
var Meson = internal.Register(MustNewLazyLexer(
10+
&Config{
11+
Name: "Meson",
12+
Aliases: []string{"meson", "meson.build"},
13+
Filenames: []string{"meson.build", "meson_options.txt"},
14+
MimeTypes: []string{"text/x-meson"},
15+
},
16+
func() Rules {
17+
return Rules{
18+
"root": {
19+
{`#.*?$`, Comment, nil},
20+
{`'''.*'''`, LiteralStringSingle, nil},
21+
{`[1-9][0-9]*`, LiteralNumberInteger, nil},
22+
{`0o[0-7]+`, LiteralNumberOct, nil},
23+
{`0x[a-fA-F0-9]+`, LiteralNumberHex, nil},
24+
Include("string"),
25+
Include("keywords"),
26+
Include("expr"),
27+
{`[a-zA-Z_][a-zA-Z_0-9]*`, Name, nil},
28+
{`\s+`, TextWhitespace, nil},
29+
},
30+
"string": {
31+
{`[']{3}([']{0,2}([^\\']|\\(.|\n)))*[']{3}`, LiteralString, nil},
32+
{`'.*?(?<!\\)(\\\\)*?'`, LiteralString, nil},
33+
},
34+
"keywords": {
35+
{Words(``, `\b`, `if`, `elif`, `else`, `endif`, `foreach`, `endforeach`, `break`, `continue`), Keyword, nil},
36+
},
37+
"expr": {
38+
{`(in|and|or|not)\b`, OperatorWord, nil},
39+
{`(\*=|/=|%=|\+]=|-=|==|!=|\+|-|=)`, Operator, nil},
40+
{`[\[\]{}:().,?]`, Punctuation, nil},
41+
{Words(``, `\b`, `true`, `false`), KeywordConstant, nil},
42+
Include("builtins"),
43+
{Words(``, `\b`, `meson`, `build_machine`, `host_machine`, `target_machine`), NameVariableMagic, nil},
44+
},
45+
"builtins": {
46+
{Words(`(?<!\.)`, `\b`, `add_global_arguments`, `add_global_link_arguments`, `add_languages`, `add_project_arguments`, `add_project_link_arguments`, `add_test_setup`, `assert`, `benchmark`, `both_libraries`, `build_target`, `configuration_data`, `configure_file`, `custom_target`, `declare_dependency`, `dependency`, `disabler`, `environment`, `error`, `executable`, `files`, `find_library`, `find_program`, `generator`, `get_option`, `get_variable`, `include_directories`, `install_data`, `install_headers`, `install_man`, `install_subdir`, `is_disabler`, `is_variable`, `jar`, `join_paths`, `library`, `message`, `project`, `range`, `run_command`, `set_variable`, `shared_library`, `shared_module`, `static_library`, `subdir`, `subdir_done`, `subproject`, `summary`, `test`, `vcs_tag`, `warning`), NameBuiltin, nil},
47+
{`(?<!\.)import\b`, NameNamespace, nil},
48+
},
49+
}
50+
},
51+
))

lexers/testdata/meson.actual

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
project('foo', 'cpp', default_options : ['warning_level=3'])
2+
3+
required_modules = ['gnome']
4+
5+
foreach mod in required_modules
6+
import(mod)
7+
endforeach
8+
9+
# Comment
10+
zlib_dep = dependency('zlib')
11+
executable('''bar''', 'main.cc', dependencies : zlib_dep)

lexers/testdata/meson.expected

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
[
2+
{"type":"NameBuiltin","value":"project"},
3+
{"type":"Punctuation","value":"("},
4+
{"type":"LiteralString","value":"'foo'"},
5+
{"type":"Punctuation","value":","},
6+
{"type":"TextWhitespace","value":" "},
7+
{"type":"LiteralString","value":"'cpp'"},
8+
{"type":"Punctuation","value":","},
9+
{"type":"TextWhitespace","value":" "},
10+
{"type":"Name","value":"default_options"},
11+
{"type":"TextWhitespace","value":" "},
12+
{"type":"Punctuation","value":":"},
13+
{"type":"TextWhitespace","value":" "},
14+
{"type":"Punctuation","value":"["},
15+
{"type":"LiteralString","value":"'warning_level=3'"},
16+
{"type":"Punctuation","value":"])"},
17+
{"type":"TextWhitespace","value":"\n\n"},
18+
{"type":"Name","value":"required_modules"},
19+
{"type":"TextWhitespace","value":" "},
20+
{"type":"Operator","value":"="},
21+
{"type":"TextWhitespace","value":" "},
22+
{"type":"Punctuation","value":"["},
23+
{"type":"LiteralString","value":"'gnome'"},
24+
{"type":"Punctuation","value":"]"},
25+
{"type":"TextWhitespace","value":"\n\n"},
26+
{"type":"Keyword","value":"foreach"},
27+
{"type":"TextWhitespace","value":" "},
28+
{"type":"Name","value":"mod"},
29+
{"type":"TextWhitespace","value":" "},
30+
{"type":"OperatorWord","value":"in"},
31+
{"type":"TextWhitespace","value":" "},
32+
{"type":"Name","value":"required_modules"},
33+
{"type":"TextWhitespace","value":"\n "},
34+
{"type":"NameNamespace","value":"import"},
35+
{"type":"Punctuation","value":"("},
36+
{"type":"Name","value":"mod"},
37+
{"type":"Punctuation","value":")"},
38+
{"type":"TextWhitespace","value":"\n"},
39+
{"type":"Keyword","value":"endforeach"},
40+
{"type":"TextWhitespace","value":"\n\n"},
41+
{"type":"Comment","value":"# Comment"},
42+
{"type":"TextWhitespace","value":"\n"},
43+
{"type":"Name","value":"zlib_dep"},
44+
{"type":"TextWhitespace","value":" "},
45+
{"type":"Operator","value":"="},
46+
{"type":"TextWhitespace","value":" "},
47+
{"type":"NameBuiltin","value":"dependency"},
48+
{"type":"Punctuation","value":"("},
49+
{"type":"LiteralString","value":"'zlib'"},
50+
{"type":"Punctuation","value":")"},
51+
{"type":"TextWhitespace","value":"\n"},
52+
{"type":"NameBuiltin","value":"executable"},
53+
{"type":"Punctuation","value":"("},
54+
{"type":"LiteralStringSingle","value":"'''bar'''"},
55+
{"type":"Punctuation","value":","},
56+
{"type":"TextWhitespace","value":" "},
57+
{"type":"LiteralString","value":"'main.cc'"},
58+
{"type":"Punctuation","value":","},
59+
{"type":"TextWhitespace","value":" "},
60+
{"type":"Name","value":"dependencies"},
61+
{"type":"TextWhitespace","value":" "},
62+
{"type":"Punctuation","value":":"},
63+
{"type":"TextWhitespace","value":" "},
64+
{"type":"Name","value":"zlib_dep"},
65+
{"type":"Punctuation","value":")"},
66+
{"type":"TextWhitespace","value":"\n"}
67+
]

0 commit comments

Comments
 (0)