diff options
Diffstat (limited to 'lua/lexers/rust.lua')
| -rw-r--r-- | lua/lexers/rust.lua | 130 |
1 files changed, 62 insertions, 68 deletions
diff --git a/lua/lexers/rust.lua b/lua/lexers/rust.lua index a5f8ecd..25555d6 100644 --- a/lua/lexers/rust.lua +++ b/lua/lexers/rust.lua @@ -1,88 +1,82 @@ --- Copyright 2015-2017 Alejandro Baez (https://keybase.io/baez). See LICENSE. +-- Copyright 2015-2022 Alejandro Baez (https://keybase.io/baez). See LICENSE. -- Rust LPeg lexer. -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 C, Cmt = lpeg.C, lpeg.Cmt -local M = {_NAME = 'rust'} +local lex = lexer.new('rust') -- Whitespace. -local ws = token(l.WHITESPACE, l.space^1) - --- Comments. -local line_comment = '//' * l.nonnewline_esc^0 -local block_comment = '/*' * (l.any - '*/')^0 * P('*/')^-1 -local comment = token(l.COMMENT, line_comment + block_comment) - --- Strings. -local sq_str = P('L')^-1 * l.delimited_range("'") -local dq_str = P('L')^-1 * l.delimited_range('"') -local raw_str = '#"' * (l.any - '#')^0 * P('#')^-1 -local string = token(l.STRING, dq_str + raw_str) - --- Numbers. -local number = token(l.NUMBER, l.float + (l.dec_num + "_")^1 + - "0b" * (l.dec_num + "_")^1 + l.integer) +lex:add_rule('whitespace', token(lexer.WHITESPACE, lexer.space^1)) -- Keywords. -local keyword = token(l.KEYWORD, word_match{ - 'abstract', 'alignof', 'as', 'become', 'box', - 'break', 'const', 'continue', 'crate', 'do', - 'else', 'enum', 'extern', 'false', 'final', - 'fn', 'for', 'if', 'impl', 'in', - 'let', 'loop', 'macro', 'match', 'mod', - 'move', 'mut', "offsetof", 'override', 'priv', - 'proc', 'pub', 'pure', 'ref', 'return', - 'Self', 'self', 'sizeof', 'static', 'struct', - 'super', 'trait', 'true', 'type', 'typeof', - 'unsafe', 'unsized', 'use', 'virtual', 'where', - 'while', 'yield' -}) +-- https://github.com/rust-lang/rust/blob/stable/src/libsyntax_pos/symbol.rs +lex:add_rule('keyword', token(lexer.KEYWORD, word_match{ + 'Self', 'abstract', 'as', 'async', 'auto', 'await', 'become', 'box', 'break', 'catch', 'const', + 'continue', 'crate', 'default', 'do', 'dyn', 'else', 'enum', 'extern', 'false', 'final', 'fn', + 'for', 'if', 'impl', 'in', 'let', 'loop', 'macro', 'match', 'mod', 'move', 'mut', 'override', + 'priv', 'pub', 'ref', 'return', 'self', 'static', 'struct', 'super', 'trait', 'true', 'try', + 'type', 'typeof', 'union', 'unsafe', 'unsized', 'use', 'virtual', 'where', 'while', 'yield' +})) + +-- Macro names. +lex:add_rule('macro', token(lexer.FUNCTION, lexer.word * S("!"))) -- Library types -local library = token(l.LABEL, l.upper * (l.lower + l.dec_num)^1) - --- syntax extensions -local extension = l.word^1 * S("!") +lex:add_rule('library', token(lexer.LABEL, lexer.upper * (lexer.lower + lexer.dec_num)^1)) -local func = token(l.FUNCTION, extension) +-- 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', token(lexer.NUMBER, float + integer)) -- Types. -local type = token(l.TYPE, word_match{ - '()', 'bool', 'isize', 'usize', 'char', 'str', - 'u8', 'u16', 'u32', 'u64', 'i8', 'i16', 'i32', 'i64', - 'f32','f64', -}) +lex:add_rule('type', token(lexer.TYPE, word_match( + '() bool isize usize char str u8 u16 u32 u64 u128 i8 i16 i32 i64 i128 f32 f64'))) + +-- Strings. +local sq_str = P('b')^-1 * lexer.range("'", true) +local dq_str = P('b')^-1 * lexer.range('"') +local raw_str = Cmt(P('b')^-1 * P('r') * C(P('#')^0) * '"', function(input, index, hashes) + local _, e = input:find('"' .. hashes, index, true) + return (e or #input) + 1 +end) +lex:add_rule('string', token(lexer.STRING, sq_str + dq_str + raw_str)) -- Identifiers. -local identifier = token(l.IDENTIFIER, l.word) +lex:add_rule('identifier', token(lexer.IDENTIFIER, identifier)) --- Operators. -local operator = token(l.OPERATOR, S('+-/*%<>!=`^~@&|?#~:;,.()[]{}')) +-- Comments. +local line_comment = lexer.to_eol('//', true) +local block_comment = lexer.range('/*', '*/', false, false, true) +lex:add_rule('comment', token(lexer.COMMENT, line_comment + block_comment)) -- Attributes. -local attribute = token(l.PREPROCESSOR, "#[" * - (l.nonnewline - ']')^0 * P("]")^-1) +lex:add_rule('preprocessor', token(lexer.PREPROCESSOR, '#' * lexer.range('[', ']', true))) -M._rules = { - {'whitespace', ws}, - {'keyword', keyword}, - {'function', func}, - {'library', library}, - {'type', type}, - {'identifier', identifier}, - {'string', string}, - {'comment', comment}, - {'number', number}, - {'operator', operator}, - {'preprocessor', attribute}, -} +-- Operators. +lex:add_rule('operator', token(lexer.OPERATOR, S('+-/*%<>!=`^~@&|?#~:;,.()[]{}'))) -M._foldsymbols = { - _patterns = {'%l+', '[{}]', '/%*', '%*/', '//'}, - [l.COMMENT] = {['/*'] = 1, ['*/'] = -1, ['//'] = l.fold_line_comments('//')}, - [l.OPERATOR] = {['('] = 1, ['{'] = 1, [')'] = -1, ['}'] = -1} -} +-- Fold points. +lex:add_fold_point(lexer.COMMENT, '/*', '*/') +lex:add_fold_point(lexer.COMMENT, lexer.fold_consecutive_lines('//')) +lex:add_fold_point(lexer.OPERATOR, '(', ')') +lex:add_fold_point(lexer.OPERATOR, '{', '}') -return M +return lex |
