mode-swift.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. ace.define("ace/mode/doc_comment_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 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",["require","exports","module","ace/lib/oop","ace/lib/lang","ace/mode/doc_comment_highlight_rules","ace/mode/text_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|open|internal|fileprivate|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: true
  154. }),
  155. string('"', {
  156. escape: /\\(?:[0\\tnr"']|u{[a-fA-F1-9]{0,8}})/,
  157. interpolation: {lead: "\\", open: "(", close: ")"},
  158. error: /\\./,
  159. multiline: false
  160. }),
  161. comments(),
  162. {
  163. regex: /@[a-zA-Z_$][a-zA-Z_$\d\u0080-\ufffe]*/,
  164. token: "variable.parameter"
  165. },
  166. {
  167. regex: /[a-zA-Z_$][a-zA-Z_$\d\u0080-\ufffe]*/,
  168. token: keywordMapper
  169. },
  170. {
  171. token : "constant.numeric",
  172. regex : /[+-]?(?:0(?:b[01]+|o[0-7]+|x[\da-fA-F])|\d+(?:(?:\.\d*)?(?:[PpEe][+-]?\d+)?)\b)/
  173. }, {
  174. token : "keyword.operator",
  175. regex : /--|\+\+|===|==|=|!=|!==|<=|>=|<<=|>>=|>>>=|<>|<|>|!|&&|\|\||\?:|[!$%&*+\-~\/^]=?/,
  176. next : "start"
  177. }, {
  178. token : "punctuation.operator",
  179. regex : /[?:,;.]/,
  180. next : "start"
  181. }, {
  182. token : "paren.lparen",
  183. regex : /[\[({]/,
  184. next : "start"
  185. }, {
  186. token : "paren.rparen",
  187. regex : /[\])}]/
  188. }
  189. ]
  190. };
  191. this.embedRules(DocCommentHighlightRules, "doc-",
  192. [ DocCommentHighlightRules.getEndRule("start") ]);
  193. this.normalizeRules();
  194. };
  195. oop.inherits(SwiftHighlightRules, TextHighlightRules);
  196. exports.HighlightRules = SwiftHighlightRules;
  197. });
  198. ace.define("ace/mode/folding/cstyle",["require","exports","module","ace/lib/oop","ace/range","ace/mode/folding/fold_mode"], function(require, exports, module) {
  199. "use strict";
  200. var oop = require("../../lib/oop");
  201. var Range = require("../../range").Range;
  202. var BaseFoldMode = require("./fold_mode").FoldMode;
  203. var FoldMode = exports.FoldMode = function(commentRegex) {
  204. if (commentRegex) {
  205. this.foldingStartMarker = new RegExp(
  206. this.foldingStartMarker.source.replace(/\|[^|]*?$/, "|" + commentRegex.start)
  207. );
  208. this.foldingStopMarker = new RegExp(
  209. this.foldingStopMarker.source.replace(/\|[^|]*?$/, "|" + commentRegex.end)
  210. );
  211. }
  212. };
  213. oop.inherits(FoldMode, BaseFoldMode);
  214. (function() {
  215. this.foldingStartMarker = /([\{\[\(])[^\}\]\)]*$|^\s*(\/\*)/;
  216. this.foldingStopMarker = /^[^\[\{\(]*([\}\]\)])|^[\s\*]*(\*\/)/;
  217. this.singleLineBlockCommentRe= /^\s*(\/\*).*\*\/\s*$/;
  218. this.tripleStarBlockCommentRe = /^\s*(\/\*\*\*).*\*\/\s*$/;
  219. this.startRegionRe = /^\s*(\/\*|\/\/)#?region\b/;
  220. this._getFoldWidgetBase = this.getFoldWidget;
  221. this.getFoldWidget = function(session, foldStyle, row) {
  222. var line = session.getLine(row);
  223. if (this.singleLineBlockCommentRe.test(line)) {
  224. if (!this.startRegionRe.test(line) && !this.tripleStarBlockCommentRe.test(line))
  225. return "";
  226. }
  227. var fw = this._getFoldWidgetBase(session, foldStyle, row);
  228. if (!fw && this.startRegionRe.test(line))
  229. return "start"; // lineCommentRegionStart
  230. return fw;
  231. };
  232. this.getFoldWidgetRange = function(session, foldStyle, row, forceMultiline) {
  233. var line = session.getLine(row);
  234. if (this.startRegionRe.test(line))
  235. return this.getCommentRegionBlock(session, line, row);
  236. var match = line.match(this.foldingStartMarker);
  237. if (match) {
  238. var i = match.index;
  239. if (match[1])
  240. return this.openingBracketBlock(session, match[1], row, i);
  241. var range = session.getCommentFoldRange(row, i + match[0].length, 1);
  242. if (range && !range.isMultiLine()) {
  243. if (forceMultiline) {
  244. range = this.getSectionRange(session, row);
  245. } else if (foldStyle != "all")
  246. range = null;
  247. }
  248. return range;
  249. }
  250. if (foldStyle === "markbegin")
  251. return;
  252. var match = line.match(this.foldingStopMarker);
  253. if (match) {
  254. var i = match.index + match[0].length;
  255. if (match[1])
  256. return this.closingBracketBlock(session, match[1], row, i);
  257. return session.getCommentFoldRange(row, i, -1);
  258. }
  259. };
  260. this.getSectionRange = function(session, row) {
  261. var line = session.getLine(row);
  262. var startIndent = line.search(/\S/);
  263. var startRow = row;
  264. var startColumn = line.length;
  265. row = row + 1;
  266. var endRow = row;
  267. var maxRow = session.getLength();
  268. while (++row < maxRow) {
  269. line = session.getLine(row);
  270. var indent = line.search(/\S/);
  271. if (indent === -1)
  272. continue;
  273. if (startIndent > indent)
  274. break;
  275. var subRange = this.getFoldWidgetRange(session, "all", row);
  276. if (subRange) {
  277. if (subRange.start.row <= startRow) {
  278. break;
  279. } else if (subRange.isMultiLine()) {
  280. row = subRange.end.row;
  281. } else if (startIndent == indent) {
  282. break;
  283. }
  284. }
  285. endRow = row;
  286. }
  287. return new Range(startRow, startColumn, endRow, session.getLine(endRow).length);
  288. };
  289. this.getCommentRegionBlock = function(session, line, row) {
  290. var startColumn = line.search(/\s*$/);
  291. var maxRow = session.getLength();
  292. var startRow = row;
  293. var re = /^\s*(?:\/\*|\/\/|--)#?(end)?region\b/;
  294. var depth = 1;
  295. while (++row < maxRow) {
  296. line = session.getLine(row);
  297. var m = re.exec(line);
  298. if (!m) continue;
  299. if (m[1]) depth--;
  300. else depth++;
  301. if (!depth) break;
  302. }
  303. var endRow = row;
  304. if (endRow > startRow) {
  305. return new Range(startRow, startColumn, endRow, line.length);
  306. }
  307. };
  308. }).call(FoldMode.prototype);
  309. });
  310. ace.define("ace/mode/swift",["require","exports","module","ace/lib/oop","ace/mode/text","ace/mode/swift_highlight_rules","ace/mode/behaviour/cstyle","ace/mode/folding/cstyle"], function(require, exports, module) {
  311. "use strict";
  312. var oop = require("../lib/oop");
  313. var TextMode = require("./text").Mode;
  314. var HighlightRules = require("./swift_highlight_rules").HighlightRules;
  315. var CstyleBehaviour = require("./behaviour/cstyle").CstyleBehaviour;
  316. var FoldMode = require("./folding/cstyle").FoldMode;
  317. var Mode = function() {
  318. this.HighlightRules = HighlightRules;
  319. this.foldingRules = new FoldMode();
  320. this.$behaviour = new CstyleBehaviour();
  321. this.$behaviour = this.$defaultBehaviour;
  322. };
  323. oop.inherits(Mode, TextMode);
  324. (function() {
  325. this.lineCommentStart = "//";
  326. this.blockComment = {start: "/*", end: "*/", nestable: true};
  327. this.$id = "ace/mode/swift";
  328. }).call(Mode.prototype);
  329. exports.Mode = Mode;
  330. }); (function() {
  331. ace.require(["ace/mode/swift"], function(m) {
  332. if (typeof module == "object" && typeof exports == "object" && module) {
  333. module.exports = m;
  334. }
  335. });
  336. })();