Skip to content

Commit

Permalink
fix: modernize zig lexer
Browse files Browse the repository at this point in the history
References: orbitalquark#76
  • Loading branch information
mcepl committed Jan 24, 2025
1 parent b9cd4ef commit 79dbcbb
Showing 1 changed file with 18 additions and 15 deletions.
33 changes: 18 additions & 15 deletions lexers/zig.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
-- (Based on the C++ LPeg lexer from Mitchell.)

local lexer = lexer
local token, word_match = lexer.token, lexer.word_match
local P, S = lpeg.P, lpeg.S

local lex = lexer.new(...)

-- Keywords.
lex:add_rule('keyword', token(lexer.KEYWORD, word_match{
lex:add_rule('keyword', lex:tag(lexer.KEYWORD, lex:word_match(lexer.KEYWORD)))
lex:set_word_list(lexer.KEYWORD, {
-- Keywords.
'inline', 'pub', 'fn', 'comptime', 'const', 'extern', 'return', 'var', 'usingnamespace',
-- Defering code blocks.
Expand All @@ -24,10 +24,11 @@ lex:add_rule('keyword', token(lexer.KEYWORD, word_match{
'try',
-- Not keyword but overly used variable name with always the same semantic.
'self'
}))
})

-- Types.
lex:add_rule('type', token(lexer.TYPE, word_match{
lex:add_rule('type', lex:tag(lexer.TYPE, lex:word_match(lexer.TYPE)))
lex:set_word_list(lexer.TYPE, {
'enum', 'struct', 'union', --
'i8', 'u8', 'i16', 'u16', 'i32', 'u32', 'i64', 'u64', 'i128', 'u128', --
'isize', 'usize', --
Expand All @@ -37,16 +38,18 @@ lex:add_rule('type', token(lexer.TYPE, word_match{
'f16', 'f32', 'f64', 'f128', --
'bool', 'void', 'noreturn', 'type', 'anytype', 'error', 'anyerror', --
'comptime_int', 'comptime_float'
}))
})

-- Constants.
lex:add_rule('constant', token(lexer.CONSTANT, word_match{
-- Special values.
lex:add_rule('constant', lex:tag(lexer.CONSTANT, lex:word_match(lexer.CONSTANT)))
-- Special values.
lex:set_word_list(lexer.CONSTANT, {
'false', 'true', 'null', 'undefined'
}))
})

-- Built-in functions.
lex:add_rule('function', token(lexer.FUNCTION, '@' * word_match{
lex:add_rule('function', lex:tag(lexer.FUNCTION, '@' * lex:word_match(lexer.FUNCTION)))
lex:set_word_list(lexer.FUNCTION, {
'addWithOverflow', 'alignCast', 'alignOf', 'as', 'asyncCall', 'atomicLoad', 'atomicRmw',
'atomicStore', 'bitCast', 'bitOffsetOf', 'boolToInt', 'bitSizeOf', 'breakpoint', 'mulAdd',
'byteSwap', 'bitReverse', 'byteOffsetOf', 'call', 'cDefine', 'cImport', 'cInclude', 'clz',
Expand All @@ -61,26 +64,26 @@ lex:add_rule('function', token(lexer.FUNCTION, '@' * word_match{
'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))
lex:add_rule('string', lex:tag(lexer.STRING, sq_str + dq_str))

-- Identifiers.
lex:add_rule('identifier', token(lexer.IDENTIFIER, lexer.word))
lex:add_rule('identifier', lex:tag(lexer.IDENTIFIER, lexer.word))

-- Comments.
local doc_comment = lexer.to_eol('///', true)
local comment = lexer.to_eol('//', true)
lex:add_rule('comment', token(lexer.COMMENT, doc_comment + comment))
lex:add_rule('comment', lex:tag(lexer.COMMENT, doc_comment + comment))

-- Numbers.
lex:add_rule('number', token(lexer.NUMBER, lexer.number))
lex:add_rule('number', lex:tag(lexer.NUMBER, lexer.number))

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

-- Fold points.
lex:add_fold_point(lexer.OPERATOR, '{', '}')
Expand Down

0 comments on commit 79dbcbb

Please sign in to comment.