mode-sh.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. ace.define("ace/mode/sh_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 reservedKeywords = exports.reservedKeywords = (
  6. '!|{|}|case|do|done|elif|else|'+
  7. 'esac|fi|for|if|in|then|until|while|'+
  8. '&|;|export|local|read|typeset|unset|'+
  9. 'elif|select|set|function|declare|readonly'
  10. );
  11. var languageConstructs = exports.languageConstructs = (
  12. '[|]|alias|bg|bind|break|builtin|'+
  13. 'cd|command|compgen|complete|continue|'+
  14. 'dirs|disown|echo|enable|eval|exec|'+
  15. 'exit|fc|fg|getopts|hash|help|history|'+
  16. 'jobs|kill|let|logout|popd|printf|pushd|'+
  17. 'pwd|return|set|shift|shopt|source|'+
  18. 'suspend|test|times|trap|type|ulimit|'+
  19. 'umask|unalias|wait'
  20. );
  21. var ShHighlightRules = function() {
  22. var keywordMapper = this.createKeywordMapper({
  23. "keyword": reservedKeywords,
  24. "support.function.builtin": languageConstructs,
  25. "invalid.deprecated": "debugger"
  26. }, "identifier");
  27. var integer = "(?:(?:[1-9]\\d*)|(?:0))";
  28. var fraction = "(?:\\.\\d+)";
  29. var intPart = "(?:\\d+)";
  30. var pointFloat = "(?:(?:" + intPart + "?" + fraction + ")|(?:" + intPart + "\\.))";
  31. var exponentFloat = "(?:(?:" + pointFloat + "|" + intPart + ")" + ")";
  32. var floatNumber = "(?:" + exponentFloat + "|" + pointFloat + ")";
  33. var fileDescriptor = "(?:&" + intPart + ")";
  34. var variableName = "[a-zA-Z_][a-zA-Z0-9_]*";
  35. var variable = "(?:" + variableName + "(?==))";
  36. var builtinVariable = "(?:\\$(?:SHLVL|\\$|\\!|\\?))";
  37. var func = "(?:" + variableName + "\\s*\\(\\))";
  38. this.$rules = {
  39. "start" : [{
  40. token : "constant",
  41. regex : /\\./
  42. }, {
  43. token : ["text", "comment"],
  44. regex : /(^|\s)(#.*)$/
  45. }, {
  46. token : "string.start",
  47. regex : '"',
  48. push : [{
  49. token : "constant.language.escape",
  50. regex : /\\(?:[$`"\\]|$)/
  51. }, {
  52. include : "variables"
  53. }, {
  54. token : "keyword.operator",
  55. regex : /`/ // TODO highlight `
  56. }, {
  57. token : "string.end",
  58. regex : '"',
  59. next: "pop"
  60. }, {
  61. defaultToken: "string"
  62. }]
  63. }, {
  64. token : "string",
  65. regex : "\\$'",
  66. push : [{
  67. token : "constant.language.escape",
  68. regex : /\\(?:[abeEfnrtv\\'"]|x[a-fA-F\d]{1,2}|u[a-fA-F\d]{4}([a-fA-F\d]{4})?|c.|\d{1,3})/
  69. }, {
  70. token : "string",
  71. regex : "'",
  72. next: "pop"
  73. }, {
  74. defaultToken: "string"
  75. }]
  76. }, {
  77. regex : "<<<",
  78. token : "keyword.operator"
  79. }, {
  80. stateName: "heredoc",
  81. regex : "(<<-?)(\\s*)(['\"`]?)([\\w\\-]+)(['\"`]?)",
  82. onMatch : function(value, currentState, stack) {
  83. var next = value[2] == '-' ? "indentedHeredoc" : "heredoc";
  84. var tokens = value.split(this.splitRegex);
  85. stack.push(next, tokens[4]);
  86. return [
  87. {type:"constant", value: tokens[1]},
  88. {type:"text", value: tokens[2]},
  89. {type:"string", value: tokens[3]},
  90. {type:"support.class", value: tokens[4]},
  91. {type:"string", value: tokens[5]}
  92. ];
  93. },
  94. rules: {
  95. heredoc: [{
  96. onMatch: function(value, currentState, stack) {
  97. if (value === stack[1]) {
  98. stack.shift();
  99. stack.shift();
  100. this.next = stack[0] || "start";
  101. return "support.class";
  102. }
  103. this.next = "";
  104. return "string";
  105. },
  106. regex: ".*$",
  107. next: "start"
  108. }],
  109. indentedHeredoc: [{
  110. token: "string",
  111. regex: "^\t+"
  112. }, {
  113. onMatch: function(value, currentState, stack) {
  114. if (value === stack[1]) {
  115. stack.shift();
  116. stack.shift();
  117. this.next = stack[0] || "start";
  118. return "support.class";
  119. }
  120. this.next = "";
  121. return "string";
  122. },
  123. regex: ".*$",
  124. next: "start"
  125. }]
  126. }
  127. }, {
  128. regex : "$",
  129. token : "empty",
  130. next : function(currentState, stack) {
  131. if (stack[0] === "heredoc" || stack[0] === "indentedHeredoc")
  132. return stack[0];
  133. return currentState;
  134. }
  135. }, {
  136. token : ["keyword", "text", "text", "text", "variable"],
  137. regex : /(declare|local|readonly)(\s+)(?:(-[fixar]+)(\s+))?([a-zA-Z_][a-zA-Z0-9_]*\b)/
  138. }, {
  139. token : "variable.language",
  140. regex : builtinVariable
  141. }, {
  142. token : "variable",
  143. regex : variable
  144. }, {
  145. include : "variables"
  146. }, {
  147. token : "support.function",
  148. regex : func
  149. }, {
  150. token : "support.function",
  151. regex : fileDescriptor
  152. }, {
  153. token : "string", // ' string
  154. start : "'", end : "'"
  155. }, {
  156. token : "constant.numeric", // float
  157. regex : floatNumber
  158. }, {
  159. token : "constant.numeric", // integer
  160. regex : integer + "\\b"
  161. }, {
  162. token : keywordMapper,
  163. regex : "[a-zA-Z_][a-zA-Z0-9_]*\\b"
  164. }, {
  165. token : "keyword.operator",
  166. regex : "\\+|\\-|\\*|\\*\\*|\\/|\\/\\/|~|<|>|<=|=>|=|!=|[%&|`]"
  167. }, {
  168. token : "punctuation.operator",
  169. regex : ";"
  170. }, {
  171. token : "paren.lparen",
  172. regex : "[\\[\\(\\{]"
  173. }, {
  174. token : "paren.rparen",
  175. regex : "[\\]]"
  176. }, {
  177. token : "paren.rparen",
  178. regex : "[\\)\\}]",
  179. next : "pop"
  180. }],
  181. variables: [{
  182. token : "variable",
  183. regex : /(\$)(\w+)/
  184. }, {
  185. token : ["variable", "paren.lparen"],
  186. regex : /(\$)(\()/,
  187. push : "start"
  188. }, {
  189. token : ["variable", "paren.lparen", "keyword.operator", "variable", "keyword.operator"],
  190. regex : /(\$)(\{)([#!]?)(\w+|[*@#?\-$!0_])(:[?+\-=]?|##?|%%?|,,?\/|\^\^?)?/,
  191. push : "start"
  192. }, {
  193. token : "variable",
  194. regex : /\$[*@#?\-$!0_]/
  195. }, {
  196. token : ["variable", "paren.lparen"],
  197. regex : /(\$)(\{)/,
  198. push : "start"
  199. }]
  200. };
  201. this.normalizeRules();
  202. };
  203. oop.inherits(ShHighlightRules, TextHighlightRules);
  204. exports.ShHighlightRules = ShHighlightRules;
  205. });
  206. ace.define("ace/mode/folding/cstyle",[], function(require, exports, module) {
  207. "use strict";
  208. var oop = require("../../lib/oop");
  209. var Range = require("../../range").Range;
  210. var BaseFoldMode = require("./fold_mode").FoldMode;
  211. var FoldMode = exports.FoldMode = function(commentRegex) {
  212. if (commentRegex) {
  213. this.foldingStartMarker = new RegExp(
  214. this.foldingStartMarker.source.replace(/\|[^|]*?$/, "|" + commentRegex.start)
  215. );
  216. this.foldingStopMarker = new RegExp(
  217. this.foldingStopMarker.source.replace(/\|[^|]*?$/, "|" + commentRegex.end)
  218. );
  219. }
  220. };
  221. oop.inherits(FoldMode, BaseFoldMode);
  222. (function() {
  223. this.foldingStartMarker = /([\{\[\(])[^\}\]\)]*$|^\s*(\/\*)/;
  224. this.foldingStopMarker = /^[^\[\{\(]*([\}\]\)])|^[\s\*]*(\*\/)/;
  225. this.singleLineBlockCommentRe= /^\s*(\/\*).*\*\/\s*$/;
  226. this.tripleStarBlockCommentRe = /^\s*(\/\*\*\*).*\*\/\s*$/;
  227. this.startRegionRe = /^\s*(\/\*|\/\/)#?region\b/;
  228. this._getFoldWidgetBase = this.getFoldWidget;
  229. this.getFoldWidget = function(session, foldStyle, row) {
  230. var line = session.getLine(row);
  231. if (this.singleLineBlockCommentRe.test(line)) {
  232. if (!this.startRegionRe.test(line) && !this.tripleStarBlockCommentRe.test(line))
  233. return "";
  234. }
  235. var fw = this._getFoldWidgetBase(session, foldStyle, row);
  236. if (!fw && this.startRegionRe.test(line))
  237. return "start"; // lineCommentRegionStart
  238. return fw;
  239. };
  240. this.getFoldWidgetRange = function(session, foldStyle, row, forceMultiline) {
  241. var line = session.getLine(row);
  242. if (this.startRegionRe.test(line))
  243. return this.getCommentRegionBlock(session, line, row);
  244. var match = line.match(this.foldingStartMarker);
  245. if (match) {
  246. var i = match.index;
  247. if (match[1])
  248. return this.openingBracketBlock(session, match[1], row, i);
  249. var range = session.getCommentFoldRange(row, i + match[0].length, 1);
  250. if (range && !range.isMultiLine()) {
  251. if (forceMultiline) {
  252. range = this.getSectionRange(session, row);
  253. } else if (foldStyle != "all")
  254. range = null;
  255. }
  256. return range;
  257. }
  258. if (foldStyle === "markbegin")
  259. return;
  260. var match = line.match(this.foldingStopMarker);
  261. if (match) {
  262. var i = match.index + match[0].length;
  263. if (match[1])
  264. return this.closingBracketBlock(session, match[1], row, i);
  265. return session.getCommentFoldRange(row, i, -1);
  266. }
  267. };
  268. this.getSectionRange = function(session, row) {
  269. var line = session.getLine(row);
  270. var startIndent = line.search(/\S/);
  271. var startRow = row;
  272. var startColumn = line.length;
  273. row = row + 1;
  274. var endRow = row;
  275. var maxRow = session.getLength();
  276. while (++row < maxRow) {
  277. line = session.getLine(row);
  278. var indent = line.search(/\S/);
  279. if (indent === -1)
  280. continue;
  281. if (startIndent > indent)
  282. break;
  283. var subRange = this.getFoldWidgetRange(session, "all", row);
  284. if (subRange) {
  285. if (subRange.start.row <= startRow) {
  286. break;
  287. } else if (subRange.isMultiLine()) {
  288. row = subRange.end.row;
  289. } else if (startIndent == indent) {
  290. break;
  291. }
  292. }
  293. endRow = row;
  294. }
  295. return new Range(startRow, startColumn, endRow, session.getLine(endRow).length);
  296. };
  297. this.getCommentRegionBlock = function(session, line, row) {
  298. var startColumn = line.search(/\s*$/);
  299. var maxRow = session.getLength();
  300. var startRow = row;
  301. var re = /^\s*(?:\/\*|\/\/|--)#?(end)?region\b/;
  302. var depth = 1;
  303. while (++row < maxRow) {
  304. line = session.getLine(row);
  305. var m = re.exec(line);
  306. if (!m) continue;
  307. if (m[1]) depth--;
  308. else depth++;
  309. if (!depth) break;
  310. }
  311. var endRow = row;
  312. if (endRow > startRow) {
  313. return new Range(startRow, startColumn, endRow, line.length);
  314. }
  315. };
  316. }).call(FoldMode.prototype);
  317. });
  318. ace.define("ace/mode/sh",[], function(require, exports, module) {
  319. "use strict";
  320. var oop = require("../lib/oop");
  321. var TextMode = require("./text").Mode;
  322. var ShHighlightRules = require("./sh_highlight_rules").ShHighlightRules;
  323. var Range = require("../range").Range;
  324. var CStyleFoldMode = require("./folding/cstyle").FoldMode;
  325. var CstyleBehaviour = require("./behaviour/cstyle").CstyleBehaviour;
  326. var Mode = function() {
  327. this.HighlightRules = ShHighlightRules;
  328. this.foldingRules = new CStyleFoldMode();
  329. this.$behaviour = new CstyleBehaviour();
  330. };
  331. oop.inherits(Mode, TextMode);
  332. (function() {
  333. this.lineCommentStart = "#";
  334. this.getNextLineIndent = function(state, line, tab) {
  335. var indent = this.$getIndent(line);
  336. var tokenizedLine = this.getTokenizer().getLineTokens(line, state);
  337. var tokens = tokenizedLine.tokens;
  338. if (tokens.length && tokens[tokens.length-1].type == "comment") {
  339. return indent;
  340. }
  341. if (state == "start") {
  342. var match = line.match(/^.*[\{\(\[:]\s*$/);
  343. if (match) {
  344. indent += tab;
  345. }
  346. }
  347. return indent;
  348. };
  349. var outdents = {
  350. "pass": 1,
  351. "return": 1,
  352. "raise": 1,
  353. "break": 1,
  354. "continue": 1
  355. };
  356. this.checkOutdent = function(state, line, input) {
  357. if (input !== "\r\n" && input !== "\r" && input !== "\n")
  358. return false;
  359. var tokens = this.getTokenizer().getLineTokens(line.trim(), state).tokens;
  360. if (!tokens)
  361. return false;
  362. do {
  363. var last = tokens.pop();
  364. } while (last && (last.type == "comment" || (last.type == "text" && last.value.match(/^\s+$/))));
  365. if (!last)
  366. return false;
  367. return (last.type == "keyword" && outdents[last.value]);
  368. };
  369. this.autoOutdent = function(state, doc, row) {
  370. row += 1;
  371. var indent = this.$getIndent(doc.getLine(row));
  372. var tab = doc.getTabString();
  373. if (indent.slice(-tab.length) == tab)
  374. doc.remove(new Range(row, indent.length-tab.length, row, indent.length));
  375. };
  376. this.$id = "ace/mode/sh";
  377. }).call(Mode.prototype);
  378. exports.Mode = Mode;
  379. });
  380. (function() {
  381. ace.require(["ace/mode/sh"], function(m) {
  382. if (typeof module == "object" && typeof exports == "object" && module) {
  383. module.exports = m;
  384. }
  385. });
  386. })();