prism-rust.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. },
  29. 'attribute': {
  30. pattern: /#!?\[(?:[^\[\]"]|"(?:\\[\s\S]|[^\\"])*")*\]/,
  31. greedy: true,
  32. alias: 'attr-name',
  33. inside: {
  34. 'string': null // see below
  35. }
  36. },
  37. // Closure params should not be confused with bitwise OR |
  38. 'closure-params': {
  39. pattern: /([=(,:]\s*|\bmove\s*)\|[^|]*\||\|[^|]*\|(?=\s*(?:\{|->))/,
  40. lookbehind: true,
  41. greedy: true,
  42. inside: {
  43. 'closure-punctuation': {
  44. pattern: /^\||\|$/,
  45. alias: 'punctuation'
  46. },
  47. rest: null // see below
  48. }
  49. },
  50. 'lifetime-annotation': {
  51. pattern: /'\w+/,
  52. alias: 'symbol'
  53. },
  54. 'fragment-specifier': {
  55. pattern: /(\$\w+:)[a-z]+/,
  56. lookbehind: true,
  57. alias: 'punctuation'
  58. },
  59. 'variable': /\$\w+/,
  60. 'function-definition': {
  61. pattern: /(\bfn\s+)\w+/,
  62. lookbehind: true,
  63. alias: 'function'
  64. },
  65. 'type-definition': {
  66. pattern: /(\b(?:enum|struct|trait|type|union)\s+)\w+/,
  67. lookbehind: true,
  68. alias: 'class-name'
  69. },
  70. 'module-declaration': [
  71. {
  72. pattern: /(\b(?:crate|mod)\s+)[a-z][a-z_\d]*/,
  73. lookbehind: true,
  74. alias: 'namespace'
  75. },
  76. {
  77. pattern: /(\b(?:crate|self|super)\s*)::\s*[a-z][a-z_\d]*\b(?:\s*::(?:\s*[a-z][a-z_\d]*\s*::)*)?/,
  78. lookbehind: true,
  79. alias: 'namespace',
  80. inside: {
  81. 'punctuation': /::/
  82. }
  83. }
  84. ],
  85. 'keyword': [
  86. // https://github.com/rust-lang/reference/blob/master/src/keywords.md
  87. /\b(?:Self|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|static|struct|super|trait|try|type|typeof|union|unsafe|unsized|use|virtual|where|while|yield)\b/,
  88. // primitives and str
  89. // https://doc.rust-lang.org/stable/rust-by-example/primitives.html
  90. /\b(?:bool|char|f(?:32|64)|[ui](?:8|16|32|64|128|size)|str)\b/
  91. ],
  92. // functions can technically start with an upper-case letter, but this will introduce a lot of false positives
  93. // and Rust's naming conventions recommend snake_case anyway.
  94. // https://doc.rust-lang.org/1.0.0/style/style/naming/README.html
  95. 'function': /\b[a-z_]\w*(?=\s*(?:::\s*<|\())/,
  96. 'macro': {
  97. pattern: /\b\w+!/,
  98. alias: 'property'
  99. },
  100. 'constant': /\b[A-Z_][A-Z_\d]+\b/,
  101. 'class-name': /\b[A-Z]\w*\b/,
  102. 'namespace': {
  103. pattern: /(?:\b[a-z][a-z_\d]*\s*::\s*)*\b[a-z][a-z_\d]*\s*::(?!\s*<)/,
  104. inside: {
  105. 'punctuation': /::/
  106. }
  107. },
  108. // Hex, oct, bin, dec numbers with visual separators and type suffix
  109. 'number': /\b(?:0x[\dA-Fa-f](?:_?[\dA-Fa-f])*|0o[0-7](?:_?[0-7])*|0b[01](?:_?[01])*|(?:(?:\d(?:_?\d)*)?\.)?\d(?:_?\d)*(?:[Ee][+-]?\d+)?)(?:_?(?:f32|f64|[iu](?:8|16|32|64|size)?))?\b/,
  110. 'boolean': /\b(?:false|true)\b/,
  111. 'punctuation': /->|\.\.=|\.{1,3}|::|[{}[\];(),:]/,
  112. 'operator': /[-+*\/%!^]=?|=[=>]?|&[&=]?|\|[|=]?|<<?=?|>>?=?|[@?]/
  113. };
  114. Prism.languages.rust['closure-params'].inside.rest = Prism.languages.rust;
  115. Prism.languages.rust['attribute'].inside['string'] = Prism.languages.rust['string'];
  116. }(Prism));