mode-forth.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. ace.define("ace/mode/forth_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 ForthHighlightRules = function() {
  6. this.$rules = { start: [ { include: '#forth' } ],
  7. '#comment':
  8. [ { token: 'comment.line.double-dash.forth',
  9. regex: '(?:^|\\s)--\\s.*$',
  10. comment: 'line comments for iForth' },
  11. { token: 'comment.line.backslash.forth',
  12. regex: '(?:^|\\s)\\\\[\\s\\S]*$',
  13. comment: 'ANSI line comment' },
  14. { token: 'comment.line.backslash-g.forth',
  15. regex: '(?:^|\\s)\\\\[Gg] .*$',
  16. comment: 'gForth line comment' },
  17. { token: 'comment.block.forth',
  18. regex: '(?:^|\\s)\\(\\*(?=\\s|$)',
  19. push:
  20. [ { token: 'comment.block.forth',
  21. regex: '(?:^|\\s)\\*\\)(?=\\s|$)',
  22. next: 'pop' },
  23. { defaultToken: 'comment.block.forth' } ],
  24. comment: 'multiline comments for iForth' },
  25. { token: 'comment.block.documentation.forth',
  26. regex: '\\bDOC\\b',
  27. caseInsensitive: true,
  28. push:
  29. [ { token: 'comment.block.documentation.forth',
  30. regex: '\\bENDDOC\\b',
  31. caseInsensitive: true,
  32. next: 'pop' },
  33. { defaultToken: 'comment.block.documentation.forth' } ],
  34. comment: 'documentation comments for iForth' },
  35. { token: 'comment.line.parentheses.forth',
  36. regex: '(?:^|\\s)\\.?\\( [^)]*\\)',
  37. comment: 'ANSI line comment' } ],
  38. '#constant':
  39. [ { token: 'constant.language.forth',
  40. regex: '(?:^|\\s)(?:TRUE|FALSE|BL|PI|CELL|C/L|R/O|W/O|R/W)(?=\\s|$)',
  41. caseInsensitive: true},
  42. { token: 'constant.numeric.forth',
  43. regex: '(?:^|\\s)[$#%]?[-+]?[0-9]+(?:\\.[0-9]*e-?[0-9]+|\\.?[0-9a-fA-F]*)(?=\\s|$)'},
  44. { token: 'constant.character.forth',
  45. regex: '(?:^|\\s)(?:[&^]\\S|(?:"|\')\\S(?:"|\'))(?=\\s|$)'}],
  46. '#forth':
  47. [ { include: '#constant' },
  48. { include: '#comment' },
  49. { include: '#string' },
  50. { include: '#word' },
  51. { include: '#variable' },
  52. { include: '#storage' },
  53. { include: '#word-def' } ],
  54. '#storage':
  55. [ { token: 'storage.type.forth',
  56. regex: '(?:^|\\s)(?:2CONSTANT|2VARIABLE|ALIAS|CONSTANT|CREATE-INTERPRET/COMPILE[:]?|CREATE|DEFER|FCONSTANT|FIELD|FVARIABLE|USER|VALUE|VARIABLE|VOCABULARY)(?=\\s|$)',
  57. caseInsensitive: true}],
  58. '#string':
  59. [ { token: 'string.quoted.double.forth',
  60. regex: '(ABORT" |BREAK" |\\." |C" |0"|S\\\\?" )([^"]+")',
  61. caseInsensitive: true},
  62. { token: 'string.unquoted.forth',
  63. regex: '(?:INCLUDE|NEEDS|REQUIRE|USE)[ ]\\S+(?=\\s|$)',
  64. caseInsensitive: true}],
  65. '#variable':
  66. [ { token: 'variable.language.forth',
  67. regex: '\\b(?:I|J)\\b',
  68. caseInsensitive: true } ],
  69. '#word':
  70. [ { token: 'keyword.control.immediate.forth',
  71. regex: '(?:^|\\s)\\[(?:\\?DO|\\+LOOP|AGAIN|BEGIN|DEFINED|DO|ELSE|ENDIF|FOR|IF|IFDEF|IFUNDEF|LOOP|NEXT|REPEAT|THEN|UNTIL|WHILE)\\](?=\\s|$)',
  72. caseInsensitive: true},
  73. { token: 'keyword.other.immediate.forth',
  74. regex: '(?:^|\\s)(?:COMPILE-ONLY|IMMEDIATE|IS|RESTRICT|TO|WHAT\'S|])(?=\\s|$)',
  75. caseInsensitive: true},
  76. { token: 'keyword.control.compile-only.forth',
  77. regex: '(?:^|\\s)(?:-DO|\\-LOOP|\\?DO|\\?LEAVE|\\+DO|\\+LOOP|ABORT\\"|AGAIN|AHEAD|BEGIN|CASE|DO|ELSE|ENDCASE|ENDIF|ENDOF|ENDTRY\\-IFERROR|ENDTRY|FOR|IF|IFERROR|LEAVE|LOOP|NEXT|RECOVER|REPEAT|RESTORE|THEN|TRY|U\\-DO|U\\+DO|UNTIL|WHILE)(?=\\s|$)',
  78. caseInsensitive: true},
  79. { token: 'keyword.other.compile-only.forth',
  80. regex: '(?:^|\\s)(?:\\?DUP-0=-IF|\\?DUP-IF|\\)|\\[|\\[\'\\]|\\[CHAR\\]|\\[COMPILE\\]|\\[IS\\]|\\[TO\\]|<COMPILATION|<INTERPRETATION|ASSERT\\(|ASSERT0\\(|ASSERT1\\(|ASSERT2\\(|ASSERT3\\(|COMPILATION>|DEFERS|DOES>|INTERPRETATION>|OF|POSTPONE)(?=\\s|$)',
  81. caseInsensitive: true},
  82. { token: 'keyword.other.non-immediate.forth',
  83. regex: '(?:^|\\s)(?:\'|<IS>|<TO>|CHAR|END-STRUCT|INCLUDE[D]?|LOAD|NEEDS|REQUIRE[D]?|REVISION|SEE|STRUCT|THRU|USE)(?=\\s|$)',
  84. caseInsensitive: true},
  85. { token: 'keyword.other.warning.forth',
  86. regex: '(?:^|\\s)(?:~~|BREAK:|BREAK"|DBG)(?=\\s|$)',
  87. caseInsensitive: true}],
  88. '#word-def':
  89. [ { token:
  90. [ 'keyword.other.compile-only.forth',
  91. 'keyword.other.compile-only.forth',
  92. 'meta.block.forth',
  93. 'entity.name.function.forth' ],
  94. regex: '(:NONAME)|(^:|\\s:)(\\s)(\\S+)(?=\\s|$)',
  95. caseInsensitive: true,
  96. push:
  97. [ { token: 'keyword.other.compile-only.forth',
  98. regex: ';(?:CODE)?',
  99. caseInsensitive: true,
  100. next: 'pop' },
  101. { include: '#constant' },
  102. { include: '#comment' },
  103. { include: '#string' },
  104. { include: '#word' },
  105. { include: '#variable' },
  106. { include: '#storage' },
  107. { defaultToken: 'meta.block.forth' } ] } ] };
  108. this.normalizeRules();
  109. };
  110. ForthHighlightRules.metaData = { fileTypes: [ 'frt', 'fs', 'ldr', 'fth', '4th' ],
  111. foldingStartMarker: '/\\*\\*|\\{\\s*$',
  112. foldingStopMarker: '\\*\\*/|^\\s*\\}',
  113. keyEquivalent: '^~F',
  114. name: 'Forth',
  115. scopeName: 'source.forth' };
  116. oop.inherits(ForthHighlightRules, TextHighlightRules);
  117. exports.ForthHighlightRules = ForthHighlightRules;
  118. });
  119. ace.define("ace/mode/folding/cstyle",[], function(require, exports, module) {
  120. "use strict";
  121. var oop = require("../../lib/oop");
  122. var Range = require("../../range").Range;
  123. var BaseFoldMode = require("./fold_mode").FoldMode;
  124. var FoldMode = exports.FoldMode = function(commentRegex) {
  125. if (commentRegex) {
  126. this.foldingStartMarker = new RegExp(
  127. this.foldingStartMarker.source.replace(/\|[^|]*?$/, "|" + commentRegex.start)
  128. );
  129. this.foldingStopMarker = new RegExp(
  130. this.foldingStopMarker.source.replace(/\|[^|]*?$/, "|" + commentRegex.end)
  131. );
  132. }
  133. };
  134. oop.inherits(FoldMode, BaseFoldMode);
  135. (function() {
  136. this.foldingStartMarker = /([\{\[\(])[^\}\]\)]*$|^\s*(\/\*)/;
  137. this.foldingStopMarker = /^[^\[\{\(]*([\}\]\)])|^[\s\*]*(\*\/)/;
  138. this.singleLineBlockCommentRe= /^\s*(\/\*).*\*\/\s*$/;
  139. this.tripleStarBlockCommentRe = /^\s*(\/\*\*\*).*\*\/\s*$/;
  140. this.startRegionRe = /^\s*(\/\*|\/\/)#?region\b/;
  141. this._getFoldWidgetBase = this.getFoldWidget;
  142. this.getFoldWidget = function(session, foldStyle, row) {
  143. var line = session.getLine(row);
  144. if (this.singleLineBlockCommentRe.test(line)) {
  145. if (!this.startRegionRe.test(line) && !this.tripleStarBlockCommentRe.test(line))
  146. return "";
  147. }
  148. var fw = this._getFoldWidgetBase(session, foldStyle, row);
  149. if (!fw && this.startRegionRe.test(line))
  150. return "start"; // lineCommentRegionStart
  151. return fw;
  152. };
  153. this.getFoldWidgetRange = function(session, foldStyle, row, forceMultiline) {
  154. var line = session.getLine(row);
  155. if (this.startRegionRe.test(line))
  156. return this.getCommentRegionBlock(session, line, row);
  157. var match = line.match(this.foldingStartMarker);
  158. if (match) {
  159. var i = match.index;
  160. if (match[1])
  161. return this.openingBracketBlock(session, match[1], row, i);
  162. var range = session.getCommentFoldRange(row, i + match[0].length, 1);
  163. if (range && !range.isMultiLine()) {
  164. if (forceMultiline) {
  165. range = this.getSectionRange(session, row);
  166. } else if (foldStyle != "all")
  167. range = null;
  168. }
  169. return range;
  170. }
  171. if (foldStyle === "markbegin")
  172. return;
  173. var match = line.match(this.foldingStopMarker);
  174. if (match) {
  175. var i = match.index + match[0].length;
  176. if (match[1])
  177. return this.closingBracketBlock(session, match[1], row, i);
  178. return session.getCommentFoldRange(row, i, -1);
  179. }
  180. };
  181. this.getSectionRange = function(session, row) {
  182. var line = session.getLine(row);
  183. var startIndent = line.search(/\S/);
  184. var startRow = row;
  185. var startColumn = line.length;
  186. row = row + 1;
  187. var endRow = row;
  188. var maxRow = session.getLength();
  189. while (++row < maxRow) {
  190. line = session.getLine(row);
  191. var indent = line.search(/\S/);
  192. if (indent === -1)
  193. continue;
  194. if (startIndent > indent)
  195. break;
  196. var subRange = this.getFoldWidgetRange(session, "all", row);
  197. if (subRange) {
  198. if (subRange.start.row <= startRow) {
  199. break;
  200. } else if (subRange.isMultiLine()) {
  201. row = subRange.end.row;
  202. } else if (startIndent == indent) {
  203. break;
  204. }
  205. }
  206. endRow = row;
  207. }
  208. return new Range(startRow, startColumn, endRow, session.getLine(endRow).length);
  209. };
  210. this.getCommentRegionBlock = function(session, line, row) {
  211. var startColumn = line.search(/\s*$/);
  212. var maxRow = session.getLength();
  213. var startRow = row;
  214. var re = /^\s*(?:\/\*|\/\/|--)#?(end)?region\b/;
  215. var depth = 1;
  216. while (++row < maxRow) {
  217. line = session.getLine(row);
  218. var m = re.exec(line);
  219. if (!m) continue;
  220. if (m[1]) depth--;
  221. else depth++;
  222. if (!depth) break;
  223. }
  224. var endRow = row;
  225. if (endRow > startRow) {
  226. return new Range(startRow, startColumn, endRow, line.length);
  227. }
  228. };
  229. }).call(FoldMode.prototype);
  230. });
  231. ace.define("ace/mode/forth",[], function(require, exports, module) {
  232. "use strict";
  233. var oop = require("../lib/oop");
  234. var TextMode = require("./text").Mode;
  235. var ForthHighlightRules = require("./forth_highlight_rules").ForthHighlightRules;
  236. var FoldMode = require("./folding/cstyle").FoldMode;
  237. var Mode = function() {
  238. this.HighlightRules = ForthHighlightRules;
  239. this.foldingRules = new FoldMode();
  240. this.$behaviour = this.$defaultBehaviour;
  241. };
  242. oop.inherits(Mode, TextMode);
  243. (function() {
  244. this.lineCommentStart = "--";
  245. this.blockComment = null;
  246. this.$id = "ace/mode/forth";
  247. }).call(Mode.prototype);
  248. exports.Mode = Mode;
  249. });
  250. (function() {
  251. ace.require(["ace/mode/forth"], function(m) {
  252. if (typeof module == "object" && typeof exports == "object" && module) {
  253. module.exports = m;
  254. }
  255. });
  256. })();