logo

oasis-root

Compiled tree of Oasis Linux based on own branch at <https://hacktivis.me/git/oasis/> git clone https://anongit.hacktivis.me/git/oasis-root.git

rust.lua (3399B)


  1. -- Copyright 2015-2024 Alejandro Baez (https://keybase.io/baez). See LICENSE.
  2. -- Rust LPeg lexer.
  3. local lexer = lexer
  4. local P, S = lpeg.P, lpeg.S
  5. local C, Cmt = lpeg.C, lpeg.Cmt
  6. local lex = lexer.new(...)
  7. -- Keywords.
  8. lex:add_rule('keyword', lex:tag(lexer.KEYWORD, lex:word_match(lexer.KEYWORD)))
  9. -- Library types.
  10. lex:add_rule('library', lex:tag(lexer.TYPE, lexer.upper * (lexer.lower + lexer.dec_num)^1))
  11. -- Types.
  12. lex:add_rule('type', lex:tag(lexer.TYPE, lex:word_match(lexer.TYPE)))
  13. -- Lifetime annotation.
  14. lex:add_rule('lifetime', lex:tag(lexer.OPERATOR, S('<&') * P("'")))
  15. -- Strings.
  16. local sq_str = P('b')^-1 * lexer.range("'", true)
  17. local dq_str = P('b')^-1 * lexer.range('"')
  18. local raw_str = Cmt(P('b')^-1 * P('r') * C(P('#')^0) * '"', function(input, index, hashes)
  19. local _, e = input:find('"' .. hashes, index, true)
  20. return (e or #input) + 1
  21. end)
  22. lex:add_rule('string', lex:tag(lexer.STRING, sq_str + dq_str + raw_str))
  23. -- Functions.
  24. local builtin_macros = lex:tag(lexer.FUNCTION_BUILTIN, lex:word_match(lexer.FUNCTION_BUILTIN) * '!')
  25. local macros = lex:tag(lexer.FUNCTION, lexer.word * '!')
  26. local func = lex:tag(lexer.FUNCTION, lexer.word)
  27. lex:add_rule('function', (builtin_macros + macros + func) * #(lexer.space^0 * '('))
  28. -- Identifiers.
  29. local identifier = P('r#')^-1 * lexer.word
  30. lex:add_rule('identifier', lex:tag(lexer.IDENTIFIER, identifier))
  31. -- Comments.
  32. local line_comment = lexer.to_eol('//', true)
  33. local block_comment = lexer.range('/*', '*/', false, false, true)
  34. lex:add_rule('comment', lex:tag(lexer.COMMENT, line_comment + block_comment))
  35. -- Numbers.
  36. lex:add_rule('number', lex:tag(lexer.NUMBER, lexer.number_('_')))
  37. -- Attributes.
  38. lex:add_rule('preprocessor', lex:tag(lexer.PREPROCESSOR, '#' * lexer.range('[', ']', true)))
  39. -- Operators.
  40. lex:add_rule('operator', lex:tag(lexer.OPERATOR, S('+-/*%<>!=`^~@&|?#~:;,.()[]{}')))
  41. -- Fold points.
  42. lex:add_fold_point(lexer.COMMENT, '/*', '*/')
  43. lex:add_fold_point(lexer.OPERATOR, '(', ')')
  44. lex:add_fold_point(lexer.OPERATOR, '{', '}')
  45. -- https://doc.rust-lang.org/std/#keywords
  46. lex:set_word_list(lexer.KEYWORD, {
  47. 'SelfTy', 'as', 'async', 'await', 'break', 'const', 'continue', 'crate', 'dyn', 'else', 'enum',
  48. 'extern', 'false', 'fn', 'for', 'if', 'impl', 'in', 'let', 'loop', 'match', 'mod', 'move', 'mut',
  49. 'pub', 'ref', 'return', 'self', 'static', 'struct', 'super', 'trait', 'true', 'type', 'union',
  50. 'unsafe', 'use', 'where', 'while'
  51. })
  52. -- https://doc.rust-lang.org/std/#primitives
  53. lex:set_word_list(lexer.TYPE, {
  54. 'never', 'array', 'bool', 'char', 'f32', 'f64', 'fn', 'i8', 'i16', 'i32', 'i64', 'i128', 'isize',
  55. 'pointer', 'reference', 'slice', 'str', 'tuple', 'u8', 'u16', 'u32', 'u64', 'u128', 'unit',
  56. 'usize'
  57. })
  58. lex:set_word_list(lexer.FUNCTION_BUILTIN, {
  59. 'assert', 'assert_eq', 'assert_ne', 'cfg', 'column', 'compile_error', 'concat', 'dbg',
  60. 'debug_assert', 'debug_assert_eq', 'debug_assert_ne', 'env', 'eprint', 'eprintln', 'file',
  61. 'format', 'format_args', 'include', 'include_bytes', 'include_str', 'line', 'matches',
  62. 'module_path', 'option_env', 'panic', 'print', 'println', 'stringify', 'thread_local', 'todo',
  63. 'unimplemented', 'unreachable', 'vec', 'write', 'writeln',
  64. -- Experimental
  65. 'concat_bytes', 'concat_idents', 'const_format_args', 'format_args_nl', 'log_syntax',
  66. 'trace_macros',
  67. -- Deprecated
  68. 'try'
  69. })
  70. lexer.property['scintillua.comment'] = '//'
  71. return lex