mode-rust.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. ace.define("ace/mode/rust_highlight_rules",["require","exports","module","ace/lib/oop","ace/mode/text_highlight_rules"], function(require, exports, module) {
  2. "use strict";
  3. var oop = require("../lib/oop");
  4. var TextHighlightRules = require("./text_highlight_rules").TextHighlightRules;
  5. var stringEscape = /\\(?:[nrt0'"\\]|x[\da-fA-F]{2}|u\{[\da-fA-F]{6}\})/.source;
  6. var RustHighlightRules = function() {
  7. this.$rules = { start:
  8. [ { token: 'variable.other.source.rust',
  9. regex: '\'[a-zA-Z_][a-zA-Z0-9_]*(?![\\\'])' },
  10. { token: 'string.quoted.single.source.rust',
  11. regex: "'(?:[^'\\\\]|" + stringEscape + ")'" },
  12. {
  13. stateName: "bracketedComment",
  14. onMatch : function(value, currentState, stack){
  15. stack.unshift(this.next, value.length - 1, currentState);
  16. return "string.quoted.raw.source.rust";
  17. },
  18. regex : /r#*"/,
  19. next : [
  20. {
  21. onMatch : function(value, currentState, stack) {
  22. var token = "string.quoted.raw.source.rust";
  23. if (value.length >= stack[1]) {
  24. if (value.length > stack[1])
  25. token = "invalid";
  26. stack.shift();
  27. stack.shift();
  28. this.next = stack.shift();
  29. } else {
  30. this.next = "";
  31. }
  32. return token;
  33. },
  34. regex : /"#*/,
  35. next : "start"
  36. }, {
  37. defaultToken : "string.quoted.raw.source.rust"
  38. }
  39. ]
  40. },
  41. { token: 'string.quoted.double.source.rust',
  42. regex: '"',
  43. push:
  44. [ { token: 'string.quoted.double.source.rust',
  45. regex: '"',
  46. next: 'pop' },
  47. { token: 'constant.character.escape.source.rust',
  48. regex: stringEscape },
  49. { defaultToken: 'string.quoted.double.source.rust' } ] },
  50. { token: [ 'keyword.source.rust', 'text', 'entity.name.function.source.rust' ],
  51. regex: '\\b(fn)(\\s+)([a-zA-Z_][a-zA-Z0-9_]*)' },
  52. { token: 'support.constant', regex: '\\b[a-zA-Z_][\\w\\d]*::' },
  53. { token: 'keyword.source.rust',
  54. regex: '\\b(?:abstract|alignof|as|box|break|continue|const|crate|do|else|enum|extern|for|final|if|impl|in|let|loop|macro|match|mod|move|mut|offsetof|override|priv|proc|pub|pure|ref|return|self|sizeof|static|struct|super|trait|type|typeof|unsafe|unsized|use|virtual|where|while|yield)\\b' },
  55. { token: 'storage.type.source.rust',
  56. regex: '\\b(?:Self|isize|usize|char|bool|u8|u16|u32|u64|u128|f16|f32|f64|i8|i16|i32|i64|i128|str|option|either|c_float|c_double|c_void|FILE|fpos_t|DIR|dirent|c_char|c_schar|c_uchar|c_short|c_ushort|c_int|c_uint|c_long|c_ulong|size_t|ptrdiff_t|clock_t|time_t|c_longlong|c_ulonglong|intptr_t|uintptr_t|off_t|dev_t|ino_t|pid_t|mode_t|ssize_t)\\b' },
  57. { token: 'variable.language.source.rust', regex: '\\bself\\b' },
  58. { token: 'comment.line.doc.source.rust',
  59. regex: '//!.*$' },
  60. { token: 'comment.line.double-dash.source.rust',
  61. regex: '//.*$' },
  62. { token: 'comment.start.block.source.rust',
  63. regex: '/\\*',
  64. stateName: 'comment',
  65. push:
  66. [ { token: 'comment.start.block.source.rust',
  67. regex: '/\\*',
  68. push: 'comment' },
  69. { token: 'comment.end.block.source.rust',
  70. regex: '\\*/',
  71. next: 'pop' },
  72. { defaultToken: 'comment.block.source.rust' } ] },
  73. { token: 'keyword.operator',
  74. regex: /\$|[-=]>|[-+%^=!&|<>]=?|[*/](?![*/])=?/ },
  75. { token : "punctuation.operator", regex : /[?:,;.]/ },
  76. { token : "paren.lparen", regex : /[\[({]/ },
  77. { token : "paren.rparen", regex : /[\])}]/ },
  78. { token: 'constant.language.source.rust',
  79. regex: '\\b(?:true|false|Some|None|Ok|Err)\\b' },
  80. { token: 'support.constant.source.rust',
  81. regex: '\\b(?:EXIT_FAILURE|EXIT_SUCCESS|RAND_MAX|EOF|SEEK_SET|SEEK_CUR|SEEK_END|_IOFBF|_IONBF|_IOLBF|BUFSIZ|FOPEN_MAX|FILENAME_MAX|L_tmpnam|TMP_MAX|O_RDONLY|O_WRONLY|O_RDWR|O_APPEND|O_CREAT|O_EXCL|O_TRUNC|S_IFIFO|S_IFCHR|S_IFBLK|S_IFDIR|S_IFREG|S_IFMT|S_IEXEC|S_IWRITE|S_IREAD|S_IRWXU|S_IXUSR|S_IWUSR|S_IRUSR|F_OK|R_OK|W_OK|X_OK|STDIN_FILENO|STDOUT_FILENO|STDERR_FILENO)\\b' },
  82. { token: 'meta.preprocessor.source.rust',
  83. regex: '\\b\\w\\(\\w\\)*!|#\\[[\\w=\\(\\)_]+\\]\\b' },
  84. { token: 'constant.numeric.source.rust',
  85. regex: /\b(?:0x[a-fA-F0-9_]+|0o[0-7_]+|0b[01_]+|[0-9][0-9_]*(?!\.))(?:[iu](?:size|8|16|32|64|128))?\b/ },
  86. { token: 'constant.numeric.source.rust',
  87. regex: /\b(?:[0-9][0-9_]*)(?:\.[0-9][0-9_]*)?(?:[Ee][+-][0-9][0-9_]*)?(?:f32|f64)?\b/ } ] }
  88. this.normalizeRules();
  89. };
  90. RustHighlightRules.metaData = { fileTypes: [ 'rs', 'rc' ],
  91. foldingStartMarker: '^.*\\bfn\\s*(\\w+\\s*)?\\([^\\)]*\\)(\\s*\\{[^\\}]*)?\\s*$',
  92. foldingStopMarker: '^\\s*\\}',
  93. name: 'Rust',
  94. scopeName: 'source.rust' }
  95. oop.inherits(RustHighlightRules, TextHighlightRules);
  96. exports.RustHighlightRules = RustHighlightRules;
  97. });
  98. ace.define("ace/mode/folding/cstyle",["require","exports","module","ace/lib/oop","ace/range","ace/mode/folding/fold_mode"], function(require, exports, module) {
  99. "use strict";
  100. var oop = require("../../lib/oop");
  101. var Range = require("../../range").Range;
  102. var BaseFoldMode = require("./fold_mode").FoldMode;
  103. var FoldMode = exports.FoldMode = function(commentRegex) {
  104. if (commentRegex) {
  105. this.foldingStartMarker = new RegExp(
  106. this.foldingStartMarker.source.replace(/\|[^|]*?$/, "|" + commentRegex.start)
  107. );
  108. this.foldingStopMarker = new RegExp(
  109. this.foldingStopMarker.source.replace(/\|[^|]*?$/, "|" + commentRegex.end)
  110. );
  111. }
  112. };
  113. oop.inherits(FoldMode, BaseFoldMode);
  114. (function() {
  115. this.foldingStartMarker = /(\{|\[)[^\}\]]*$|^\s*(\/\*)/;
  116. this.foldingStopMarker = /^[^\[\{]*(\}|\])|^[\s\*]*(\*\/)/;
  117. this.singleLineBlockCommentRe= /^\s*(\/\*).*\*\/\s*$/;
  118. this.tripleStarBlockCommentRe = /^\s*(\/\*\*\*).*\*\/\s*$/;
  119. this.startRegionRe = /^\s*(\/\*|\/\/)#?region\b/;
  120. this._getFoldWidgetBase = this.getFoldWidget;
  121. this.getFoldWidget = function(session, foldStyle, row) {
  122. var line = session.getLine(row);
  123. if (this.singleLineBlockCommentRe.test(line)) {
  124. if (!this.startRegionRe.test(line) && !this.tripleStarBlockCommentRe.test(line))
  125. return "";
  126. }
  127. var fw = this._getFoldWidgetBase(session, foldStyle, row);
  128. if (!fw && this.startRegionRe.test(line))
  129. return "start"; // lineCommentRegionStart
  130. return fw;
  131. };
  132. this.getFoldWidgetRange = function(session, foldStyle, row, forceMultiline) {
  133. var line = session.getLine(row);
  134. if (this.startRegionRe.test(line))
  135. return this.getCommentRegionBlock(session, line, row);
  136. var match = line.match(this.foldingStartMarker);
  137. if (match) {
  138. var i = match.index;
  139. if (match[1])
  140. return this.openingBracketBlock(session, match[1], row, i);
  141. var range = session.getCommentFoldRange(row, i + match[0].length, 1);
  142. if (range && !range.isMultiLine()) {
  143. if (forceMultiline) {
  144. range = this.getSectionRange(session, row);
  145. } else if (foldStyle != "all")
  146. range = null;
  147. }
  148. return range;
  149. }
  150. if (foldStyle === "markbegin")
  151. return;
  152. var match = line.match(this.foldingStopMarker);
  153. if (match) {
  154. var i = match.index + match[0].length;
  155. if (match[1])
  156. return this.closingBracketBlock(session, match[1], row, i);
  157. return session.getCommentFoldRange(row, i, -1);
  158. }
  159. };
  160. this.getSectionRange = function(session, row) {
  161. var line = session.getLine(row);
  162. var startIndent = line.search(/\S/);
  163. var startRow = row;
  164. var startColumn = line.length;
  165. row = row + 1;
  166. var endRow = row;
  167. var maxRow = session.getLength();
  168. while (++row < maxRow) {
  169. line = session.getLine(row);
  170. var indent = line.search(/\S/);
  171. if (indent === -1)
  172. continue;
  173. if (startIndent > indent)
  174. break;
  175. var subRange = this.getFoldWidgetRange(session, "all", row);
  176. if (subRange) {
  177. if (subRange.start.row <= startRow) {
  178. break;
  179. } else if (subRange.isMultiLine()) {
  180. row = subRange.end.row;
  181. } else if (startIndent == indent) {
  182. break;
  183. }
  184. }
  185. endRow = row;
  186. }
  187. return new Range(startRow, startColumn, endRow, session.getLine(endRow).length);
  188. };
  189. this.getCommentRegionBlock = function(session, line, row) {
  190. var startColumn = line.search(/\s*$/);
  191. var maxRow = session.getLength();
  192. var startRow = row;
  193. var re = /^\s*(?:\/\*|\/\/|--)#?(end)?region\b/;
  194. var depth = 1;
  195. while (++row < maxRow) {
  196. line = session.getLine(row);
  197. var m = re.exec(line);
  198. if (!m) continue;
  199. if (m[1]) depth--;
  200. else depth++;
  201. if (!depth) break;
  202. }
  203. var endRow = row;
  204. if (endRow > startRow) {
  205. return new Range(startRow, startColumn, endRow, line.length);
  206. }
  207. };
  208. }).call(FoldMode.prototype);
  209. });
  210. ace.define("ace/mode/rust",["require","exports","module","ace/lib/oop","ace/mode/text","ace/mode/rust_highlight_rules","ace/mode/folding/cstyle"], function(require, exports, module) {
  211. "use strict";
  212. var oop = require("../lib/oop");
  213. var TextMode = require("./text").Mode;
  214. var RustHighlightRules = require("./rust_highlight_rules").RustHighlightRules;
  215. var FoldMode = require("./folding/cstyle").FoldMode;
  216. var Mode = function() {
  217. this.HighlightRules = RustHighlightRules;
  218. this.foldingRules = new FoldMode();
  219. this.$behaviour = this.$defaultBehaviour;
  220. };
  221. oop.inherits(Mode, TextMode);
  222. (function() {
  223. this.lineCommentStart = "//";
  224. this.blockComment = {start: "/*", end: "*/", nestable: true};
  225. this.$id = "ace/mode/rust";
  226. }).call(Mode.prototype);
  227. exports.Mode = Mode;
  228. });