-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
livescript.js
233 lines (227 loc) · 5.06 KB
/
livescript.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
Language: LiveScript
Author: Taneli Vatanen <taneli.vatanen@gmail.com>
Contributors: Jen Evers-Corvina <jen@sevvie.net>
Origin: coffeescript.js
Description: LiveScript is a programming language that transcompiles to JavaScript. For info about language see http://livescript.net/
Website: https://livescript.net
Category: scripting
*/
import * as ECMAScript from './lib/ecmascript.js';
export default function(hljs) {
const LIVESCRIPT_BUILT_INS = [
'npm',
'print'
];
const LIVESCRIPT_LITERALS = [
'yes',
'no',
'on',
'off',
'it',
'that',
'void'
];
const LIVESCRIPT_KEYWORDS = [
'then',
'unless',
'until',
'loop',
'of',
'by',
'when',
'and',
'or',
'is',
'isnt',
'not',
'it',
'that',
'otherwise',
'from',
'to',
'til',
'fallthrough',
'case',
'enum',
'native',
'list',
'map',
'__hasProp',
'__extends',
'__slice',
'__bind',
'__indexOf'
];
const KEYWORDS = {
keyword: ECMAScript.KEYWORDS.concat(LIVESCRIPT_KEYWORDS),
literal: ECMAScript.LITERALS.concat(LIVESCRIPT_LITERALS),
built_in: ECMAScript.BUILT_INS.concat(LIVESCRIPT_BUILT_INS)
};
const JS_IDENT_RE = '[A-Za-z$_](?:-[0-9A-Za-z$_]|[0-9A-Za-z$_])*';
const TITLE = hljs.inherit(hljs.TITLE_MODE, { begin: JS_IDENT_RE });
const SUBST = {
className: 'subst',
begin: /#\{/,
end: /\}/,
keywords: KEYWORDS
};
const SUBST_SIMPLE = {
className: 'subst',
begin: /#[A-Za-z$_]/,
end: /(?:-[0-9A-Za-z$_]|[0-9A-Za-z$_])*/,
keywords: KEYWORDS
};
const EXPRESSIONS = [
hljs.BINARY_NUMBER_MODE,
{
className: 'number',
begin: '(\\b0[xX][a-fA-F0-9_]+)|(\\b\\d(\\d|_\\d)*(\\.(\\d(\\d|_\\d)*)?)?(_*[eE]([-+]\\d(_\\d|\\d)*)?)?[_a-z]*)',
relevance: 0,
starts: {
end: '(\\s*/)?',
relevance: 0
} // a number tries to eat the following slash to prevent treating it as a regexp
},
{
className: 'string',
variants: [
{
begin: /'''/,
end: /'''/,
contains: [ hljs.BACKSLASH_ESCAPE ]
},
{
begin: /'/,
end: /'/,
contains: [ hljs.BACKSLASH_ESCAPE ]
},
{
begin: /"""/,
end: /"""/,
contains: [
hljs.BACKSLASH_ESCAPE,
SUBST,
SUBST_SIMPLE
]
},
{
begin: /"/,
end: /"/,
contains: [
hljs.BACKSLASH_ESCAPE,
SUBST,
SUBST_SIMPLE
]
},
{
begin: /\\/,
end: /(\s|$)/,
excludeEnd: true
}
]
},
{
className: 'regexp',
variants: [
{
begin: '//',
end: '//[gim]*',
contains: [
SUBST,
hljs.HASH_COMMENT_MODE
]
},
{
// regex can't start with space to parse x / 2 / 3 as two divisions
// regex can't start with *, and it supports an "illegal" in the main mode
begin: /\/(?![ *])(\\.|[^\\\n])*?\/[gim]*(?=\W)/ }
]
},
{ begin: '@' + JS_IDENT_RE },
{
begin: '``',
end: '``',
excludeBegin: true,
excludeEnd: true,
subLanguage: 'javascript'
}
];
SUBST.contains = EXPRESSIONS;
const PARAMS = {
className: 'params',
begin: '\\(',
returnBegin: true,
/* We need another contained nameless mode to not have every nested
pair of parens to be called "params" */
contains: [
{
begin: /\(/,
end: /\)/,
keywords: KEYWORDS,
contains: [ 'self' ].concat(EXPRESSIONS)
}
]
};
const SYMBOLS = { begin: '(#=>|=>|\\|>>|-?->|!->)' };
const CLASS_DEFINITION = {
variants: [
{ match: [
/class\s+/,
JS_IDENT_RE,
/\s+extends\s+/,
JS_IDENT_RE
] },
{ match: [
/class\s+/,
JS_IDENT_RE
] }
],
scope: {
2: "title.class",
4: "title.class.inherited"
},
keywords: KEYWORDS
};
return {
name: 'LiveScript',
aliases: [ 'ls' ],
keywords: KEYWORDS,
illegal: /\/\*/,
contains: EXPRESSIONS.concat([
hljs.COMMENT('\\/\\*', '\\*\\/'),
hljs.HASH_COMMENT_MODE,
SYMBOLS, // relevance booster
{
className: 'function',
contains: [
TITLE,
PARAMS
],
returnBegin: true,
variants: [
{
begin: '(' + JS_IDENT_RE + '\\s*(?:=|:=)\\s*)?(\\(.*\\)\\s*)?\\B->\\*?',
end: '->\\*?'
},
{
begin: '(' + JS_IDENT_RE + '\\s*(?:=|:=)\\s*)?!?(\\(.*\\)\\s*)?\\B[-~]{1,2}>\\*?',
end: '[-~]{1,2}>\\*?'
},
{
begin: '(' + JS_IDENT_RE + '\\s*(?:=|:=)\\s*)?(\\(.*\\)\\s*)?\\B!?[-~]{1,2}>\\*?',
end: '!?[-~]{1,2}>\\*?'
}
]
},
CLASS_DEFINITION,
{
begin: JS_IDENT_RE + ':',
end: ':',
returnBegin: true,
returnEnd: true,
relevance: 0
}
])
};
}