| 1 |
var tokenBase = function(stream, state) { |
| 2 |
var next_rule = state.next || "start"; |
| 3 |
if (next_rule) { |
| 4 |
state.next = state.next; |
| 5 |
var nr = Rules[next_rule]; |
| 6 |
if (nr.splice) { |
| 7 |
for (var i$ = 0; i$ < nr.length; ++i$) { |
| 8 |
var r = nr[i$]; |
| 9 |
if (r.regex && stream.match(r.regex)) { |
| 10 |
state.next = r.next || state.next; |
| 11 |
return r.token; |
| 12 |
} |
| 13 |
} |
| 14 |
stream.next(); |
| 15 |
return 'error'; |
| 16 |
} |
| 17 |
if (stream.match(r = Rules[next_rule])) { |
| 18 |
if (r.regex && stream.match(r.regex)) { |
| 19 |
state.next = r.next; |
| 20 |
return r.token; |
| 21 |
} else { |
| 22 |
stream.next(); |
| 23 |
return 'error'; |
| 24 |
} |
| 25 |
} |
| 26 |
} |
| 27 |
stream.next(); |
| 28 |
return 'error'; |
| 29 |
}; |
| 30 |
|
| 31 |
var identifier = '(?![\\d\\s])[$\\w\\xAA-\\uFFDC](?:(?!\\s)[$\\w\\xAA-\\uFFDC]|-[A-Za-z])*'; |
| 32 |
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*$'); |
| 33 |
var keywordend = '(?![$\\w]|-[A-Za-z]|\\s*:(?![:=]))'; |
| 34 |
var stringfill = { |
| 35 |
token: 'string', |
| 36 |
regex: '.+' |
| 37 |
}; |
| 38 |
var Rules = { |
| 39 |
start: [ |
| 40 |
{ |
| 41 |
token: 'docComment', |
| 42 |
regex: '/\\*', |
| 43 |
next: 'comment' |
| 44 |
}, { |
| 45 |
token: 'comment', |
| 46 |
regex: '#.*' |
| 47 |
}, { |
| 48 |
token: 'keyword', |
| 49 |
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 |
| 50 |
}, { |
| 51 |
token: 'atom', |
| 52 |
regex: '(?:true|false|yes|no|on|off|null|void|undefined)' + keywordend |
| 53 |
}, { |
| 54 |
token: 'invalid', |
| 55 |
regex: '(?:p(?:ackage|r(?:ivate|otected)|ublic)|i(?:mplements|nterface)|enum|static|yield)' + keywordend |
| 56 |
}, { |
| 57 |
token: 'className.standard', |
| 58 |
regex: '(?:R(?:e(?:gExp|ferenceError)|angeError)|S(?:tring|yntaxError)|E(?:rror|valError)|Array|Boolean|Date|Function|Number|Object|TypeError|URIError)' + keywordend |
| 59 |
}, { |
| 60 |
token: 'variableName.function.standard', |
| 61 |
regex: '(?:is(?:NaN|Finite)|parse(?:Int|Float)|Math|JSON|(?:en|de)codeURI(?:Component)?)' + keywordend |
| 62 |
}, { |
| 63 |
token: 'variableName.standard', |
| 64 |
regex: '(?:t(?:hat|il|o)|f(?:rom|allthrough)|it|by|e)' + keywordend |
| 65 |
}, { |
| 66 |
token: 'variableName', |
| 67 |
regex: identifier + '\\s*:(?![:=])' |
| 68 |
}, { |
| 69 |
token: 'variableName', |
| 70 |
regex: identifier |
| 71 |
}, { |
| 72 |
token: 'operatorKeyword', |
| 73 |
regex: '(?:\\.{3}|\\s+\\?)' |
| 74 |
}, { |
| 75 |
token: 'keyword', |
| 76 |
regex: '(?:@+|::|\\.\\.)', |
| 77 |
next: 'key' |
| 78 |
}, { |
| 79 |
token: 'operatorKeyword', |
| 80 |
regex: '\\.\\s*', |
| 81 |
next: 'key' |
| 82 |
}, { |
| 83 |
token: 'string', |
| 84 |
regex: '\\\\\\S[^\\s,;)}\\]]*' |
| 85 |
}, { |
| 86 |
token: 'docString', |
| 87 |
regex: '\'\'\'', |
| 88 |
next: 'qdoc' |
| 89 |
}, { |
| 90 |
token: 'docString', |
| 91 |
regex: '"""', |
| 92 |
next: 'qqdoc' |
| 93 |
}, { |
| 94 |
token: 'string', |
| 95 |
regex: '\'', |
| 96 |
next: 'qstring' |
| 97 |
}, { |
| 98 |
token: 'string', |
| 99 |
regex: '"', |
| 100 |
next: 'qqstring' |
| 101 |
}, { |
| 102 |
token: 'string', |
| 103 |
regex: '`', |
| 104 |
next: 'js' |
| 105 |
}, { |
| 106 |
token: 'string', |
| 107 |
regex: '<\\[', |
| 108 |
next: 'words' |
| 109 |
}, { |
| 110 |
token: 'regexp', |
| 111 |
regex: '//', |
| 112 |
next: 'heregex' |
| 113 |
}, { |
| 114 |
token: 'regexp', |
| 115 |
regex: '\\/(?:[^[\\/\\n\\\\]*(?:(?:\\\\.|\\[[^\\]\\n\\\\]*(?:\\\\.[^\\]\\n\\\\]*)*\\])[^[\\/\\n\\\\]*)*)\\/[gimy$]{0,4}', |
| 116 |
next: 'key' |
| 117 |
}, { |
| 118 |
token: 'number', |
| 119 |
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$]*)' |
| 120 |
}, { |
| 121 |
token: 'paren', |
| 122 |
regex: '[({[]' |
| 123 |
}, { |
| 124 |
token: 'paren', |
| 125 |
regex: '[)}\\]]', |
| 126 |
next: 'key' |
| 127 |
}, { |
| 128 |
token: 'operatorKeyword', |
| 129 |
regex: '\\S+' |
| 130 |
}, { |
| 131 |
token: 'content', |
| 132 |
regex: '\\s+' |
| 133 |
} |
| 134 |
], |
| 135 |
heregex: [ |
| 136 |
{ |
| 137 |
token: 'regexp', |
| 138 |
regex: '.*?//[gimy$?]{0,4}', |
| 139 |
next: 'start' |
| 140 |
}, { |
| 141 |
token: 'regexp', |
| 142 |
regex: '\\s*#{' |
| 143 |
}, { |
| 144 |
token: 'comment', |
| 145 |
regex: '\\s+(?:#.*)?' |
| 146 |
}, { |
| 147 |
token: 'regexp', |
| 148 |
regex: '\\S+' |
| 149 |
} |
| 150 |
], |
| 151 |
key: [ |
| 152 |
{ |
| 153 |
token: 'operatorKeyword', |
| 154 |
regex: '[.?@!]+' |
| 155 |
}, { |
| 156 |
token: 'variableName', |
| 157 |
regex: identifier, |
| 158 |
next: 'start' |
| 159 |
}, { |
| 160 |
token: 'content', |
| 161 |
regex: '', |
| 162 |
next: 'start' |
| 163 |
} |
| 164 |
], |
| 165 |
comment: [ |
| 166 |
{ |
| 167 |
token: 'docComment', |
| 168 |
regex: '.*?\\*/', |
| 169 |
next: 'start' |
| 170 |
}, { |
| 171 |
token: 'docComment', |
| 172 |
regex: '.+' |
| 173 |
} |
| 174 |
], |
| 175 |
qdoc: [ |
| 176 |
{ |
| 177 |
token: 'string', |
| 178 |
regex: ".*?'''", |
| 179 |
next: 'key' |
| 180 |
}, stringfill |
| 181 |
], |
| 182 |
qqdoc: [ |
| 183 |
{ |
| 184 |
token: 'string', |
| 185 |
regex: '.*?"""', |
| 186 |
next: 'key' |
| 187 |
}, stringfill |
| 188 |
], |
| 189 |
qstring: [ |
| 190 |
{ |
| 191 |
token: 'string', |
| 192 |
regex: '[^\\\\\']*(?:\\\\.[^\\\\\']*)*\'', |
| 193 |
next: 'key' |
| 194 |
}, stringfill |
| 195 |
], |
| 196 |
qqstring: [ |
| 197 |
{ |
| 198 |
token: 'string', |
| 199 |
regex: '[^\\\\"]*(?:\\\\.[^\\\\"]*)*"', |
| 200 |
next: 'key' |
| 201 |
}, stringfill |
| 202 |
], |
| 203 |
js: [ |
| 204 |
{ |
| 205 |
token: 'string', |
| 206 |
regex: '[^\\\\`]*(?:\\\\.[^\\\\`]*)*`', |
| 207 |
next: 'key' |
| 208 |
}, stringfill |
| 209 |
], |
| 210 |
words: [ |
| 211 |
{ |
| 212 |
token: 'string', |
| 213 |
regex: '.*?\\]>', |
| 214 |
next: 'key' |
| 215 |
}, stringfill |
| 216 |
] |
| 217 |
}; |
| 218 |
for (var idx in Rules) { |
| 219 |
var r = Rules[idx]; |
| 220 |
if (r.splice) { |
| 221 |
for (var i = 0, len = r.length; i < len; ++i) { |
| 222 |
var rr = r[i]; |
| 223 |
if (typeof rr.regex === 'string') { |
| 224 |
Rules[idx][i].regex = new RegExp('^' + rr.regex); |
| 225 |
} |
| 226 |
} |
| 227 |
} else if (typeof rr.regex === 'string') { |
| 228 |
Rules[idx].regex = new RegExp('^' + r.regex); |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
export const liveScript = { |
| 233 |
name: "livescript", |
| 234 |
startState: function(){ |
| 235 |
return { |
| 236 |
next: 'start', |
| 237 |
lastToken: {style: null, indent: 0, content: ""} |
| 238 |
}; |
| 239 |
}, |
| 240 |
token: function(stream, state){ |
| 241 |
while (stream.pos == stream.start) |
| 242 |
var style = tokenBase(stream, state); |
| 243 |
state.lastToken = { |
| 244 |
style: style, |
| 245 |
indent: stream.indentation(), |
| 246 |
content: stream.current() |
| 247 |
}; |
| 248 |
return style.replace(/\./g, ' '); |
| 249 |
}, |
| 250 |
indent: function(state){ |
| 251 |
var indentation = state.lastToken.indent; |
| 252 |
if (state.lastToken.content.match(indenter)) { |
| 253 |
indentation += 2; |
| 254 |
} |
| 255 |
return indentation; |
| 256 |
} |
| 257 |
}; |