javascript_highlight_rules.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. var JavaScriptHighlightRules = function() {
  36. // see: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects
  37. var keywordMapper = this.createKeywordMapper({
  38. "variable.language":
  39. "Array|Boolean|Date|Function|Iterator|Number|Object|RegExp|String|Proxy|" + // Constructors
  40. "Namespace|QName|XML|XMLList|" + // E4X
  41. "ArrayBuffer|Float32Array|Float64Array|Int16Array|Int32Array|Int8Array|" +
  42. "Uint16Array|Uint32Array|Uint8Array|Uint8ClampedArray|" +
  43. "Error|EvalError|InternalError|RangeError|ReferenceError|StopIteration|" + // Errors
  44. "SyntaxError|TypeError|URIError|" +
  45. "decodeURI|decodeURIComponent|encodeURI|encodeURIComponent|eval|isFinite|" + // Non-constructor functions
  46. "isNaN|parseFloat|parseInt|" +
  47. "JSON|Math|" + // Other
  48. "this|arguments|prototype|window|document" , // Pseudo
  49. "keyword":
  50. "const|yield|import|get|set|" +
  51. "break|case|catch|continue|default|delete|do|else|finally|for|function|" +
  52. "if|in|instanceof|new|return|switch|throw|try|typeof|let|var|while|with|debugger|" +
  53. // invalid or reserved
  54. "__parent__|__count__|escape|unescape|with|__proto__|" +
  55. "class|enum|extends|super|export|implements|private|public|interface|package|protected|static",
  56. "storage.type":
  57. "const|let|var|function",
  58. "constant.language":
  59. "null|Infinity|NaN|undefined",
  60. "support.function":
  61. "alert",
  62. "constant.language.boolean": "true|false"
  63. }, "identifier");
  64. // keywords which can be followed by regular expressions
  65. var kwBeforeRe = "case|do|else|finally|in|instanceof|return|throw|try|typeof|yield|void";
  66. // TODO: Unicode escape sequences
  67. var identifierRe = "[a-zA-Z\\$_\u00a1-\uffff][a-zA-Z\\d\\$_\u00a1-\uffff]*\\b";
  68. var escapedRe = "\\\\(?:x[0-9a-fA-F]{2}|" + // hex
  69. "u[0-9a-fA-F]{4}|" + // unicode
  70. "[0-2][0-7]{0,2}|" + // oct
  71. "3[0-6][0-7]?|" + // oct
  72. "37[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. {
  80. token : "comment",
  81. regex : "\\/\\/",
  82. next : "line_comment"
  83. },
  84. DocCommentHighlightRules.getStartRule("doc-start"),
  85. {
  86. token : "comment", // multi line comment
  87. regex : /\/\*/,
  88. next : "comment"
  89. }, {
  90. token : "string",
  91. regex : "'(?=.)",
  92. next : "qstring"
  93. }, {
  94. token : "string",
  95. regex : '"(?=.)',
  96. next : "qqstring"
  97. }, {
  98. token : "constant.numeric", // hex
  99. regex : /0[xX][0-9a-fA-F]+\b/
  100. }, {
  101. token : "constant.numeric", // float
  102. regex : /[+-]?\d+(?:(?:\.\d*)?(?:[eE][+-]?\d+)?)?\b/
  103. }, {
  104. // Sound.prototype.play =
  105. token : [
  106. "storage.type", "punctuation.operator", "support.function",
  107. "punctuation.operator", "entity.name.function", "text","keyword.operator"
  108. ],
  109. regex : "(" + identifierRe + ")(\\.)(prototype)(\\.)(" + identifierRe +")(\\s*)(=)",
  110. next: "function_arguments"
  111. }, {
  112. // Sound.play = function() { }
  113. token : [
  114. "storage.type", "punctuation.operator", "entity.name.function", "text",
  115. "keyword.operator", "text", "storage.type", "text", "paren.lparen"
  116. ],
  117. regex : "(" + identifierRe + ")(\\.)(" + identifierRe +")(\\s*)(=)(\\s*)(function)(\\s*)(\\()",
  118. next: "function_arguments"
  119. }, {
  120. // play = function() { }
  121. token : [
  122. "entity.name.function", "text", "keyword.operator", "text", "storage.type",
  123. "text", "paren.lparen"
  124. ],
  125. regex : "(" + identifierRe +")(\\s*)(=)(\\s*)(function)(\\s*)(\\()",
  126. next: "function_arguments"
  127. }, {
  128. // Sound.play = function play() { }
  129. token : [
  130. "storage.type", "punctuation.operator", "entity.name.function", "text",
  131. "keyword.operator", "text",
  132. "storage.type", "text", "entity.name.function", "text", "paren.lparen"
  133. ],
  134. regex : "(" + identifierRe + ")(\\.)(" + identifierRe +")(\\s*)(=)(\\s*)(function)(\\s+)(\\w+)(\\s*)(\\()",
  135. next: "function_arguments"
  136. }, {
  137. // function myFunc(arg) { }
  138. token : [
  139. "storage.type", "text", "entity.name.function", "text", "paren.lparen"
  140. ],
  141. regex : "(function)(\\s+)(" + identifierRe + ")(\\s*)(\\()",
  142. next: "function_arguments"
  143. }, {
  144. // foobar: function() { }
  145. token : [
  146. "entity.name.function", "text", "punctuation.operator",
  147. "text", "storage.type", "text", "paren.lparen"
  148. ],
  149. regex : "(" + identifierRe + ")(\\s*)(:)(\\s*)(function)(\\s*)(\\()",
  150. next: "function_arguments"
  151. }, {
  152. // : function() { } (this is for issues with 'foo': function() { })
  153. token : [
  154. "text", "text", "storage.type", "text", "paren.lparen"
  155. ],
  156. regex : "(:)(\\s*)(function)(\\s*)(\\()",
  157. next: "function_arguments"
  158. }, {
  159. token : "keyword",
  160. regex : "(?:" + kwBeforeRe + ")\\b",
  161. next : "start"
  162. }, {
  163. token : ["punctuation.operator", "support.function"],
  164. 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(?=\()/
  165. }, {
  166. token : ["punctuation.operator", "support.function.dom"],
  167. 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)|ById)|Attribute(?:Node)?)|blur)\b(?=\()/
  168. }, {
  169. token : ["punctuation.operator", "support.constant"],
  170. 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/
  171. }, {
  172. token : ["storage.type", "punctuation.operator", "support.function.firebug"],
  173. regex : /(console)(\.)(warn|info|log|error|time|timeEnd|assert)\b/
  174. }, {
  175. token : keywordMapper,
  176. regex : identifierRe
  177. }, {
  178. token : "keyword.operator",
  179. regex : /--|\+\+|[!$%&*+\-~]|===|==|=|!=|!==|<=|>=|<<=|>>=|>>>=|<>|<|>|!|&&|\|\||\?\:|\*=|%=|\+=|\-=|&=|\^=/,
  180. next : "start"
  181. }, {
  182. token : "punctuation.operator",
  183. regex : /\?|\:|\,|\;|\./,
  184. next : "start"
  185. }, {
  186. token : "paren.lparen",
  187. regex : /[\[({]/,
  188. next : "start"
  189. }, {
  190. token : "paren.rparen",
  191. regex : /[\])}]/
  192. }, {
  193. token : "keyword.operator",
  194. regex : /\/=?/,
  195. next : "start"
  196. }, {
  197. token: "comment",
  198. regex: /^#!.*$/
  199. }
  200. ],
  201. // regular expressions are only allowed after certain tokens. This
  202. // makes sure we don't mix up regexps with the divison operator
  203. "start": [
  204. DocCommentHighlightRules.getStartRule("doc-start"),
  205. {
  206. token : "comment", // multi line comment
  207. regex : "\\/\\*",
  208. next : "comment_regex_allowed"
  209. }, {
  210. token : "comment",
  211. regex : "\\/\\/",
  212. next : "line_comment_regex_allowed"
  213. }, {
  214. token: "string.regexp",
  215. regex: "\\/",
  216. next: "regex"
  217. }, {
  218. token : "text",
  219. regex : "\\s+|^$",
  220. next : "start"
  221. }, {
  222. // immediately return to the start mode without matching
  223. // anything
  224. token: "empty",
  225. regex: "",
  226. next: "no_regex"
  227. }
  228. ],
  229. "regex": [
  230. {
  231. // escapes
  232. token: "regexp.keyword.operator",
  233. regex: "\\\\(?:u[\\da-fA-F]{4}|x[\\da-fA-F]{2}|.)"
  234. }, {
  235. // flag
  236. token: "string.regexp",
  237. regex: "/\\w*",
  238. next: "no_regex"
  239. }, {
  240. // invalid operators
  241. token : "invalid",
  242. regex: /\{\d+\b,?\d*\}[+*]|[+*$^?][+*]|[$^][?]|\?{3,}/
  243. }, {
  244. // operators
  245. token : "constant.language.escape",
  246. regex: /\(\?[:=!]|\)|\{\d+\b,?\d*\}|[+*]\?|[()$^+*?]/
  247. }, {
  248. token : "constant.language.delimiter",
  249. regex: /\|/
  250. }, {
  251. token: "constant.language.escape",
  252. regex: /\[\^?/,
  253. next: "regex_character_class"
  254. }, {
  255. token: "empty",
  256. regex: "$",
  257. next: "no_regex"
  258. }, {
  259. defaultToken: "string.regexp"
  260. }
  261. ],
  262. "regex_character_class": [
  263. {
  264. token: "regexp.keyword.operator",
  265. regex: "\\\\(?:u[\\da-fA-F]{4}|x[\\da-fA-F]{2}|.)"
  266. }, {
  267. token: "constant.language.escape",
  268. regex: "]",
  269. next: "regex"
  270. }, {
  271. token: "constant.language.escape",
  272. regex: "-"
  273. }, {
  274. token: "empty",
  275. regex: "$",
  276. next: "no_regex"
  277. }, {
  278. defaultToken: "string.regexp.charachterclass"
  279. }
  280. ],
  281. "function_arguments": [
  282. {
  283. token: "variable.parameter",
  284. regex: identifierRe
  285. }, {
  286. token: "punctuation.operator",
  287. regex: "[, ]+"
  288. }, {
  289. token: "punctuation.operator",
  290. regex: "$"
  291. }, {
  292. token: "empty",
  293. regex: "",
  294. next: "no_regex"
  295. }
  296. ],
  297. "comment_regex_allowed" : [
  298. {token : "comment", regex : "\\*\\/", next : "start"},
  299. {defaultToken : "comment"}
  300. ],
  301. "comment" : [
  302. {token : "comment", regex : "\\*\\/", next : "no_regex"},
  303. {defaultToken : "comment"}
  304. ],
  305. "line_comment_regex_allowed" : [
  306. {token : "comment", regex : "$|^", next : "start"},
  307. {defaultToken : "comment"}
  308. ],
  309. "line_comment" : [
  310. {token : "comment", regex : "$|^", next : "no_regex"},
  311. {defaultToken : "comment"}
  312. ],
  313. "qqstring" : [
  314. {
  315. token : "constant.language.escape",
  316. regex : escapedRe
  317. }, {
  318. token : "string",
  319. regex : "\\\\$",
  320. next : "qqstring"
  321. }, {
  322. token : "string",
  323. regex : '"|$',
  324. next : "no_regex"
  325. }, {
  326. defaultToken: "string"
  327. }
  328. ],
  329. "qstring" : [
  330. {
  331. token : "constant.language.escape",
  332. regex : escapedRe
  333. }, {
  334. token : "string",
  335. regex : "\\\\$",
  336. next : "qstring"
  337. }, {
  338. token : "string",
  339. regex : "'|$",
  340. next : "no_regex"
  341. }, {
  342. defaultToken: "string"
  343. }
  344. ]
  345. };
  346. this.embedRules(DocCommentHighlightRules, "doc-",
  347. [ DocCommentHighlightRules.getEndRule("no_regex") ]);
  348. };
  349. oop.inherits(JavaScriptHighlightRules, TextHighlightRules);
  350. exports.JavaScriptHighlightRules = JavaScriptHighlightRules;
  351. });