vis
a vi-like editor based on Plan 9's structural regular expressions
git clone https://9o.is/git/vis.git
zig.lua
(3773B)
1 -- Copyright 2020-2025 Karchnu karchnu@karchnu.fr. See LICENSE.
2 -- Zig LPeg lexer.
3 -- (Based on the C++ LPeg lexer from Mitchell.)
4
5 local lexer = require('lexer')
6 local token, word_match = lexer.token, lexer.word_match
7 local P, S = lpeg.P, lpeg.S
8
9 local lex = lexer.new('zig')
10
11 -- Whitespace.
12 lex:add_rule('whitespace', token(lexer.WHITESPACE, lexer.space^1))
13
14 -- Keywords.
15 lex:add_rule('keyword', token(lexer.KEYWORD, word_match{
16 -- Keywords.
17 'inline', 'pub', 'fn', 'comptime', 'const', 'extern', 'return', 'var', 'usingnamespace',
18 -- Defering code blocks.
19 'defer', 'errdefer',
20 -- Functions and structures related keywords.
21 'align', 'allowzero', 'noalias', 'noinline', 'callconv', 'packed', 'linksection', 'unreachable',
22 'test', 'asm', 'volatile',
23 -- Parallelism and concurrency related keywords.
24 'async', 'await', 'noasync', 'suspend', 'nosuspend', 'resume', 'threadlocalanyframe',
25 -- Control flow: conditions and loops.
26 'if', 'else', 'orelse', 'or', 'and', 'while', 'for', 'switch', 'continue', 'break', 'catch',
27 'try',
28 -- Not keyword but overly used variable name with always the same semantic.
29 'self'
30 }))
31
32 -- Types.
33 lex:add_rule('type', token(lexer.TYPE, word_match{
34 'enum', 'struct', 'union', --
35 'i8', 'u8', 'i16', 'u16', 'i32', 'u32', 'i64', 'u64', 'i128', 'u128', --
36 'isize', 'usize', --
37 'c_short', 'c_ushort', 'c_int', 'c_uint', --
38 'c_long', 'c_ulong', 'c_longlong', 'c_ulonglong', 'c_longdouble', --
39 'c_void', --
40 'f16', 'f32', 'f64', 'f128', --
41 'bool', 'void', 'noreturn', 'type', 'anytype', 'error', 'anyerror', --
42 'comptime_int', 'comptime_float'
43 }))
44
45 -- Constants.
46 lex:add_rule('constant', token(lexer.CONSTANT, word_match{
47 -- Special values.
48 'false', 'true', 'null', 'undefined'
49 }))
50
51 -- Built-in functions.
52 lex:add_rule('function', token(lexer.FUNCTION, '@' * word_match{
53 'addWithOverflow', 'alignCast', 'alignOf', 'as', 'asyncCall', 'atomicLoad', 'atomicRmw',
54 'atomicStore', 'bitCast', 'bitOffsetOf', 'boolToInt', 'bitSizeOf', 'breakpoint', 'mulAdd',
55 'byteSwap', 'bitReverse', 'byteOffsetOf', 'call', 'cDefine', 'cImport', 'cInclude', 'clz',
56 'cmpxchgStrong', 'cmpxchgWeak', 'compileError', 'compileLog', 'ctz', 'cUndef', 'divExact',
57 'divFloor', 'divTrunc', 'embedFile', 'enumToInt', 'errorName', 'errorReturnTrace', 'errorToInt',
58 'errSetCast', 'export', 'fence', 'field', 'fieldParentPtr', 'floatCast', 'floatToInt', 'frame',
59 'Frame', 'frameAddress', 'frameSize', 'hasDecl', 'hasField', 'import', 'intCast', 'intToEnum',
60 'intToError', 'intToFloat', 'intToPtr', 'memcpy', 'memset', 'wasmMemorySize', 'wasmMemoryGrow',
61 'mod', 'mulWithOverflow', 'panic', 'popCount', 'ptrCast', 'ptrToInt', 'rem', 'returnAddress',
62 'setAlignStack', 'setCold', 'setEvalBranchQuota', 'setFloatMode', 'setRuntimeSafety', 'shlExact',
63 'shlWithOverflow', 'shrExact', 'shuffle', 'sizeOf', 'splat', 'reduce', 'src', 'sqrt', 'sin',
64 'cos', 'exp', 'exp2', 'log', 'log2', 'log10', 'fabs', 'floor', 'ceil', 'trunc', 'round',
65 'subWithOverflow', 'tagName', 'TagType', 'This', 'truncate', 'Type', 'typeInfo', 'typeName',
66 'TypeOf', 'unionInit'
67 }))
68
69 -- Strings.
70 local sq_str = P('L')^-1 * lexer.range("'", true)
71 local dq_str = P('L')^-1 * lexer.range('"', true)
72 lex:add_rule('string', token(lexer.STRING, sq_str + dq_str))
73
74 -- Identifiers.
75 lex:add_rule('identifier', token(lexer.IDENTIFIER, lexer.word))
76
77 -- Comments.
78 local doc_comment = lexer.to_eol('///', true)
79 local comment = lexer.to_eol('//', true)
80 lex:add_rule('comment', token(lexer.COMMENT, doc_comment + comment))
81
82 -- Numbers.
83 lex:add_rule('number', token(lexer.NUMBER, lexer.number))
84
85 -- Operators.
86 lex:add_rule('operator', token(lexer.OPERATOR, S('+-/*%<>!=^&|?~:;,.()[]{}')))
87
88 -- Fold points.
89 lex:add_fold_point(lexer.OPERATOR, '{', '}')
90
91 lexer.property['scintillua.comment'] = '//'
92
93 return lex