mode-perl.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. ace.define("ace/mode/perl_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 PerlHighlightRules = function() {
  6. var keywords = (
  7. "base|constant|continue|else|elsif|for|foreach|format|goto|if|last|local|my|next|" +
  8. "no|package|parent|redo|require|scalar|sub|unless|until|while|use|vars"
  9. );
  10. var buildinConstants = ("ARGV|ENV|INC|SIG");
  11. var builtinFunctions = (
  12. "getprotobynumber|getprotobyname|getservbyname|gethostbyaddr|" +
  13. "gethostbyname|getservbyport|getnetbyaddr|getnetbyname|getsockname|" +
  14. "getpeername|setpriority|getprotoent|setprotoent|getpriority|" +
  15. "endprotoent|getservent|setservent|endservent|sethostent|socketpair|" +
  16. "getsockopt|gethostent|endhostent|setsockopt|setnetent|quotemeta|" +
  17. "localtime|prototype|getnetent|endnetent|rewinddir|wantarray|getpwuid|" +
  18. "closedir|getlogin|readlink|endgrent|getgrgid|getgrnam|shmwrite|" +
  19. "shutdown|readline|endpwent|setgrent|readpipe|formline|truncate|" +
  20. "dbmclose|syswrite|setpwent|getpwnam|getgrent|getpwent|ucfirst|sysread|" +
  21. "setpgrp|shmread|sysseek|sysopen|telldir|defined|opendir|connect|" +
  22. "lcfirst|getppid|binmode|syscall|sprintf|getpgrp|readdir|seekdir|" +
  23. "waitpid|reverse|unshift|symlink|dbmopen|semget|msgrcv|rename|listen|" +
  24. "chroot|msgsnd|shmctl|accept|unpack|exists|fileno|shmget|system|" +
  25. "unlink|printf|gmtime|msgctl|semctl|values|rindex|substr|splice|" +
  26. "length|msgget|select|socket|return|caller|delete|alarm|ioctl|index|" +
  27. "undef|lstat|times|srand|chown|fcntl|close|write|umask|rmdir|study|" +
  28. "sleep|chomp|untie|print|utime|mkdir|atan2|split|crypt|flock|chmod|" +
  29. "BEGIN|bless|chdir|semop|shift|reset|link|stat|chop|grep|fork|dump|" +
  30. "join|open|tell|pipe|exit|glob|warn|each|bind|sort|pack|eval|push|" +
  31. "keys|getc|kill|seek|sqrt|send|wait|rand|tied|read|time|exec|recv|" +
  32. "eof|chr|int|ord|exp|pos|pop|sin|log|abs|oct|hex|tie|cos|vec|END|ref|" +
  33. "map|die|uc|lc|do"
  34. );
  35. var keywordMapper = this.createKeywordMapper({
  36. "keyword": keywords,
  37. "constant.language": buildinConstants,
  38. "support.function": builtinFunctions
  39. }, "identifier");
  40. this.$rules = {
  41. "start" : [
  42. {
  43. token : "comment.doc",
  44. regex : "^=(?:begin|item)\\b",
  45. next : "block_comment"
  46. }, {
  47. token : "string.regexp",
  48. regex : "[/](?:(?:\\[(?:\\\\]|[^\\]])+\\])|(?:\\\\/|[^\\]/]))*[/]\\w*\\s*(?=[).,;]|$)"
  49. }, {
  50. token : "string", // single line
  51. regex : '["](?:(?:\\\\.)|(?:[^"\\\\]))*?["]'
  52. }, {
  53. token : "string", // multi line string start
  54. regex : '["].*\\\\$',
  55. next : "qqstring"
  56. }, {
  57. token : "string", // single line
  58. regex : "['](?:(?:\\\\.)|(?:[^'\\\\]))*?[']"
  59. }, {
  60. token : "string", // multi line string start
  61. regex : "['].*\\\\$",
  62. next : "qstring"
  63. }, {
  64. token : "constant.numeric", // hex
  65. regex : "0x[0-9a-fA-F]+\\b"
  66. }, {
  67. token : "constant.numeric", // float
  68. regex : "[+-]?\\d+(?:(?:\\.\\d*)?(?:[eE][+-]?\\d+)?)?\\b"
  69. }, {
  70. token : keywordMapper,
  71. regex : "[a-zA-Z_$][a-zA-Z0-9_$]*\\b"
  72. }, {
  73. token : "keyword.operator",
  74. regex : "%#|\\$#|\\.\\.\\.|\\|\\|=|>>=|<<=|<=>|&&=|=>|!~|\\^=|&=|\\|=|\\.=|x=|%=|\\/=|\\*=|\\-=|\\+=|=~|\\*\\*|\\-\\-|\\.\\.|\\|\\||&&|\\+\\+|\\->|!=|==|>=|<=|>>|<<|,|=|\\?\\:|\\^|\\||x|%|\\/|\\*|<|&|\\\\|~|!|>|\\.|\\-|\\+|\\-C|\\-b|\\-S|\\-u|\\-t|\\-p|\\-l|\\-d|\\-f|\\-g|\\-s|\\-z|\\-k|\\-e|\\-O|\\-T|\\-B|\\-M|\\-A|\\-X|\\-W|\\-c|\\-R|\\-o|\\-x|\\-w|\\-r|\\b(?:and|cmp|eq|ge|gt|le|lt|ne|not|or|xor)"
  75. }, {
  76. token : "comment",
  77. regex : "#.*$"
  78. }, {
  79. token : "lparen",
  80. regex : "[[({]"
  81. }, {
  82. token : "rparen",
  83. regex : "[\\])}]"
  84. }, {
  85. token : "text",
  86. regex : "\\s+"
  87. }
  88. ],
  89. "qqstring" : [
  90. {
  91. token : "string",
  92. regex : '(?:(?:\\\\.)|(?:[^"\\\\]))*?"',
  93. next : "start"
  94. }, {
  95. token : "string",
  96. regex : '.+'
  97. }
  98. ],
  99. "qstring" : [
  100. {
  101. token : "string",
  102. regex : "(?:(?:\\\\.)|(?:[^'\\\\]))*?'",
  103. next : "start"
  104. }, {
  105. token : "string",
  106. regex : '.+'
  107. }
  108. ],
  109. "block_comment": [
  110. {
  111. token: "comment.doc",
  112. regex: "^=cut\\b",
  113. next: "start"
  114. },
  115. {
  116. defaultToken: "comment.doc"
  117. }
  118. ]
  119. };
  120. };
  121. oop.inherits(PerlHighlightRules, TextHighlightRules);
  122. exports.PerlHighlightRules = PerlHighlightRules;
  123. });
  124. ace.define("ace/mode/matching_brace_outdent",["require","exports","module","ace/range"], function(require, exports, module) {
  125. "use strict";
  126. var Range = require("../range").Range;
  127. var MatchingBraceOutdent = function() {};
  128. (function() {
  129. this.checkOutdent = function(line, input) {
  130. if (! /^\s+$/.test(line))
  131. return false;
  132. return /^\s*\}/.test(input);
  133. };
  134. this.autoOutdent = function(doc, row) {
  135. var line = doc.getLine(row);
  136. var match = line.match(/^(\s*\})/);
  137. if (!match) return 0;
  138. var column = match[1].length;
  139. var openBracePos = doc.findMatchingBracket({row: row, column: column});
  140. if (!openBracePos || openBracePos.row == row) return 0;
  141. var indent = this.$getIndent(doc.getLine(openBracePos.row));
  142. doc.replace(new Range(row, 0, row, column-1), indent);
  143. };
  144. this.$getIndent = function(line) {
  145. return line.match(/^\s*/)[0];
  146. };
  147. }).call(MatchingBraceOutdent.prototype);
  148. exports.MatchingBraceOutdent = MatchingBraceOutdent;
  149. });
  150. ace.define("ace/mode/folding/cstyle",["require","exports","module","ace/lib/oop","ace/range","ace/mode/folding/fold_mode"], function(require, exports, module) {
  151. "use strict";
  152. var oop = require("../../lib/oop");
  153. var Range = require("../../range").Range;
  154. var BaseFoldMode = require("./fold_mode").FoldMode;
  155. var FoldMode = exports.FoldMode = function(commentRegex) {
  156. if (commentRegex) {
  157. this.foldingStartMarker = new RegExp(
  158. this.foldingStartMarker.source.replace(/\|[^|]*?$/, "|" + commentRegex.start)
  159. );
  160. this.foldingStopMarker = new RegExp(
  161. this.foldingStopMarker.source.replace(/\|[^|]*?$/, "|" + commentRegex.end)
  162. );
  163. }
  164. };
  165. oop.inherits(FoldMode, BaseFoldMode);
  166. (function() {
  167. this.foldingStartMarker = /(\{|\[)[^\}\]]*$|^\s*(\/\*)/;
  168. this.foldingStopMarker = /^[^\[\{]*(\}|\])|^[\s\*]*(\*\/)/;
  169. this.singleLineBlockCommentRe= /^\s*(\/\*).*\*\/\s*$/;
  170. this.tripleStarBlockCommentRe = /^\s*(\/\*\*\*).*\*\/\s*$/;
  171. this.startRegionRe = /^\s*(\/\*|\/\/)#?region\b/;
  172. this._getFoldWidgetBase = this.getFoldWidget;
  173. this.getFoldWidget = function(session, foldStyle, row) {
  174. var line = session.getLine(row);
  175. if (this.singleLineBlockCommentRe.test(line)) {
  176. if (!this.startRegionRe.test(line) && !this.tripleStarBlockCommentRe.test(line))
  177. return "";
  178. }
  179. var fw = this._getFoldWidgetBase(session, foldStyle, row);
  180. if (!fw && this.startRegionRe.test(line))
  181. return "start"; // lineCommentRegionStart
  182. return fw;
  183. };
  184. this.getFoldWidgetRange = function(session, foldStyle, row, forceMultiline) {
  185. var line = session.getLine(row);
  186. if (this.startRegionRe.test(line))
  187. return this.getCommentRegionBlock(session, line, row);
  188. var match = line.match(this.foldingStartMarker);
  189. if (match) {
  190. var i = match.index;
  191. if (match[1])
  192. return this.openingBracketBlock(session, match[1], row, i);
  193. var range = session.getCommentFoldRange(row, i + match[0].length, 1);
  194. if (range && !range.isMultiLine()) {
  195. if (forceMultiline) {
  196. range = this.getSectionRange(session, row);
  197. } else if (foldStyle != "all")
  198. range = null;
  199. }
  200. return range;
  201. }
  202. if (foldStyle === "markbegin")
  203. return;
  204. var match = line.match(this.foldingStopMarker);
  205. if (match) {
  206. var i = match.index + match[0].length;
  207. if (match[1])
  208. return this.closingBracketBlock(session, match[1], row, i);
  209. return session.getCommentFoldRange(row, i, -1);
  210. }
  211. };
  212. this.getSectionRange = function(session, row) {
  213. var line = session.getLine(row);
  214. var startIndent = line.search(/\S/);
  215. var startRow = row;
  216. var startColumn = line.length;
  217. row = row + 1;
  218. var endRow = row;
  219. var maxRow = session.getLength();
  220. while (++row < maxRow) {
  221. line = session.getLine(row);
  222. var indent = line.search(/\S/);
  223. if (indent === -1)
  224. continue;
  225. if (startIndent > indent)
  226. break;
  227. var subRange = this.getFoldWidgetRange(session, "all", row);
  228. if (subRange) {
  229. if (subRange.start.row <= startRow) {
  230. break;
  231. } else if (subRange.isMultiLine()) {
  232. row = subRange.end.row;
  233. } else if (startIndent == indent) {
  234. break;
  235. }
  236. }
  237. endRow = row;
  238. }
  239. return new Range(startRow, startColumn, endRow, session.getLine(endRow).length);
  240. };
  241. this.getCommentRegionBlock = function(session, line, row) {
  242. var startColumn = line.search(/\s*$/);
  243. var maxRow = session.getLength();
  244. var startRow = row;
  245. var re = /^\s*(?:\/\*|\/\/|--)#?(end)?region\b/;
  246. var depth = 1;
  247. while (++row < maxRow) {
  248. line = session.getLine(row);
  249. var m = re.exec(line);
  250. if (!m) continue;
  251. if (m[1]) depth--;
  252. else depth++;
  253. if (!depth) break;
  254. }
  255. var endRow = row;
  256. if (endRow > startRow) {
  257. return new Range(startRow, startColumn, endRow, line.length);
  258. }
  259. };
  260. }).call(FoldMode.prototype);
  261. });
  262. ace.define("ace/mode/perl",["require","exports","module","ace/lib/oop","ace/mode/text","ace/mode/perl_highlight_rules","ace/mode/matching_brace_outdent","ace/mode/folding/cstyle"], function(require, exports, module) {
  263. "use strict";
  264. var oop = require("../lib/oop");
  265. var TextMode = require("./text").Mode;
  266. var PerlHighlightRules = require("./perl_highlight_rules").PerlHighlightRules;
  267. var MatchingBraceOutdent = require("./matching_brace_outdent").MatchingBraceOutdent;
  268. var CStyleFoldMode = require("./folding/cstyle").FoldMode;
  269. var Mode = function() {
  270. this.HighlightRules = PerlHighlightRules;
  271. this.$outdent = new MatchingBraceOutdent();
  272. this.foldingRules = new CStyleFoldMode({start: "^=(begin|item)\\b", end: "^=(cut)\\b"});
  273. this.$behaviour = this.$defaultBehaviour;
  274. };
  275. oop.inherits(Mode, TextMode);
  276. (function() {
  277. this.lineCommentStart = "#";
  278. this.blockComment = [
  279. {start: "=begin", end: "=cut", lineStartOnly: true},
  280. {start: "=item", end: "=cut", lineStartOnly: true}
  281. ];
  282. this.getNextLineIndent = function(state, line, tab) {
  283. var indent = this.$getIndent(line);
  284. var tokenizedLine = this.getTokenizer().getLineTokens(line, state);
  285. var tokens = tokenizedLine.tokens;
  286. if (tokens.length && tokens[tokens.length-1].type == "comment") {
  287. return indent;
  288. }
  289. if (state == "start") {
  290. var match = line.match(/^.*[\{\(\[:]\s*$/);
  291. if (match) {
  292. indent += tab;
  293. }
  294. }
  295. return indent;
  296. };
  297. this.checkOutdent = function(state, line, input) {
  298. return this.$outdent.checkOutdent(line, input);
  299. };
  300. this.autoOutdent = function(state, doc, row) {
  301. this.$outdent.autoOutdent(doc, row);
  302. };
  303. this.$id = "ace/mode/perl";
  304. }).call(Mode.prototype);
  305. exports.Mode = Mode;
  306. });