Skip to content

Commit c8fde8b

Browse files
authored
Initial Nim language support (#6123)
1 parent 5d7c90c commit c8fde8b

File tree

5 files changed

+412
-0
lines changed

5 files changed

+412
-0
lines changed

book/src/generated/lang-support.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
| msbuild || || |
8989
| nasm ||| | |
9090
| nickel || || `nls` |
91+
| nim |||| `nimlangserver` |
9192
| nix || | | `nil` |
9293
| nu || | | |
9394
| ocaml || || `ocamllsp` |

languages.toml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2398,3 +2398,26 @@ grammar = "rego"
23982398
[[grammar]]
23992399
name = "rego"
24002400
source = { git = "https://github.com/FallenAngel97/tree-sitter-rego", rev = "b2667c975f07b33be3ceb83bea5cfbad88095866" }
2401+
2402+
[[language]]
2403+
name = "nim"
2404+
scope = "source.nim"
2405+
injection-regex = "nim"
2406+
file-types = ["nim", "nims", "nimble"]
2407+
shebangs = []
2408+
roots = []
2409+
comment-token = "#"
2410+
indent = { tab-width = 2, unit = " " }
2411+
language-server = { command = "nimlangserver" }
2412+
2413+
[language.auto-pairs]
2414+
'(' = ')'
2415+
'[' = ']'
2416+
'"' = '"'
2417+
"'" = "'"
2418+
'{' = '}'
2419+
2420+
# Nim's tree-sitter grammar is in heavy development.
2421+
[[grammar]]
2422+
name = "nim"
2423+
source = { git = "https://github.com/aMOPel/tree-sitter-nim", rev = "240239b232550e431d67de250d1b5856209e7f06" }

runtime/queries/nim/highlights.scm

Lines changed: 315 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,315 @@
1+
;; Constants, Comments, and Literals
2+
3+
(comment) @comment.line
4+
(multilineComment) @comment.block
5+
(docComment) @comment.block.documentation
6+
(multilineDocComment) @comment.block.documentation
7+
; comments
8+
9+
[(literal) (generalizedLit)] @constant
10+
[(nil_lit)] @constant.builtin
11+
[(bool_lit)] @constant.builtin.boolean
12+
[(char_lit)] @constant.character
13+
[(char_esc_seq) (str_esc_seq)] @constant.character.escape
14+
[(custom_numeric_lit)] @constant.numeric
15+
[(int_lit) (int_suffix)] @constant.numeric.integer
16+
[(float_lit) (float_suffix)] @constant.numeric.float
17+
; literals
18+
; note: somewhat irritatingly for testing, lits have the same syntax highlighting as types
19+
20+
[
21+
(str_lit)
22+
(triplestr_lit)
23+
(rstr_lit)
24+
(generalized_str_lit)
25+
(generalized_triplestr_lit)
26+
(interpolated_str_lit)
27+
(interpolated_triplestr_lit)
28+
] @string
29+
; [] @string.regexp
30+
; string literals
31+
32+
[
33+
"."
34+
","
35+
";"
36+
":"
37+
] @punctuation.delimiter
38+
[
39+
"("
40+
")"
41+
"["
42+
"]"
43+
"{"
44+
"}"
45+
"{."
46+
".}"
47+
"#["
48+
"]#"
49+
] @punctuation.bracket
50+
(interpolated_str_lit "&" @punctuation.special)
51+
(interpolated_str_lit "{" @punctuation.special)
52+
(interpolated_str_lit "}" @punctuation.special)
53+
; punctuation
54+
55+
[
56+
"and"
57+
"or"
58+
"xor"
59+
"not"
60+
"in"
61+
"notin"
62+
"is"
63+
"isnot"
64+
"div"
65+
"mod"
66+
"shl"
67+
"shr"
68+
] @keyword.operator
69+
; operators: we list them explicitly to deliminate them from symbolic operators
70+
71+
[(operator) (opr) "="] @operator
72+
; all operators (must come after @keyword.operator)
73+
74+
(pragma) @attribute
75+
; pragmas
76+
77+
78+
;; Imports and Exports
79+
80+
(importStmt
81+
(keyw) @keyword.control.import
82+
(expr (primary (symbol) @namespace))?
83+
(expr (primary (arrayConstr (exprColonExprList (exprColonExpr (expr (primary (symbol) @namespace)))))))?)
84+
(exportStmt
85+
(keyw) @keyword.control.import
86+
(expr (primary (symbol) @namespace))?
87+
(expr (primary (arrayConstr (exprColonExprList (exprColonExpr (expr (primary (symbol) @namespace)))))))?)
88+
(fromStmt
89+
(keyw) @keyword.control.import
90+
(expr (primary (symbol) @namespace))?
91+
(expr (primary (arrayConstr (exprColonExprList (exprColonExpr (expr (primary (symbol) @namespace)))))))?)
92+
(includeStmt
93+
(keyw) @keyword.control.import
94+
(expr (primary (symbol) @namespace))?
95+
(expr (primary (arrayConstr (exprColonExprList (exprColonExpr (expr (primary (symbol) @namespace)))))))?)
96+
(importExceptStmt
97+
(keyw) @keyword.control.import
98+
(expr (primary (symbol) @namespace))?
99+
(expr (primary (arrayConstr (exprColonExprList (exprColonExpr (expr (primary (symbol) @namespace)))))))?)
100+
; import statements
101+
; yeah, this is a bit gross.
102+
103+
104+
;; Control Flow
105+
106+
(ifStmt (keyw) @keyword.control.conditional)
107+
(whenStmt (keyw) @keyword.control.conditional)
108+
(elifStmt (keyw) @keyword.control.conditional)
109+
(elseStmt (keyw) @keyword.control.conditional)
110+
(caseStmt (keyw) @keyword.control.conditional)
111+
(ofBranch (keyw) @keyword.control.conditional)
112+
(inlineIfStmt (keyw) @keyword.control.conditional)
113+
(inlineWhenStmt (keyw) @keyword.control.conditional)
114+
; conditional statements
115+
; todo: do block
116+
117+
(forStmt
118+
. (keyw) @keyword.control.repeat
119+
. (symbol) @variable
120+
. (keyw) @keyword.control.repeat)
121+
(whileStmt (keyw) @keyword.control.repeat)
122+
; loop statements
123+
124+
(returnStmt (keyw) @keyword.control.repeat)
125+
(yieldStmt (keyw) @keyword.control.repeat)
126+
(discardStmt (keyw) @keyword.control.repeat)
127+
(breakStmt (keyw) @keyword.control.repeat)
128+
(continueStmt (keyw) @keyword.control.repeat)
129+
; control flow statements
130+
131+
(raiseStmt (keyw) @keyword.control.exception)
132+
(tryStmt (keyw) @keyword.control.exception)
133+
(tryExceptStmt (keyw) @keyword.control.exception)
134+
(tryFinallyStmt (keyw) @keyword.control.exception)
135+
(inlineTryStmt (keyw) @keyword.control.exception)
136+
; (inlineTryExceptStmt (keyw) @keyword.control.exception)
137+
; (inlineTryFinallyStmt (keyw) @keyword.control.exception)
138+
; exception handling statements
139+
140+
(staticStmt (keyw) @keyword)
141+
(deferStmt (keyw) @keyword)
142+
(asmStmt (keyw) @keyword)
143+
(bindStmt (keyw) @keyword)
144+
(mixinStmt (keyw) @keyword)
145+
; miscellaneous blocks
146+
147+
(blockStmt
148+
(keyw) @keyword.control
149+
(symbol) @label)
150+
; block statements
151+
152+
153+
;; Types and Type Declarations
154+
155+
(typeDef
156+
(keyw) @keyword.storage.type
157+
(symbol) @type)
158+
; names of new types type declarations
159+
160+
(exprColonEqExpr
161+
. (expr (primary (symbol) @variable))
162+
. (expr (primary (symbol) @type)))
163+
; variables in inline tuple declarations
164+
165+
(primarySuffix
166+
(indexSuffix
167+
(exprColonEqExprList
168+
(exprColonEqExpr
169+
(expr
170+
(primary
171+
(symbol) @type))))))
172+
; nested types in brackets, i.e. seq[string]
173+
174+
(primaryTypeDef (symbol) @type)
175+
; primary types of type declarations (NOT nested types)
176+
177+
(primaryTypeDef (primaryPrefix (keyw) @type))
178+
; for consistency
179+
180+
(primaryTypeDesc (symbol) @type)
181+
; type annotations, on declarations or in objects
182+
183+
(primaryTypeDesc (primaryPrefix (keyw) @type))
184+
; var types etc
185+
186+
(genericParamList (genericParam (symbol) @type))
187+
; types in generic blocks
188+
189+
(enumDecl (keyw) @keyword.storage.type)
190+
(enumElement (symbol) @type.enum.variant)
191+
; enum declarations and elements
192+
193+
(tupleDecl (keyw) @keyword.storage.type)
194+
; tuple declarations
195+
196+
(objectDecl (keyw) @keyword.storage.type)
197+
(objectPart (symbol) @variable.other.member)
198+
; object declarations and fields
199+
200+
(objectCase
201+
(keyw) @keyword.control.conditional
202+
(symbol) @variable.other.member)
203+
(objectBranch (keyw) @keyword.control.conditional)
204+
(objectElif (keyw) @keyword.control.conditional)
205+
(objectElse (keyw) @keyword.control.conditional)
206+
(objectWhen (keyw) @keyword.control.conditional)
207+
; variant objects
208+
209+
(conceptDecl (keyw) @keyword.storage.type)
210+
(conceptParam (keyw) @type)
211+
(conceptParam (symbol) @variable)
212+
; concept declarations, parameters, and qualifiers on those parameters
213+
214+
((expr
215+
(primary (symbol))
216+
(operator) @operator
217+
(primary (symbol) @type))
218+
(#match? @operator "is"))
219+
((exprStmt
220+
(primary (symbol))
221+
(operator) @operator
222+
(primary (symbol) @type))
223+
(#match? @operator "is"))
224+
; symbols likely to be types: "x is t" means t is either a type or a type variable
225+
226+
; distinct?
227+
228+
229+
;; Functions
230+
231+
(routine
232+
. (keyw) @keyword.function
233+
. (symbol) @function)
234+
; function declarations
235+
236+
(routineExpr (keyw) @keyword.function)
237+
; discarded function
238+
239+
(routineExprTypeDesc (keyw) @keyword.function)
240+
; function declarations as types
241+
242+
(primary
243+
. (symbol) @function.call
244+
. (primarySuffix (functionCall)))
245+
; regular function calls
246+
247+
(primary
248+
. (symbol) @function.call
249+
. (primarySuffix (cmdCall)))
250+
; function calls without parenthesis
251+
252+
(primary
253+
(primarySuffix (qualifiedSuffix (symbol) @function.call))
254+
. (primarySuffix (functionCall)))
255+
; uniform function call syntax calls
256+
257+
(primary
258+
(primarySuffix (qualifiedSuffix (symbol) @function.call))
259+
. (primarySuffix (cmdCall)))
260+
; just in case
261+
262+
(primary
263+
(symbol) @constructor
264+
(primarySuffix (objectConstr)))
265+
; object constructor
266+
267+
; does not appear to be a way to distinguish these without verbatium matching
268+
; [] @function.builtin
269+
; [] @function.method
270+
; [] @function.macro
271+
; [] @function.special
272+
273+
274+
;; Variables
275+
276+
(paramList (paramColonEquals (symbol) @variable.parameter))
277+
; parameter identifiers
278+
279+
(identColon (ident) @variable.other.member)
280+
; named parts of tuples
281+
282+
(symbolColonExpr (symbol) @variable)
283+
; object constructor parameters
284+
285+
(symbolEqExpr (symbol) @variable)
286+
; named parameters
287+
288+
(variable
289+
(keyw) @keyword.storage.type
290+
(declColonEquals (symbol) @variable))
291+
; let, var, const expressions
292+
293+
((primary (symbol) @variable.builtin)
294+
(#match? @variable.builtin "result"))
295+
; `result` is an implicit builtin variable inside function scopes
296+
297+
((primary (symbol) @type)
298+
(#match? @type "^[A-Z]"))
299+
; assume PascalCase identifiers to be types
300+
301+
((primary
302+
(primarySuffix
303+
(qualifiedSuffix
304+
(symbol) @type)))
305+
(#match? @type "^[A-Z]"))
306+
; assume PascalCase member variables to be enum entries
307+
308+
(primary (symbol) @variable)
309+
; overzealous, matches variables
310+
311+
(primary (primarySuffix (qualifiedSuffix (symbol) @variable.other.member)))
312+
; overzealous, matches member variables: i.e. x in foo.x
313+
314+
(keyw) @keyword
315+
; more specific matches are done above whenever possible

0 commit comments

Comments
 (0)