aboutsummaryrefslogtreecommitdiff
path: root/lua/lexers/nim.lua
diff options
context:
space:
mode:
authorqiu-x <alex@alexslomka.xyz>2022-06-29 07:56:51 +0200
committerFelix Van der Jeugt <felix.vanderjeugt@posteo.net>2022-11-29 21:57:18 +0100
commit8a420ecc4c1ed50111464ec66901bd983eaf2dbd (patch)
treef31d2186cafaee6e7f18d32fe99144c3e8148c00 /lua/lexers/nim.lua
parent981b90a203484182feace48471fe2b53dae7676f (diff)
downloadvis-8a420ecc4c1ed50111464ec66901bd983eaf2dbd.tar.gz
vis-8a420ecc4c1ed50111464ec66901bd983eaf2dbd.tar.xz
Resync the lexers with Scintillua
- Resync the lexers with Scintillua - Update the lexer readme - Update `zenburn` theme to fix some highlighting issues - lexers: redirect print function to vis:info() - Fix support for custom style names - As per error message "lexer.delimited_range() is deprecated, use lexer.range()". - Remove remaining `lexer.delimited_range()` call - Set syntax to `nil` if the file type has no matching lexer - Updated Go lexer for Go 1.18. - lexers/dsv: convert to new lexer format (cherry picked from commit 9edbc3cd9ea1d7142b1305840432a3d2739e755a) - lexers/gemini: disable legacy gemini lexer This reverts commit 468f9ee1b027a7ce98b1a249fa1af5888feeb989. It is in legacy format and of questionable quality. Ideally it should be contributed upstream from where it will eventually trickle down to us. - lexers/git-rebase: convert to new lexer format (cherry picked from commit 4000a4cc9ac4a4c2869dfae772b977a82aee8d8c) - lexers/strace: convert to new lexer format (cherry picked from commit e420451320d97eb164f5629c1bcfab0b595be29d) - lexers/typescript: add new upstream lexer revision 28e2b60 (cherry picked from commit 7326e6deecdaa75fa94ae9ebdb653f9f907b33f2) - use `package.searchpath` instead of a local `searchpath` function - Restore `filetype: support filetype detection via hashbang` - Remove redundant comment - Restore gemini lexer
Diffstat (limited to 'lua/lexers/nim.lua')
-rw-r--r--lua/lexers/nim.lua172
1 files changed, 75 insertions, 97 deletions
diff --git a/lua/lexers/nim.lua b/lua/lexers/nim.lua
index d99ef19..af333bb 100644
--- a/lua/lexers/nim.lua
+++ b/lua/lexers/nim.lua
@@ -1,124 +1,102 @@
--- Copyright 2006-2017 Mitchell mitchell.att.foicica.com. See LICENSE.
+-- Copyright 2006-2022 Mitchell. See LICENSE.
-- Nim LPeg lexer.
-local l = require('lexer')
-local token, word_match = l.token, l.word_match
-local P, R, S = lpeg.P, lpeg.R, lpeg.S
+local lexer = require('lexer')
+local token, word_match = lexer.token, lexer.word_match
+local P, S = lpeg.P, lpeg.S
-local M = {_NAME = 'nim'}
+local lex = lexer.new('nim', {fold_by_indentation = true})
-- Whitespace.
-local ws = token(l.WHITESPACE, l.space^1)
-
--- Comments.
-local comment = token(l.COMMENT, '#' * l.nonnewline_esc^0)
-
--- Strings.
-local sq_str = l.delimited_range("'", true)
-local dq_str = l.delimited_range('"', true)
-local triple_dq_str = '"""' * (l.any - '"""')^0 * P('"""')^-1
-local raw_dq_str = 'r' * l.delimited_range('"', false, true)
-local string = token(l.STRING, triple_dq_str + sq_str + dq_str + raw_dq_str)
-
--- Numbers.
-local dec = l.digit^1 * ('_' * l.digit^1)^0
-local hex = '0' * S('xX') * l.xdigit^1 * ('_' * l.xdigit^1)^0
-local bin = '0' * S('bB') * S('01')^1 * ('_' * S('01')^1)^0
-local oct = '0o' * R('07')^1
-local integer = S('+-')^-1 * (bin + hex + oct + dec) *
- ("'" * S('iIuUfF') * (P('8') + '16' + '32' + '64'))^-1
-local float = l.digit^1 * ('_' * l.digit^1)^0 * ('.' * ('_' * l.digit)^0)^-1 *
- S('eE') * S('+-')^-1 * l.digit^1 * ('_' * l.digit^1)^0
-local number = token(l.NUMBER, l.float + integer)
+lex:add_rule('whitespace', token(lexer.WHITESPACE, lexer.space^1))
-- Keywords.
-local keyword = token(l.KEYWORD, word_match({
- 'addr', 'and', 'as', 'asm', 'atomic', 'bind', 'block', 'break', 'case',
- 'cast', 'const', 'continue', 'converter', 'discard', 'distinct', 'div', 'do',
- 'elif', 'else', 'end', 'enum', 'except', 'export', 'finally', 'for', 'from',
- 'generic', 'if', 'import', 'in', 'include', 'interface', 'is', 'isnot',
- 'iterator', 'lambda', 'let', 'macro', 'method', 'mixin', 'mod', 'nil', 'not',
- 'notin', 'object', 'of', 'or', 'out', 'proc', 'ptr', 'raise', 'ref', 'return',
- 'shared', 'shl', 'static', 'template', 'try', 'tuple', 'type', 'var', 'when',
- 'while', 'with', 'without', 'xor', 'yield'
-}, nil, true))
+lex:add_rule('keyword', token(lexer.KEYWORD, word_match({
+ 'addr', 'and', 'as', 'asm', 'atomic', 'bind', 'block', 'break', 'case', 'cast', 'const',
+ 'continue', 'converter', 'discard', 'distinct', 'div', 'do', 'elif', 'else', 'end', 'enum',
+ 'except', 'export', 'finally', 'for', 'from', 'generic', 'if', 'import', 'in', 'include',
+ 'interface', 'is', 'isnot', 'iterator', 'lambda', 'let', 'macro', 'method', 'mixin', 'mod', 'nil',
+ 'not', 'notin', 'object', 'of', 'or', 'out', 'proc', 'ptr', 'raise', 'ref', 'return', 'shared',
+ 'shl', 'static', 'template', 'try', 'tuple', 'type', 'var', 'when', 'while', 'with', 'without',
+ 'xor', 'yield'
+}, true)))
-- Functions.
-local func = token(l.FUNCTION, word_match({
+lex:add_rule('function', token(lexer.FUNCTION, word_match({
-- Procs.
- 'defined', 'definedInScope', 'new', 'unsafeNew', 'internalNew', 'reset',
- 'high', 'low', 'sizeof', 'succ', 'pred', 'inc', 'dec', 'newSeq', 'len',
- 'incl', 'excl', 'card', 'ord', 'chr', 'ze', 'ze64', 'toU8', 'toU16', 'toU32',
- 'abs', 'min', 'max', 'contains', 'cmp', 'setLen', 'newString',
- 'newStringOfCap', 'add', 'compileOption', 'quit', 'shallowCopy', 'del',
- 'delete', 'insert', 'repr', 'toFloat', 'toBiggestFloat', 'toInt',
- 'toBiggestInt', 'addQuitProc', 'substr', 'zeroMem', 'copyMem', 'moveMem',
- 'equalMem', 'swap', 'getRefcount', 'clamp', 'isNil', 'find', 'contains',
- 'pop', 'each', 'map', 'GC_ref', 'GC_unref', 'echo', 'debugEcho',
- 'getTypeInfo', 'Open', 'repopen', 'Close', 'EndOfFile', 'readChar',
- 'FlushFile', 'readAll', 'readFile', 'writeFile', 'write', 'readLine',
- 'writeln', 'getFileSize', 'ReadBytes', 'ReadChars', 'readBuffer',
- 'writeBytes', 'writeChars', 'writeBuffer', 'setFilePos', 'getFilePos',
- 'fileHandle', 'cstringArrayToSeq', 'allocCStringArray', 'deallocCStringArray',
- 'atomicInc', 'atomicDec', 'compareAndSwap', 'setControlCHook',
- 'writeStackTrace', 'getStackTrace', 'alloc', 'alloc0', 'dealloc', 'realloc',
- 'getFreeMem', 'getTotalMem', 'getOccupiedMem', 'allocShared', 'allocShared0',
- 'deallocShared', 'reallocShared', 'IsOnStack', 'GC_addCycleRoot',
- 'GC_disable', 'GC_enable', 'GC_setStrategy', 'GC_enableMarkAndSweep',
- 'GC_disableMarkAndSweep', 'GC_fullCollect', 'GC_getStatistics',
- 'nimDestroyRange', 'getCurrentException', 'getCurrentExceptionMsg', 'onRaise',
- 'likely', 'unlikely', 'rawProc', 'rawEnv', 'finished', 'slurp', 'staticRead',
- 'gorge', 'staticExec', 'rand', 'astToStr', 'InstatiationInfo', 'raiseAssert',
- 'shallow', 'compiles', 'safeAdd', 'locals',
+ 'defined', 'definedInScope', 'new', 'unsafeNew', 'internalNew', 'reset', 'high', 'low', 'sizeof',
+ 'succ', 'pred', 'inc', 'dec', 'newSeq', 'len', 'incl', 'excl', 'card', 'ord', 'chr', 'ze', 'ze64',
+ 'toU8', 'toU16', 'toU32', 'abs', 'min', 'max', 'contains', 'cmp', 'setLen', 'newString',
+ 'newStringOfCap', 'add', 'compileOption', 'quit', 'shallowCopy', 'del', 'delete', 'insert',
+ 'repr', 'toFloat', 'toBiggestFloat', 'toInt', 'toBiggestInt', 'addQuitProc', 'substr', 'zeroMem',
+ 'copyMem', 'moveMem', 'equalMem', 'swap', 'getRefcount', 'clamp', 'isNil', 'find', 'contains',
+ 'pop', 'each', 'map', 'GC_ref', 'GC_unref', 'echo', 'debugEcho', 'getTypeInfo', 'Open', 'repopen',
+ 'Close', 'EndOfFile', 'readChar', 'FlushFile', 'readAll', 'readFile', 'writeFile', 'write',
+ 'readLine', 'writeln', 'getFileSize', 'ReadBytes', 'ReadChars', 'readBuffer', 'writeBytes',
+ 'writeChars', 'writeBuffer', 'setFilePos', 'getFilePos', 'fileHandle', 'cstringArrayToSeq',
+ 'allocCStringArray', 'deallocCStringArray', 'atomicInc', 'atomicDec', 'compareAndSwap',
+ 'setControlCHook', 'writeStackTrace', 'getStackTrace', 'alloc', 'alloc0', 'dealloc', 'realloc',
+ 'getFreeMem', 'getTotalMem', 'getOccupiedMem', 'allocShared', 'allocShared0', 'deallocShared',
+ 'reallocShared', 'IsOnStack', 'GC_addCycleRoot', 'GC_disable', 'GC_enable', 'GC_setStrategy',
+ 'GC_enableMarkAndSweep', 'GC_disableMarkAndSweep', 'GC_fullCollect', 'GC_getStatistics',
+ 'nimDestroyRange', 'getCurrentException', 'getCurrentExceptionMsg', 'onRaise', 'likely',
+ 'unlikely', 'rawProc', 'rawEnv', 'finished', 'slurp', 'staticRead', 'gorge', 'staticExec', 'rand',
+ 'astToStr', 'InstatiationInfo', 'raiseAssert', 'shallow', 'compiles', 'safeAdd', 'locals',
-- Iterators.
'countdown', 'countup', 'items', 'pairs', 'fields', 'fieldPairs', 'lines',
-- Templates.
- 'accumulateResult', 'newException', 'CurrentSourcePath', 'assert', 'doAssert',
- 'onFailedAssert', 'eval',
+ 'accumulateResult', 'newException', 'CurrentSourcePath', 'assert', 'doAssert', 'onFailedAssert',
+ 'eval',
-- Threads.
- 'running', 'joinThread', 'joinThreads', 'createThread', 'threadId',
- 'myThreadId',
+ 'running', 'joinThread', 'joinThreads', 'createThread', 'threadId', 'myThreadId',
-- Channels.
'send', 'recv', 'peek', 'ready'
-}, nil, true))
+}, true)))
-- Types.
-local type = token(l.TYPE , word_match({
- 'int', 'int8', 'int16', 'int32', 'int64', 'uint', 'uint8', 'uint16', 'uint32',
- 'uint64', 'float', 'float32', 'float64', 'bool', 'char', 'string', 'cstring',
- 'pointer', 'Ordinal', 'auto', 'any', 'TSignedInt', 'TUnsignedInt', 'TInteger',
- 'TOrdinal', 'TReal', 'TNumber', 'range', 'array', 'openarray', 'varargs',
- 'seq', 'set', 'TSlice', 'TThread', 'TChannel',
+lex:add_rule('type', token(lexer.TYPE, word_match({
+ 'int', 'int8', 'int16', 'int32', 'int64', 'uint', 'uint8', 'uint16', 'uint32', 'uint64', 'float',
+ 'float32', 'float64', 'bool', 'char', 'string', 'cstring', 'pointer', 'Ordinal', 'auto', 'any',
+ 'TSignedInt', 'TUnsignedInt', 'TInteger', 'TOrdinal', 'TReal', 'TNumber', 'range', 'array',
+ 'openarray', 'varargs', 'seq', 'set', 'TSlice', 'TThread', 'TChannel',
-- Meta Types.
- 'expr', 'stmt', 'typeDesc', 'void',
-}, nil, true))
+ 'expr', 'stmt', 'typeDesc', 'void'
+}, true)))
-- Constants.
-local constant = token(l.CONSTANT, word_match{
- 'on', 'off', 'isMainModule', 'CompileDate', 'CompileTime', 'NimVersion',
- 'NimMajor', 'NimMinor', 'NimPatch', 'cpuEndian', 'hostOS', 'hostCPU',
- 'appType', 'QuitSuccess', 'QuitFailure', 'inf', 'neginf', 'nan'
-})
+lex:add_rule('constant', token(lexer.CONSTANT, word_match{
+ 'on', 'off', 'isMainModule', 'CompileDate', 'CompileTime', 'NimVersion', 'NimMajor', 'NimMinor',
+ 'NimPatch', 'cpuEndian', 'hostOS', 'hostCPU', 'appType', 'QuitSuccess', 'QuitFailure', 'inf',
+ 'neginf', 'nan'
+}))
+
+-- Strings.
+local sq_str = lexer.range("'", true)
+local dq_str = lexer.range('"', true)
+local tq_str = lexer.range('"""')
+local raw_str = 'r' * lexer.range('"', false, false)
+lex:add_rule('string', token(lexer.STRING, tq_str + sq_str + dq_str + raw_str))
-- Identifiers.
-local identifier = token(l.IDENTIFIER, l.word)
+lex:add_rule('identifier', token(lexer.IDENTIFIER, lexer.word))
--- Operators.
-local operator = token(l.OPERATOR, S('=+-*/<>@$~&%|!?^.:\\`()[]{},;'))
+-- Comments.
+local line_comment = lexer.to_eol('#', true)
+local block_comment = lexer.range('#[', ']#')
+lex:add_rule('comment', token(lexer.COMMENT, block_comment + line_comment))
-M._rules = {
- {'whitespace', ws},
- {'keyword', keyword},
- {'function', func},
- {'type', type},
- {'constant', constant},
- {'identifier', identifier},
- {'comment', comment},
- {'string', string},
- {'number', number},
- {'operator', operator},
-}
+-- Numbers.
+local dec = lexer.digit^1 * ('_' * lexer.digit^1)^0
+local hex = '0' * S('xX') * lexer.xdigit^1 * ('_' * lexer.xdigit^1)^0
+local bin = '0' * S('bB') * S('01')^1 * ('_' * S('01')^1)^0 * -lexer.xdigit
+local oct = '0o' * lpeg.R('07')^1
+local integer = S('+-')^-1 * (bin + hex + oct + dec) *
+ ("'" * S('iIuUfF') * (P('8') + '16' + '32' + '64'))^-1
+local float = lexer.digit^1 * ('_' * lexer.digit^1)^0 * ('.' * ('_' * lexer.digit)^0)^-1 * S('eE') *
+ S('+-')^-1 * lexer.digit^1 * ('_' * lexer.digit^1)^0
+lex:add_rule('number', token(lexer.NUMBER, float + integer))
-M._FOLDBYINDENTATION = true
+-- Operators.
+lex:add_rule('operator', token(lexer.OPERATOR, S('=+-*/<>@$~&%|!?^.:\\`()[]{},;')))
-return M
+return lex