logo

oasis-root

Compiled tree of Oasis Linux based on own branch at <https://hacktivis.me/git/oasis/> git clone https://anongit.hacktivis.me/git/oasis-root.git

nim.lua (4798B)


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