5
if (typeof exports == "object" && typeof module == "object")
6
mod(require("../../lib/codemirror"));
7
else if (typeof define == "function" && define.amd)
8
define(["../../lib/codemirror"], mod);
11
})(function(CodeMirror) {
14
CodeMirror.defineMode("crystal", function(config) {
15
function wordRegExp(words, end) {
16
return new RegExp((end ? "" : "^") + "(?:" + words.join("|") + ")" + (end ? "$" : "\\b"));
19
function chain(tokenize, stream, state) {
20
state.tokenize.push(tokenize);
21
return tokenize(stream, state);
24
var operators = /^(?:[-+/%|&^]|\*\*?|[<>]{2})/;
25
var conditionalOperators = /^(?:[=!]~|===|<=>|[<>=!]=?|[|&]{2}|~)/;
26
var indexingOperators = /^(?:\[\][?=]?)/;
27
var anotherOperators = /^(?:\.(?:\.{2})?|->|[?:])/;
28
var idents = /^[a-z_\u009F-\uFFFF][a-zA-Z0-9_\u009F-\uFFFF]*/;
29
var types = /^[A-Z_\u009F-\uFFFF][a-zA-Z0-9_\u009F-\uFFFF]*/;
30
var keywords = wordRegExp([
31
"abstract", "alias", "as", "asm", "begin", "break", "case", "class", "def", "do",
32
"else", "elsif", "end", "ensure", "enum", "extend", "for", "fun", "if",
33
"include", "instance_sizeof", "lib", "macro", "module", "next", "of", "out", "pointerof",
34
"private", "protected", "rescue", "return", "require", "select", "sizeof", "struct",
35
"super", "then", "type", "typeof", "uninitialized", "union", "unless", "until", "when", "while", "with",
36
"yield", "__DIR__", "__END_LINE__", "__FILE__", "__LINE__"
38
var atomWords = wordRegExp(["true", "false", "nil", "self"]);
39
var indentKeywordsArray = [
40
"def", "fun", "macro",
41
"class", "module", "struct", "lib", "enum", "union",
44
var indentKeywords = wordRegExp(indentKeywordsArray);
45
var indentExpressionKeywordsArray = ["if", "unless", "case", "while", "until", "begin", "then"];
46
var indentExpressionKeywords = wordRegExp(indentExpressionKeywordsArray);
47
var dedentKeywordsArray = ["end", "else", "elsif", "rescue", "ensure"];
48
var dedentKeywords = wordRegExp(dedentKeywordsArray);
49
var dedentPunctualsArray = ["\\)", "\\}", "\\]"];
50
var dedentPunctuals = new RegExp("^(?:" + dedentPunctualsArray.join("|") + ")$");
52
"def": tokenFollowIdent, "fun": tokenFollowIdent, "macro": tokenMacroDef,
53
"class": tokenFollowType, "module": tokenFollowType, "struct": tokenFollowType,
54
"lib": tokenFollowType, "enum": tokenFollowType, "union": tokenFollowType
56
var matching = {"[": "]", "{": "}", "(": ")", "<": ">"};
58
function tokenBase(stream, state) {
59
if (stream.eatSpace()) {
64
if (state.lastToken != "\\" && stream.match("{%", false)) {
65
return chain(tokenMacro("%", "%"), stream, state);
68
if (state.lastToken != "\\" && stream.match("{{", false)) {
69
return chain(tokenMacro("{", "}"), stream, state);
73
if (stream.peek() == "#") {
80
if (stream.match(idents)) {
83
matched = stream.current();
84
if (stream.eat(":")) {
86
} else if (state.lastToken == ".") {
88
} else if (keywords.test(matched)) {
89
if (indentKeywords.test(matched)) {
90
if (!(matched == "fun" && state.blocks.indexOf("lib") >= 0) && !(matched == "def" && state.lastToken == "abstract")) {
91
state.blocks.push(matched);
92
state.currentIndent += 1;
94
} else if ((state.lastStyle == "operator" || !state.lastStyle) && indentExpressionKeywords.test(matched)) {
95
state.blocks.push(matched);
96
state.currentIndent += 1;
97
} else if (matched == "end") {
99
state.currentIndent -= 1;
102
if (nextTokenizer.hasOwnProperty(matched)) {
103
state.tokenize.push(nextTokenizer[matched]);
107
} else if (atomWords.test(matched)) {
116
if (stream.eat("@")) {
117
if (stream.peek() == "[") {
118
return chain(tokenNest("[", "]", "meta"), stream, state);
122
stream.match(idents) || stream.match(types);
127
if (stream.match(types)) {
132
if (stream.eat(":")) {
133
if (stream.eat("\"")) {
134
return chain(tokenQuote("\"", "atom", false), stream, state);
135
} else if (stream.match(idents) || stream.match(types) ||
136
stream.match(operators) || stream.match(conditionalOperators) || stream.match(indexingOperators)) {
144
if (stream.eat("\"")) {
145
return chain(tokenQuote("\"", "string", true), stream, state);
149
if (stream.peek() == "%") {
150
var style = "string";
154
if (stream.match("%r")) {
157
delim = stream.next();
158
} else if (stream.match("%w")) {
160
delim = stream.next();
161
} else if (stream.match("%q")) {
163
delim = stream.next();
165
if(delim = stream.match(/^%([^\w\s=])/)) {
167
} else if (stream.match(/^%[a-zA-Z_\u009F-\uFFFF][\w\u009F-\uFFFF]*/)) {
170
} else if (stream.eat('%')) {
176
if (matching.hasOwnProperty(delim)) {
177
delim = matching[delim];
179
return chain(tokenQuote(delim, style, embed), stream, state);
183
if (matched = stream.match(/^<<-('?)([A-Z]\w*)\1/)) {
184
return chain(tokenHereDoc(matched[2], !matched[1]), stream, state)
188
if (stream.eat("'")) {
189
stream.match(/^(?:[^']|\\(?:[befnrtv0'"]|[0-7]{3}|u(?:[0-9a-fA-F]{4}|\{[0-9a-fA-F]{1,6}\})))/);
195
if (stream.eat("0")) {
196
if (stream.eat("x")) {
197
stream.match(/^[0-9a-fA-F_]+/);
198
} else if (stream.eat("o")) {
199
stream.match(/^[0-7_]+/);
200
} else if (stream.eat("b")) {
201
stream.match(/^[01_]+/);
206
if (stream.eat(/^\d/)) {
207
stream.match(/^[\d_]*(?:\.[\d_]+)?(?:[eE][+-]?\d+)?/);
212
if (stream.match(operators)) {
217
if (stream.match(conditionalOperators) || stream.match(anotherOperators)) {
222
if (matched = stream.match(/[({[]/, false)) {
223
matched = matched[0];
224
return chain(tokenNest(matched, matching[matched], null), stream, state);
228
if (stream.eat("\\")) {
237
function tokenNest(begin, end, style, started) {
238
return function (stream, state) {
239
if (!started && stream.match(begin)) {
240
state.tokenize[state.tokenize.length - 1] = tokenNest(begin, end, style, true);
241
state.currentIndent += 1;
245
var nextStyle = tokenBase(stream, state);
246
if (stream.current() === end) {
247
state.tokenize.pop();
248
state.currentIndent -= 1;
256
function tokenMacro(begin, end, started) {
257
return function (stream, state) {
258
if (!started && stream.match("{" + begin)) {
259
state.currentIndent += 1;
260
state.tokenize[state.tokenize.length - 1] = tokenMacro(begin, end, true);
264
if (stream.match(end + "}")) {
265
state.currentIndent -= 1;
266
state.tokenize.pop();
270
return tokenBase(stream, state);
274
function tokenMacroDef(stream, state) {
275
if (stream.eatSpace()) {
280
if (matched = stream.match(idents)) {
281
if (matched == "def") {
287
state.tokenize.pop();
291
function tokenFollowIdent(stream, state) {
292
if (stream.eatSpace()) {
296
if (stream.match(idents)) {
299
stream.match(operators) || stream.match(conditionalOperators) || stream.match(indexingOperators);
301
state.tokenize.pop();
305
function tokenFollowType(stream, state) {
306
if (stream.eatSpace()) {
311
state.tokenize.pop();
315
function tokenQuote(end, style, embed) {
316
return function (stream, state) {
319
while (stream.peek()) {
321
if (stream.match("{%", false)) {
322
state.tokenize.push(tokenMacro("%", "%"));
326
if (stream.match("{{", false)) {
327
state.tokenize.push(tokenMacro("{", "}"));
331
if (embed && stream.match("#{", false)) {
332
state.tokenize.push(tokenNest("#{", "}", "meta"));
336
var ch = stream.next();
339
state.tokenize.pop();
343
escaped = embed && ch == "\\";
354
function tokenHereDoc(phrase, embed) {
355
return function (stream, state) {
358
if (stream.match(phrase)) {
359
state.tokenize.pop();
365
while (stream.peek()) {
367
if (stream.match("{%", false)) {
368
state.tokenize.push(tokenMacro("%", "%"));
372
if (stream.match("{{", false)) {
373
state.tokenize.push(tokenMacro("{", "}"));
377
if (embed && stream.match("#{", false)) {
378
state.tokenize.push(tokenNest("#{", "}", "meta"));
382
escaped = embed && stream.next() == "\\";
394
startState: function () {
396
tokenize: [tokenBase],
404
token: function (stream, state) {
405
var style = state.tokenize[state.tokenize.length - 1](stream, state);
406
var token = stream.current();
408
if (style && style != "comment") {
409
state.lastToken = token;
410
state.lastStyle = style;
416
indent: function (state, textAfter) {
417
textAfter = textAfter.replace(/^\s*(?:\{%)?\s*|\s*(?:%\})?\s*$/g, "");
419
if (dedentKeywords.test(textAfter) || dedentPunctuals.test(textAfter)) {
420
return config.indentUnit * (state.currentIndent - 1);
423
return config.indentUnit * state.currentIndent;
427
electricInput: wordRegExp(dedentPunctualsArray.concat(dedentKeywordsArray), true),
432
CodeMirror.defineMIME("text/x-crystal", "crystal");