Skip to content

Commit

Permalink
Simplified Rust number highlighting.
Browse files Browse the repository at this point in the history
Highlight type suffixes separately.
  • Loading branch information
orbitalquark committed Mar 23, 2024
1 parent f971ac5 commit b789dde
Showing 1 changed file with 4 additions and 19 deletions.
23 changes: 4 additions & 19 deletions lexers/rust.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,6 @@ lex:add_rule('keyword', lex:tag(lexer.KEYWORD, lex:word_match(lexer.KEYWORD)))
-- Library types.
lex:add_rule('library', lex:tag(lexer.TYPE, lexer.upper * (lexer.lower + lexer.dec_num)^1))

-- Numbers.
local identifier = P('r#')^-1 * lexer.word
local digit = lexer.digit
local decimal_literal = digit * (digit + '_')^0
local function integer_suffix(digit) return P('_')^0 * digit * (digit + '_')^0 end
local function opt_cap(patt) return C(patt^-1) end
local float = decimal_literal *
(Cmt(opt_cap('.' * decimal_literal) * opt_cap(S('eE') * S('+-')^-1 * integer_suffix(digit)) *
opt_cap(P('f32') + 'f64'), function(input, index, decimals, exponent, type)
return decimals ~= '' or exponent ~= '' or type ~= ''
end) + '.' * -(S('._') + identifier))
local function prefixed_integer(prefix, digit) return P(prefix) * integer_suffix(digit) end
local bin = prefixed_integer('0b', S('01'))
local oct = prefixed_integer('0o', lpeg.R('07'))
local hex = prefixed_integer('0x', lexer.xdigit)
local integer = (bin + oct + hex + decimal_literal) *
(S('iu') * (P('8') + '16' + '32' + '64' + '128' + 'size'))^-1
lex:add_rule('number', lex:tag(lexer.NUMBER, float + integer))

-- Types.
lex:add_rule('type', lex:tag(lexer.TYPE, lex:word_match(lexer.TYPE)))

Expand All @@ -54,13 +35,17 @@ local func = lex:tag(lexer.FUNCTION, lexer.word)
lex:add_rule('function', (builtin_macros + macros + func) * #(lexer.space^0 * '('))

-- Identifiers.
local identifier = P('r#')^-1 * lexer.word
lex:add_rule('identifier', lex:tag(lexer.IDENTIFIER, identifier))

-- Comments.
local line_comment = lexer.to_eol('//', true)
local block_comment = lexer.range('/*', '*/', false, false, true)
lex:add_rule('comment', lex:tag(lexer.COMMENT, line_comment + block_comment))

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

-- Attributes.
lex:add_rule('preprocessor', lex:tag(lexer.PREPROCESSOR, '#' * lexer.range('[', ']', true)))

Expand Down

0 comments on commit b789dde

Please sign in to comment.