You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

307 lines
8.0 KiB
JavaScript

7 years ago
const CodeMirror = require('codemirror');
7 years ago
CodeMirror.defineMode('kotlin', function(config, parserConfig) {
function words(str) {
var obj = {},
7 years ago
words = str.split(' ');
for (var i = 0; i < words.length; ++i) obj[words[i]] = true;
return obj;
}
var multiLineStrings = parserConfig.multiLineStrings;
var keywords = words(
7 years ago
'package continue return object while break class data trait throw super ' +
'when type this else This try val var fun for is in if do as true false null get set ' +
'import where by get set abstract enum open annotation override private public internal ' +
'protected catch out vararg inline finally final ref const'
);
var blockKeywords = words('catch class do else finally for if where try while enum');
var atoms = words('null true false this');
var builtins = words(
7 years ago
'Int Double Float Long Short Byte IntArray ShortArray ByteArray String Boolean List Set ' +
'Map MutableList MutableSet print println shl shr ushr and or xor inv '
);
var curPunc;
function tokenBase(stream, state) {
var ch = stream.next();
if (ch == '"' || ch == "'") {
return startString(ch, stream, state);
}
// Wildcard import w/o trailing semicolon (import smth.*)
7 years ago
if (ch == '.' && stream.eat('*')) {
return 'word';
}
if (/[\[\]{}\(\),;\:\.]/.test(ch)) {
curPunc = ch;
return null;
}
if (/\d/.test(ch)) {
if (stream.eat(/eE/)) {
stream.eat(/\+\-/);
stream.eatWhile(/\d/);
}
7 years ago
return 'number';
}
7 years ago
if (ch == '/') {
if (stream.eat('*')) {
state.tokenize.push(tokenComment);
return tokenComment(stream, state);
}
7 years ago
if (stream.eat('/')) {
stream.skipToEnd();
7 years ago
return 'comment';
}
if (expectExpression(state.lastToken)) {
return startString(ch, stream, state);
}
}
// Commented
7 years ago
if (ch == '-' && stream.eat('>')) {
curPunc = '->';
return null;
}
if (/[\-+*&%=<>!?|\/~]/.test(ch)) {
stream.eatWhile(/[\-+*&%=<>|~]/);
7 years ago
return 'operator';
}
stream.eatWhile(/[\w\$_]/);
var cur = stream.current();
if (atoms.propertyIsEnumerable(cur)) {
7 years ago
return 'atom';
}
if (keywords.propertyIsEnumerable(cur)) {
7 years ago
if (blockKeywords.propertyIsEnumerable(cur)) curPunc = 'newstatement';
return 'keyword';
}
if (builtins.propertyIsEnumerable(cur)) {
7 years ago
return 'builtin';
}
7 years ago
return 'word';
}
tokenBase.isBase = true;
function startString(quote, stream, state) {
var tripleQuoted = false;
7 years ago
if (quote != '/' && stream.eat(quote)) {
if (stream.eat(quote)) tripleQuoted = true;
7 years ago
else return 'string';
}
function t(stream, state) {
var escaped = false,
7 years ago
next,
end = !tripleQuoted;
while ((next = stream.next()) != null) {
if (next == quote && !escaped) {
if (!tripleQuoted) {
break;
}
if (stream.match(quote + quote)) {
end = true;
break;
}
}
7 years ago
if (quote == '"' && next == '$' && !escaped && stream.eat('{')) {
state.tokenize.push(tokenBaseUntilBrace());
7 years ago
return 'string';
}
7 years ago
if (next == '$' && !escaped && !stream.eat(' ')) {
state.tokenize.push(tokenBaseUntilSpace());
7 years ago
return 'string';
}
7 years ago
escaped = !escaped && next == '\\';
}
7 years ago
if (multiLineStrings) state.tokenize.push(t);
if (end) state.tokenize.pop();
7 years ago
return 'string';
}
state.tokenize.push(t);
return t(stream, state);
}
function tokenBaseUntilBrace() {
var depth = 1;
function t(stream, state) {
7 years ago
if (stream.peek() == '}') {
depth--;
if (depth == 0) {
state.tokenize.pop();
return state.tokenize[state.tokenize.length - 1](stream, state);
}
7 years ago
} else if (stream.peek() == '{') {
depth++;
}
return tokenBase(stream, state);
}
t.isBase = true;
return t;
}
function tokenBaseUntilSpace() {
function t(stream, state) {
if (stream.eat(/[\w]/)) {
var isWord = stream.eatWhile(/[\w]/);
if (isWord) {
state.tokenize.pop();
7 years ago
return 'word';
}
}
state.tokenize.pop();
7 years ago
return 'string';
}
t.isBase = true;
return t;
}
function tokenComment(stream, state) {
var maybeEnd = false,
ch;
7 years ago
while ((ch = stream.next())) {
if (ch == '/' && maybeEnd) {
state.tokenize.pop();
break;
}
7 years ago
maybeEnd = ch == '*';
}
7 years ago
return 'comment';
}
function expectExpression(last) {
7 years ago
return (
!last ||
last == 'operator' ||
last == '->' ||
/[\.\[\{\(,;:]/.test(last) ||
last == 'newstatement' ||
last == 'keyword' ||
last == 'proplabel'
);
}
function Context(indented, column, type, align, prev) {
this.indented = indented;
this.column = column;
this.type = type;
this.align = align;
this.prev = prev;
}
function pushContext(state, col, type) {
7 years ago
return (state.context = new Context(state.indented, col, type, null, state.context));
}
function popContext(state) {
var t = state.context.type;
7 years ago
if (t == ')' || t == ']' || t == '}') state.indented = state.context.indented;
return (state.context = state.context.prev);
}
// Interface
return {
startState: function(basecolumn) {
return {
tokenize: [tokenBase],
7 years ago
context: new Context((basecolumn || 0) - config.indentUnit, 0, 'top', false),
indented: 0,
startOfLine: true,
lastToken: null
};
},
token: function(stream, state) {
var ctx = state.context;
if (stream.sol()) {
if (ctx.align == null) ctx.align = false;
state.indented = stream.indentation();
state.startOfLine = true;
// Automatic semicolon insertion
7 years ago
if (ctx.type == 'statement' && !expectExpression(state.lastToken)) {
popContext(state);
ctx = state.context;
}
}
if (stream.eatSpace()) return null;
curPunc = null;
var style = state.tokenize[state.tokenize.length - 1](stream, state);
7 years ago
if (style == 'comment') return style;
if (ctx.align == null) ctx.align = true;
7 years ago
if ((curPunc == ';' || curPunc == ':') && ctx.type == 'statement') popContext(state);
else if (curPunc == '->' && ctx.type == 'statement' && ctx.prev.type == '}') {
// Handle indentation for {x -> \n ... }
popContext(state);
state.context.align = false;
7 years ago
} else if (curPunc == '{') pushContext(state, stream.column(), '}');
else if (curPunc == '[') pushContext(state, stream.column(), ']');
else if (curPunc == '(') pushContext(state, stream.column(), ')');
else if (curPunc == '}') {
while (ctx.type == 'statement') ctx = popContext(state);
if (ctx.type == '}') ctx = popContext(state);
while (ctx.type == 'statement') ctx = popContext(state);
} else if (curPunc == ctx.type) popContext(state);
7 years ago
else if (
ctx.type == '}' ||
ctx.type == 'top' ||
(ctx.type == 'statement' && curPunc == 'newstatement')
)
pushContext(state, stream.column(), 'statement');
state.startOfLine = false;
state.lastToken = curPunc || style;
return style;
},
indent: function(state, textAfter) {
if (!state.tokenize[state.tokenize.length - 1].isBase) return 0;
var firstChar = textAfter && textAfter.charAt(0),
ctx = state.context;
7 years ago
if (ctx.type == 'statement' && !expectExpression(state.lastToken)) ctx = ctx.prev;
var closing = firstChar == ctx.type;
7 years ago
if (ctx.type == 'statement') {
return ctx.indented + (firstChar == '{' ? 0 : config.indentUnit);
} else if (ctx.align) return ctx.column + (closing ? 0 : 1);
else return ctx.indented + (closing ? 0 : config.indentUnit);
},
closeBrackets: {
7 years ago
triples: '\'"'
},
7 years ago
electricChars: '{}'
};
});
7 years ago
CodeMirror.defineMIME('text/x-kotlin', 'kotlin');