: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
// CodeMirror, copyright (c) by Marijn Haverbeke and others
// Distributed under an MIT license: http://codemirror.net/LICENSE
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
})(function(CodeMirror) {
CodeMirror.defineMode("crystal", function(config) {
function wordRegExp(words, end) {
return new RegExp((end ? "" : "^") + "(?:" + words.join("|") + ")" + (end ? "$" : "\\b"));
function chain(tokenize, stream, state) {
state.tokenize.push(tokenize);
return tokenize(stream, state);
var operators = /^(?:[-+/%|&^]|\*\*?|[<>]{2})/;
var conditionalOperators = /^(?:[=!]~|===|<=>|[<>=!]=?|[|&]{2}|~)/;
var indexingOperators = /^(?:\[\][?=]?)/;
var anotherOperators = /^(?:\.(?:\.{2})?|->|[?:])/;
var idents = /^[a-z_\u009F-\uFFFF][a-zA-Z0-9_\u009F-\uFFFF]*/;
var types = /^[A-Z_\u009F-\uFFFF][a-zA-Z0-9_\u009F-\uFFFF]*/;
var keywords = wordRegExp([
"abstract", "alias", "as", "asm", "begin", "break", "case", "class", "def", "do",
"else", "elsif", "end", "ensure", "enum", "extend", "for", "fun", "if", "ifdef",
"include", "instance_sizeof", "lib", "macro", "module", "next", "of", "out", "pointerof",
"private", "protected", "rescue", "return", "require", "sizeof", "struct",
"super", "then", "type", "typeof", "union", "unless", "until", "when", "while", "with",
"yield", "__DIR__", "__FILE__", "__LINE__"
var atomWords = wordRegExp(["true", "false", "nil", "self"]);
var indentKeywordsArray = [
"class", "module", "struct", "lib", "enum", "union",
"if", "unless", "case", "while", "until", "begin", "then",
var indentKeywords = wordRegExp(indentKeywordsArray);
var dedentKeywordsArray = [
var dedentKeywords = wordRegExp(dedentKeywordsArray);
var dedentPunctualsArray = ["\\)", "\\}", "\\]"];
var dedentPunctuals = new RegExp("^(?:" + dedentPunctualsArray.join("|") + ")$");
"def": tokenFollowIdent, "fun": tokenFollowIdent, "macro": tokenMacroDef,
"class": tokenFollowType, "module": tokenFollowType, "struct": tokenFollowType,
"lib": tokenFollowType, "enum": tokenFollowType, "union": tokenFollowType
var matching = {"[": "]", "{": "}", "(": ")", "<": ">"};
function tokenBase(stream, state) {
if (state.lastToken != "\\" && stream.match("{%", false)) {
return chain(tokenMacro("%", "%"), stream, state);
if (state.lastToken != "\\" && stream.match("{{", false)) {
return chain(tokenMacro("{", "}"), stream, state);
if (stream.peek() == "#") {
// Variables and keywords
if (stream.match(idents)) {
matched = stream.current();
} else if (state.lastToken == ".") {
} else if (keywords.test(matched)) {
if (state.lastToken != "abstract" && indentKeywords.test(matched)) {
if (!(matched == "fun" && state.blocks.indexOf("lib") >= 0)) {
state.blocks.push(matched);
state.currentIndent += 1;
} else if (dedentKeywords.test(matched)) {
state.currentIndent -= 1;
if (nextTokenizer.hasOwnProperty(matched)) {
state.tokenize.push(nextTokenizer[matched]);
} else if (atomWords.test(matched)) {
// Class variables and instance variables
if (stream.peek() == "[") {
return chain(tokenNest("[", "]", "meta"), stream, state);
stream.match(idents) || stream.match(types);
stream.eat(/[0-9]+|\?/) || stream.match(idents) || stream.match(types);
if (stream.match(types)) {
// Symbols or ':' operator
return chain(tokenQuote("\"", "atom", false), stream, state);
} else if (stream.match(idents) || stream.match(types) ||
stream.match(operators) || stream.match(conditionalOperators) || stream.match(indexingOperators)) {
return chain(tokenQuote("\"", "string", true), stream, state);
// Strings or regexps or macro variables or '%' operator
if (stream.peek() == "%") {
if (stream.match("%r")) {
} else if (stream.match("%w")) {
if(delim = stream.match(/^%([^\w\s=])/)) {
} else if (stream.match(/^%[a-zA-Z0-9_\u009F-\uFFFF]*/)) {
if (matching.hasOwnProperty(delim)) {
return chain(tokenQuote(delim, style, embed), stream, state);
stream.match(/^(?:[^']|\\(?:[befnrtv0'"]|[0-7]{3}|u(?:[0-9a-fA-F]{4}|\{[0-9a-fA-F]{1,6}\})))/);
stream.match(/^[0-9a-fA-F]+/);
} else if (stream.eat("o")) {
} else if (stream.eat("b")) {
stream.match(/^\d*(?:\.\d+)?(?:[eE][+-]?\d+)?/);
if (stream.match(operators)) {
stream.eat("="); // Operators can follow assign symbol.
if (stream.match(conditionalOperators) || stream.match(anotherOperators)) {
if (matched = stream.match(/[({[]/, false)) {
return chain(tokenNest(matched, matching[matched], null), stream, state);
function tokenNest(begin, end, style, started) {
return function (stream, state) {
if (!started && stream.match(begin)) {
state.tokenize[state.tokenize.length - 1] = tokenNest(begin, end, style, true);
state.currentIndent += 1;
var nextStyle = tokenBase(stream, state);
if (stream.current() === end) {
state.currentIndent -= 1;
function tokenMacro(begin, end, started) {
return function (stream, state) {
if (!started && stream.match("{" + begin)) {
state.currentIndent += 1;
state.tokenize[state.tokenize.length - 1] = tokenMacro(begin, end, true);
if (stream.match(end + "}")) {
state.currentIndent -= 1;
return tokenBase(stream, state);
function tokenMacroDef(stream, state) {
if (matched = stream.match(idents)) {
function tokenFollowIdent(stream, state) {
if (stream.match(idents)) {
stream.match(operators) || stream.match(conditionalOperators) || stream.match(indexingOperators);
function tokenFollowType(stream, state) {
function tokenQuote(end, style, embed) {
return function (stream, state) {
if (stream.match("{%", false)) {
state.tokenize.push(tokenMacro("%", "%"));
if (stream.match("{{", false)) {
state.tokenize.push(tokenMacro("{", "}"));
if (embed && stream.match("#{", false)) {
state.tokenize.push(tokenNest("#{", "}", "meta"));
startState: function () {
token: function (stream, state) {
var style = state.tokenize[state.tokenize.length - 1](stream, state);
var token = stream.current();
if (style && style != "comment") {
indent: function (state, textAfter) {
textAfter = textAfter.replace(/^\s*(?:\{%)?\s*|\s*(?:%\})?\s*$/g, "");
if (dedentKeywords.test(textAfter) || dedentPunctuals.test(textAfter)) {
return config.indentUnit * (state.currentIndent - 1);
return config.indentUnit * state.currentIndent;
electricInput: wordRegExp(dedentPunctualsArray.concat(dedentKeywordsArray), true),
CodeMirror.defineMIME("text/x-crystal", "crystal");