UNPKG

7.67 kBJavaScriptView Raw
1// CodeMirror, copyright (c) by Marijn Haverbeke and others
2// Distributed under an MIT license: https://codemirror.net/LICENSE
3
4/**
5 * Link to the project's GitHub page:
6 * https://github.com/duralog/CodeMirror
7 */
8
9(function(mod) {
10 if (typeof exports == "object" && typeof module == "object") // CommonJS
11 mod(require("../../lib/codemirror"));
12 else if (typeof define == "function" && define.amd) // AMD
13 define(["../../lib/codemirror"], mod);
14 else // Plain browser env
15 mod(CodeMirror);
16})(function(CodeMirror) {
17 "use strict";
18
19 CodeMirror.defineMode('livescript', function(){
20 var tokenBase = function(stream, state) {
21 var next_rule = state.next || "start";
22 if (next_rule) {
23 state.next = state.next;
24 var nr = Rules[next_rule];
25 if (nr.splice) {
26 for (var i$ = 0; i$ < nr.length; ++i$) {
27 var r = nr[i$];
28 if (r.regex && stream.match(r.regex)) {
29 state.next = r.next || state.next;
30 return r.token;
31 }
32 }
33 stream.next();
34 return 'error';
35 }
36 if (stream.match(r = Rules[next_rule])) {
37 if (r.regex && stream.match(r.regex)) {
38 state.next = r.next;
39 return r.token;
40 } else {
41 stream.next();
42 return 'error';
43 }
44 }
45 }
46 stream.next();
47 return 'error';
48 };
49 var external = {
50 startState: function(){
51 return {
52 next: 'start',
53 lastToken: {style: null, indent: 0, content: ""}
54 };
55 },
56 token: function(stream, state){
57 while (stream.pos == stream.start)
58 var style = tokenBase(stream, state);
59 state.lastToken = {
60 style: style,
61 indent: stream.indentation(),
62 content: stream.current()
63 };
64 return style.replace(/\./g, ' ');
65 },
66 indent: function(state){
67 var indentation = state.lastToken.indent;
68 if (state.lastToken.content.match(indenter)) {
69 indentation += 2;
70 }
71 return indentation;
72 }
73 };
74 return external;
75 });
76
77 var identifier = '(?![\\d\\s])[$\\w\\xAA-\\uFFDC](?:(?!\\s)[$\\w\\xAA-\\uFFDC]|-[A-Za-z])*';
78 var indenter = RegExp('(?:[({[=:]|[-~]>|\\b(?:e(?:lse|xport)|d(?:o|efault)|t(?:ry|hen)|finally|import(?:\\s*all)?|const|var|let|new|catch(?:\\s*' + identifier + ')?))\\s*$');
79 var keywordend = '(?![$\\w]|-[A-Za-z]|\\s*:(?![:=]))';
80 var stringfill = {
81 token: 'string',
82 regex: '.+'
83 };
84 var Rules = {
85 start: [
86 {
87 token: 'comment.doc',
88 regex: '/\\*',
89 next: 'comment'
90 }, {
91 token: 'comment',
92 regex: '#.*'
93 }, {
94 token: 'keyword',
95 regex: '(?:t(?:h(?:is|row|en)|ry|ypeof!?)|c(?:on(?:tinue|st)|a(?:se|tch)|lass)|i(?:n(?:stanceof)?|mp(?:ort(?:\\s+all)?|lements)|[fs])|d(?:e(?:fault|lete|bugger)|o)|f(?:or(?:\\s+own)?|inally|unction)|s(?:uper|witch)|e(?:lse|x(?:tends|port)|val)|a(?:nd|rguments)|n(?:ew|ot)|un(?:less|til)|w(?:hile|ith)|o[fr]|return|break|let|var|loop)' + keywordend
96 }, {
97 token: 'constant.language',
98 regex: '(?:true|false|yes|no|on|off|null|void|undefined)' + keywordend
99 }, {
100 token: 'invalid.illegal',
101 regex: '(?:p(?:ackage|r(?:ivate|otected)|ublic)|i(?:mplements|nterface)|enum|static|yield)' + keywordend
102 }, {
103 token: 'language.support.class',
104 regex: '(?:R(?:e(?:gExp|ferenceError)|angeError)|S(?:tring|yntaxError)|E(?:rror|valError)|Array|Boolean|Date|Function|Number|Object|TypeError|URIError)' + keywordend
105 }, {
106 token: 'language.support.function',
107 regex: '(?:is(?:NaN|Finite)|parse(?:Int|Float)|Math|JSON|(?:en|de)codeURI(?:Component)?)' + keywordend
108 }, {
109 token: 'variable.language',
110 regex: '(?:t(?:hat|il|o)|f(?:rom|allthrough)|it|by|e)' + keywordend
111 }, {
112 token: 'identifier',
113 regex: identifier + '\\s*:(?![:=])'
114 }, {
115 token: 'variable',
116 regex: identifier
117 }, {
118 token: 'keyword.operator',
119 regex: '(?:\\.{3}|\\s+\\?)'
120 }, {
121 token: 'keyword.variable',
122 regex: '(?:@+|::|\\.\\.)',
123 next: 'key'
124 }, {
125 token: 'keyword.operator',
126 regex: '\\.\\s*',
127 next: 'key'
128 }, {
129 token: 'string',
130 regex: '\\\\\\S[^\\s,;)}\\]]*'
131 }, {
132 token: 'string.doc',
133 regex: '\'\'\'',
134 next: 'qdoc'
135 }, {
136 token: 'string.doc',
137 regex: '"""',
138 next: 'qqdoc'
139 }, {
140 token: 'string',
141 regex: '\'',
142 next: 'qstring'
143 }, {
144 token: 'string',
145 regex: '"',
146 next: 'qqstring'
147 }, {
148 token: 'string',
149 regex: '`',
150 next: 'js'
151 }, {
152 token: 'string',
153 regex: '<\\[',
154 next: 'words'
155 }, {
156 token: 'string.regex',
157 regex: '//',
158 next: 'heregex'
159 }, {
160 token: 'string.regex',
161 regex: '\\/(?:[^[\\/\\n\\\\]*(?:(?:\\\\.|\\[[^\\]\\n\\\\]*(?:\\\\.[^\\]\\n\\\\]*)*\\])[^[\\/\\n\\\\]*)*)\\/[gimy$]{0,4}',
162 next: 'key'
163 }, {
164 token: 'constant.numeric',
165 regex: '(?:0x[\\da-fA-F][\\da-fA-F_]*|(?:[2-9]|[12]\\d|3[0-6])r[\\da-zA-Z][\\da-zA-Z_]*|(?:\\d[\\d_]*(?:\\.\\d[\\d_]*)?|\\.\\d[\\d_]*)(?:e[+-]?\\d[\\d_]*)?[\\w$]*)'
166 }, {
167 token: 'lparen',
168 regex: '[({[]'
169 }, {
170 token: 'rparen',
171 regex: '[)}\\]]',
172 next: 'key'
173 }, {
174 token: 'keyword.operator',
175 regex: '\\S+'
176 }, {
177 token: 'text',
178 regex: '\\s+'
179 }
180 ],
181 heregex: [
182 {
183 token: 'string.regex',
184 regex: '.*?//[gimy$?]{0,4}',
185 next: 'start'
186 }, {
187 token: 'string.regex',
188 regex: '\\s*#{'
189 }, {
190 token: 'comment.regex',
191 regex: '\\s+(?:#.*)?'
192 }, {
193 token: 'string.regex',
194 regex: '\\S+'
195 }
196 ],
197 key: [
198 {
199 token: 'keyword.operator',
200 regex: '[.?@!]+'
201 }, {
202 token: 'identifier',
203 regex: identifier,
204 next: 'start'
205 }, {
206 token: 'text',
207 regex: '',
208 next: 'start'
209 }
210 ],
211 comment: [
212 {
213 token: 'comment.doc',
214 regex: '.*?\\*/',
215 next: 'start'
216 }, {
217 token: 'comment.doc',
218 regex: '.+'
219 }
220 ],
221 qdoc: [
222 {
223 token: 'string',
224 regex: ".*?'''",
225 next: 'key'
226 }, stringfill
227 ],
228 qqdoc: [
229 {
230 token: 'string',
231 regex: '.*?"""',
232 next: 'key'
233 }, stringfill
234 ],
235 qstring: [
236 {
237 token: 'string',
238 regex: '[^\\\\\']*(?:\\\\.[^\\\\\']*)*\'',
239 next: 'key'
240 }, stringfill
241 ],
242 qqstring: [
243 {
244 token: 'string',
245 regex: '[^\\\\"]*(?:\\\\.[^\\\\"]*)*"',
246 next: 'key'
247 }, stringfill
248 ],
249 js: [
250 {
251 token: 'string',
252 regex: '[^\\\\`]*(?:\\\\.[^\\\\`]*)*`',
253 next: 'key'
254 }, stringfill
255 ],
256 words: [
257 {
258 token: 'string',
259 regex: '.*?\\]>',
260 next: 'key'
261 }, stringfill
262 ]
263 };
264 for (var idx in Rules) {
265 var r = Rules[idx];
266 if (r.splice) {
267 for (var i = 0, len = r.length; i < len; ++i) {
268 var rr = r[i];
269 if (typeof rr.regex === 'string') {
270 Rules[idx][i].regex = new RegExp('^' + rr.regex);
271 }
272 }
273 } else if (typeof rr.regex === 'string') {
274 Rules[idx].regex = new RegExp('^' + r.regex);
275 }
276 }
277
278 CodeMirror.defineMIME('text/x-livescript', 'livescript');
279
280});