mode-scheme.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. ace.define("ace/mode/scheme_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 SchemeHighlightRules = function() {
  6. var keywordControl = "case|do|let|loop|if|else|when";
  7. var keywordOperator = "eq?|eqv?|equal?|and|or|not|null?";
  8. var constantLanguage = "#t|#f";
  9. var supportFunctions = "cons|car|cdr|cond|lambda|lambda*|syntax-rules|format|set!|quote|eval|append|list|list?|member?|load";
  10. var keywordMapper = this.createKeywordMapper({
  11. "keyword.control": keywordControl,
  12. "keyword.operator": keywordOperator,
  13. "constant.language": constantLanguage,
  14. "support.function": supportFunctions
  15. }, "identifier", true);
  16. this.$rules =
  17. {
  18. "start": [
  19. {
  20. token : "comment",
  21. regex : ";.*$"
  22. },
  23. {
  24. "token": ["storage.type.function-type.scheme", "text", "entity.name.function.scheme"],
  25. "regex": "(?:\\b(?:(define|define-syntax|define-macro))\\b)(\\s+)((?:\\w|\\-|\\!|\\?)*)"
  26. },
  27. {
  28. "token": "punctuation.definition.constant.character.scheme",
  29. "regex": "#:\\S+"
  30. },
  31. {
  32. "token": ["punctuation.definition.variable.scheme", "variable.other.global.scheme", "punctuation.definition.variable.scheme"],
  33. "regex": "(\\*)(\\S*)(\\*)"
  34. },
  35. {
  36. "token" : "constant.numeric", // hex
  37. "regex" : "#[xXoObB][0-9a-fA-F]+"
  38. },
  39. {
  40. "token" : "constant.numeric", // float
  41. "regex" : "[+-]?\\d+(?:(?:\\.\\d*)?(?:[eE][+-]?\\d+)?)?"
  42. },
  43. {
  44. "token" : keywordMapper,
  45. "regex" : "[a-zA-Z_#][a-zA-Z0-9_\\-\\?\\!\\*]*"
  46. },
  47. {
  48. "token" : "string",
  49. "regex" : '"(?=.)',
  50. "next" : "qqstring"
  51. }
  52. ],
  53. "qqstring": [
  54. {
  55. "token": "constant.character.escape.scheme",
  56. "regex": "\\\\."
  57. },
  58. {
  59. "token" : "string",
  60. "regex" : '[^"\\\\]+',
  61. "merge" : true
  62. }, {
  63. "token" : "string",
  64. "regex" : "\\\\$",
  65. "next" : "qqstring",
  66. "merge" : true
  67. }, {
  68. "token" : "string",
  69. "regex" : '"|$',
  70. "next" : "start",
  71. "merge" : true
  72. }
  73. ]
  74. };
  75. };
  76. oop.inherits(SchemeHighlightRules, TextHighlightRules);
  77. exports.SchemeHighlightRules = SchemeHighlightRules;
  78. });
  79. ace.define("ace/mode/matching_parens_outdent",[], function(require, exports, module) {
  80. "use strict";
  81. var Range = require("../range").Range;
  82. var MatchingParensOutdent = function() {};
  83. (function() {
  84. this.checkOutdent = function(line, input) {
  85. if (! /^\s+$/.test(line))
  86. return false;
  87. return /^\s*\)/.test(input);
  88. };
  89. this.autoOutdent = function(doc, row) {
  90. var line = doc.getLine(row);
  91. var match = line.match(/^(\s*\))/);
  92. if (!match) return 0;
  93. var column = match[1].length;
  94. var openBracePos = doc.findMatchingBracket({row: row, column: column});
  95. if (!openBracePos || openBracePos.row == row) return 0;
  96. var indent = this.$getIndent(doc.getLine(openBracePos.row));
  97. doc.replace(new Range(row, 0, row, column-1), indent);
  98. };
  99. this.$getIndent = function(line) {
  100. var match = line.match(/^(\s+)/);
  101. if (match) {
  102. return match[1];
  103. }
  104. return "";
  105. };
  106. }).call(MatchingParensOutdent.prototype);
  107. exports.MatchingParensOutdent = MatchingParensOutdent;
  108. });
  109. ace.define("ace/mode/scheme",[], function(require, exports, module) {
  110. "use strict";
  111. var oop = require("../lib/oop");
  112. var TextMode = require("./text").Mode;
  113. var SchemeHighlightRules = require("./scheme_highlight_rules").SchemeHighlightRules;
  114. var MatchingParensOutdent = require("./matching_parens_outdent").MatchingParensOutdent;
  115. var Mode = function() {
  116. this.HighlightRules = SchemeHighlightRules;
  117. this.$outdent = new MatchingParensOutdent();
  118. this.$behaviour = this.$defaultBehaviour;
  119. };
  120. oop.inherits(Mode, TextMode);
  121. (function() {
  122. this.lineCommentStart = ";";
  123. this.minorIndentFunctions = ["define", "lambda", "define-macro", "define-syntax", "syntax-rules", "define-record-type", "define-structure"];
  124. this.$toIndent = function(str) {
  125. return str.split('').map(function(ch) {
  126. if (/\s/.exec(ch)) {
  127. return ch;
  128. } else {
  129. return ' ';
  130. }
  131. }).join('');
  132. };
  133. this.$calculateIndent = function(line, tab) {
  134. var baseIndent = this.$getIndent(line);
  135. var delta = 0;
  136. var isParen, ch;
  137. for (var i = line.length - 1; i >= 0; i--) {
  138. ch = line[i];
  139. if (ch === '(') {
  140. delta--;
  141. isParen = true;
  142. } else if (ch === '(' || ch === '[' || ch === '{') {
  143. delta--;
  144. isParen = false;
  145. } else if (ch === ')' || ch === ']' || ch === '}') {
  146. delta++;
  147. }
  148. if (delta < 0) {
  149. break;
  150. }
  151. }
  152. if (delta < 0 && isParen) {
  153. i += 1;
  154. var iBefore = i;
  155. var fn = '';
  156. while (true) {
  157. ch = line[i];
  158. if (ch === ' ' || ch === '\t') {
  159. if(this.minorIndentFunctions.indexOf(fn) !== -1) {
  160. return this.$toIndent(line.substring(0, iBefore - 1) + tab);
  161. } else {
  162. return this.$toIndent(line.substring(0, i + 1));
  163. }
  164. } else if (ch === undefined) {
  165. return this.$toIndent(line.substring(0, iBefore - 1) + tab);
  166. }
  167. fn += line[i];
  168. i++;
  169. }
  170. } else if(delta < 0 && !isParen) {
  171. return this.$toIndent(line.substring(0, i+1));
  172. } else if(delta > 0) {
  173. baseIndent = baseIndent.substring(0, baseIndent.length - tab.length);
  174. return baseIndent;
  175. } else {
  176. return baseIndent;
  177. }
  178. };
  179. this.getNextLineIndent = function(state, line, tab) {
  180. return this.$calculateIndent(line, tab);
  181. };
  182. this.checkOutdent = function(state, line, input) {
  183. return this.$outdent.checkOutdent(line, input);
  184. };
  185. this.autoOutdent = function(state, doc, row) {
  186. this.$outdent.autoOutdent(doc, row);
  187. };
  188. this.$id = "ace/mode/scheme";
  189. }).call(Mode.prototype);
  190. exports.Mode = Mode;
  191. });
  192. (function() {
  193. ace.require(["ace/mode/scheme"], function(m) {
  194. if (typeof module == "object" && typeof exports == "object" && module) {
  195. module.exports = m;
  196. }
  197. });
  198. })();