Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ Plugin | Description
[`language_ksy`](https://raw.githubusercontent.com/whiteh0le/lite-plugins/main/plugins/language_ksy.lua?raw=1) | Syntax for [Kaitai](http://kaitai.io/) struct files
[`language_make`](plugins/language_make.lua?raw=1) | Syntax for the Make build system language
[`language_meson`](plugins/language_meson.lua?raw=1) | Syntax for the [Meson](https://mesonbuild.com) build system language
[`language_nim`](plugins/language_nim.lua?raw=1) | Syntax for the [Nim](https://nim-lang.org) programming language
[`language_odin`](plugins/language_odin.lua?raw=1) | Syntax for the [Odin](https://github.com/odin-lang/Odin) programming language
[`language_php`](plugins/language_php.lua?raw=1) | Syntax for the [PHP](https://php.net) programming language
[`language_pico8`](plugins/language_pico8.lua?raw=1) | Syntax for [Pico-8](https://www.lexaloffle.com/pico-8.php) cartridge files
Expand Down
120 changes: 120 additions & 0 deletions plugins/language_nim.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
local syntax = require "core.syntax"

local patterns = {}

local symbols = {
["nil"] = "literal",
["true"] = "literal",
["false"] = "literal",
}

local number_patterns = {
"0[bB][01][01_]*",
"0o[0-7][0-7_]*",
"0[xX]%x[%x_]*",
"%d[%d_]*%.%d[%d_]*[eE][-+]?%d[%d_]*",
"%d[%d_]*%.%d[%d_]*",
"%d[%d_]*",
}

local type_suffix_patterns = {}

for _, size in ipairs({"", "8", "16", "32", "64"}) do
table.insert(type_suffix_patterns, "'?[fuiFUI]"..size)
end

for _, pattern in ipairs(number_patterns) do
for _, suffix in ipairs(type_suffix_patterns) do
table.insert(patterns, { pattern = pattern..suffix, type = "literal" })
end
table.insert(patterns, { pattern = pattern, type = "literal" })
end

local keywords = {
"addr", "and", "as", "asm",
"bind", "block", "break",
"case", "cast", "concept", "const", "continue", "converter",
"defer", "discard", "distinct", "div", "do",
"elif", "else", "end", "enum", "except", "export",
"finally", "for", "from", "func",
"if", "import", "in", "include", "interface", "is", "isnot", "iterator",
"let",
"macro", "method", "mixin", "mod",
"not", "notin",
"object", "of", "or", "out",
"proc", "ptr",
"raise", "ref", "return",
"shl", "shr", "static",
"template", "try", "tuple", "type",
"using",
"var",
"when", "while",
"xor",
"yield",
}

for _, keyword in ipairs(keywords) do
symbols[keyword] = "keyword"
end

local standard_types = {
"bool", "byte",
"int", "int8", "int16", "int32", "int64",
"uint", "uint8", "uint16", "uint32", "uint64",
"float", "float32", "float64",
"char", "string", "cstring",
"pointer",
"typedesc",
"void", "auto", "any",
"untyped", "typed",
"clong", "culong", "cchar", "cschar", "cshort", "cint", "csize", "csize_t",
"clonglong", "cfloat", "cdouble", "clongdouble", "cuchar", "cushort",
"cuint", "culonglong", "cstringArray",
}

for _, type in ipairs(standard_types) do
symbols[type] = "keyword2"
end

local standard_generic_types = {
"range",
"array", "open[aA]rray", "varargs", "seq", "set",
"sink", "lent", "owned",
}

for _, type in ipairs(standard_generic_types) do
table.insert(patterns, { pattern = type.."%f[%[]", type = "keyword2" })
table.insert(patterns, { pattern = type.." +%f[%w]", type = "keyword2" })
end

local user_patterns = {
-- comments
{ pattern = { "##?%[", "]##?" }, type = "comment" },
{ pattern = "##?.-\n", type = "comment" },
-- strings and chars
{ pattern = { '"', '"', '\\' }, type = "string" },
{ pattern = { '"""', '"""[^"]' }, type = "string" },
{ pattern = { "'", "'", '\\' }, type = "literal" },
-- function calls
{ pattern = "[a-zA-Z][a-zA-Z0-9_]*%f[(]", type = "function" },
-- identifiers
{ pattern = "[A-Z][a-zA-Z0-9_]*", type = "keyword2" },
{ pattern = "[a-zA-Z][a-zA-Z0-9_]*", type = "symbol" },
-- operators
{ pattern = "%.%f[^.]", type = "normal" },
{ pattern = ":%f[ ]", type = "normal" },
{ pattern = "[=+%-*/<>@$~&%%|!?%^&.:\\]+", type = "operator" },
}

for _, pattern in ipairs(user_patterns) do
table.insert(patterns, pattern)
end

local nim = {
files = { "%.nim$", "%.nims$", "%.nimble$" },
comment = "#",
patterns = patterns,
symbols = symbols,
}

syntax.add(nim)