PageRenderTime 56ms CodeModel.GetById 19ms RepoModel.GetById 2ms app.codeStats 0ms

/src/resources-packaged/xbl/orbeon/code-mirror/CodeMirror-2.0/mode/haskell/haskell.js

https://github.com/andreaskc/orbeon-forms
JavaScript | 242 lines | 230 code | 11 blank | 1 comment | 21 complexity | 970a55f083aff98f737f24be7931cdea MD5 | raw file
  1. CodeMirror.defineMode("haskell", function(cmCfg, modeCfg) {
  2. function switchState(source, setState, f) {
  3. setState(f);
  4. return f(source, setState);
  5. }
  6. // These should all be Unicode extended, as per the Haskell 2010 report
  7. var smallRE = /[a-z_]/;
  8. var largeRE = /[A-Z]/;
  9. var digitRE = /[0-9]/;
  10. var hexitRE = /[0-9A-Fa-f]/;
  11. var octitRE = /[0-7]/;
  12. var idRE = /[a-z_A-Z0-9']/;
  13. var symbolRE = /[-!#$%&*+.\/<=>?@\\^|~:]/;
  14. var specialRE = /[(),;[\]`{}]/;
  15. var whiteCharRE = /[ \t\v\f]/; // newlines are handled in tokenizer
  16. function normal(source, setState) {
  17. if (source.eatWhile(whiteCharRE)) {
  18. return null;
  19. }
  20. var ch = source.next();
  21. if (specialRE.test(ch)) {
  22. if (ch == '{' && source.eat('-')) {
  23. var t = "hs-comment";
  24. if (source.eat('#')) {
  25. t = "hs-pragma";
  26. }
  27. return switchState(source, setState, ncomment(t, 1));
  28. }
  29. return "hs-special";
  30. }
  31. if (ch == '\'') {
  32. if (source.eat('\\')) {
  33. source.next(); // should handle other escapes here
  34. }
  35. else {
  36. source.next();
  37. }
  38. if (source.eat('\'')) {
  39. return "hs-char";
  40. }
  41. return "hs-error";
  42. }
  43. if (ch == '"') {
  44. return switchState(source, setState, stringLiteral);
  45. }
  46. if (largeRE.test(ch)) {
  47. source.eatWhile(idRE);
  48. if (source.eat('.')) {
  49. return "hs-qualifier";
  50. }
  51. return "hs-conid";
  52. }
  53. if (smallRE.test(ch)) {
  54. source.eatWhile(idRE);
  55. return "hs-varid";
  56. }
  57. if (digitRE.test(ch)) {
  58. if (ch == '0') {
  59. if (source.eat(/[xX]/)) {
  60. source.eatWhile(hexitRE); // should require at least 1
  61. return "hs-integer";
  62. }
  63. if (source.eat(/[oO]/)) {
  64. source.eatWhile(octitRE); // should require at least 1
  65. return "hs-integer";
  66. }
  67. }
  68. source.eatWhile(digitRE);
  69. var t = "hs-integer";
  70. if (source.eat('.')) {
  71. t = "hs-float";
  72. source.eatWhile(digitRE); // should require at least 1
  73. }
  74. if (source.eat(/[eE]/)) {
  75. t = "hs-float";
  76. source.eat(/[-+]/);
  77. source.eatWhile(digitRE); // should require at least 1
  78. }
  79. return t;
  80. }
  81. if (symbolRE.test(ch)) {
  82. if (ch == '-' && source.eat(/-/)) {
  83. source.eatWhile(/-/);
  84. if (!source.eat(symbolRE)) {
  85. source.skipToEnd();
  86. return "hs-comment";
  87. }
  88. }
  89. var t = "hs-varsym";
  90. if (ch == ':') {
  91. t = "hs-consym";
  92. }
  93. source.eatWhile(symbolRE);
  94. return t;
  95. }
  96. return "hs-error";
  97. }
  98. function ncomment(type, nest) {
  99. if (nest == 0) {
  100. return normal;
  101. }
  102. return function(source, setState) {
  103. var currNest = nest;
  104. while (!source.eol()) {
  105. ch = source.next();
  106. if (ch == '{' && source.eat('-')) {
  107. ++currNest;
  108. }
  109. else if (ch == '-' && source.eat('}')) {
  110. --currNest;
  111. if (currNest == 0) {
  112. setState(normal);
  113. return type;
  114. }
  115. }
  116. }
  117. setState(ncomment(type, currNest));
  118. return type;
  119. }
  120. }
  121. function stringLiteral(source, setState) {
  122. while (!source.eol()) {
  123. var ch = source.next();
  124. if (ch == '"') {
  125. setState(normal);
  126. return "hs-string";
  127. }
  128. if (ch == '\\') {
  129. if (source.eol() || source.eat(whiteCharRE)) {
  130. setState(stringGap);
  131. return "hs-string";
  132. }
  133. if (source.eat('&')) {
  134. }
  135. else {
  136. source.next(); // should handle other escapes here
  137. }
  138. }
  139. }
  140. setState(normal);
  141. return "hs-error";
  142. }
  143. function stringGap(source, setState) {
  144. if (source.eat('\\')) {
  145. return switchState(source, setState, stringLiteral);
  146. }
  147. source.next();
  148. setState(normal);
  149. return "hs-error";
  150. }
  151. var wellKnownWords = (function() {
  152. var wkw = {};
  153. function setType(t) {
  154. return function () {
  155. for (var i = 0; i < arguments.length; i++)
  156. wkw[arguments[i]] = t;
  157. }
  158. }
  159. setType("hs-reservedid")(
  160. "case", "class", "data", "default", "deriving", "do", "else", "foreign",
  161. "if", "import", "in", "infix", "infixl", "infixr", "instance", "let",
  162. "module", "newtype", "of", "then", "type", "where", "_");
  163. setType("hs-reservedop")(
  164. "\.\.", ":", "::", "=", "\\", "\"", "<-", "->", "@", "~", "=>");
  165. setType("hs-prelude-varsym")(
  166. "!!", "$!", "$", "&&", "+", "++", "-", ".", "/", "/=", "<", "<=", "=<<",
  167. "==", ">", ">=", ">>", ">>=", "^", "^^", "||", "*", "**");
  168. setType("hs-prelude-conid")(
  169. "Bool", "Bounded", "Char", "Double", "EQ", "Either", "Enum", "Eq",
  170. "False", "FilePath", "Float", "Floating", "Fractional", "Functor", "GT",
  171. "IO", "IOError", "Int", "Integer", "Integral", "Just", "LT", "Left",
  172. "Maybe", "Monad", "Nothing", "Num", "Ord", "Ordering", "Rational", "Read",
  173. "ReadS", "Real", "RealFloat", "RealFrac", "Right", "Show", "ShowS",
  174. "String", "True");
  175. setType("hs-prelude-varid")(
  176. "abs", "acos", "acosh", "all", "and", "any", "appendFile", "asTypeOf",
  177. "asin", "asinh", "atan", "atan2", "atanh", "break", "catch", "ceiling",
  178. "compare", "concat", "concatMap", "const", "cos", "cosh", "curry",
  179. "cycle", "decodeFloat", "div", "divMod", "drop", "dropWhile", "either",
  180. "elem", "encodeFloat", "enumFrom", "enumFromThen", "enumFromThenTo",
  181. "enumFromTo", "error", "even", "exp", "exponent", "fail", "filter",
  182. "flip", "floatDigits", "floatRadix", "floatRange", "floor", "fmap",
  183. "foldl", "foldl1", "foldr", "foldr1", "fromEnum", "fromInteger",
  184. "fromIntegral", "fromRational", "fst", "gcd", "getChar", "getContents",
  185. "getLine", "head", "id", "init", "interact", "ioError", "isDenormalized",
  186. "isIEEE", "isInfinite", "isNaN", "isNegativeZero", "iterate", "last",
  187. "lcm", "length", "lex", "lines", "log", "logBase", "lookup", "map",
  188. "mapM", "mapM_", "max", "maxBound", "maximum", "maybe", "min", "minBound",
  189. "minimum", "mod", "negate", "not", "notElem", "null", "odd", "or",
  190. "otherwise", "pi", "pred", "print", "product", "properFraction",
  191. "putChar", "putStr", "putStrLn", "quot", "quotRem", "read", "readFile",
  192. "readIO", "readList", "readLn", "readParen", "reads", "readsPrec",
  193. "realToFrac", "recip", "rem", "repeat", "replicate", "return", "reverse",
  194. "round", "scaleFloat", "scanl", "scanl1", "scanr", "scanr1", "seq",
  195. "sequence", "sequence_", "show", "showChar", "showList", "showParen",
  196. "showString", "shows", "showsPrec", "significand", "signum", "sin",
  197. "sinh", "snd", "span", "splitAt", "sqrt", "subtract", "succ", "sum",
  198. "tail", "take", "takeWhile", "tan", "tanh", "toEnum", "toInteger",
  199. "toRational", "truncate", "uncurry", "undefined", "unlines", "until",
  200. "unwords", "unzip", "unzip3", "userError", "words", "writeFile", "zip",
  201. "zip3", "zipWith", "zipWith3");
  202. return wkw;
  203. })();
  204. return {
  205. startState: function () { return { f: normal }; },
  206. copyState: function (s) { return { f: s.f }; },
  207. token: function(stream, state) {
  208. var t = state.f(stream, function(s) { state.f = s; });
  209. var w = stream.current();
  210. return (w in wellKnownWords) ? wellKnownWords[w] : t;
  211. }
  212. };
  213. });
  214. CodeMirror.defineMIME("text/x-haskell", "haskell");