livescript.js 7.5 KB

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