Repository: alecthomas/chroma
Pull request #532 (2021-07-22 15:20:20)

Add lexer for Meson build system
Ported pygments' lexer using `pygments2chroma.py`.


Patch:
diff --git a/lexers/m/meson.go b/lexers/m/meson.go
new file mode 100644
index 0000000..2a6a22f
--- /dev/null
+++ b/lexers/m/meson.go
@@ -0,0 +1,51 @@
+package m
+
+import (
+	. "github.com/alecthomas/chroma" // nolint
+	"github.com/alecthomas/chroma/lexers/internal"
+)
+
+// Meson lexer.
+var Meson = internal.Register(MustNewLazyLexer(
+	&Config{
+		Name:      "Meson",
+		Aliases:   []string{"meson", "meson.build"},
+		Filenames: []string{"meson.build", "meson_options.txt"},
+		MimeTypes: []string{"text/x-meson"},
+	},
+	func() Rules {
+		return Rules{
+			"root": {
+				{`#.*?$`, Comment, nil},
+				{`'''.*'''`, LiteralStringSingle, nil},
+				{`[1-9][0-9]*`, LiteralNumberInteger, nil},
+				{`0o[0-7]+`, LiteralNumberOct, nil},
+				{`0x[a-fA-F0-9]+`, LiteralNumberHex, nil},
+				Include("string"),
+				Include("keywords"),
+				Include("expr"),
+				{`[a-zA-Z_][a-zA-Z_0-9]*`, Name, nil},
+				{`\s+`, TextWhitespace, nil},
+			},
+			"string": {
+				{`[']{3}([']{0,2}([^\\']|\\(.|\n)))*[']{3}`, LiteralString, nil},
+				{`'.*?(?<!\\)(\\\\)*?'`, LiteralString, nil},
+			},
+			"keywords": {
+				{Words(``, `\b`, `if`, `elif`, `else`, `endif`, `foreach`, `endforeach`, `break`, `continue`), Keyword, nil},
+			},
+			"expr": {
+				{`(in|and|or|not)\b`, OperatorWord, nil},
+				{`(\*=|/=|%=|\+]=|-=|==|!=|\+|-|=)`, Operator, nil},
+				{`[\[\]{}:().,?]`, Punctuation, nil},
+				{Words(``, `\b`, `true`, `false`), KeywordConstant, nil},
+				Include("builtins"),
+				{Words(``, `\b`, `meson`, `build_machine`, `host_machine`, `target_machine`), NameVariableMagic, nil},
+			},
+			"builtins": {
+				{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},
+				{`(?<!\.)import\b`, NameNamespace, nil},
+			},
+		}
+	},
+))


Test patch:
diff --git a/lexers/testdata/meson.actual b/lexers/testdata/meson.actual
new file mode 100644
index 0000000..a7d92f3
--- /dev/null
+++ b/lexers/testdata/meson.actual
@@ -0,0 +1,11 @@
+project('foo', 'cpp', default_options : ['warning_level=3'])
+
+required_modules = ['gnome']
+
+foreach mod in required_modules
+  import(mod)
+endforeach
+
+# Comment
+zlib_dep = dependency('zlib')
+executable('''bar''', 'main.cc', dependencies : zlib_dep)
diff --git a/lexers/testdata/meson.expected b/lexers/testdata/meson.expected
new file mode 100644
index 0000000..abc9388
--- /dev/null
+++ b/lexers/testdata/meson.expected
@@ -0,0 +1,67 @@
+[
+  {"type":"NameBuiltin","value":"project"},
+  {"type":"Punctuation","value":"("},
+  {"type":"LiteralString","value":"'foo'"},
+  {"type":"Punctuation","value":","},
+  {"type":"TextWhitespace","value":" "},
+  {"type":"LiteralString","value":"'cpp'"},
+  {"type":"Punctuation","value":","},
+  {"type":"TextWhitespace","value":" "},
+  {"type":"Name","value":"default_options"},
+  {"type":"TextWhitespace","value":" "},
+  {"type":"Punctuation","value":":"},
+  {"type":"TextWhitespace","value":" "},
+  {"type":"Punctuation","value":"["},
+  {"type":"LiteralString","value":"'warning_level=3'"},
+  {"type":"Punctuation","value":"])"},
+  {"type":"TextWhitespace","value":"\n\n"},
+  {"type":"Name","value":"required_modules"},
+  {"type":"TextWhitespace","value":" "},
+  {"type":"Operator","value":"="},
+  {"type":"TextWhitespace","value":" "},
+  {"type":"Punctuation","value":"["},
+  {"type":"LiteralString","value":"'gnome'"},
+  {"type":"Punctuation","value":"]"},
+  {"type":"TextWhitespace","value":"\n\n"},
+  {"type":"Keyword","value":"foreach"},
+  {"type":"TextWhitespace","value":" "},
+  {"type":"Name","value":"mod"},
+  {"type":"TextWhitespace","value":" "},
+  {"type":"OperatorWord","value":"in"},
+  {"type":"TextWhitespace","value":" "},
+  {"type":"Name","value":"required_modules"},
+  {"type":"TextWhitespace","value":"\n  "},
+  {"type":"NameNamespace","value":"import"},
+  {"type":"Punctuation","value":"("},
+  {"type":"Name","value":"mod"},
+  {"type":"Punctuation","value":")"},
+  {"type":"TextWhitespace","value":"\n"},
+  {"type":"Keyword","value":"endforeach"},
+  {"type":"TextWhitespace","value":"\n\n"},
+  {"type":"Comment","value":"# Comment"},
+  {"type":"TextWhitespace","value":"\n"},
+  {"type":"Name","value":"zlib_dep"},
+  {"type":"TextWhitespace","value":" "},
+  {"type":"Operator","value":"="},
+  {"type":"TextWhitespace","value":" "},
+  {"type":"NameBuiltin","value":"dependency"},
+  {"type":"Punctuation","value":"("},
+  {"type":"LiteralString","value":"'zlib'"},
+  {"type":"Punctuation","value":")"},
+  {"type":"TextWhitespace","value":"\n"},
+  {"type":"NameBuiltin","value":"executable"},
+  {"type":"Punctuation","value":"("},
+  {"type":"LiteralStringSingle","value":"'''bar'''"},
+  {"type":"Punctuation","value":","},
+  {"type":"TextWhitespace","value":" "},
+  {"type":"LiteralString","value":"'main.cc'"},
+  {"type":"Punctuation","value":","},
+  {"type":"TextWhitespace","value":" "},
+  {"type":"Name","value":"dependencies"},
+  {"type":"TextWhitespace","value":" "},
+  {"type":"Punctuation","value":":"},
+  {"type":"TextWhitespace","value":" "},
+  {"type":"Name","value":"zlib_dep"},
+  {"type":"Punctuation","value":")"},
+  {"type":"TextWhitespace","value":"\n"}
+]