diff options
Diffstat (limited to 'lua/lexers/zig.lua')
| -rw-r--r-- | lua/lexers/zig.lua | 167 |
1 files changed, 65 insertions, 102 deletions
diff --git a/lua/lexers/zig.lua b/lua/lexers/zig.lua index f8eb74b..efc3a1f 100644 --- a/lua/lexers/zig.lua +++ b/lua/lexers/zig.lua @@ -1,130 +1,93 @@ --- Copyright 2020 Karchnu karchnu@karchnu.fr. +-- Copyright 2020-2022 Karchnu karchnu@karchnu.fr. See LICENSE. -- Zig LPeg lexer. --- (Based on the C++ LPeg lexer from Mitchell mitchell.att.foicica.com.) +-- (Based on the C++ LPeg lexer from Mitchell.) -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 = 'zig'} +local lex = lexer.new('zig') -- Whitespace. -local ws = token(l.WHITESPACE, l.space^1) - --- Comments. -local line_comment = '//' * l.nonnewline_esc^0 -local comment = token(l.COMMENT, line_comment) --- For documentation, I took the liberty of using the preprocessor coloration, --- since it doesn't exist in Zig anyway. -local doc_comment = '///' * l.nonnewline_esc^0 -local preprocessor = token(l.PREPROCESSOR, doc_comment) - --- Strings. -local sq_str = P('L')^-1 * l.delimited_range("'", true) -local dq_str = P('L')^-1 * l.delimited_range('"', true) -local string = token(l.STRING, sq_str + dq_str) - --- Numbers. -local number = token(l.NUMBER, l.float + l.integer) +lex:add_rule('whitespace', token(lexer.WHITESPACE, lexer.space^1)) -- Keywords. -local keyword = token(l.KEYWORD, word_match{ - +lex:add_rule('keyword', token(lexer.KEYWORD, word_match{ -- Keywords. - 'inline', 'pub', 'fn', 'comptime', 'const', - 'extern', 'return', 'var', 'usingnamespace', - + 'inline', 'pub', 'fn', 'comptime', 'const', 'extern', 'return', 'var', 'usingnamespace', -- Defering code blocks. 'defer', 'errdefer', - -- Functions and structures related keywords. - 'align', 'allowzero', - 'noalias', 'noinline', - 'callconv', 'packed', 'linksection', - 'unreachable', - 'test', - 'asm', 'volatile', - + 'align', 'allowzero', 'noalias', 'noinline', 'callconv', 'packed', 'linksection', 'unreachable', + 'test', 'asm', 'volatile', -- Parallelism and concurrency related keywords. - 'async', 'await', 'noasync', - 'suspend', 'nosuspend', 'resume', - 'threadlocal','anyframe', - + 'async', 'await', 'noasync', 'suspend', 'nosuspend', 'resume', 'threadlocalanyframe', -- Control flow: conditions and loops. - 'if', 'else', 'orelse', - 'or', 'and', - 'while', 'for', 'switch', 'continue', 'break', - 'catch', 'try', - - -- Not keyword, but overly used variable name with always the same semantic. - 'self', -}) + 'if', 'else', 'orelse', 'or', 'and', 'while', 'for', 'switch', 'continue', 'break', 'catch', + 'try', + -- Not keyword but overly used variable name with always the same semantic. + 'self' +})) -- Types. -local type = token(l.TYPE, word_match{ - 'enum', 'struct', 'union', - 'i8', 'u8', 'i16', 'u16', 'i32', 'u32', 'i64', 'u64', 'i128', 'u128', - 'isize', 'usize', - 'c_short', 'c_ushort', 'c_int', 'c_uint', - 'c_long', 'c_ulong', 'c_longlong', 'c_ulonglong', 'c_longdouble', - 'c_void', - 'f16', 'f32', 'f64', 'f128', - 'bool', - 'void', - 'noreturn', - 'type', 'anytype', 'error', 'anyerror', - 'comptime_int', 'comptime_float', -}) +lex:add_rule('type', token(lexer.TYPE, word_match{ + 'enum', 'struct', 'union', -- + 'i8', 'u8', 'i16', 'u16', 'i32', 'u32', 'i64', 'u64', 'i128', 'u128', -- + 'isize', 'usize', -- + 'c_short', 'c_ushort', 'c_int', 'c_uint', -- + 'c_long', 'c_ulong', 'c_longlong', 'c_ulonglong', 'c_longdouble', -- + 'c_void', -- + 'f16', 'f32', 'f64', 'f128', -- + 'bool', 'void', 'noreturn', 'type', 'anytype', 'error', 'anyerror', -- + 'comptime_int', 'comptime_float' +})) -- Constants. -local constant = token(l.CONSTANT, word_match{ - -- special values - 'false', 'true', 'null', 'undefined', -}) - --- Identifiers. -local identifier = token(l.IDENTIFIER, l.word) +lex:add_rule('constant', token(lexer.CONSTANT, word_match{ + -- Special values. + 'false', 'true', 'null', 'undefined' +})) -- Built-in functions. -local functions = token(l.FUNCTION, word_match{ +lex:add_rule('function', token(lexer.FUNCTION, '@' * word_match{ 'addWithOverflow', 'alignCast', 'alignOf', 'as', 'asyncCall', 'atomicLoad', 'atomicRmw', 'atomicStore', 'bitCast', 'bitOffsetOf', 'boolToInt', 'bitSizeOf', 'breakpoint', 'mulAdd', 'byteSwap', 'bitReverse', 'byteOffsetOf', 'call', 'cDefine', 'cImport', 'cInclude', 'clz', 'cmpxchgStrong', 'cmpxchgWeak', 'compileError', 'compileLog', 'ctz', 'cUndef', 'divExact', - 'divFloor', 'divTrunc', 'embedFile', 'enumToInt', 'errorName', 'errorReturnTrace', - 'errorToInt', 'errSetCast', 'export', 'fence', 'field', 'fieldParentPtr', 'floatCast', - 'floatToInt', 'frame', 'Frame', 'frameAddress', 'frameSize', 'hasDecl', 'hasField', 'import', - 'intCast', 'intToEnum', 'intToError', 'intToFloat', 'intToPtr', 'memcpy', 'memset', 'wasmMemorySize', - 'wasmMemoryGrow', 'mod', 'mulWithOverflow', 'panic', 'popCount', 'ptrCast', 'ptrToInt', 'rem', - 'returnAddress', 'setAlignStack', 'setCold', 'setEvalBranchQuota', 'setFloatMode', 'setRuntimeSafety', - 'shlExact', 'shlWithOverflow', 'shrExact', 'shuffle', 'sizeOf', 'splat', 'reduce', - 'src', 'sqrt', 'sin', 'cos', 'exp', 'exp2', 'log', 'log2', 'log10', 'fabs', 'floor', - 'ceil', 'trunc', 'round', 'subWithOverflow', 'tagName', 'TagType', 'This', 'truncate', - 'Type', 'typeInfo', 'typeName', 'TypeOf', 'unionInit', -}) + 'divFloor', 'divTrunc', 'embedFile', 'enumToInt', 'errorName', 'errorReturnTrace', 'errorToInt', + 'errSetCast', 'export', 'fence', 'field', 'fieldParentPtr', 'floatCast', 'floatToInt', 'frame', + 'Frame', 'frameAddress', 'frameSize', 'hasDecl', 'hasField', 'import', 'intCast', 'intToEnum', + 'intToError', 'intToFloat', 'intToPtr', 'memcpy', 'memset', 'wasmMemorySize', 'wasmMemoryGrow', + 'mod', 'mulWithOverflow', 'panic', 'popCount', 'ptrCast', 'ptrToInt', 'rem', 'returnAddress', + 'setAlignStack', 'setCold', 'setEvalBranchQuota', 'setFloatMode', 'setRuntimeSafety', 'shlExact', + 'shlWithOverflow', 'shrExact', 'shuffle', 'sizeOf', 'splat', 'reduce', 'src', 'sqrt', 'sin', + 'cos', 'exp', 'exp2', 'log', 'log2', 'log10', 'fabs', 'floor', 'ceil', 'trunc', 'round', + 'subWithOverflow', 'tagName', 'TagType', 'This', 'truncate', 'Type', 'typeInfo', 'typeName', + 'TypeOf', 'unionInit' +})) +-- Strings. +local sq_str = P('L')^-1 * lexer.range("'", true) +local dq_str = P('L')^-1 * lexer.range('"', true) +lex:add_rule('string', token(lexer.STRING, sq_str + dq_str)) --- Operators. -local operator = token(l.OPERATOR, S('+-/*%<>!=^&|?~:;,.()[]{}')) +-- Identifiers. +lex:add_rule('identifier', token(lexer.IDENTIFIER, lexer.word)) -M._rules = { - {'whitespace', ws}, - {'keyword', keyword}, - {'constant', constant}, - {'function', functions}, - {'type', type}, - {'identifier', identifier}, - {'string', string}, - {'preprocessor', preprocessor}, - {'comment', comment}, - {'number', number}, - {'operator', operator}, -} +-- Comments. +lex:add_rule('doc_comment', token('doc_comment', lexer.to_eol('///', true))) +lex:add_style('doc_comment', lexer.styles.comment) +lex:add_rule('comment', token(lexer.COMMENT, lexer.to_eol('//', true))) + +-- Numbers. +lex:add_rule('number', token(lexer.NUMBER, lexer.number)) + +-- Operators. +lex:add_rule('operator', token(lexer.OPERATOR, S('+-/*%<>!=^&|?~:;,.()[]{}'))) -M._foldsymbols = { - _patterns = {'%l+', '[{}]', '/%*', '%*/', '//'}, - [l.OPERATOR] = {['{'] = 1, ['}'] = -1}, - [l.COMMENT] = {['//'] = l.fold_line_comments('//')} -} +-- Fold points. +lex:add_fold_point(lexer.OPERATOR, '{', '}') +lex:add_fold_point(lexer.COMMENT, lexer.fold_consecutive_lines('//')) +lex:add_fold_point(lexer.PREPROCESSOR, lexer.fold_consecutive_lines('///')) -return M +return lex |
