rust.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. /** @type LanguageFn */
  9. function rust(hljs) {
  10. const regex = hljs.regex;
  11. // ============================================
  12. // Added to support the r# keyword, which is a raw identifier in Rust.
  13. const RAW_IDENTIFIER = /(r#)?/;
  14. const UNDERSCORE_IDENT_RE = regex.concat(RAW_IDENTIFIER, hljs.UNDERSCORE_IDENT_RE);
  15. const IDENT_RE = regex.concat(RAW_IDENTIFIER, hljs.IDENT_RE);
  16. // ============================================
  17. const FUNCTION_INVOKE = {
  18. className: "title.function.invoke",
  19. relevance: 0,
  20. begin: regex.concat(
  21. /\b/,
  22. /(?!let|for|while|if|else|match\b)/,
  23. IDENT_RE,
  24. regex.lookahead(/\s*\(/))
  25. };
  26. const NUMBER_SUFFIX = '([ui](8|16|32|64|128|size)|f(32|64))\?';
  27. const KEYWORDS = [
  28. "abstract",
  29. "as",
  30. "async",
  31. "await",
  32. "become",
  33. "box",
  34. "break",
  35. "const",
  36. "continue",
  37. "crate",
  38. "do",
  39. "dyn",
  40. "else",
  41. "enum",
  42. "extern",
  43. "false",
  44. "final",
  45. "fn",
  46. "for",
  47. "if",
  48. "impl",
  49. "in",
  50. "let",
  51. "loop",
  52. "macro",
  53. "match",
  54. "mod",
  55. "move",
  56. "mut",
  57. "override",
  58. "priv",
  59. "pub",
  60. "ref",
  61. "return",
  62. "self",
  63. "Self",
  64. "static",
  65. "struct",
  66. "super",
  67. "trait",
  68. "true",
  69. "try",
  70. "type",
  71. "typeof",
  72. "union",
  73. "unsafe",
  74. "unsized",
  75. "use",
  76. "virtual",
  77. "where",
  78. "while",
  79. "yield"
  80. ];
  81. const LITERALS = [
  82. "true",
  83. "false",
  84. "Some",
  85. "None",
  86. "Ok",
  87. "Err"
  88. ];
  89. const BUILTINS = [
  90. // functions
  91. 'drop ',
  92. // traits
  93. "Copy",
  94. "Send",
  95. "Sized",
  96. "Sync",
  97. "Drop",
  98. "Fn",
  99. "FnMut",
  100. "FnOnce",
  101. "ToOwned",
  102. "Clone",
  103. "Debug",
  104. "PartialEq",
  105. "PartialOrd",
  106. "Eq",
  107. "Ord",
  108. "AsRef",
  109. "AsMut",
  110. "Into",
  111. "From",
  112. "Default",
  113. "Iterator",
  114. "Extend",
  115. "IntoIterator",
  116. "DoubleEndedIterator",
  117. "ExactSizeIterator",
  118. "SliceConcatExt",
  119. "ToString",
  120. // macros
  121. "assert!",
  122. "assert_eq!",
  123. "bitflags!",
  124. "bytes!",
  125. "cfg!",
  126. "col!",
  127. "concat!",
  128. "concat_idents!",
  129. "debug_assert!",
  130. "debug_assert_eq!",
  131. "env!",
  132. "eprintln!",
  133. "panic!",
  134. "file!",
  135. "format!",
  136. "format_args!",
  137. "include_bytes!",
  138. "include_str!",
  139. "line!",
  140. "local_data_key!",
  141. "module_path!",
  142. "option_env!",
  143. "print!",
  144. "println!",
  145. "select!",
  146. "stringify!",
  147. "try!",
  148. "unimplemented!",
  149. "unreachable!",
  150. "vec!",
  151. "write!",
  152. "writeln!",
  153. "macro_rules!",
  154. "assert_ne!",
  155. "debug_assert_ne!"
  156. ];
  157. const TYPES = [
  158. "i8",
  159. "i16",
  160. "i32",
  161. "i64",
  162. "i128",
  163. "isize",
  164. "u8",
  165. "u16",
  166. "u32",
  167. "u64",
  168. "u128",
  169. "usize",
  170. "f32",
  171. "f64",
  172. "str",
  173. "char",
  174. "bool",
  175. "Box",
  176. "Option",
  177. "Result",
  178. "String",
  179. "Vec"
  180. ];
  181. return {
  182. name: 'Rust',
  183. aliases: [ 'rs' ],
  184. keywords: {
  185. $pattern: hljs.IDENT_RE + '!?',
  186. type: TYPES,
  187. keyword: KEYWORDS,
  188. literal: LITERALS,
  189. built_in: BUILTINS
  190. },
  191. illegal: '</',
  192. contains: [
  193. hljs.C_LINE_COMMENT_MODE,
  194. hljs.COMMENT('/\\*', '\\*/', { contains: [ 'self' ] }),
  195. hljs.inherit(hljs.QUOTE_STRING_MODE, {
  196. begin: /b?"/,
  197. illegal: null
  198. }),
  199. {
  200. className: 'string',
  201. variants: [
  202. { begin: /b?r(#*)"(.|\n)*?"\1(?!#)/ },
  203. { begin: /b?'\\?(x\w{2}|u\w{4}|U\w{8}|.)'/ }
  204. ]
  205. },
  206. {
  207. className: 'symbol',
  208. begin: /'[a-zA-Z_][a-zA-Z0-9_]*/
  209. },
  210. {
  211. className: 'number',
  212. variants: [
  213. { begin: '\\b0b([01_]+)' + NUMBER_SUFFIX },
  214. { begin: '\\b0o([0-7_]+)' + NUMBER_SUFFIX },
  215. { begin: '\\b0x([A-Fa-f0-9_]+)' + NUMBER_SUFFIX },
  216. { begin: '\\b(\\d[\\d_]*(\\.[0-9_]+)?([eE][+-]?[0-9_]+)?)'
  217. + NUMBER_SUFFIX }
  218. ],
  219. relevance: 0
  220. },
  221. {
  222. begin: [
  223. /fn/,
  224. /\s+/,
  225. UNDERSCORE_IDENT_RE
  226. ],
  227. className: {
  228. 1: "keyword",
  229. 3: "title.function"
  230. }
  231. },
  232. {
  233. className: 'meta',
  234. begin: '#!?\\[',
  235. end: '\\]',
  236. contains: [
  237. {
  238. className: 'string',
  239. begin: /"/,
  240. end: /"/,
  241. contains: [
  242. hljs.BACKSLASH_ESCAPE
  243. ]
  244. }
  245. ]
  246. },
  247. {
  248. begin: [
  249. /let/,
  250. /\s+/,
  251. /(?:mut\s+)?/,
  252. UNDERSCORE_IDENT_RE
  253. ],
  254. className: {
  255. 1: "keyword",
  256. 3: "keyword",
  257. 4: "variable"
  258. }
  259. },
  260. // must come before impl/for rule later
  261. {
  262. begin: [
  263. /for/,
  264. /\s+/,
  265. UNDERSCORE_IDENT_RE,
  266. /\s+/,
  267. /in/
  268. ],
  269. className: {
  270. 1: "keyword",
  271. 3: "variable",
  272. 5: "keyword"
  273. }
  274. },
  275. {
  276. begin: [
  277. /type/,
  278. /\s+/,
  279. UNDERSCORE_IDENT_RE
  280. ],
  281. className: {
  282. 1: "keyword",
  283. 3: "title.class"
  284. }
  285. },
  286. {
  287. begin: [
  288. /(?:trait|enum|struct|union|impl|for)/,
  289. /\s+/,
  290. UNDERSCORE_IDENT_RE
  291. ],
  292. className: {
  293. 1: "keyword",
  294. 3: "title.class"
  295. }
  296. },
  297. {
  298. begin: hljs.IDENT_RE + '::',
  299. keywords: {
  300. keyword: "Self",
  301. built_in: BUILTINS,
  302. type: TYPES
  303. }
  304. },
  305. {
  306. className: "punctuation",
  307. begin: '->'
  308. },
  309. FUNCTION_INVOKE
  310. ]
  311. };
  312. }
  313. export { rust as default };