rust.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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") // CommonJS
  5. mod(require("../../lib/codemirror"), require("../../addon/mode/simple"));
  6. else if (typeof define == "function" && define.amd) // AMD
  7. define(["../../lib/codemirror", "../../addon/mode/simple"], mod);
  8. else // Plain browser env
  9. mod(CodeMirror);
  10. })(function(CodeMirror) {
  11. "use strict";
  12. CodeMirror.defineSimpleMode("rust",{
  13. start: [
  14. // string and byte string
  15. {regex: /b?"/, token: "string", next: "string"},
  16. // raw string and raw byte string
  17. {regex: /b?r"/, token: "string", next: "string_raw"},
  18. {regex: /b?r#+"/, token: "string", next: "string_raw_hash"},
  19. // character
  20. {regex: /'(?:[^'\\]|\\(?:[nrt0'"]|x[\da-fA-F]{2}|u\{[\da-fA-F]{6}\}))'/, token: "string-2"},
  21. // byte
  22. {regex: /b'(?:[^']|\\(?:['\\nrt0]|x[\da-fA-F]{2}))'/, token: "string-2"},
  23. {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)?/,
  24. token: "number"},
  25. {regex: /(let(?:\s+mut)?|fn|enum|mod|struct|type|union)(\s+)([a-zA-Z_][a-zA-Z0-9_]*)/, token: ["keyword", null, "def"]},
  26. {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/, token: "keyword"},
  27. {regex: /\b(?:Self|isize|usize|char|bool|u8|u16|u32|u64|f16|f32|f64|i8|i16|i32|i64|str|Option)\b/, token: "atom"},
  28. {regex: /\b(?:true|false|Some|None|Ok|Err)\b/, token: "builtin"},
  29. {regex: /\b(fn)(\s+)([a-zA-Z_][a-zA-Z0-9_]*)/,
  30. token: ["keyword", null ,"def"]},
  31. {regex: /#!?\[.*\]/, token: "meta"},
  32. {regex: /\/\/.*/, token: "comment"},
  33. {regex: /\/\*/, token: "comment", next: "comment"},
  34. {regex: /[-+\/*=<>!]+/, token: "operator"},
  35. {regex: /[a-zA-Z_]\w*!/,token: "variable-3"},
  36. {regex: /[a-zA-Z_]\w*/, token: "variable"},
  37. {regex: /[\{\[\(]/, indent: true},
  38. {regex: /[\}\]\)]/, dedent: true}
  39. ],
  40. string: [
  41. {regex: /"/, token: "string", next: "start"},
  42. {regex: /(?:[^\\"]|\\(?:.|$))*/, token: "string"}
  43. ],
  44. string_raw: [
  45. {regex: /"/, token: "string", next: "start"},
  46. {regex: /[^"]*/, token: "string"}
  47. ],
  48. string_raw_hash: [
  49. {regex: /"#+/, token: "string", next: "start"},
  50. {regex: /(?:[^"]|"(?!#))*/, token: "string"}
  51. ],
  52. comment: [
  53. {regex: /.*?\*\//, token: "comment", next: "start"},
  54. {regex: /.*/, token: "comment"}
  55. ],
  56. meta: {
  57. dontIndentStates: ["comment"],
  58. electricInput: /^\s*\}$/,
  59. blockCommentStart: "/*",
  60. blockCommentEnd: "*/",
  61. lineComment: "//",
  62. fold: "brace"
  63. }
  64. });
  65. CodeMirror.defineMIME("text/x-rustsrc", "rust");
  66. CodeMirror.defineMIME("text/rust", "rust");
  67. });