mllike.js
359 lines
| 8.5 KiB
| application/javascript
|
JavascriptLexer
r1 | // CodeMirror, copyright (c) by Marijn Haverbeke and others | |||
r4105 | // Distributed under an MIT license: https://codemirror.net/LICENSE | |||
r1 | ||||
(function(mod) { | ||||
if (typeof exports == "object" && typeof module == "object") // CommonJS | ||||
mod(require("../../lib/codemirror")); | ||||
else if (typeof define == "function" && define.amd) // AMD | ||||
define(["../../lib/codemirror"], mod); | ||||
else // Plain browser env | ||||
mod(CodeMirror); | ||||
})(function(CodeMirror) { | ||||
"use strict"; | ||||
CodeMirror.defineMode('mllike', function(_config, parserConfig) { | ||||
var words = { | ||||
r4105 | 'as': 'keyword', | |||
r1 | 'do': 'keyword', | |||
r4105 | 'else': 'keyword', | |||
'end': 'keyword', | ||||
'exception': 'keyword', | ||||
r1 | 'fun': 'keyword', | |||
r4105 | 'functor': 'keyword', | |||
'if': 'keyword', | ||||
'in': 'keyword', | ||||
'include': 'keyword', | ||||
'let': 'keyword', | ||||
'of': 'keyword', | ||||
'open': 'keyword', | ||||
'rec': 'keyword', | ||||
'struct': 'keyword', | ||||
'then': 'keyword', | ||||
r1 | 'type': 'keyword', | |||
r4105 | 'val': 'keyword', | |||
'while': 'keyword', | ||||
'with': 'keyword' | ||||
r1 | }; | |||
var extraWords = parserConfig.extraWords || {}; | ||||
for (var prop in extraWords) { | ||||
if (extraWords.hasOwnProperty(prop)) { | ||||
words[prop] = parserConfig.extraWords[prop]; | ||||
} | ||||
} | ||||
r4105 | var hintWords = []; | |||
for (var k in words) { hintWords.push(k); } | ||||
CodeMirror.registerHelper("hintWords", "mllike", hintWords); | ||||
r1 | ||||
function tokenBase(stream, state) { | ||||
var ch = stream.next(); | ||||
if (ch === '"') { | ||||
state.tokenize = tokenString; | ||||
return state.tokenize(stream, state); | ||||
} | ||||
r4105 | if (ch === '{') { | |||
if (stream.eat('|')) { | ||||
state.longString = true; | ||||
state.tokenize = tokenLongString; | ||||
return state.tokenize(stream, state); | ||||
} | ||||
} | ||||
r1 | if (ch === '(') { | |||
if (stream.eat('*')) { | ||||
state.commentLevel++; | ||||
state.tokenize = tokenComment; | ||||
return state.tokenize(stream, state); | ||||
} | ||||
} | ||||
r4105 | if (ch === '~' || ch === '?') { | |||
r1 | stream.eatWhile(/\w/); | |||
return 'variable-2'; | ||||
} | ||||
if (ch === '`') { | ||||
stream.eatWhile(/\w/); | ||||
return 'quote'; | ||||
} | ||||
if (ch === '/' && parserConfig.slashComments && stream.eat('/')) { | ||||
stream.skipToEnd(); | ||||
return 'comment'; | ||||
} | ||||
if (/\d/.test(ch)) { | ||||
r4105 | if (ch === '0' && stream.eat(/[bB]/)) { | |||
stream.eatWhile(/[01]/); | ||||
} if (ch === '0' && stream.eat(/[xX]/)) { | ||||
stream.eatWhile(/[0-9a-fA-F]/) | ||||
} if (ch === '0' && stream.eat(/[oO]/)) { | ||||
stream.eatWhile(/[0-7]/); | ||||
} else { | ||||
stream.eatWhile(/[\d_]/); | ||||
if (stream.eat('.')) { | ||||
stream.eatWhile(/[\d]/); | ||||
} | ||||
if (stream.eat(/[eE]/)) { | ||||
stream.eatWhile(/[\d\-+]/); | ||||
} | ||||
r1 | } | |||
return 'number'; | ||||
} | ||||
r4105 | if ( /[+\-*&%=<>!?|@\.~:]/.test(ch)) { | |||
r1 | return 'operator'; | |||
} | ||||
r4105 | if (/[\w\xa1-\uffff]/.test(ch)) { | |||
stream.eatWhile(/[\w\xa1-\uffff]/); | ||||
var cur = stream.current(); | ||||
return words.hasOwnProperty(cur) ? words[cur] : 'variable'; | ||||
} | ||||
return null | ||||
r1 | } | |||
function tokenString(stream, state) { | ||||
var next, end = false, escaped = false; | ||||
while ((next = stream.next()) != null) { | ||||
if (next === '"' && !escaped) { | ||||
end = true; | ||||
break; | ||||
} | ||||
escaped = !escaped && next === '\\'; | ||||
} | ||||
if (end && !escaped) { | ||||
state.tokenize = tokenBase; | ||||
} | ||||
return 'string'; | ||||
}; | ||||
function tokenComment(stream, state) { | ||||
var prev, next; | ||||
while(state.commentLevel > 0 && (next = stream.next()) != null) { | ||||
if (prev === '(' && next === '*') state.commentLevel++; | ||||
if (prev === '*' && next === ')') state.commentLevel--; | ||||
prev = next; | ||||
} | ||||
if (state.commentLevel <= 0) { | ||||
state.tokenize = tokenBase; | ||||
} | ||||
return 'comment'; | ||||
} | ||||
r4105 | function tokenLongString(stream, state) { | |||
var prev, next; | ||||
while (state.longString && (next = stream.next()) != null) { | ||||
if (prev === '|' && next === '}') state.longString = false; | ||||
prev = next; | ||||
} | ||||
if (!state.longString) { | ||||
state.tokenize = tokenBase; | ||||
} | ||||
return 'string'; | ||||
} | ||||
r1 | return { | |||
r4105 | startState: function() {return {tokenize: tokenBase, commentLevel: 0, longString: false};}, | |||
r1 | token: function(stream, state) { | |||
if (stream.eatSpace()) return null; | ||||
return state.tokenize(stream, state); | ||||
}, | ||||
blockCommentStart: "(*", | ||||
blockCommentEnd: "*)", | ||||
lineComment: parserConfig.slashComments ? "//" : null | ||||
}; | ||||
}); | ||||
CodeMirror.defineMIME('text/x-ocaml', { | ||||
name: 'mllike', | ||||
extraWords: { | ||||
r4105 | 'and': 'keyword', | |||
'assert': 'keyword', | ||||
'begin': 'keyword', | ||||
'class': 'keyword', | ||||
'constraint': 'keyword', | ||||
'done': 'keyword', | ||||
'downto': 'keyword', | ||||
'external': 'keyword', | ||||
'function': 'keyword', | ||||
'initializer': 'keyword', | ||||
'lazy': 'keyword', | ||||
'match': 'keyword', | ||||
'method': 'keyword', | ||||
'module': 'keyword', | ||||
'mutable': 'keyword', | ||||
'new': 'keyword', | ||||
'nonrec': 'keyword', | ||||
'object': 'keyword', | ||||
'private': 'keyword', | ||||
'sig': 'keyword', | ||||
'to': 'keyword', | ||||
'try': 'keyword', | ||||
'value': 'keyword', | ||||
'virtual': 'keyword', | ||||
'when': 'keyword', | ||||
// builtins | ||||
'raise': 'builtin', | ||||
'failwith': 'builtin', | ||||
'true': 'builtin', | ||||
'false': 'builtin', | ||||
// Pervasives builtins | ||||
'asr': 'builtin', | ||||
'land': 'builtin', | ||||
'lor': 'builtin', | ||||
'lsl': 'builtin', | ||||
'lsr': 'builtin', | ||||
'lxor': 'builtin', | ||||
'mod': 'builtin', | ||||
'or': 'builtin', | ||||
// More Pervasives | ||||
'raise_notrace': 'builtin', | ||||
r1 | 'trace': 'builtin', | |||
'exit': 'builtin', | ||||
'print_string': 'builtin', | ||||
'print_endline': 'builtin', | ||||
r4105 | ||||
'int': 'type', | ||||
'float': 'type', | ||||
'bool': 'type', | ||||
'char': 'type', | ||||
'string': 'type', | ||||
'unit': 'type', | ||||
// Modules | ||||
'List': 'builtin' | ||||
r1 | } | |||
}); | ||||
CodeMirror.defineMIME('text/x-fsharp', { | ||||
name: 'mllike', | ||||
extraWords: { | ||||
'abstract': 'keyword', | ||||
'assert': 'keyword', | ||||
'base': 'keyword', | ||||
r4105 | 'begin': 'keyword', | |||
r1 | 'class': 'keyword', | |||
'default': 'keyword', | ||||
'delegate': 'keyword', | ||||
r4105 | 'do!': 'keyword', | |||
'done': 'keyword', | ||||
r1 | 'downcast': 'keyword', | |||
'downto': 'keyword', | ||||
'elif': 'keyword', | ||||
'extern': 'keyword', | ||||
'finally': 'keyword', | ||||
r4105 | 'for': 'keyword', | |||
'function': 'keyword', | ||||
r1 | 'global': 'keyword', | |||
'inherit': 'keyword', | ||||
'inline': 'keyword', | ||||
'interface': 'keyword', | ||||
'internal': 'keyword', | ||||
'lazy': 'keyword', | ||||
'let!': 'keyword', | ||||
r4105 | 'match': 'keyword', | |||
'member': 'keyword', | ||||
r1 | 'module': 'keyword', | |||
r4105 | 'mutable': 'keyword', | |||
r1 | 'namespace': 'keyword', | |||
'new': 'keyword', | ||||
'null': 'keyword', | ||||
'override': 'keyword', | ||||
'private': 'keyword', | ||||
'public': 'keyword', | ||||
r4105 | 'return!': 'keyword', | |||
r1 | 'return': 'keyword', | |||
'select': 'keyword', | ||||
'static': 'keyword', | ||||
r4105 | 'to': 'keyword', | |||
'try': 'keyword', | ||||
r1 | 'upcast': 'keyword', | |||
'use!': 'keyword', | ||||
r4105 | 'use': 'keyword', | |||
'void': 'keyword', | ||||
r1 | 'when': 'keyword', | |||
r4105 | 'yield!': 'keyword', | |||
r1 | 'yield': 'keyword', | |||
r4105 | // Reserved words | |||
'atomic': 'keyword', | ||||
'break': 'keyword', | ||||
'checked': 'keyword', | ||||
'component': 'keyword', | ||||
'const': 'keyword', | ||||
'constraint': 'keyword', | ||||
'constructor': 'keyword', | ||||
'continue': 'keyword', | ||||
'eager': 'keyword', | ||||
'event': 'keyword', | ||||
'external': 'keyword', | ||||
'fixed': 'keyword', | ||||
'method': 'keyword', | ||||
'mixin': 'keyword', | ||||
'object': 'keyword', | ||||
'parallel': 'keyword', | ||||
'process': 'keyword', | ||||
'protected': 'keyword', | ||||
'pure': 'keyword', | ||||
'sealed': 'keyword', | ||||
'tailcall': 'keyword', | ||||
'trait': 'keyword', | ||||
'virtual': 'keyword', | ||||
'volatile': 'keyword', | ||||
// builtins | ||||
r1 | 'List': 'builtin', | |||
'Seq': 'builtin', | ||||
'Map': 'builtin', | ||||
'Set': 'builtin', | ||||
r4105 | 'Option': 'builtin', | |||
r1 | 'int': 'builtin', | |||
'string': 'builtin', | ||||
'not': 'builtin', | ||||
'true': 'builtin', | ||||
r4105 | 'false': 'builtin', | |||
'raise': 'builtin', | ||||
'failwith': 'builtin' | ||||
}, | ||||
slashComments: true | ||||
}); | ||||
CodeMirror.defineMIME('text/x-sml', { | ||||
name: 'mllike', | ||||
extraWords: { | ||||
'abstype': 'keyword', | ||||
'and': 'keyword', | ||||
'andalso': 'keyword', | ||||
'case': 'keyword', | ||||
'datatype': 'keyword', | ||||
'fn': 'keyword', | ||||
'handle': 'keyword', | ||||
'infix': 'keyword', | ||||
'infixr': 'keyword', | ||||
'local': 'keyword', | ||||
'nonfix': 'keyword', | ||||
'op': 'keyword', | ||||
'orelse': 'keyword', | ||||
'raise': 'keyword', | ||||
'withtype': 'keyword', | ||||
'eqtype': 'keyword', | ||||
'sharing': 'keyword', | ||||
'sig': 'keyword', | ||||
'signature': 'keyword', | ||||
'structure': 'keyword', | ||||
'where': 'keyword', | ||||
'true': 'keyword', | ||||
'false': 'keyword', | ||||
// types | ||||
'int': 'builtin', | ||||
'real': 'builtin', | ||||
'string': 'builtin', | ||||
'char': 'builtin', | ||||
'bool': 'builtin' | ||||
r1 | }, | |||
slashComments: true | ||||
}); | ||||
}); | ||||