rust.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. const NUM_SUFFIX = '([ui](8|16|32|64|128|size)|f(32|64))\?';
  10. const 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. const 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('/\\*', '\\*/', {
  51. contains: [ 'self' ]
  52. }),
  53. hljs.inherit(hljs.QUOTE_STRING_MODE, {
  54. begin: /b?"/,
  55. illegal: null
  56. }),
  57. {
  58. className: 'string',
  59. variants: [
  60. {
  61. begin: /r(#*)"(.|\n)*?"\1(?!#)/
  62. },
  63. {
  64. begin: /b?'\\?(x\w{2}|u\w{4}|U\w{8}|.)'/
  65. }
  66. ]
  67. },
  68. {
  69. className: 'symbol',
  70. begin: /'[a-zA-Z_][a-zA-Z0-9_]*/
  71. },
  72. {
  73. className: 'number',
  74. variants: [
  75. {
  76. begin: '\\b0b([01_]+)' + NUM_SUFFIX
  77. },
  78. {
  79. begin: '\\b0o([0-7_]+)' + NUM_SUFFIX
  80. },
  81. {
  82. begin: '\\b0x([A-Fa-f0-9_]+)' + NUM_SUFFIX
  83. },
  84. {
  85. begin: '\\b(\\d[\\d_]*(\\.[0-9_]+)?([eE][+-]?[0-9_]+)?)' +
  86. NUM_SUFFIX
  87. }
  88. ],
  89. relevance: 0
  90. },
  91. {
  92. className: 'function',
  93. beginKeywords: 'fn',
  94. end: '(\\(|<)',
  95. excludeEnd: true,
  96. contains: [ hljs.UNDERSCORE_TITLE_MODE ]
  97. },
  98. {
  99. className: 'meta',
  100. begin: '#!?\\[',
  101. end: '\\]',
  102. contains: [
  103. {
  104. className: 'meta-string',
  105. begin: /"/,
  106. end: /"/
  107. }
  108. ]
  109. },
  110. {
  111. className: 'class',
  112. beginKeywords: 'type',
  113. end: ';',
  114. contains: [
  115. hljs.inherit(hljs.UNDERSCORE_TITLE_MODE, {
  116. endsParent: true
  117. })
  118. ],
  119. illegal: '\\S'
  120. },
  121. {
  122. className: 'class',
  123. beginKeywords: 'trait enum struct union',
  124. end: /\{/,
  125. contains: [
  126. hljs.inherit(hljs.UNDERSCORE_TITLE_MODE, {
  127. endsParent: true
  128. })
  129. ],
  130. illegal: '[\\w\\d]'
  131. },
  132. {
  133. begin: hljs.IDENT_RE + '::',
  134. keywords: {
  135. built_in: BUILTINS
  136. }
  137. },
  138. {
  139. begin: '->'
  140. }
  141. ]
  142. };
  143. }
  144. module.exports = rust;