golang_highlight_rules.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. define(function(require, exports, module) {
  2. var oop = require("../lib/oop");
  3. var DocCommentHighlightRules = require("./doc_comment_highlight_rules").DocCommentHighlightRules;
  4. var TextHighlightRules = require("./text_highlight_rules").TextHighlightRules;
  5. var GolangHighlightRules = function() {
  6. var keywords = (
  7. "else|break|case|return|goto|if|const|select|" +
  8. "continue|struct|default|switch|for|range|" +
  9. "func|import|package|chan|defer|fallthrough|go|interface|map|range|" +
  10. "select|type|var"
  11. );
  12. var builtinTypes = (
  13. "string|uint8|uint16|uint32|uint64|int8|int16|int32|int64|float32|" +
  14. "float64|complex64|complex128|byte|rune|uint|int|uintptr|bool|error"
  15. );
  16. var builtinFunctions = (
  17. "new|close|cap|copy|panic|panicln|print|println|len|make|delete|real|recover|imag|append"
  18. );
  19. var builtinConstants = ("nil|true|false|iota");
  20. var keywordMapper = this.createKeywordMapper({
  21. "keyword": keywords,
  22. "constant.language": builtinConstants,
  23. "support.function": builtinFunctions,
  24. "support.type": builtinTypes
  25. }, "");
  26. var stringEscapeRe = "\\\\(?:[0-7]{3}|x\\h{2}|u{4}|U\\h{6}|[abfnrtv'\"\\\\])".replace(/\\h/g, "[a-fA-F\\d]");
  27. this.$rules = {
  28. "start" : [
  29. {
  30. token : "comment",
  31. regex : "\\/\\/.*$"
  32. },
  33. DocCommentHighlightRules.getStartRule("doc-start"),
  34. {
  35. token : "comment.start", // multi line comment
  36. regex : "\\/\\*",
  37. next : "comment"
  38. }, {
  39. token : "string", // single line
  40. regex : /"(?:[^"\\]|\\.)*?"/
  41. }, {
  42. token : "string", // raw
  43. regex : '[`](?:[^`]*)$',
  44. next : "bqstring"
  45. }, {
  46. token : "constant.numeric", // rune
  47. regex : "'(?:[^\\'\uD800-\uDBFF]|[\uD800-\uDBFF][\uDC00-\uDFFF]|" + stringEscapeRe.replace('"', '') + ")'"
  48. }, {
  49. token : "constant.numeric", // hex
  50. regex : "0[xX][0-9a-fA-F]+\\b"
  51. }, {
  52. token : "constant.numeric", // float
  53. regex : "[+-]?\\d+(?:(?:\\.\\d*)?(?:[eE][+-]?\\d+)?)?\\b"
  54. }, {
  55. token : ["keyword", "text", "entity.name.function"],
  56. regex : "(func)(\\s+)([a-zA-Z_$][a-zA-Z0-9_$]*)\\b"
  57. }, {
  58. token : function(val) {
  59. if (val[val.length - 1] == "(") {
  60. return [{
  61. type: keywordMapper(val.slice(0, -1)) || "support.function",
  62. value: val.slice(0, -1)
  63. }, {
  64. type: "paren.lparen",
  65. value: val.slice(-1)
  66. }];
  67. }
  68. return keywordMapper(val) || "identifier";
  69. },
  70. regex : "[a-zA-Z_$][a-zA-Z0-9_$]*\\b\\(?"
  71. }, {
  72. token : "keyword.operator",
  73. regex : "!|\\$|%|&|\\*|\\-\\-|\\-|\\+\\+|\\+|~|==|=|!=|<=|>=|<<=|>>=|>>>=|<>|<|>|!|&&|\\|\\||\\?\\:|\\*=|%=|\\+=|\\-=|&=|\\^="
  74. }, {
  75. token : "punctuation.operator",
  76. regex : "\\?|\\:|\\,|\\;|\\."
  77. }, {
  78. token : "paren.lparen",
  79. regex : "[[({]"
  80. }, {
  81. token : "paren.rparen",
  82. regex : "[\\])}]"
  83. }, {
  84. token : "text",
  85. regex : "\\s+"
  86. }
  87. ],
  88. "comment" : [
  89. {
  90. token : "comment.end",
  91. regex : "\\*\\/",
  92. next : "start"
  93. }, {
  94. defaultToken : "comment",
  95. }
  96. ],
  97. "bqstring" : [
  98. {
  99. token : "string",
  100. regex : '`',
  101. next : "start"
  102. }, {
  103. defaultToken : "string"
  104. }
  105. ]
  106. };
  107. this.embedRules(DocCommentHighlightRules, "doc-",
  108. [ DocCommentHighlightRules.getEndRule("start") ]);
  109. };
  110. oop.inherits(GolangHighlightRules, TextHighlightRules);
  111. exports.GolangHighlightRules = GolangHighlightRules;
  112. });