mode-json.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. ace.define("ace/mode/json_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 JsonHighlightRules = function() {
  6. this.$rules = {
  7. "start" : [
  8. {
  9. token : "variable", // single line
  10. regex : '["](?:(?:\\\\.)|(?:[^"\\\\]))*?["]\\s*(?=:)'
  11. }, {
  12. token : "string", // single line
  13. regex : '"',
  14. next : "string"
  15. }, {
  16. token : "constant.numeric", // hex
  17. regex : "0[xX][0-9a-fA-F]+\\b"
  18. }, {
  19. token : "constant.numeric", // float
  20. regex : "[+-]?\\d+(?:(?:\\.\\d*)?(?:[eE][+-]?\\d+)?)?\\b"
  21. }, {
  22. token : "constant.language.boolean",
  23. regex : "(?:true|false)\\b"
  24. }, {
  25. token : "invalid.illegal", // single quoted strings are not allowed
  26. regex : "['](?:(?:\\\\.)|(?:[^'\\\\]))*?[']"
  27. }, {
  28. token : "invalid.illegal", // comments are not allowed
  29. regex : "\\/\\/.*$"
  30. }, {
  31. token : "paren.lparen",
  32. regex : "[[({]"
  33. }, {
  34. token : "paren.rparen",
  35. regex : "[\\])}]"
  36. }, {
  37. token : "text",
  38. regex : "\\s+"
  39. }
  40. ],
  41. "string" : [
  42. {
  43. token : "constant.language.escape",
  44. regex : /\\(?:x[0-9a-fA-F]{2}|u[0-9a-fA-F]{4}|["\\\/bfnrt])/
  45. }, {
  46. token : "string",
  47. regex : '[^"\\\\]+'
  48. }, {
  49. token : "string",
  50. regex : '"',
  51. next : "start"
  52. }, {
  53. token : "string",
  54. regex : "",
  55. next : "start"
  56. }
  57. ]
  58. };
  59. };
  60. oop.inherits(JsonHighlightRules, TextHighlightRules);
  61. exports.JsonHighlightRules = JsonHighlightRules;
  62. });
  63. ace.define("ace/mode/matching_brace_outdent",["require","exports","module","ace/range"], function(require, exports, module) {
  64. "use strict";
  65. var Range = require("../range").Range;
  66. var MatchingBraceOutdent = function() {};
  67. (function() {
  68. this.checkOutdent = function(line, input) {
  69. if (! /^\s+$/.test(line))
  70. return false;
  71. return /^\s*\}/.test(input);
  72. };
  73. this.autoOutdent = function(doc, row) {
  74. var line = doc.getLine(row);
  75. var match = line.match(/^(\s*\})/);
  76. if (!match) return 0;
  77. var column = match[1].length;
  78. var openBracePos = doc.findMatchingBracket({row: row, column: column});
  79. if (!openBracePos || openBracePos.row == row) return 0;
  80. var indent = this.$getIndent(doc.getLine(openBracePos.row));
  81. doc.replace(new Range(row, 0, row, column-1), indent);
  82. };
  83. this.$getIndent = function(line) {
  84. return line.match(/^\s*/)[0];
  85. };
  86. }).call(MatchingBraceOutdent.prototype);
  87. exports.MatchingBraceOutdent = MatchingBraceOutdent;
  88. });
  89. ace.define("ace/mode/folding/cstyle",["require","exports","module","ace/lib/oop","ace/range","ace/mode/folding/fold_mode"], function(require, exports, module) {
  90. "use strict";
  91. var oop = require("../../lib/oop");
  92. var Range = require("../../range").Range;
  93. var BaseFoldMode = require("./fold_mode").FoldMode;
  94. var FoldMode = exports.FoldMode = function(commentRegex) {
  95. if (commentRegex) {
  96. this.foldingStartMarker = new RegExp(
  97. this.foldingStartMarker.source.replace(/\|[^|]*?$/, "|" + commentRegex.start)
  98. );
  99. this.foldingStopMarker = new RegExp(
  100. this.foldingStopMarker.source.replace(/\|[^|]*?$/, "|" + commentRegex.end)
  101. );
  102. }
  103. };
  104. oop.inherits(FoldMode, BaseFoldMode);
  105. (function() {
  106. this.foldingStartMarker = /(\{|\[)[^\}\]]*$|^\s*(\/\*)/;
  107. this.foldingStopMarker = /^[^\[\{]*(\}|\])|^[\s\*]*(\*\/)/;
  108. this.singleLineBlockCommentRe= /^\s*(\/\*).*\*\/\s*$/;
  109. this.tripleStarBlockCommentRe = /^\s*(\/\*\*\*).*\*\/\s*$/;
  110. this.startRegionRe = /^\s*(\/\*|\/\/)#?region\b/;
  111. this._getFoldWidgetBase = this.getFoldWidget;
  112. this.getFoldWidget = function(session, foldStyle, row) {
  113. var line = session.getLine(row);
  114. if (this.singleLineBlockCommentRe.test(line)) {
  115. if (!this.startRegionRe.test(line) && !this.tripleStarBlockCommentRe.test(line))
  116. return "";
  117. }
  118. var fw = this._getFoldWidgetBase(session, foldStyle, row);
  119. if (!fw && this.startRegionRe.test(line))
  120. return "start"; // lineCommentRegionStart
  121. return fw;
  122. };
  123. this.getFoldWidgetRange = function(session, foldStyle, row, forceMultiline) {
  124. var line = session.getLine(row);
  125. if (this.startRegionRe.test(line))
  126. return this.getCommentRegionBlock(session, line, row);
  127. var match = line.match(this.foldingStartMarker);
  128. if (match) {
  129. var i = match.index;
  130. if (match[1])
  131. return this.openingBracketBlock(session, match[1], row, i);
  132. var range = session.getCommentFoldRange(row, i + match[0].length, 1);
  133. if (range && !range.isMultiLine()) {
  134. if (forceMultiline) {
  135. range = this.getSectionRange(session, row);
  136. } else if (foldStyle != "all")
  137. range = null;
  138. }
  139. return range;
  140. }
  141. if (foldStyle === "markbegin")
  142. return;
  143. var match = line.match(this.foldingStopMarker);
  144. if (match) {
  145. var i = match.index + match[0].length;
  146. if (match[1])
  147. return this.closingBracketBlock(session, match[1], row, i);
  148. return session.getCommentFoldRange(row, i, -1);
  149. }
  150. };
  151. this.getSectionRange = function(session, row) {
  152. var line = session.getLine(row);
  153. var startIndent = line.search(/\S/);
  154. var startRow = row;
  155. var startColumn = line.length;
  156. row = row + 1;
  157. var endRow = row;
  158. var maxRow = session.getLength();
  159. while (++row < maxRow) {
  160. line = session.getLine(row);
  161. var indent = line.search(/\S/);
  162. if (indent === -1)
  163. continue;
  164. if (startIndent > indent)
  165. break;
  166. var subRange = this.getFoldWidgetRange(session, "all", row);
  167. if (subRange) {
  168. if (subRange.start.row <= startRow) {
  169. break;
  170. } else if (subRange.isMultiLine()) {
  171. row = subRange.end.row;
  172. } else if (startIndent == indent) {
  173. break;
  174. }
  175. }
  176. endRow = row;
  177. }
  178. return new Range(startRow, startColumn, endRow, session.getLine(endRow).length);
  179. };
  180. this.getCommentRegionBlock = function(session, line, row) {
  181. var startColumn = line.search(/\s*$/);
  182. var maxRow = session.getLength();
  183. var startRow = row;
  184. var re = /^\s*(?:\/\*|\/\/|--)#?(end)?region\b/;
  185. var depth = 1;
  186. while (++row < maxRow) {
  187. line = session.getLine(row);
  188. var m = re.exec(line);
  189. if (!m) continue;
  190. if (m[1]) depth--;
  191. else depth++;
  192. if (!depth) break;
  193. }
  194. var endRow = row;
  195. if (endRow > startRow) {
  196. return new Range(startRow, startColumn, endRow, line.length);
  197. }
  198. };
  199. }).call(FoldMode.prototype);
  200. });
  201. ace.define("ace/mode/json",["require","exports","module","ace/lib/oop","ace/mode/text","ace/mode/json_highlight_rules","ace/mode/matching_brace_outdent","ace/mode/behaviour/cstyle","ace/mode/folding/cstyle","ace/worker/worker_client"], function(require, exports, module) {
  202. "use strict";
  203. var oop = require("../lib/oop");
  204. var TextMode = require("./text").Mode;
  205. var HighlightRules = require("./json_highlight_rules").JsonHighlightRules;
  206. var MatchingBraceOutdent = require("./matching_brace_outdent").MatchingBraceOutdent;
  207. var CstyleBehaviour = require("./behaviour/cstyle").CstyleBehaviour;
  208. var CStyleFoldMode = require("./folding/cstyle").FoldMode;
  209. var WorkerClient = require("../worker/worker_client").WorkerClient;
  210. var Mode = function() {
  211. this.HighlightRules = HighlightRules;
  212. this.$outdent = new MatchingBraceOutdent();
  213. this.$behaviour = new CstyleBehaviour();
  214. this.foldingRules = new CStyleFoldMode();
  215. };
  216. oop.inherits(Mode, TextMode);
  217. (function() {
  218. this.getNextLineIndent = function(state, line, tab) {
  219. var indent = this.$getIndent(line);
  220. if (state == "start") {
  221. var match = line.match(/^.*[\{\(\[]\s*$/);
  222. if (match) {
  223. indent += tab;
  224. }
  225. }
  226. return indent;
  227. };
  228. this.checkOutdent = function(state, line, input) {
  229. return this.$outdent.checkOutdent(line, input);
  230. };
  231. this.autoOutdent = function(state, doc, row) {
  232. this.$outdent.autoOutdent(doc, row);
  233. };
  234. this.createWorker = function(session) {
  235. var worker = new WorkerClient(["ace"], "ace/mode/json_worker", "JsonWorker");
  236. worker.attachToDocument(session.getDocument());
  237. worker.on("annotate", function(e) {
  238. session.setAnnotations(e.data);
  239. });
  240. worker.on("terminate", function() {
  241. session.clearAnnotations();
  242. });
  243. return worker;
  244. };
  245. this.$id = "ace/mode/json";
  246. }).call(Mode.prototype);
  247. exports.Mode = Mode;
  248. });