rust.js 3.2 KB

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