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