javascript_highlight_rules.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Distributed under the BSD license:
  3. *
  4. * Copyright (c) 2010, Ajax.org B.V.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * * Neither the name of Ajax.org B.V. nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL AJAX.ORG B.V. BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * ***** END LICENSE BLOCK ***** */
  30. define(function(require, exports, module) {
  31. "use strict";
  32. var oop = require("../lib/oop");
  33. var DocCommentHighlightRules = require("./doc_comment_highlight_rules").DocCommentHighlightRules;
  34. var TextHighlightRules = require("./text_highlight_rules").TextHighlightRules;
  35. // TODO: Unicode escape sequences
  36. var identifierRe = "[a-zA-Z\\$_\u00a1-\uffff][a-zA-Z\\d\\$_\u00a1-\uffff]*\\b";
  37. var JavaScriptHighlightRules = function(options) {
  38. // see: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects
  39. var keywordMapper = this.createKeywordMapper({
  40. "variable.language":
  41. "Array|Boolean|Date|Function|Iterator|Number|Object|RegExp|String|Proxy|" + // Constructors
  42. "Namespace|QName|XML|XMLList|" + // E4X
  43. "ArrayBuffer|Float32Array|Float64Array|Int16Array|Int32Array|Int8Array|" +
  44. "Uint16Array|Uint32Array|Uint8Array|Uint8ClampedArray|" +
  45. "Error|EvalError|InternalError|RangeError|ReferenceError|StopIteration|" + // Errors
  46. "SyntaxError|TypeError|URIError|" +
  47. "decodeURI|decodeURIComponent|encodeURI|encodeURIComponent|eval|isFinite|" + // Non-constructor functions
  48. "isNaN|parseFloat|parseInt|" +
  49. "JSON|Math|" + // Other
  50. "this|arguments|prototype|window|document" , // Pseudo
  51. "keyword":
  52. "const|yield|import|get|set|" +
  53. "break|case|catch|continue|default|delete|do|else|finally|for|function|" +
  54. "if|in|instanceof|new|return|switch|throw|try|typeof|let|var|while|with|debugger|" +
  55. // invalid or reserved
  56. "__parent__|__count__|escape|unescape|with|__proto__|" +
  57. "class|enum|extends|super|export|implements|private|public|interface|package|protected|static",
  58. "storage.type":
  59. "const|let|var|function",
  60. "constant.language":
  61. "null|Infinity|NaN|undefined",
  62. "support.function":
  63. "alert",
  64. "constant.language.boolean": "true|false"
  65. }, "identifier");
  66. // keywords which can be followed by regular expressions
  67. var kwBeforeRe = "case|do|else|finally|in|instanceof|return|throw|try|typeof|yield|void";
  68. var escapedRe = "\\\\(?:x[0-9a-fA-F]{2}|" + // hex
  69. "u[0-9a-fA-F]{4}|" + // unicode
  70. "u{[0-9a-fA-F]{1,6}}|" + // es6 unicode
  71. "[0-2][0-7]{0,2}|" + // oct
  72. "3[0-7][0-7]?|" + // oct
  73. "[4-7][0-7]?|" + //oct
  74. ".)";
  75. // regexp must not have capturing parentheses. Use (?:) instead.
  76. // regexps are ordered -> the first match is used
  77. this.$rules = {
  78. "no_regex" : [
  79. DocCommentHighlightRules.getStartRule("doc-start"),
  80. comments("no_regex"),
  81. {
  82. token : "string",
  83. regex : "'(?=.)",
  84. next : "qstring"
  85. }, {
  86. token : "string",
  87. regex : '"(?=.)',
  88. next : "qqstring"
  89. }, {
  90. token : "constant.numeric", // hex
  91. regex : /0(?:[xX][0-9a-fA-F]+|[bB][01]+)\b/
  92. }, {
  93. token : "constant.numeric", // float
  94. regex : /[+-]?\d[\d_]*(?:(?:\.\d*)?(?:[eE][+-]?\d+)?)?\b/
  95. }, {
  96. // Sound.prototype.play =
  97. token : [
  98. "storage.type", "punctuation.operator", "support.function",
  99. "punctuation.operator", "entity.name.function", "text","keyword.operator"
  100. ],
  101. regex : "(" + identifierRe + ")(\\.)(prototype)(\\.)(" + identifierRe +")(\\s*)(=)",
  102. next: "function_arguments"
  103. }, {
  104. // Sound.play = function() { }
  105. token : [
  106. "storage.type", "punctuation.operator", "entity.name.function", "text",
  107. "keyword.operator", "text", "storage.type", "text", "paren.lparen"
  108. ],
  109. regex : "(" + identifierRe + ")(\\.)(" + identifierRe +")(\\s*)(=)(\\s*)(function)(\\s*)(\\()",
  110. next: "function_arguments"
  111. }, {
  112. // play = function() { }
  113. token : [
  114. "entity.name.function", "text", "keyword.operator", "text", "storage.type",
  115. "text", "paren.lparen"
  116. ],
  117. regex : "(" + identifierRe +")(\\s*)(=)(\\s*)(function)(\\s*)(\\()",
  118. next: "function_arguments"
  119. }, {
  120. // Sound.play = function play() { }
  121. token : [
  122. "storage.type", "punctuation.operator", "entity.name.function", "text",
  123. "keyword.operator", "text",
  124. "storage.type", "text", "entity.name.function", "text", "paren.lparen"
  125. ],
  126. regex : "(" + identifierRe + ")(\\.)(" + identifierRe +")(\\s*)(=)(\\s*)(function)(\\s+)(\\w+)(\\s*)(\\()",
  127. next: "function_arguments"
  128. }, {
  129. // function myFunc(arg) { }
  130. token : [
  131. "storage.type", "text", "entity.name.function", "text", "paren.lparen"
  132. ],
  133. regex : "(function)(\\s+)(" + identifierRe + ")(\\s*)(\\()",
  134. next: "function_arguments"
  135. }, {
  136. // foobar: function() { }
  137. token : [
  138. "entity.name.function", "text", "punctuation.operator",
  139. "text", "storage.type", "text", "paren.lparen"
  140. ],
  141. regex : "(" + identifierRe + ")(\\s*)(:)(\\s*)(function)(\\s*)(\\()",
  142. next: "function_arguments"
  143. }, {
  144. // : function() { } (this is for issues with 'foo': function() { })
  145. token : [
  146. "text", "text", "storage.type", "text", "paren.lparen"
  147. ],
  148. regex : "(:)(\\s*)(function)(\\s*)(\\()",
  149. next: "function_arguments"
  150. }, {
  151. token : "keyword",
  152. regex : "(?:" + kwBeforeRe + ")\\b",
  153. next : "start"
  154. }, {
  155. token : ["support.constant"],
  156. regex : /that\b/
  157. }, {
  158. token : ["storage.type", "punctuation.operator", "support.function.firebug"],
  159. regex : /(console)(\.)(warn|info|log|error|time|trace|timeEnd|assert)\b/
  160. }, {
  161. token : keywordMapper,
  162. regex : identifierRe
  163. }, {
  164. token : "punctuation.operator",
  165. regex : /[.](?![.])/,
  166. next : "property"
  167. }, {
  168. token : "keyword.operator",
  169. regex : /--|\+\+|\.{3}|===|==|=|!=|!==|<+=?|>+=?|!|&&|\|\||\?\:|[!$%&*+\-~\/^]=?/,
  170. next : "start"
  171. }, {
  172. token : "punctuation.operator",
  173. regex : /[?:,;.]/,
  174. next : "start"
  175. }, {
  176. token : "paren.lparen",
  177. regex : /[\[({]/,
  178. next : "start"
  179. }, {
  180. token : "paren.rparen",
  181. regex : /[\])}]/
  182. }, {
  183. token: "comment",
  184. regex: /^#!.*$/
  185. }
  186. ],
  187. property: [{
  188. token : "text",
  189. regex : "\\s+"
  190. }, {
  191. // Sound.play = function play() { }
  192. token : [
  193. "storage.type", "punctuation.operator", "entity.name.function", "text",
  194. "keyword.operator", "text",
  195. "storage.type", "text", "entity.name.function", "text", "paren.lparen"
  196. ],
  197. regex : "(" + identifierRe + ")(\\.)(" + identifierRe +")(\\s*)(=)(\\s*)(function)(?:(\\s+)(\\w+))?(\\s*)(\\()",
  198. next: "function_arguments"
  199. }, {
  200. token : "punctuation.operator",
  201. regex : /[.](?![.])/
  202. }, {
  203. token : "support.function",
  204. regex : /(s(?:h(?:ift|ow(?:Mod(?:elessDialog|alDialog)|Help))|croll(?:X|By(?:Pages|Lines)?|Y|To)?|t(?:op|rike)|i(?:n|zeToContent|debar|gnText)|ort|u(?:p|b(?:str(?:ing)?)?)|pli(?:ce|t)|e(?:nd|t(?:Re(?:sizable|questHeader)|M(?:i(?:nutes|lliseconds)|onth)|Seconds|Ho(?:tKeys|urs)|Year|Cursor|Time(?:out)?|Interval|ZOptions|Date|UTC(?:M(?:i(?:nutes|lliseconds)|onth)|Seconds|Hours|Date|FullYear)|FullYear|Active)|arch)|qrt|lice|avePreferences|mall)|h(?:ome|andleEvent)|navigate|c(?:har(?:CodeAt|At)|o(?:s|n(?:cat|textual|firm)|mpile)|eil|lear(?:Timeout|Interval)?|a(?:ptureEvents|ll)|reate(?:StyleSheet|Popup|EventObject))|t(?:o(?:GMTString|S(?:tring|ource)|U(?:TCString|pperCase)|Lo(?:caleString|werCase))|est|a(?:n|int(?:Enabled)?))|i(?:s(?:NaN|Finite)|ndexOf|talics)|d(?:isableExternalCapture|ump|etachEvent)|u(?:n(?:shift|taint|escape|watch)|pdateCommands)|j(?:oin|avaEnabled)|p(?:o(?:p|w)|ush|lugins.refresh|a(?:ddings|rse(?:Int|Float)?)|r(?:int|ompt|eference))|e(?:scape|nableExternalCapture|val|lementFromPoint|x(?:p|ec(?:Script|Command)?))|valueOf|UTC|queryCommand(?:State|Indeterm|Enabled|Value)|f(?:i(?:nd|le(?:ModifiedDate|Size|CreatedDate|UpdatedDate)|xed)|o(?:nt(?:size|color)|rward)|loor|romCharCode)|watch|l(?:ink|o(?:ad|g)|astIndexOf)|a(?:sin|nchor|cos|t(?:tachEvent|ob|an(?:2)?)|pply|lert|b(?:s|ort))|r(?:ou(?:nd|teEvents)|e(?:size(?:By|To)|calc|turnValue|place|verse|l(?:oad|ease(?:Capture|Events)))|andom)|g(?:o|et(?:ResponseHeader|M(?:i(?:nutes|lliseconds)|onth)|Se(?:conds|lection)|Hours|Year|Time(?:zoneOffset)?|Da(?:y|te)|UTC(?:M(?:i(?:nutes|lliseconds)|onth)|Seconds|Hours|Da(?:y|te)|FullYear)|FullYear|A(?:ttention|llResponseHeaders)))|m(?:in|ove(?:B(?:y|elow)|To(?:Absolute)?|Above)|ergeAttributes|a(?:tch|rgins|x))|b(?:toa|ig|o(?:ld|rderWidths)|link|ack))\b(?=\()/
  205. }, {
  206. token : "support.function.dom",
  207. regex : /(s(?:ub(?:stringData|mit)|plitText|e(?:t(?:NamedItem|Attribute(?:Node)?)|lect))|has(?:ChildNodes|Feature)|namedItem|c(?:l(?:ick|o(?:se|neNode))|reate(?:C(?:omment|DATASection|aption)|T(?:Head|extNode|Foot)|DocumentFragment|ProcessingInstruction|E(?:ntityReference|lement)|Attribute))|tabIndex|i(?:nsert(?:Row|Before|Cell|Data)|tem)|open|delete(?:Row|C(?:ell|aption)|T(?:Head|Foot)|Data)|focus|write(?:ln)?|a(?:dd|ppend(?:Child|Data))|re(?:set|place(?:Child|Data)|move(?:NamedItem|Child|Attribute(?:Node)?)?)|get(?:NamedItem|Element(?:sBy(?:Name|TagName|ClassName)|ById)|Attribute(?:Node)?)|blur)\b(?=\()/
  208. }, {
  209. token : "support.constant",
  210. regex : /(s(?:ystemLanguage|cr(?:ipts|ollbars|een(?:X|Y|Top|Left))|t(?:yle(?:Sheets)?|atus(?:Text|bar)?)|ibling(?:Below|Above)|ource|uffixes|e(?:curity(?:Policy)?|l(?:ection|f)))|h(?:istory|ost(?:name)?|as(?:h|Focus))|y|X(?:MLDocument|SLDocument)|n(?:ext|ame(?:space(?:s|URI)|Prop))|M(?:IN_VALUE|AX_VALUE)|c(?:haracterSet|o(?:n(?:structor|trollers)|okieEnabled|lorDepth|mp(?:onents|lete))|urrent|puClass|l(?:i(?:p(?:boardData)?|entInformation)|osed|asses)|alle(?:e|r)|rypto)|t(?:o(?:olbar|p)|ext(?:Transform|Indent|Decoration|Align)|ags)|SQRT(?:1_2|2)|i(?:n(?:ner(?:Height|Width)|put)|ds|gnoreCase)|zIndex|o(?:scpu|n(?:readystatechange|Line)|uter(?:Height|Width)|p(?:sProfile|ener)|ffscreenBuffering)|NEGATIVE_INFINITY|d(?:i(?:splay|alog(?:Height|Top|Width|Left|Arguments)|rectories)|e(?:scription|fault(?:Status|Ch(?:ecked|arset)|View)))|u(?:ser(?:Profile|Language|Agent)|n(?:iqueID|defined)|pdateInterval)|_content|p(?:ixelDepth|ort|ersonalbar|kcs11|l(?:ugins|atform)|a(?:thname|dding(?:Right|Bottom|Top|Left)|rent(?:Window|Layer)?|ge(?:X(?:Offset)?|Y(?:Offset)?))|r(?:o(?:to(?:col|type)|duct(?:Sub)?|mpter)|e(?:vious|fix)))|e(?:n(?:coding|abledPlugin)|x(?:ternal|pando)|mbeds)|v(?:isibility|endor(?:Sub)?|Linkcolor)|URLUnencoded|P(?:I|OSITIVE_INFINITY)|f(?:ilename|o(?:nt(?:Size|Family|Weight)|rmName)|rame(?:s|Element)|gColor)|E|whiteSpace|l(?:i(?:stStyleType|n(?:eHeight|kColor))|o(?:ca(?:tion(?:bar)?|lName)|wsrc)|e(?:ngth|ft(?:Context)?)|a(?:st(?:M(?:odified|atch)|Index|Paren)|yer(?:s|X)|nguage))|a(?:pp(?:MinorVersion|Name|Co(?:deName|re)|Version)|vail(?:Height|Top|Width|Left)|ll|r(?:ity|guments)|Linkcolor|bove)|r(?:ight(?:Context)?|e(?:sponse(?:XML|Text)|adyState))|global|x|m(?:imeTypes|ultiline|enubar|argin(?:Right|Bottom|Top|Left))|L(?:N(?:10|2)|OG(?:10E|2E))|b(?:o(?:ttom|rder(?:Width|RightWidth|BottomWidth|Style|Color|TopWidth|LeftWidth))|ufferDepth|elow|ackground(?:Color|Image)))\b/
  211. }, {
  212. token : "identifier",
  213. regex : identifierRe
  214. }, {
  215. regex: "",
  216. token: "empty",
  217. next: "no_regex"
  218. }
  219. ],
  220. // regular expressions are only allowed after certain tokens. This
  221. // makes sure we don't mix up regexps with the divison operator
  222. "start": [
  223. DocCommentHighlightRules.getStartRule("doc-start"),
  224. comments("start"),
  225. {
  226. token: "string.regexp",
  227. regex: "\\/",
  228. next: "regex"
  229. }, {
  230. token : "text",
  231. regex : "\\s+|^$",
  232. next : "start"
  233. }, {
  234. // immediately return to the start mode without matching
  235. // anything
  236. token: "empty",
  237. regex: "",
  238. next: "no_regex"
  239. }
  240. ],
  241. "regex": [
  242. {
  243. // escapes
  244. token: "regexp.keyword.operator",
  245. regex: "\\\\(?:u[\\da-fA-F]{4}|x[\\da-fA-F]{2}|.)"
  246. }, {
  247. // flag
  248. token: "string.regexp",
  249. regex: "/[sxngimy]*",
  250. next: "no_regex"
  251. }, {
  252. // invalid operators
  253. token : "invalid",
  254. regex: /\{\d+\b,?\d*\}[+*]|[+*$^?][+*]|[$^][?]|\?{3,}/
  255. }, {
  256. // operators
  257. token : "constant.language.escape",
  258. regex: /\(\?[:=!]|\)|\{\d+\b,?\d*\}|[+*]\?|[()$^+*?.]/
  259. }, {
  260. token : "constant.language.delimiter",
  261. regex: /\|/
  262. }, {
  263. token: "constant.language.escape",
  264. regex: /\[\^?/,
  265. next: "regex_character_class"
  266. }, {
  267. token: "empty",
  268. regex: "$",
  269. next: "no_regex"
  270. }, {
  271. defaultToken: "string.regexp"
  272. }
  273. ],
  274. "regex_character_class": [
  275. {
  276. token: "regexp.charclass.keyword.operator",
  277. regex: "\\\\(?:u[\\da-fA-F]{4}|x[\\da-fA-F]{2}|.)"
  278. }, {
  279. token: "constant.language.escape",
  280. regex: "]",
  281. next: "regex"
  282. }, {
  283. token: "constant.language.escape",
  284. regex: "-"
  285. }, {
  286. token: "empty",
  287. regex: "$",
  288. next: "no_regex"
  289. }, {
  290. defaultToken: "string.regexp.charachterclass"
  291. }
  292. ],
  293. "function_arguments": [
  294. {
  295. token: "variable.parameter",
  296. regex: identifierRe
  297. }, {
  298. token: "punctuation.operator",
  299. regex: "[, ]+"
  300. }, {
  301. token: "punctuation.operator",
  302. regex: "$"
  303. }, {
  304. token: "empty",
  305. regex: "",
  306. next: "no_regex"
  307. }
  308. ],
  309. "qqstring" : [
  310. {
  311. token : "constant.language.escape",
  312. regex : escapedRe
  313. }, {
  314. token : "string",
  315. regex : "\\\\$",
  316. next : "qqstring"
  317. }, {
  318. token : "string",
  319. regex : '"|$',
  320. next : "no_regex"
  321. }, {
  322. defaultToken: "string"
  323. }
  324. ],
  325. "qstring" : [
  326. {
  327. token : "constant.language.escape",
  328. regex : escapedRe
  329. }, {
  330. token : "string",
  331. regex : "\\\\$",
  332. next : "qstring"
  333. }, {
  334. token : "string",
  335. regex : "'|$",
  336. next : "no_regex"
  337. }, {
  338. defaultToken: "string"
  339. }
  340. ]
  341. };
  342. if (!options || !options.noES6) {
  343. this.$rules.no_regex.unshift({
  344. regex: "[{}]", onMatch: function(val, state, stack) {
  345. this.next = val == "{" ? this.nextState : "";
  346. if (val == "{" && stack.length) {
  347. stack.unshift("start", state);
  348. return "paren";
  349. }
  350. if (val == "}" && stack.length) {
  351. stack.shift();
  352. this.next = stack.shift();
  353. if (this.next.indexOf("string") != -1 || this.next.indexOf("jsx") != -1)
  354. return "paren.quasi.end";
  355. }
  356. return val == "{" ? "paren.lparen" : "paren.rparen";
  357. },
  358. nextState: "start"
  359. }, {
  360. token : "string.quasi.start",
  361. regex : /`/,
  362. push : [{
  363. token : "constant.language.escape",
  364. regex : escapedRe
  365. }, {
  366. token : "paren.quasi.start",
  367. regex : /\${/,
  368. push : "start"
  369. }, {
  370. token : "string.quasi.end",
  371. regex : /`/,
  372. next : "pop"
  373. }, {
  374. defaultToken: "string.quasi"
  375. }]
  376. });
  377. if (!options || !options.noJSX)
  378. JSX.call(this);
  379. }
  380. this.embedRules(DocCommentHighlightRules, "doc-",
  381. [ DocCommentHighlightRules.getEndRule("no_regex") ]);
  382. this.normalizeRules();
  383. };
  384. oop.inherits(JavaScriptHighlightRules, TextHighlightRules);
  385. function JSX() {
  386. var tagRegex = identifierRe.replace("\\d", "\\d\\-");
  387. var jsxTag = {
  388. onMatch : function(val, state, stack) {
  389. var offset = val.charAt(1) == "/" ? 2 : 1;
  390. if (offset == 1) {
  391. if (state != this.nextState)
  392. stack.unshift(this.next, this.nextState, 0);
  393. else
  394. stack.unshift(this.next);
  395. stack[2]++;
  396. } else if (offset == 2) {
  397. if (state == this.nextState) {
  398. stack[1]--;
  399. if (!stack[1] || stack[1] < 0) {
  400. stack.shift();
  401. stack.shift();
  402. }
  403. }
  404. }
  405. return [{
  406. type: "meta.tag.punctuation." + (offset == 1 ? "" : "end-") + "tag-open.xml",
  407. value: val.slice(0, offset)
  408. }, {
  409. type: "meta.tag.tag-name.xml",
  410. value: val.substr(offset)
  411. }];
  412. },
  413. regex : "</?" + tagRegex + "",
  414. next: "jsxAttributes",
  415. nextState: "jsx"
  416. };
  417. this.$rules.start.unshift(jsxTag);
  418. var jsxJsRule = {
  419. regex: "{",
  420. token: "paren.quasi.start",
  421. push: "start"
  422. };
  423. this.$rules.jsx = [
  424. jsxJsRule,
  425. jsxTag,
  426. {include : "reference"},
  427. {defaultToken: "string"}
  428. ];
  429. this.$rules.jsxAttributes = [{
  430. token : "meta.tag.punctuation.tag-close.xml",
  431. regex : "/?>",
  432. onMatch : function(value, currentState, stack) {
  433. if (currentState == stack[0])
  434. stack.shift();
  435. if (value.length == 2) {
  436. if (stack[0] == this.nextState)
  437. stack[1]--;
  438. if (!stack[1] || stack[1] < 0) {
  439. stack.splice(0, 2);
  440. }
  441. }
  442. this.next = stack[0] || "start";
  443. return [{type: this.token, value: value}];
  444. },
  445. nextState: "jsx"
  446. },
  447. jsxJsRule,
  448. comments("jsxAttributes"),
  449. {
  450. token : "entity.other.attribute-name.xml",
  451. regex : tagRegex
  452. }, {
  453. token : "keyword.operator.attribute-equals.xml",
  454. regex : "="
  455. }, {
  456. token : "text.tag-whitespace.xml",
  457. regex : "\\s+"
  458. }, {
  459. token : "string.attribute-value.xml",
  460. regex : "'",
  461. stateName : "jsx_attr_q",
  462. push : [
  463. {token : "string.attribute-value.xml", regex: "'", next: "pop"},
  464. jsxJsRule,
  465. {include : "reference"},
  466. {defaultToken : "string.attribute-value.xml"}
  467. ]
  468. }, {
  469. token : "string.attribute-value.xml",
  470. regex : '"',
  471. stateName : "jsx_attr_qq",
  472. push : [
  473. jsxJsRule,
  474. {token : "string.attribute-value.xml", regex: '"', next: "pop"},
  475. {include : "reference"},
  476. {defaultToken : "string.attribute-value.xml"}
  477. ]
  478. }];
  479. this.$rules.reference = [{
  480. token : "constant.language.escape.reference.xml",
  481. regex : "(?:&#[0-9]+;)|(?:&#x[0-9a-fA-F]+;)|(?:&[a-zA-Z0-9_:\\.-]+;)"
  482. }];
  483. }
  484. function comments(next) {
  485. return [
  486. {
  487. token : "comment", // multi line comment
  488. regex : /\/\*/,
  489. next: [
  490. DocCommentHighlightRules.getTagRule(),
  491. {token : "comment", regex : "\\*\\/", next : next || "pop"},
  492. {defaultToken : "comment", caseInsensitive: true}
  493. ]
  494. }, {
  495. token : "comment",
  496. regex : "\\/\\/",
  497. next: [
  498. DocCommentHighlightRules.getTagRule(),
  499. {token : "comment", regex : "$|^", next : next || "pop"},
  500. {defaultToken : "comment", caseInsensitive: true}
  501. ]
  502. }
  503. ];
  504. }
  505. exports.JavaScriptHighlightRules = JavaScriptHighlightRules;
  506. });