vis
a vi-like editor based on Plan 9's structural regular expressions
git clone https://9o.is/git/vis.git
nim.lua
(4753B)
1 -- Copyright 2006-2025 Mitchell. See LICENSE.
2 -- Nim LPeg lexer.
3
4 local lexer = require('lexer')
5 local token, word_match = lexer.token, lexer.word_match
6 local P, S = lpeg.P, lpeg.S
7
8 local lex = lexer.new('nim', {fold_by_indentation = true})
9
10 -- Whitespace.
11 lex:add_rule('whitespace', token(lexer.WHITESPACE, lexer.space^1))
12
13 -- Keywords.
14 lex:add_rule('keyword', token(lexer.KEYWORD, word_match({
15 'addr', 'and', 'as', 'asm', 'atomic', 'bind', 'block', 'break', 'case', 'cast', 'const',
16 'continue', 'converter', 'discard', 'distinct', 'div', 'do', 'elif', 'else', 'end', 'enum',
17 'except', 'export', 'finally', 'for', 'from', 'generic', 'if', 'import', 'in', 'include',
18 'interface', 'is', 'isnot', 'iterator', 'lambda', 'let', 'macro', 'method', 'mixin', 'mod', 'nil',
19 'not', 'notin', 'object', 'of', 'or', 'out', 'proc', 'ptr', 'raise', 'ref', 'return', 'shared',
20 'shl', 'static', 'template', 'try', 'tuple', 'type', 'var', 'when', 'while', 'with', 'without',
21 'xor', 'yield'
22 }, true)))
23
24 -- Functions.
25 lex:add_rule('function', token(lexer.FUNCTION, word_match({
26 -- Procs.
27 'defined', 'definedInScope', 'new', 'unsafeNew', 'internalNew', 'reset', 'high', 'low', 'sizeof',
28 'succ', 'pred', 'inc', 'dec', 'newSeq', 'len', 'incl', 'excl', 'card', 'ord', 'chr', 'ze', 'ze64',
29 'toU8', 'toU16', 'toU32', 'abs', 'min', 'max', 'contains', 'cmp', 'setLen', 'newString',
30 'newStringOfCap', 'add', 'compileOption', 'quit', 'shallowCopy', 'del', 'delete', 'insert',
31 'repr', 'toFloat', 'toBiggestFloat', 'toInt', 'toBiggestInt', 'addQuitProc', 'substr', 'zeroMem',
32 'copyMem', 'moveMem', 'equalMem', 'swap', 'getRefcount', 'clamp', 'isNil', 'find', 'contains',
33 'pop', 'each', 'map', 'GC_ref', 'GC_unref', 'echo', 'debugEcho', 'getTypeInfo', 'Open', 'repopen',
34 'Close', 'EndOfFile', 'readChar', 'FlushFile', 'readAll', 'readFile', 'writeFile', 'write',
35 'readLine', 'writeln', 'getFileSize', 'ReadBytes', 'ReadChars', 'readBuffer', 'writeBytes',
36 'writeChars', 'writeBuffer', 'setFilePos', 'getFilePos', 'fileHandle', 'cstringArrayToSeq',
37 'allocCStringArray', 'deallocCStringArray', 'atomicInc', 'atomicDec', 'compareAndSwap',
38 'setControlCHook', 'writeStackTrace', 'getStackTrace', 'alloc', 'alloc0', 'dealloc', 'realloc',
39 'getFreeMem', 'getTotalMem', 'getOccupiedMem', 'allocShared', 'allocShared0', 'deallocShared',
40 'reallocShared', 'IsOnStack', 'GC_addCycleRoot', 'GC_disable', 'GC_enable', 'GC_setStrategy',
41 'GC_enableMarkAndSweep', 'GC_disableMarkAndSweep', 'GC_fullCollect', 'GC_getStatistics',
42 'nimDestroyRange', 'getCurrentException', 'getCurrentExceptionMsg', 'onRaise', 'likely',
43 'unlikely', 'rawProc', 'rawEnv', 'finished', 'slurp', 'staticRead', 'gorge', 'staticExec', 'rand',
44 'astToStr', 'InstatiationInfo', 'raiseAssert', 'shallow', 'compiles', 'safeAdd', 'locals',
45 -- Iterators.
46 'countdown', 'countup', 'items', 'pairs', 'fields', 'fieldPairs', 'lines',
47 -- Templates.
48 'accumulateResult', 'newException', 'CurrentSourcePath', 'assert', 'doAssert', 'onFailedAssert',
49 'eval',
50 -- Threads.
51 'running', 'joinThread', 'joinThreads', 'createThread', 'threadId', 'myThreadId',
52 -- Channels.
53 'send', 'recv', 'peek', 'ready'
54 }, true)))
55
56 -- Types.
57 lex:add_rule('type', token(lexer.TYPE, word_match({
58 'int', 'int8', 'int16', 'int32', 'int64', 'uint', 'uint8', 'uint16', 'uint32', 'uint64', 'float',
59 'float32', 'float64', 'bool', 'char', 'string', 'cstring', 'pointer', 'Ordinal', 'auto', 'any',
60 'TSignedInt', 'TUnsignedInt', 'TInteger', 'TOrdinal', 'TReal', 'TNumber', 'range', 'array',
61 'openarray', 'varargs', 'seq', 'set', 'TSlice', 'TThread', 'TChannel',
62 -- Meta Types.
63 'expr', 'stmt', 'typeDesc', 'void'
64 }, true)))
65
66 -- Constants.
67 lex:add_rule('constant', token(lexer.CONSTANT, word_match{
68 'on', 'off', 'isMainModule', 'CompileDate', 'CompileTime', 'NimVersion', 'NimMajor', 'NimMinor',
69 'NimPatch', 'cpuEndian', 'hostOS', 'hostCPU', 'appType', 'QuitSuccess', 'QuitFailure', 'inf',
70 'neginf', 'nan'
71 }))
72
73 -- Strings.
74 local sq_str = lexer.range("'", true)
75 local dq_str = lexer.range('"', true)
76 local tq_str = lexer.range('"""')
77 local raw_str = 'r' * lexer.range('"', false, false)
78 lex:add_rule('string', token(lexer.STRING, tq_str + sq_str + dq_str + raw_str))
79
80 -- Identifiers.
81 lex:add_rule('identifier', token(lexer.IDENTIFIER, lexer.word))
82
83 -- Comments.
84 local line_comment = lexer.to_eol('#', true)
85 local block_comment = lexer.range('#[', ']#')
86 lex:add_rule('comment', token(lexer.COMMENT, block_comment + line_comment))
87
88 -- Numbers.
89 lex:add_rule('number', token(lexer.NUMBER, lexer.float_('_') + lexer.integer_('_') *
90 ("'" * S('iIuUfF') * (P('8') + '16' + '32' + '64'))^-1))
91
92 -- Operators.
93 lex:add_rule('operator', token(lexer.OPERATOR, S('=+-*/<>@$~&%|!?^.:\\`()[]{},;')))
94
95 lexer.property['scintillua.comment'] = '#'
96
97 return lex