jsx_highlight_rules.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. define(function(require, exports, module) {
  2. var oop = require("../lib/oop");
  3. var lang = require("../lib/lang");
  4. var DocCommentHighlightRules = require("./doc_comment_highlight_rules").DocCommentHighlightRules;
  5. var TextHighlightRules = require("./text_highlight_rules").TextHighlightRules;
  6. var JsxHighlightRules = function() {
  7. var keywords = lang.arrayToMap(
  8. ("break|do|instanceof|typeof|case|else|new|var|catch|finally|return|void|continue|for|switch|default|while|function|this|" +
  9. "if|throw|" +
  10. "delete|in|try|" +
  11. "class|extends|super|import|from|into|implements|interface|static|mixin|override|abstract|final|" +
  12. "number|int|string|boolean|variant|" +
  13. "log|assert").split("|")
  14. );
  15. var buildinConstants = lang.arrayToMap(
  16. ("null|true|false|NaN|Infinity|__FILE__|__LINE__|undefined").split("|")
  17. );
  18. var reserved = lang.arrayToMap(
  19. ("debugger|with|" +
  20. "const|export|" +
  21. "let|private|public|yield|protected|" +
  22. "extern|native|as|operator|__fake__|__readonly__").split("|")
  23. );
  24. var identifierRe = "[a-zA-Z_][a-zA-Z0-9_]*\\b";
  25. this.$rules = {
  26. "start" : [
  27. {
  28. token : "comment",
  29. regex : "\\/\\/.*$"
  30. },
  31. DocCommentHighlightRules.getStartRule("doc-start"),
  32. {
  33. token : "comment", // multi line comment
  34. regex : "\\/\\*",
  35. next : "comment"
  36. }, {
  37. token : "string.regexp",
  38. regex : "[/](?:(?:\\[(?:\\\\]|[^\\]])+\\])|(?:\\\\/|[^\\]/]))*[/]\\w*\\s*(?=[).,;]|$)"
  39. }, {
  40. token : "string", // single line
  41. regex : '["](?:(?:\\\\.)|(?:[^"\\\\]))*?["]'
  42. }, {
  43. token : "string", // single line
  44. regex : "['](?:(?:\\\\.)|(?:[^'\\\\]))*?[']"
  45. }, {
  46. token : "constant.numeric", // hex
  47. regex : "0[xX][0-9a-fA-F]+\\b"
  48. }, {
  49. token : "constant.numeric", // float
  50. regex : "[+-]?\\d+(?:(?:\\.\\d*)?(?:[eE][+-]?\\d+)?)?\\b"
  51. }, {
  52. token : "constant.language.boolean",
  53. regex : "(?:true|false)\\b"
  54. }, {
  55. token : [
  56. "storage.type",
  57. "text",
  58. "entity.name.function"
  59. ],
  60. regex : "(function)(\\s+)(" + identifierRe + ")"
  61. }, {
  62. token : function(value) {
  63. if (value == "this")
  64. return "variable.language";
  65. else if (value == "function")
  66. return "storage.type";
  67. else if (keywords.hasOwnProperty(value) || reserved.hasOwnProperty(value))
  68. return "keyword";
  69. else if (buildinConstants.hasOwnProperty(value))
  70. return "constant.language";
  71. else if (/^_?[A-Z][a-zA-Z0-9_]*$/.test(value))
  72. return "language.support.class";
  73. else
  74. return "identifier";
  75. },
  76. // TODO: Unicode escape sequences
  77. // TODO: Unicode identifiers
  78. regex : identifierRe
  79. }, {
  80. token : "keyword.operator",
  81. regex : "!|%|&|\\*|\\-\\-|\\-|\\+\\+|\\+|~|==|=|!=|<=|>=|<<=|>>=|>>>=|<>|<|>|!|&&|\\|\\||\\?\\:|\\*=|%=|\\+=|\\-=|&=|\\^=|\\b(?:in|instanceof|new|delete|typeof|void)"
  82. }, {
  83. token : "punctuation.operator",
  84. regex : "\\?|\\:|\\,|\\;|\\."
  85. }, {
  86. token : "paren.lparen",
  87. regex : "[[({<]"
  88. }, {
  89. token : "paren.rparen",
  90. regex : "[\\])}>]"
  91. }, {
  92. token : "text",
  93. regex : "\\s+"
  94. }
  95. ],
  96. "comment" : [
  97. {
  98. token : "comment", // closing comment
  99. regex : ".*?\\*\\/",
  100. next : "start"
  101. }, {
  102. token : "comment", // comment spanning whole line
  103. regex : ".+"
  104. }
  105. ]
  106. };
  107. this.embedRules(DocCommentHighlightRules, "doc-",
  108. [ DocCommentHighlightRules.getEndRule("start") ]);
  109. };
  110. oop.inherits(JsxHighlightRules, TextHighlightRules);
  111. exports.JsxHighlightRules = JsxHighlightRules;
  112. });