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