aboutsummaryrefslogtreecommitdiff
path: root/lua/lexers/zig.lua
blob: f8eb74bea7b8c863844acd4654bff491d8e7f8be (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
-- Copyright 2020 Karchnu karchnu@karchnu.fr.
-- Zig LPeg lexer.
-- (Based on the C++ LPeg lexer from Mitchell mitchell.att.foicica.com.)

local l = require('lexer')
local token, word_match = l.token, l.word_match
local P, R, S = lpeg.P, lpeg.R, lpeg.S

local M = {_NAME = '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)

-- Keywords.
local keyword = token(l.KEYWORD, word_match{

  -- Keywords.
  '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',

  -- Parallelism and concurrency related keywords.
  'async', 'await', 'noasync',
  'suspend', 'nosuspend', 'resume',
  'threadlocal','anyframe',

  -- 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',
})

-- 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',
})

-- Constants.
local constant = token(l.CONSTANT, word_match{
  -- special values
  'false', 'true', 'null', 'undefined',
})

-- Identifiers.
local identifier = token(l.IDENTIFIER, l.word)

-- Built-in functions.
local functions = token(l.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',
})


-- Operators.
local operator = token(l.OPERATOR, S('+-/*%<>!=^&|?~:;,.()[]{}'))

M._rules = {
  {'whitespace', ws},
  {'keyword', keyword},
  {'constant', constant},
  {'function', functions},
  {'type', type},
  {'identifier', identifier},
  {'string', string},
  {'preprocessor', preprocessor},
  {'comment', comment},
  {'number', number},
  {'operator', operator},
}

M._foldsymbols = {
  _patterns = {'%l+', '[{}]', '/%*', '%*/', '//'},
  [l.OPERATOR] = {['{'] = 1, ['}'] = -1},
  [l.COMMENT] = {['//'] = l.fold_line_comments('//')}
}

return M