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