rust.js 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: https://codemirror.net/LICENSE
  3. ;(function (mod) {
  4. if (typeof exports == 'object' && typeof module == 'object')
  5. // CommonJS
  6. mod(require('../../lib/codemirror'), require('../../addon/mode/simple'))
  7. else if (typeof define == 'function' && define.amd)
  8. // AMD
  9. define(['../../lib/codemirror', '../../addon/mode/simple'], mod)
  10. // Plain browser env
  11. else mod(CodeMirror)
  12. })(function (CodeMirror) {
  13. 'use strict'
  14. CodeMirror.defineSimpleMode('rust', {
  15. start: [
  16. // string and byte string
  17. { regex: /b?"/, token: 'string', next: 'string' },
  18. // raw string and raw byte string
  19. { regex: /b?r"/, token: 'string', next: 'string_raw' },
  20. { regex: /b?r#+"/, token: 'string', next: 'string_raw_hash' },
  21. // character
  22. { regex: /'(?:[^'\\]|\\(?:[nrt0'"]|x[\da-fA-F]{2}|u\{[\da-fA-F]{6}\}))'/, token: 'string-2' },
  23. // byte
  24. { regex: /b'(?:[^']|\\(?:['\\nrt0]|x[\da-fA-F]{2}))'/, token: 'string-2' },
  25. {
  26. regex: /(?:(?:[0-9][0-9_]*)(?:(?:[Ee][+-]?[0-9_]+)|\.[0-9_]+(?:[Ee][+-]?[0-9_]+)?)(?:f32|f64)?)|(?:0(?:b[01_]+|(?:o[0-7_]+)|(?:x[0-9a-fA-F_]+))|(?:[0-9][0-9_]*))(?:u8|u16|u32|u64|i8|i16|i32|i64|isize|usize)?/,
  27. token: 'number',
  28. },
  29. { regex: /(let(?:\s+mut)?|fn|enum|mod|struct|type|union)(\s+)([a-zA-Z_][a-zA-Z0-9_]*)/, token: ['keyword', null, 'def'] },
  30. {
  31. regex: /(?:abstract|alignof|as|async|await|box|break|continue|const|crate|do|dyn|else|enum|extern|fn|for|final|if|impl|in|loop|macro|match|mod|move|offsetof|override|priv|proc|pub|pure|ref|return|self|sizeof|static|struct|super|trait|type|typeof|union|unsafe|unsized|use|virtual|where|while|yield)\b/,
  32. token: 'keyword',
  33. },
  34. { regex: /\b(?:Self|isize|usize|char|bool|u8|u16|u32|u64|f16|f32|f64|i8|i16|i32|i64|str|Option)\b/, token: 'atom' },
  35. { regex: /\b(?:true|false|Some|None|Ok|Err)\b/, token: 'builtin' },
  36. { regex: /\b(fn)(\s+)([a-zA-Z_][a-zA-Z0-9_]*)/, token: ['keyword', null, 'def'] },
  37. { regex: /#!?\[.*\]/, token: 'meta' },
  38. { regex: /\/\/.*/, token: 'comment' },
  39. { regex: /\/\*/, token: 'comment', next: 'comment' },
  40. { regex: /[-+\/*=<>!]+/, token: 'operator' },
  41. { regex: /[a-zA-Z_]\w*!/, token: 'variable-3' },
  42. { regex: /[a-zA-Z_]\w*/, token: 'variable' },
  43. { regex: /[\{\[\(]/, indent: true },
  44. { regex: /[\}\]\)]/, dedent: true },
  45. ],
  46. string: [
  47. { regex: /"/, token: 'string', next: 'start' },
  48. { regex: /(?:[^\\"]|\\(?:.|$))*/, token: 'string' },
  49. ],
  50. string_raw: [
  51. { regex: /"/, token: 'string', next: 'start' },
  52. { regex: /[^"]*/, token: 'string' },
  53. ],
  54. string_raw_hash: [
  55. { regex: /"#+/, token: 'string', next: 'start' },
  56. { regex: /(?:[^"]|"(?!#))*/, token: 'string' },
  57. ],
  58. comment: [
  59. { regex: /.*?\*\//, token: 'comment', next: 'start' },
  60. { regex: /.*/, token: 'comment' },
  61. ],
  62. meta: {
  63. dontIndentStates: ['comment'],
  64. electricInput: /^\s*\}$/,
  65. blockCommentStart: '/*',
  66. blockCommentEnd: '*/',
  67. lineComment: '//',
  68. fold: 'brace',
  69. },
  70. })
  71. CodeMirror.defineMIME('text/x-rustsrc', 'rust')
  72. CodeMirror.defineMIME('text/rust', 'rust')
  73. })