rust.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. Language: Rust
  3. Author: Andrey Vlasovskikh <andrey.vlasovskikh@gmail.com>
  4. Contributors: Roman Shmatov <romanshmatov@gmail.com>, Kasper Andersen <kma_untrusted@protonmail.com>
  5. Website: https://www.rust-lang.org
  6. Category: common, system
  7. */
  8. function rust(hljs) {
  9. var NUM_SUFFIX = '([ui](8|16|32|64|128|size)|f(32|64))\?';
  10. var KEYWORDS =
  11. 'abstract as async await become box break const continue crate do dyn ' +
  12. 'else enum extern false final fn for if impl in let loop macro match mod ' +
  13. 'move mut override priv pub ref return self Self static struct super ' +
  14. 'trait true try type typeof unsafe unsized use virtual where while yield';
  15. var BUILTINS =
  16. // functions
  17. 'drop ' +
  18. // types
  19. 'i8 i16 i32 i64 i128 isize ' +
  20. 'u8 u16 u32 u64 u128 usize ' +
  21. 'f32 f64 ' +
  22. 'str char bool ' +
  23. 'Box Option Result String Vec ' +
  24. // traits
  25. 'Copy Send Sized Sync Drop Fn FnMut FnOnce ToOwned Clone Debug ' +
  26. 'PartialEq PartialOrd Eq Ord AsRef AsMut Into From Default Iterator ' +
  27. 'Extend IntoIterator DoubleEndedIterator ExactSizeIterator ' +
  28. 'SliceConcatExt ToString ' +
  29. // macros
  30. 'assert! assert_eq! bitflags! bytes! cfg! col! concat! concat_idents! ' +
  31. 'debug_assert! debug_assert_eq! env! panic! file! format! format_args! ' +
  32. 'include_bin! include_str! line! local_data_key! module_path! ' +
  33. 'option_env! print! println! select! stringify! try! unimplemented! ' +
  34. 'unreachable! vec! write! writeln! macro_rules! assert_ne! debug_assert_ne!';
  35. return {
  36. name: 'Rust',
  37. aliases: ['rs'],
  38. keywords: {
  39. $pattern: hljs.IDENT_RE + '!?',
  40. keyword:
  41. KEYWORDS,
  42. literal:
  43. 'true false Some None Ok Err',
  44. built_in:
  45. BUILTINS
  46. },
  47. illegal: '</',
  48. contains: [
  49. hljs.C_LINE_COMMENT_MODE,
  50. hljs.COMMENT('/\\*', '\\*/', {contains: ['self']}),
  51. hljs.inherit(hljs.QUOTE_STRING_MODE, {begin: /b?"/, illegal: null}),
  52. {
  53. className: 'string',
  54. variants: [
  55. { begin: /r(#*)"(.|\n)*?"\1(?!#)/ },
  56. { begin: /b?'\\?(x\w{2}|u\w{4}|U\w{8}|.)'/ }
  57. ]
  58. },
  59. {
  60. className: 'symbol',
  61. begin: /'[a-zA-Z_][a-zA-Z0-9_]*/
  62. },
  63. {
  64. className: 'number',
  65. variants: [
  66. { begin: '\\b0b([01_]+)' + NUM_SUFFIX },
  67. { begin: '\\b0o([0-7_]+)' + NUM_SUFFIX },
  68. { begin: '\\b0x([A-Fa-f0-9_]+)' + NUM_SUFFIX },
  69. { begin: '\\b(\\d[\\d_]*(\\.[0-9_]+)?([eE][+-]?[0-9_]+)?)' +
  70. NUM_SUFFIX
  71. }
  72. ],
  73. relevance: 0
  74. },
  75. {
  76. className: 'function',
  77. beginKeywords: 'fn', end: '(\\(|<)', excludeEnd: true,
  78. contains: [hljs.UNDERSCORE_TITLE_MODE]
  79. },
  80. {
  81. className: 'meta',
  82. begin: '#\\!?\\[', end: '\\]',
  83. contains: [
  84. {
  85. className: 'meta-string',
  86. begin: /"/, end: /"/
  87. }
  88. ]
  89. },
  90. {
  91. className: 'class',
  92. beginKeywords: 'type', end: ';',
  93. contains: [
  94. hljs.inherit(hljs.UNDERSCORE_TITLE_MODE, {endsParent: true})
  95. ],
  96. illegal: '\\S'
  97. },
  98. {
  99. className: 'class',
  100. beginKeywords: 'trait enum struct union', end: '{',
  101. contains: [
  102. hljs.inherit(hljs.UNDERSCORE_TITLE_MODE, {endsParent: true})
  103. ],
  104. illegal: '[\\w\\d]'
  105. },
  106. {
  107. begin: hljs.IDENT_RE + '::',
  108. keywords: {built_in: BUILTINS}
  109. },
  110. {
  111. begin: '->'
  112. }
  113. ]
  114. };
  115. }
  116. module.exports = rust;