mode-swift.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. ace.define("ace/mode/doc_comment_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 DocCommentHighlightRules = function() {
  6. this.$rules = {
  7. "start" : [ {
  8. token : "comment.doc.tag",
  9. regex : "@[\\w\\d_]+" // TODO: fix email addresses
  10. },
  11. DocCommentHighlightRules.getTagRule(),
  12. {
  13. defaultToken : "comment.doc",
  14. caseInsensitive: true
  15. }]
  16. };
  17. };
  18. oop.inherits(DocCommentHighlightRules, TextHighlightRules);
  19. DocCommentHighlightRules.getTagRule = function(start) {
  20. return {
  21. token : "comment.doc.tag.storage.type",
  22. regex : "\\b(?:TODO|FIXME|XXX|HACK)\\b"
  23. };
  24. };
  25. DocCommentHighlightRules.getStartRule = function(start) {
  26. return {
  27. token : "comment.doc", // doc comment
  28. regex : "\\/\\*(?=\\*)",
  29. next : start
  30. };
  31. };
  32. DocCommentHighlightRules.getEndRule = function (start) {
  33. return {
  34. token : "comment.doc", // closing comment
  35. regex : "\\*\\/",
  36. next : start
  37. };
  38. };
  39. exports.DocCommentHighlightRules = DocCommentHighlightRules;
  40. });
  41. ace.define("ace/mode/swift_highlight_rules",[], function(require, exports, module) {
  42. "use strict";
  43. var oop = require("../lib/oop");
  44. var lang = require("../lib/lang");
  45. var DocCommentHighlightRules = require("./doc_comment_highlight_rules").DocCommentHighlightRules;
  46. var TextHighlightRules = require("./text_highlight_rules").TextHighlightRules;
  47. var SwiftHighlightRules = function() {
  48. var keywordMapper = this.createKeywordMapper({
  49. "variable.language": "",
  50. "keyword": "__COLUMN__|__FILE__|__FUNCTION__|__LINE__"
  51. + "|as|associativity|break|case|class|continue|default|deinit|didSet"
  52. + "|do|dynamicType|else|enum|extension|fallthrough|for|func|get|if|import"
  53. + "|in|infix|init|inout|is|left|let|let|mutating|new|none|nonmutating"
  54. + "|operator|override|postfix|precedence|prefix|protocol|return|right"
  55. + "|safe|Self|self|set|struct|subscript|switch|Type|typealias"
  56. + "|unowned|unsafe|var|weak|where|while|willSet"
  57. + "|convenience|dynamic|final|infix|lazy|mutating|nonmutating|optional|override|postfix"
  58. + "|prefix|required|static|guard|defer",
  59. "storage.type": "bool|double|Double"
  60. + "|extension|float|Float|int|Int|private|public|string|String",
  61. "constant.language":
  62. "false|Infinity|NaN|nil|no|null|null|off|on|super|this|true|undefined|yes",
  63. "support.function":
  64. ""
  65. }, "identifier");
  66. function string(start, options) {
  67. var nestable = options.nestable || options.interpolation;
  68. var interpStart = options.interpolation && options.interpolation.nextState || "start";
  69. var mainRule = {
  70. regex: start + (options.multiline ? "" : "(?=.)"),
  71. token: "string.start"
  72. };
  73. var nextState = [
  74. options.escape && {
  75. regex: options.escape,
  76. token: "character.escape"
  77. },
  78. options.interpolation && {
  79. token : "paren.quasi.start",
  80. regex : lang.escapeRegExp(options.interpolation.lead + options.interpolation.open),
  81. push : interpStart
  82. },
  83. options.error && {
  84. regex: options.error,
  85. token: "error.invalid"
  86. },
  87. {
  88. regex: start + (options.multiline ? "" : "|$"),
  89. token: "string.end",
  90. next: nestable ? "pop" : "start"
  91. }, {
  92. defaultToken: "string"
  93. }
  94. ].filter(Boolean);
  95. if (nestable)
  96. mainRule.push = nextState;
  97. else
  98. mainRule.next = nextState;
  99. if (!options.interpolation)
  100. return mainRule;
  101. var open = options.interpolation.open;
  102. var close = options.interpolation.close;
  103. var counter = {
  104. regex: "[" + lang.escapeRegExp(open + close) + "]",
  105. onMatch: function(val, state, stack) {
  106. this.next = val == open ? this.nextState : "";
  107. if (val == open && stack.length) {
  108. stack.unshift("start", state);
  109. return "paren";
  110. }
  111. if (val == close && stack.length) {
  112. stack.shift();
  113. this.next = stack.shift();
  114. if (this.next.indexOf("string") != -1)
  115. return "paren.quasi.end";
  116. }
  117. return val == open ? "paren.lparen" : "paren.rparen";
  118. },
  119. nextState: interpStart
  120. };
  121. return [counter, mainRule];
  122. }
  123. function comments() {
  124. return [{
  125. token : "comment",
  126. regex : "\\/\\/(?=.)",
  127. next : [
  128. DocCommentHighlightRules.getTagRule(),
  129. {token : "comment", regex : "$|^", next: "start"},
  130. {defaultToken : "comment", caseInsensitive: true}
  131. ]
  132. },
  133. DocCommentHighlightRules.getStartRule("doc-start"),
  134. {
  135. token : "comment.start",
  136. regex : /\/\*/,
  137. stateName: "nested_comment",
  138. push : [
  139. DocCommentHighlightRules.getTagRule(),
  140. {token : "comment.start", regex : /\/\*/, push: "nested_comment"},
  141. {token : "comment.end", regex : "\\*\\/", next : "pop"},
  142. {defaultToken : "comment", caseInsensitive: true}
  143. ]
  144. }
  145. ];
  146. }
  147. this.$rules = {
  148. start: [
  149. string('"', {
  150. escape: /\\(?:[0\\tnr"']|u{[a-fA-F1-9]{0,8}})/,
  151. interpolation: {lead: "\\", open: "(", close: ")"},
  152. error: /\\./,
  153. multiline: false
  154. }),
  155. comments(),
  156. {
  157. regex: /@[a-zA-Z_$][a-zA-Z_$\d\u0080-\ufffe]*/,
  158. token: "variable.parameter"
  159. },
  160. {
  161. regex: /[a-zA-Z_$][a-zA-Z_$\d\u0080-\ufffe]*/,
  162. token: keywordMapper
  163. },
  164. {
  165. token : "constant.numeric",
  166. regex : /[+-]?(?:0(?:b[01]+|o[0-7]+|x[\da-fA-F])|\d+(?:(?:\.\d*)?(?:[PpEe][+-]?\d+)?)\b)/
  167. }, {
  168. token : "keyword.operator",
  169. regex : /--|\+\+|===|==|=|!=|!==|<=|>=|<<=|>>=|>>>=|<>|<|>|!|&&|\|\||\?:|[!$%&*+\-~\/^]=?/,
  170. next : "start"
  171. }, {
  172. token : "punctuation.operator",
  173. regex : /[?:,;.]/,
  174. next : "start"
  175. }, {
  176. token : "paren.lparen",
  177. regex : /[\[({]/,
  178. next : "start"
  179. }, {
  180. token : "paren.rparen",
  181. regex : /[\])}]/
  182. }
  183. ]
  184. };
  185. this.embedRules(DocCommentHighlightRules, "doc-",
  186. [ DocCommentHighlightRules.getEndRule("start") ]);
  187. this.normalizeRules();
  188. };
  189. oop.inherits(SwiftHighlightRules, TextHighlightRules);
  190. exports.HighlightRules = SwiftHighlightRules;
  191. });
  192. ace.define("ace/mode/folding/cstyle",[], function(require, exports, module) {
  193. "use strict";
  194. var oop = require("../../lib/oop");
  195. var Range = require("../../range").Range;
  196. var BaseFoldMode = require("./fold_mode").FoldMode;
  197. var FoldMode = exports.FoldMode = function(commentRegex) {
  198. if (commentRegex) {
  199. this.foldingStartMarker = new RegExp(
  200. this.foldingStartMarker.source.replace(/\|[^|]*?$/, "|" + commentRegex.start)
  201. );
  202. this.foldingStopMarker = new RegExp(
  203. this.foldingStopMarker.source.replace(/\|[^|]*?$/, "|" + commentRegex.end)
  204. );
  205. }
  206. };
  207. oop.inherits(FoldMode, BaseFoldMode);
  208. (function() {
  209. this.foldingStartMarker = /([\{\[\(])[^\}\]\)]*$|^\s*(\/\*)/;
  210. this.foldingStopMarker = /^[^\[\{\(]*([\}\]\)])|^[\s\*]*(\*\/)/;
  211. this.singleLineBlockCommentRe= /^\s*(\/\*).*\*\/\s*$/;
  212. this.tripleStarBlockCommentRe = /^\s*(\/\*\*\*).*\*\/\s*$/;
  213. this.startRegionRe = /^\s*(\/\*|\/\/)#?region\b/;
  214. this._getFoldWidgetBase = this.getFoldWidget;
  215. this.getFoldWidget = function(session, foldStyle, row) {
  216. var line = session.getLine(row);
  217. if (this.singleLineBlockCommentRe.test(line)) {
  218. if (!this.startRegionRe.test(line) && !this.tripleStarBlockCommentRe.test(line))
  219. return "";
  220. }
  221. var fw = this._getFoldWidgetBase(session, foldStyle, row);
  222. if (!fw && this.startRegionRe.test(line))
  223. return "start"; // lineCommentRegionStart
  224. return fw;
  225. };
  226. this.getFoldWidgetRange = function(session, foldStyle, row, forceMultiline) {
  227. var line = session.getLine(row);
  228. if (this.startRegionRe.test(line))
  229. return this.getCommentRegionBlock(session, line, row);
  230. var match = line.match(this.foldingStartMarker);
  231. if (match) {
  232. var i = match.index;
  233. if (match[1])
  234. return this.openingBracketBlock(session, match[1], row, i);
  235. var range = session.getCommentFoldRange(row, i + match[0].length, 1);
  236. if (range && !range.isMultiLine()) {
  237. if (forceMultiline) {
  238. range = this.getSectionRange(session, row);
  239. } else if (foldStyle != "all")
  240. range = null;
  241. }
  242. return range;
  243. }
  244. if (foldStyle === "markbegin")
  245. return;
  246. var match = line.match(this.foldingStopMarker);
  247. if (match) {
  248. var i = match.index + match[0].length;
  249. if (match[1])
  250. return this.closingBracketBlock(session, match[1], row, i);
  251. return session.getCommentFoldRange(row, i, -1);
  252. }
  253. };
  254. this.getSectionRange = function(session, row) {
  255. var line = session.getLine(row);
  256. var startIndent = line.search(/\S/);
  257. var startRow = row;
  258. var startColumn = line.length;
  259. row = row + 1;
  260. var endRow = row;
  261. var maxRow = session.getLength();
  262. while (++row < maxRow) {
  263. line = session.getLine(row);
  264. var indent = line.search(/\S/);
  265. if (indent === -1)
  266. continue;
  267. if (startIndent > indent)
  268. break;
  269. var subRange = this.getFoldWidgetRange(session, "all", row);
  270. if (subRange) {
  271. if (subRange.start.row <= startRow) {
  272. break;
  273. } else if (subRange.isMultiLine()) {
  274. row = subRange.end.row;
  275. } else if (startIndent == indent) {
  276. break;
  277. }
  278. }
  279. endRow = row;
  280. }
  281. return new Range(startRow, startColumn, endRow, session.getLine(endRow).length);
  282. };
  283. this.getCommentRegionBlock = function(session, line, row) {
  284. var startColumn = line.search(/\s*$/);
  285. var maxRow = session.getLength();
  286. var startRow = row;
  287. var re = /^\s*(?:\/\*|\/\/|--)#?(end)?region\b/;
  288. var depth = 1;
  289. while (++row < maxRow) {
  290. line = session.getLine(row);
  291. var m = re.exec(line);
  292. if (!m) continue;
  293. if (m[1]) depth--;
  294. else depth++;
  295. if (!depth) break;
  296. }
  297. var endRow = row;
  298. if (endRow > startRow) {
  299. return new Range(startRow, startColumn, endRow, line.length);
  300. }
  301. };
  302. }).call(FoldMode.prototype);
  303. });
  304. ace.define("ace/mode/swift",[], function(require, exports, module) {
  305. "use strict";
  306. var oop = require("../lib/oop");
  307. var TextMode = require("./text").Mode;
  308. var HighlightRules = require("./swift_highlight_rules").HighlightRules;
  309. var CstyleBehaviour = require("./behaviour/cstyle").CstyleBehaviour;
  310. var FoldMode = require("./folding/cstyle").FoldMode;
  311. var Mode = function() {
  312. this.HighlightRules = HighlightRules;
  313. this.foldingRules = new FoldMode();
  314. this.$behaviour = new CstyleBehaviour();
  315. this.$behaviour = this.$defaultBehaviour;
  316. };
  317. oop.inherits(Mode, TextMode);
  318. (function() {
  319. this.lineCommentStart = "//";
  320. this.blockComment = {start: "/*", end: "*/", nestable: true};
  321. this.$id = "ace/mode/swift";
  322. }).call(Mode.prototype);
  323. exports.Mode = Mode;
  324. });
  325. (function() {
  326. ace.require(["ace/mode/swift"], function(m) {
  327. if (typeof module == "object" && typeof exports == "object" && module) {
  328. module.exports = m;
  329. }
  330. });
  331. })();