1 -- Copyright 2006-2015 Mitchell mitchell.att.foicica.com. See LICENSE.
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'}
11 local ws
= token(l
.WHITESPACE
, l
.space^
1)
14 local comment
= token(l
.COMMENT
, '#' * l
.nonnewline_esc^
0)
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
)
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
)
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'
47 local func
= token(l
.FUNCTION
, word_match({
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',
74 'countdown', 'countup', 'items', 'pairs', 'fields', 'fieldPairs', 'lines',
76 'accumulateResult', 'newException', 'CurrentSourcePath', 'assert', 'doAssert',
77 'onFailedAssert', 'eval',
79 'running', 'joinThread', 'joinThreads', 'createThread', 'threadId',
82 'send', 'recv', 'peek', 'ready'
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',
93 'expr', 'stmt', 'typeDesc', 'void',
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'
104 local identifier
= token(l
.IDENTIFIER
, l
.word
)
107 local operator
= token(l
.OPERATOR
, S('=+-*/<>@$~&%|!?^.:\\`()[]{},;'))
111 {'keyword', keyword
},
114 {'constant', constant
},
115 {'identifier', identifier
},
116 {'comment', comment
},
119 {'operator', operator
},
122 M
._FOLDBYINDENTATION
= true