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