mode-haskell.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. ace.define("ace/mode/haskell_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 HaskellHighlightRules = function() {
  6. this.$rules = { start:
  7. [ { token:
  8. [ 'punctuation.definition.entity.haskell',
  9. 'keyword.operator.function.infix.haskell',
  10. 'punctuation.definition.entity.haskell' ],
  11. regex: '(`)([a-zA-Z_\']*?)(`)',
  12. comment: 'In case this regex seems unusual for an infix operator, note that Haskell allows any ordinary function application (elem 4 [1..10]) to be rewritten as an infix expression (4 `elem` [1..10]).' },
  13. { token: 'constant.language.unit.haskell', regex: '\\(\\)' },
  14. { token: 'constant.language.empty-list.haskell',
  15. regex: '\\[\\]' },
  16. { token: 'keyword.other.haskell',
  17. regex: '\\bmodule\\b',
  18. push:
  19. [ { token: 'keyword.other.haskell', regex: '\\bwhere\\b', next: 'pop' },
  20. { include: '#module_name' },
  21. { include: '#module_exports' },
  22. { token: 'invalid', regex: '[a-z]+' },
  23. { defaultToken: 'meta.declaration.module.haskell' } ] },
  24. { token: 'keyword.other.haskell',
  25. regex: '\\bclass\\b',
  26. push:
  27. [ { token: 'keyword.other.haskell',
  28. regex: '\\bwhere\\b',
  29. next: 'pop' },
  30. { token: 'support.class.prelude.haskell',
  31. regex: '\\b(?:Monad|Functor|Eq|Ord|Read|Show|Num|(?:Frac|Ra)tional|Enum|Bounded|Real(?:Frac|Float)?|Integral|Floating)\\b' },
  32. { token: 'entity.other.inherited-class.haskell',
  33. regex: '[A-Z][A-Za-z_\']*' },
  34. { token: 'variable.other.generic-type.haskell',
  35. regex: '\\b[a-z][a-zA-Z0-9_\']*\\b' },
  36. { defaultToken: 'meta.declaration.class.haskell' } ] },
  37. { token: 'keyword.other.haskell',
  38. regex: '\\binstance\\b',
  39. push:
  40. [ { token: 'keyword.other.haskell',
  41. regex: '\\bwhere\\b|$',
  42. next: 'pop' },
  43. { include: '#type_signature' },
  44. { defaultToken: 'meta.declaration.instance.haskell' } ] },
  45. { token: 'keyword.other.haskell',
  46. regex: 'import',
  47. push:
  48. [ { token: 'meta.import.haskell', regex: '$|;|^', next: 'pop' },
  49. { token: 'keyword.other.haskell', regex: 'qualified|as|hiding' },
  50. { include: '#module_name' },
  51. { include: '#module_exports' },
  52. { defaultToken: 'meta.import.haskell' } ] },
  53. { token: [ 'keyword.other.haskell', 'meta.deriving.haskell' ],
  54. regex: '(deriving)(\\s*\\()',
  55. push:
  56. [ { token: 'meta.deriving.haskell', regex: '\\)', next: 'pop' },
  57. { token: 'entity.other.inherited-class.haskell',
  58. regex: '\\b[A-Z][a-zA-Z_\']*' },
  59. { defaultToken: 'meta.deriving.haskell' } ] },
  60. { token: 'keyword.other.haskell',
  61. regex: '\\b(?:deriving|where|data|type|case|of|let|in|newtype|default)\\b' },
  62. { token: 'keyword.operator.haskell', regex: '\\binfix[lr]?\\b' },
  63. { token: 'keyword.control.haskell',
  64. regex: '\\b(?:do|if|then|else)\\b' },
  65. { token: 'constant.numeric.float.haskell',
  66. regex: '\\b(?:[0-9]+\\.[0-9]+(?:[eE][+-]?[0-9]+)?|[0-9]+[eE][+-]?[0-9]+)\\b',
  67. comment: 'Floats are always decimal' },
  68. { token: 'constant.numeric.haskell',
  69. regex: '\\b(?:[0-9]+|0(?:[xX][0-9a-fA-F]+|[oO][0-7]+))\\b' },
  70. { token:
  71. [ 'meta.preprocessor.c',
  72. 'punctuation.definition.preprocessor.c',
  73. 'meta.preprocessor.c' ],
  74. regex: '^(\\s*)(#)(\\s*\\w+)',
  75. comment: 'In addition to Haskell\'s "native" syntax, GHC permits the C preprocessor to be run on a source file.' },
  76. { include: '#pragma' },
  77. { token: 'punctuation.definition.string.begin.haskell',
  78. regex: '"',
  79. push:
  80. [ { token: 'punctuation.definition.string.end.haskell',
  81. regex: '"',
  82. next: 'pop' },
  83. { token: 'constant.character.escape.haskell',
  84. regex: '\\\\(?:NUL|SOH|STX|ETX|EOT|ENQ|ACK|BEL|BS|HT|LF|VT|FF|CR|SO|SI|DLE|DC1|DC2|DC3|DC4|NAK|SYN|ETB|CAN|EM|SUB|ESC|FS|GS|RS|US|SP|DEL|[abfnrtv\\\\\\"\'\\&])' },
  85. { token: 'constant.character.escape.octal.haskell',
  86. regex: '\\\\o[0-7]+|\\\\x[0-9A-Fa-f]+|\\\\[0-9]+' },
  87. { token: 'constant.character.escape.control.haskell',
  88. regex: '\\^[A-Z@\\[\\]\\\\\\^_]' },
  89. { defaultToken: 'string.quoted.double.haskell' } ] },
  90. { token:
  91. [ 'punctuation.definition.string.begin.haskell',
  92. 'string.quoted.single.haskell',
  93. 'constant.character.escape.haskell',
  94. 'constant.character.escape.octal.haskell',
  95. 'constant.character.escape.hexadecimal.haskell',
  96. 'constant.character.escape.control.haskell',
  97. 'punctuation.definition.string.end.haskell' ],
  98. regex: '(\')(?:([\\ -\\[\\]-~])|(\\\\(?:NUL|SOH|STX|ETX|EOT|ENQ|ACK|BEL|BS|HT|LF|VT|FF|CR|SO|SI|DLE|DC1|DC2|DC3|DC4|NAK|SYN|ETB|CAN|EM|SUB|ESC|FS|GS|RS|US|SP|DEL|[abfnrtv\\\\\\"\'\\&]))|(\\\\o[0-7]+)|(\\\\x[0-9A-Fa-f]+)|(\\^[A-Z@\\[\\]\\\\\\^_]))(\')' },
  99. { token:
  100. [ 'meta.function.type-declaration.haskell',
  101. 'entity.name.function.haskell',
  102. 'meta.function.type-declaration.haskell',
  103. 'keyword.other.double-colon.haskell' ],
  104. regex: '^(\\s*)([a-z_][a-zA-Z0-9_\']*|\\([|!%$+\\-.,=</>]+\\))(\\s*)(::)',
  105. push:
  106. [ { token: 'meta.function.type-declaration.haskell',
  107. regex: '$',
  108. next: 'pop' },
  109. { include: '#type_signature' },
  110. { defaultToken: 'meta.function.type-declaration.haskell' } ] },
  111. { token: 'support.constant.haskell',
  112. regex: '\\b(?:Just|Nothing|Left|Right|True|False|LT|EQ|GT|\\(\\)|\\[\\])\\b' },
  113. { token: 'constant.other.haskell', regex: '\\b[A-Z]\\w*\\b' },
  114. { include: '#comments' },
  115. { token: 'support.function.prelude.haskell',
  116. regex: '\\b(?:abs|acos|acosh|all|and|any|appendFile|applyM|asTypeOf|asin|asinh|atan|atan2|atanh|break|catch|ceiling|compare|concat|concatMap|const|cos|cosh|curry|cycle|decodeFloat|div|divMod|drop|dropWhile|elem|encodeFloat|enumFrom|enumFromThen|enumFromThenTo|enumFromTo|error|even|exp|exponent|fail|filter|flip|floatDigits|floatRadix|floatRange|floor|fmap|foldl|foldl1|foldr|foldr1|fromEnum|fromInteger|fromIntegral|fromRational|fst|gcd|getChar|getContents|getLine|head|id|init|interact|ioError|isDenormalized|isIEEE|isInfinite|isNaN|isNegativeZero|iterate|last|lcm|length|lex|lines|log|logBase|lookup|map|mapM|mapM_|max|maxBound|maximum|maybe|min|minBound|minimum|mod|negate|not|notElem|null|odd|or|otherwise|pi|pred|print|product|properFraction|putChar|putStr|putStrLn|quot|quotRem|read|readFile|readIO|readList|readLn|readParen|reads|readsPrec|realToFrac|recip|rem|repeat|replicate|return|reverse|round|scaleFloat|scanl|scanl1|scanr|scanr1|seq|sequence|sequence_|show|showChar|showList|showParen|showString|shows|showsPrec|significand|signum|sin|sinh|snd|span|splitAt|sqrt|subtract|succ|sum|tail|take|takeWhile|tan|tanh|toEnum|toInteger|toRational|truncate|uncurry|undefined|unlines|until|unwords|unzip|unzip3|userError|words|writeFile|zip|zip3|zipWith|zipWith3)\\b' },
  117. { include: '#infix_op' },
  118. { token: 'keyword.operator.haskell',
  119. regex: '[|!%$?~+:\\-.=</>\\\\]+',
  120. comment: 'In case this regex seems overly general, note that Haskell permits the definition of new operators which can be nearly any string of punctuation characters, such as $%^&*.' },
  121. { token: 'punctuation.separator.comma.haskell', regex: ',' } ],
  122. '#block_comment':
  123. [ { token: 'punctuation.definition.comment.haskell',
  124. regex: '\\{-(?!#)',
  125. push:
  126. [ { include: '#block_comment' },
  127. { token: 'punctuation.definition.comment.haskell',
  128. regex: '-\\}',
  129. next: 'pop' },
  130. { defaultToken: 'comment.block.haskell' } ] } ],
  131. '#comments':
  132. [ { token: 'punctuation.definition.comment.haskell',
  133. regex: '--.*',
  134. push_:
  135. [ { token: 'comment.line.double-dash.haskell',
  136. regex: '$',
  137. next: 'pop' },
  138. { defaultToken: 'comment.line.double-dash.haskell' } ] },
  139. { include: '#block_comment' } ],
  140. '#infix_op':
  141. [ { token: 'entity.name.function.infix.haskell',
  142. regex: '\\([|!%$+:\\-.=</>]+\\)|\\(,+\\)' } ],
  143. '#module_exports':
  144. [ { token: 'meta.declaration.exports.haskell',
  145. regex: '\\(',
  146. push:
  147. [ { token: 'meta.declaration.exports.haskell.end',
  148. regex: '\\)',
  149. next: 'pop' },
  150. { token: 'entity.name.function.haskell',
  151. regex: '\\b[a-z][a-zA-Z_\']*' },
  152. { token: 'storage.type.haskell', regex: '\\b[A-Z][A-Za-z_\']*' },
  153. { token: 'punctuation.separator.comma.haskell', regex: ',' },
  154. { include: '#infix_op' },
  155. { token: 'meta.other.unknown.haskell',
  156. regex: '\\(.*?\\)',
  157. comment: 'So named because I don\'t know what to call this.' },
  158. { defaultToken: 'meta.declaration.exports.haskell.end' } ] } ],
  159. '#module_name':
  160. [ { token: 'support.other.module.haskell',
  161. regex: '[A-Z][A-Za-z._\']*' } ],
  162. '#pragma':
  163. [ { token: 'meta.preprocessor.haskell',
  164. regex: '\\{-#',
  165. push:
  166. [ { token: 'meta.preprocessor.haskell',
  167. regex: '#-\\}',
  168. next: 'pop' },
  169. { token: 'keyword.other.preprocessor.haskell',
  170. regex: '\\b(?:LANGUAGE|UNPACK|INLINE)\\b' },
  171. { defaultToken: 'meta.preprocessor.haskell' } ] } ],
  172. '#type_signature':
  173. [ { token:
  174. [ 'meta.class-constraint.haskell',
  175. 'entity.other.inherited-class.haskell',
  176. 'meta.class-constraint.haskell',
  177. 'variable.other.generic-type.haskell',
  178. 'meta.class-constraint.haskell',
  179. 'keyword.other.big-arrow.haskell' ],
  180. regex: '(\\(\\s*)([A-Z][A-Za-z]*)(\\s+)([a-z][A-Za-z_\']*)(\\)\\s*)(=>)' },
  181. { include: '#pragma' },
  182. { token: 'keyword.other.arrow.haskell', regex: '->' },
  183. { token: 'keyword.other.big-arrow.haskell', regex: '=>' },
  184. { token: 'support.type.prelude.haskell',
  185. regex: '\\b(?:Int(?:eger)?|Maybe|Either|Bool|Float|Double|Char|String|Ordering|ShowS|ReadS|FilePath|IO(?:Error)?)\\b' },
  186. { token: 'variable.other.generic-type.haskell',
  187. regex: '\\b[a-z][a-zA-Z0-9_\']*\\b' },
  188. { token: 'storage.type.haskell',
  189. regex: '\\b[A-Z][a-zA-Z0-9_\']*\\b' },
  190. { token: 'support.constant.unit.haskell', regex: '\\(\\)' },
  191. { include: '#comments' } ] }
  192. this.normalizeRules();
  193. };
  194. HaskellHighlightRules.metaData = { fileTypes: [ 'hs' ],
  195. keyEquivalent: '^~H',
  196. name: 'Haskell',
  197. scopeName: 'source.haskell' }
  198. oop.inherits(HaskellHighlightRules, TextHighlightRules);
  199. exports.HaskellHighlightRules = HaskellHighlightRules;
  200. });
  201. ace.define("ace/mode/folding/cstyle",["require","exports","module","ace/lib/oop","ace/range","ace/mode/folding/fold_mode"], function(require, exports, module) {
  202. "use strict";
  203. var oop = require("../../lib/oop");
  204. var Range = require("../../range").Range;
  205. var BaseFoldMode = require("./fold_mode").FoldMode;
  206. var FoldMode = exports.FoldMode = function(commentRegex) {
  207. if (commentRegex) {
  208. this.foldingStartMarker = new RegExp(
  209. this.foldingStartMarker.source.replace(/\|[^|]*?$/, "|" + commentRegex.start)
  210. );
  211. this.foldingStopMarker = new RegExp(
  212. this.foldingStopMarker.source.replace(/\|[^|]*?$/, "|" + commentRegex.end)
  213. );
  214. }
  215. };
  216. oop.inherits(FoldMode, BaseFoldMode);
  217. (function() {
  218. this.foldingStartMarker = /(\{|\[)[^\}\]]*$|^\s*(\/\*)/;
  219. this.foldingStopMarker = /^[^\[\{]*(\}|\])|^[\s\*]*(\*\/)/;
  220. this.singleLineBlockCommentRe= /^\s*(\/\*).*\*\/\s*$/;
  221. this.tripleStarBlockCommentRe = /^\s*(\/\*\*\*).*\*\/\s*$/;
  222. this.startRegionRe = /^\s*(\/\*|\/\/)#?region\b/;
  223. this._getFoldWidgetBase = this.getFoldWidget;
  224. this.getFoldWidget = function(session, foldStyle, row) {
  225. var line = session.getLine(row);
  226. if (this.singleLineBlockCommentRe.test(line)) {
  227. if (!this.startRegionRe.test(line) && !this.tripleStarBlockCommentRe.test(line))
  228. return "";
  229. }
  230. var fw = this._getFoldWidgetBase(session, foldStyle, row);
  231. if (!fw && this.startRegionRe.test(line))
  232. return "start"; // lineCommentRegionStart
  233. return fw;
  234. };
  235. this.getFoldWidgetRange = function(session, foldStyle, row, forceMultiline) {
  236. var line = session.getLine(row);
  237. if (this.startRegionRe.test(line))
  238. return this.getCommentRegionBlock(session, line, row);
  239. var match = line.match(this.foldingStartMarker);
  240. if (match) {
  241. var i = match.index;
  242. if (match[1])
  243. return this.openingBracketBlock(session, match[1], row, i);
  244. var range = session.getCommentFoldRange(row, i + match[0].length, 1);
  245. if (range && !range.isMultiLine()) {
  246. if (forceMultiline) {
  247. range = this.getSectionRange(session, row);
  248. } else if (foldStyle != "all")
  249. range = null;
  250. }
  251. return range;
  252. }
  253. if (foldStyle === "markbegin")
  254. return;
  255. var match = line.match(this.foldingStopMarker);
  256. if (match) {
  257. var i = match.index + match[0].length;
  258. if (match[1])
  259. return this.closingBracketBlock(session, match[1], row, i);
  260. return session.getCommentFoldRange(row, i, -1);
  261. }
  262. };
  263. this.getSectionRange = function(session, row) {
  264. var line = session.getLine(row);
  265. var startIndent = line.search(/\S/);
  266. var startRow = row;
  267. var startColumn = line.length;
  268. row = row + 1;
  269. var endRow = row;
  270. var maxRow = session.getLength();
  271. while (++row < maxRow) {
  272. line = session.getLine(row);
  273. var indent = line.search(/\S/);
  274. if (indent === -1)
  275. continue;
  276. if (startIndent > indent)
  277. break;
  278. var subRange = this.getFoldWidgetRange(session, "all", row);
  279. if (subRange) {
  280. if (subRange.start.row <= startRow) {
  281. break;
  282. } else if (subRange.isMultiLine()) {
  283. row = subRange.end.row;
  284. } else if (startIndent == indent) {
  285. break;
  286. }
  287. }
  288. endRow = row;
  289. }
  290. return new Range(startRow, startColumn, endRow, session.getLine(endRow).length);
  291. };
  292. this.getCommentRegionBlock = function(session, line, row) {
  293. var startColumn = line.search(/\s*$/);
  294. var maxRow = session.getLength();
  295. var startRow = row;
  296. var re = /^\s*(?:\/\*|\/\/|--)#?(end)?region\b/;
  297. var depth = 1;
  298. while (++row < maxRow) {
  299. line = session.getLine(row);
  300. var m = re.exec(line);
  301. if (!m) continue;
  302. if (m[1]) depth--;
  303. else depth++;
  304. if (!depth) break;
  305. }
  306. var endRow = row;
  307. if (endRow > startRow) {
  308. return new Range(startRow, startColumn, endRow, line.length);
  309. }
  310. };
  311. }).call(FoldMode.prototype);
  312. });
  313. ace.define("ace/mode/haskell",["require","exports","module","ace/lib/oop","ace/mode/text","ace/mode/haskell_highlight_rules","ace/mode/folding/cstyle"], function(require, exports, module) {
  314. "use strict";
  315. var oop = require("../lib/oop");
  316. var TextMode = require("./text").Mode;
  317. var HaskellHighlightRules = require("./haskell_highlight_rules").HaskellHighlightRules;
  318. var FoldMode = require("./folding/cstyle").FoldMode;
  319. var Mode = function() {
  320. this.HighlightRules = HaskellHighlightRules;
  321. this.foldingRules = new FoldMode();
  322. this.$behaviour = this.$defaultBehaviour;
  323. };
  324. oop.inherits(Mode, TextMode);
  325. (function() {
  326. this.lineCommentStart = "--";
  327. this.blockComment = null;
  328. this.$id = "ace/mode/haskell";
  329. }).call(Mode.prototype);
  330. exports.Mode = Mode;
  331. });