prism-rust.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. (function (Prism) {
  2. var multilineComment = /\/\*(?:[^*/]|\*(?!\/)|\/(?!\*)|<self>)*\*\//.source;
  3. for (var i = 0; i < 2; i++) {
  4. // support 4 levels of nested comments
  5. multilineComment = multilineComment.replace(/<self>/g, function () { return multilineComment; });
  6. }
  7. multilineComment = multilineComment.replace(/<self>/g, function () { return /[^\s\S]/.source; });
  8. Prism.languages.rust = {
  9. 'comment': [
  10. {
  11. pattern: RegExp(/(^|[^\\])/.source + multilineComment),
  12. lookbehind: true,
  13. greedy: true
  14. },
  15. {
  16. pattern: /(^|[^\\:])\/\/.*/,
  17. lookbehind: true,
  18. greedy: true
  19. }
  20. ],
  21. 'string': {
  22. pattern: /b?"(?:\\[\s\S]|[^\\"])*"|b?r(#*)"(?:[^"]|"(?!\1))*"\1/,
  23. greedy: true
  24. },
  25. 'char': {
  26. pattern: /b?'(?:\\(?:x[0-7][\da-fA-F]|u\{(?:[\da-fA-F]_*){1,6}\}|.)|[^\\\r\n\t'])'/,
  27. greedy: true,
  28. alias: 'string'
  29. },
  30. 'attribute': {
  31. pattern: /#!?\[(?:[^\[\]"]|"(?:\\[\s\S]|[^\\"])*")*\]/,
  32. greedy: true,
  33. alias: 'attr-name',
  34. inside: {
  35. 'string': null // see below
  36. }
  37. },
  38. // Closure params should not be confused with bitwise OR |
  39. 'closure-params': {
  40. pattern: /([=(,:]\s*|\bmove\s*)\|[^|]*\||\|[^|]*\|(?=\s*(?:\{|->))/,
  41. lookbehind: true,
  42. greedy: true,
  43. inside: {
  44. 'closure-punctuation': {
  45. pattern: /^\||\|$/,
  46. alias: 'punctuation'
  47. },
  48. rest: null // see below
  49. }
  50. },
  51. 'lifetime-annotation': {
  52. pattern: /'\w+/,
  53. alias: 'symbol'
  54. },
  55. 'fragment-specifier': {
  56. pattern: /(\$\w+:)[a-z]+/,
  57. lookbehind: true,
  58. alias: 'punctuation'
  59. },
  60. 'variable': /\$\w+/,
  61. 'function-definition': {
  62. pattern: /(\bfn\s+)\w+/,
  63. lookbehind: true,
  64. alias: 'function'
  65. },
  66. 'type-definition': {
  67. pattern: /(\b(?:enum|struct|union)\s+)\w+/,
  68. lookbehind: true,
  69. alias: 'class-name'
  70. },
  71. 'module-declaration': [
  72. {
  73. pattern: /(\b(?:crate|mod)\s+)[a-z][a-z_\d]*/,
  74. lookbehind: true,
  75. alias: 'namespace'
  76. },
  77. {
  78. pattern: /(\b(?:crate|self|super)\s*)::\s*[a-z][a-z_\d]*\b(?:\s*::(?:\s*[a-z][a-z_\d]*\s*::)*)?/,
  79. lookbehind: true,
  80. alias: 'namespace',
  81. inside: {
  82. 'punctuation': /::/
  83. }
  84. }
  85. ],
  86. 'keyword': [
  87. // https://github.com/rust-lang/reference/blob/master/src/keywords.md
  88. /\b(?:abstract|as|async|await|become|box|break|const|continue|crate|do|dyn|else|enum|extern|final|fn|for|if|impl|in|let|loop|macro|match|mod|move|mut|override|priv|pub|ref|return|self|Self|static|struct|super|trait|try|type|typeof|union|unsafe|unsized|use|virtual|where|while|yield)\b/,
  89. // primitives and str
  90. // https://doc.rust-lang.org/stable/rust-by-example/primitives.html
  91. /\b(?:[ui](?:8|16|32|64|128|size)|f(?:32|64)|bool|char|str)\b/
  92. ],
  93. // functions can technically start with an upper-case letter, but this will introduce a lot of false positives
  94. // and Rust's naming conventions recommend snake_case anyway.
  95. // https://doc.rust-lang.org/1.0.0/style/style/naming/README.html
  96. 'function': /\b[a-z_]\w*(?=\s*(?:::\s*<|\())/,
  97. 'macro': {
  98. pattern: /\w+!/,
  99. alias: 'property'
  100. },
  101. 'constant': /\b[A-Z_][A-Z_\d]+\b/,
  102. 'class-name': /\b[A-Z]\w*\b/,
  103. 'namespace': {
  104. pattern: /(?:\b[a-z][a-z_\d]*\s*::\s*)*\b[a-z][a-z_\d]*\s*::(?!\s*<)/,
  105. inside: {
  106. 'punctuation': /::/
  107. }
  108. },
  109. // Hex, oct, bin, dec numbers with visual separators and type suffix
  110. 'number': /\b(?:0x[\dA-Fa-f](?:_?[\dA-Fa-f])*|0o[0-7](?:_?[0-7])*|0b[01](?:_?[01])*|(?:\d(?:_?\d)*)?\.?\d(?:_?\d)*(?:[Ee][+-]?\d+)?)(?:_?(?:[iu](?:8|16|32|64|size)?|f32|f64))?\b/,
  111. 'boolean': /\b(?:false|true)\b/,
  112. 'punctuation': /->|\.\.=|\.{1,3}|::|[{}[\];(),:]/,
  113. 'operator': /[-+*\/%!^]=?|=[=>]?|&[&=]?|\|[|=]?|<<?=?|>>?=?|[@?]/
  114. };
  115. Prism.languages.rust['closure-params'].inside.rest = Prism.languages.rust;
  116. Prism.languages.rust['attribute'].inside['string'] = Prism.languages.rust['string'];
  117. }(Prism));