livescript.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. define(function(require, exports, module){
  2. var identifier, LiveScriptMode, keywordend, stringfill;
  3. identifier = '(?![\\d\\s])[$\\w\\xAA-\\uFFDC](?:(?!\\s)[$\\w\\xAA-\\uFFDC]|-[A-Za-z])*';
  4. exports.Mode = LiveScriptMode = (function(superclass){
  5. var indenter, prototype = extend$((import$(LiveScriptMode, superclass).displayName = 'LiveScriptMode', LiveScriptMode), superclass).prototype, constructor = LiveScriptMode;
  6. function LiveScriptMode(){
  7. var that;
  8. this.$tokenizer = new (require('../tokenizer')).Tokenizer(LiveScriptMode.Rules);
  9. if (that = require('../mode/matching_brace_outdent')) {
  10. this.$outdent = new that.MatchingBraceOutdent;
  11. }
  12. this.$id = "ace/mode/livescript";
  13. }
  14. indenter = RegExp('(?:[({[=:]|[-~]>|\\b(?:e(?:lse|xport)|d(?:o|efault)|t(?:ry|hen)|finally|import(?:\\s*all)?|const|var|let|new|catch(?:\\s*' + identifier + ')?))\\s*$');
  15. prototype.getNextLineIndent = function(state, line, tab){
  16. var indent, tokens;
  17. indent = this.$getIndent(line);
  18. tokens = this.$tokenizer.getLineTokens(line, state).tokens;
  19. if (!(tokens.length && tokens[tokens.length - 1].type === 'comment')) {
  20. if (state === 'start' && indenter.test(line)) {
  21. indent += tab;
  22. }
  23. }
  24. return indent;
  25. };
  26. prototype.toggleCommentLines = function(state, doc, startRow, endRow){
  27. var comment, range, i$, i, out, line;
  28. comment = /^(\s*)#/;
  29. range = new (require('../range')).Range(0, 0, 0, 0);
  30. for (i$ = startRow; i$ <= endRow; ++i$) {
  31. i = i$;
  32. if (out = comment.test(line = doc.getLine(i))) {
  33. line = line.replace(comment, '$1');
  34. } else {
  35. line = line.replace(/^\s*/, '$&#');
  36. }
  37. range.end.row = range.start.row = i;
  38. range.end.column = line.length + 1;
  39. doc.replace(range, line);
  40. }
  41. return 1 - out * 2;
  42. };
  43. prototype.checkOutdent = function(state, line, input){
  44. var ref$;
  45. return (ref$ = this.$outdent) != null ? ref$.checkOutdent(line, input) : void 8;
  46. };
  47. prototype.autoOutdent = function(state, doc, row){
  48. var ref$;
  49. return (ref$ = this.$outdent) != null ? ref$.autoOutdent(doc, row) : void 8;
  50. };
  51. return LiveScriptMode;
  52. }(require('../mode/text').Mode));
  53. keywordend = '(?![$\\w]|-[A-Za-z]|\\s*:(?![:=]))';
  54. stringfill = {
  55. defaultToken: 'string'
  56. };
  57. LiveScriptMode.Rules = {
  58. start: [
  59. {
  60. token: 'keyword',
  61. 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
  62. }, {
  63. token: 'constant.language',
  64. regex: '(?:true|false|yes|no|on|off|null|void|undefined)' + keywordend
  65. }, {
  66. token: 'invalid.illegal',
  67. regex: '(?:p(?:ackage|r(?:ivate|otected)|ublic)|i(?:mplements|nterface)|enum|static|yield)' + keywordend
  68. }, {
  69. token: 'language.support.class',
  70. regex: '(?:R(?:e(?:gExp|ferenceError)|angeError)|S(?:tring|yntaxError)|E(?:rror|valError)|Array|Boolean|Date|Function|Number|Object|TypeError|URIError)' + keywordend
  71. }, {
  72. token: 'language.support.function',
  73. regex: '(?:is(?:NaN|Finite)|parse(?:Int|Float)|Math|JSON|(?:en|de)codeURI(?:Component)?)' + keywordend
  74. }, {
  75. token: 'variable.language',
  76. regex: '(?:t(?:hat|il|o)|f(?:rom|allthrough)|it|by|e)' + keywordend
  77. }, {
  78. token: 'identifier',
  79. regex: identifier + '\\s*:(?![:=])'
  80. }, {
  81. token: 'variable',
  82. regex: identifier
  83. }, {
  84. token: 'keyword.operator',
  85. regex: '(?:\\.{3}|\\s+\\?)'
  86. }, {
  87. token: 'keyword.variable',
  88. regex: '(?:@+|::|\\.\\.)',
  89. next: 'key'
  90. }, {
  91. token: 'keyword.operator',
  92. regex: '\\.\\s*',
  93. next: 'key'
  94. }, {
  95. token: 'string',
  96. regex: '\\\\\\S[^\\s,;)}\\]]*'
  97. }, {
  98. token: 'string.doc',
  99. regex: '\'\'\'',
  100. next: 'qdoc'
  101. }, {
  102. token: 'string.doc',
  103. regex: '"""',
  104. next: 'qqdoc'
  105. }, {
  106. token: 'string',
  107. regex: '\'',
  108. next: 'qstring'
  109. }, {
  110. token: 'string',
  111. regex: '"',
  112. next: 'qqstring'
  113. }, {
  114. token: 'string',
  115. regex: '`',
  116. next: 'js'
  117. }, {
  118. token: 'string',
  119. regex: '<\\[',
  120. next: 'words'
  121. }, {
  122. token: 'string.regex',
  123. regex: '//',
  124. next: 'heregex'
  125. }, {
  126. token: 'comment.doc',
  127. regex: '/\\*',
  128. next: 'comment'
  129. }, {
  130. token: 'comment',
  131. regex: '#.*'
  132. }, {
  133. token: 'string.regex',
  134. regex: '\\/(?:[^[\\/\\n\\\\]*(?:(?:\\\\.|\\[[^\\]\\n\\\\]*(?:\\\\.[^\\]\\n\\\\]*)*\\])[^[\\/\\n\\\\]*)*)\\/[gimy$]{0,4}',
  135. next: 'key'
  136. }, {
  137. token: 'constant.numeric',
  138. 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$]*)'
  139. }, {
  140. token: 'lparen',
  141. regex: '[({[]'
  142. }, {
  143. token: 'rparen',
  144. regex: '[)}\\]]',
  145. next: 'key'
  146. }, {
  147. token: 'keyword.operator',
  148. regex: '[\\^!|&%+\\-]+'
  149. }, {
  150. token: 'text',
  151. regex: '\\s+'
  152. }
  153. ],
  154. heregex: [
  155. {
  156. token: 'string.regex',
  157. regex: '.*?//[gimy$?]{0,4}',
  158. next: 'start'
  159. }, {
  160. token: 'string.regex',
  161. regex: '\\s*#{'
  162. }, {
  163. token: 'comment.regex',
  164. regex: '\\s+(?:#.*)?'
  165. }, {
  166. defaultToken: 'string.regex'
  167. }
  168. ],
  169. key: [
  170. {
  171. token: 'keyword.operator',
  172. regex: '[.?@!]+'
  173. }, {
  174. token: 'identifier',
  175. regex: identifier,
  176. next: 'start'
  177. }, {
  178. token: 'text',
  179. regex: '',
  180. next: 'start'
  181. }
  182. ],
  183. comment: [
  184. {
  185. token: 'comment.doc',
  186. regex: '.*?\\*/',
  187. next: 'start'
  188. }, {
  189. defaultToken: 'comment.doc'
  190. }
  191. ],
  192. qdoc: [
  193. {
  194. token: 'string',
  195. regex: ".*?'''",
  196. next: 'key'
  197. }, stringfill
  198. ],
  199. qqdoc: [
  200. {
  201. token: 'string',
  202. regex: '.*?"""',
  203. next: 'key'
  204. }, stringfill
  205. ],
  206. qstring: [
  207. {
  208. token: 'string',
  209. regex: '[^\\\\\']*(?:\\\\.[^\\\\\']*)*\'',
  210. next: 'key'
  211. }, stringfill
  212. ],
  213. qqstring: [
  214. {
  215. token: 'string',
  216. regex: '[^\\\\"]*(?:\\\\.[^\\\\"]*)*"',
  217. next: 'key'
  218. }, stringfill
  219. ],
  220. js: [
  221. {
  222. token: 'string',
  223. regex: '[^\\\\`]*(?:\\\\.[^\\\\`]*)*`',
  224. next: 'key'
  225. }, stringfill
  226. ],
  227. words: [
  228. {
  229. token: 'string',
  230. regex: '.*?\\]>',
  231. next: 'key'
  232. }, stringfill
  233. ]
  234. };
  235. function extend$(sub, sup){
  236. function fun(){} fun.prototype = (sub.superclass = sup).prototype;
  237. (sub.prototype = new fun).constructor = sub;
  238. if (typeof sup.extended == 'function') sup.extended(sub);
  239. return sub;
  240. }
  241. function import$(obj, src){
  242. var own = {}.hasOwnProperty;
  243. for (var key in src) if (own.call(src, key)) obj[key] = src[key];
  244. return obj;
  245. }
  246. });