c_cpp_highlight_rules.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. define(function(require, exports, module) {
  2. "use strict";
  3. var oop = require("../lib/oop");
  4. var DocCommentHighlightRules = require("./doc_comment_highlight_rules").DocCommentHighlightRules;
  5. var TextHighlightRules = require("./text_highlight_rules").TextHighlightRules;
  6. // used by objective-c
  7. var cFunctions = exports.cFunctions = "\\s*\\bhypot(?:f|l)?|s(?:scanf|ystem|nprintf|ca(?:nf|lb(?:n(?:f|l)?|ln(?:f|l)?))|i(?:n(?:h(?:f|l)?|f|l)?|gn(?:al|bit))|tr(?:s(?:tr|pn)|nc(?:py|at|mp)|c(?:spn|hr|oll|py|at|mp)|to(?:imax|d|u(?:l(?:l)?|max)|k|f|l(?:d|l)?)|error|pbrk|ftime|len|rchr|xfrm)|printf|et(?:jmp|vbuf|locale|buf)|qrt(?:f|l)?|w(?:scanf|printf)|rand)|n(?:e(?:arbyint(?:f|l)?|xt(?:toward(?:f|l)?|after(?:f|l)?))|an(?:f|l)?)|c(?:s(?:in(?:h(?:f|l)?|f|l)?|qrt(?:f|l)?)|cos(?:h(?:f)?|f|l)?|imag(?:f|l)?|t(?:ime|an(?:h(?:f|l)?|f|l)?)|o(?:s(?:h(?:f|l)?|f|l)?|nj(?:f|l)?|pysign(?:f|l)?)|p(?:ow(?:f|l)?|roj(?:f|l)?)|e(?:il(?:f|l)?|xp(?:f|l)?)|l(?:o(?:ck|g(?:f|l)?)|earerr)|a(?:sin(?:h(?:f|l)?|f|l)?|cos(?:h(?:f|l)?|f|l)?|tan(?:h(?:f|l)?|f|l)?|lloc|rg(?:f|l)?|bs(?:f|l)?)|real(?:f|l)?|brt(?:f|l)?)|t(?:ime|o(?:upper|lower)|an(?:h(?:f|l)?|f|l)?|runc(?:f|l)?|gamma(?:f|l)?|mp(?:nam|file))|i(?:s(?:space|n(?:ormal|an)|cntrl|inf|digit|u(?:nordered|pper)|p(?:unct|rint)|finite|w(?:space|c(?:ntrl|type)|digit|upper|p(?:unct|rint)|lower|al(?:num|pha)|graph|xdigit|blank)|l(?:ower|ess(?:equal|greater)?)|al(?:num|pha)|gr(?:eater(?:equal)?|aph)|xdigit|blank)|logb(?:f|l)?|max(?:div|abs))|di(?:v|fftime)|_Exit|unget(?:c|wc)|p(?:ow(?:f|l)?|ut(?:s|c(?:har)?|wc(?:har)?)|error|rintf)|e(?:rf(?:c(?:f|l)?|f|l)?|x(?:it|p(?:2(?:f|l)?|f|l|m1(?:f|l)?)?))|v(?:s(?:scanf|nprintf|canf|printf|w(?:scanf|printf))|printf|f(?:scanf|printf|w(?:scanf|printf))|w(?:scanf|printf)|a_(?:start|copy|end|arg))|qsort|f(?:s(?:canf|e(?:tpos|ek))|close|tell|open|dim(?:f|l)?|p(?:classify|ut(?:s|c|w(?:s|c))|rintf)|e(?:holdexcept|set(?:e(?:nv|xceptflag)|round)|clearexcept|testexcept|of|updateenv|r(?:aiseexcept|ror)|get(?:e(?:nv|xceptflag)|round))|flush|w(?:scanf|ide|printf|rite)|loor(?:f|l)?|abs(?:f|l)?|get(?:s|c|pos|w(?:s|c))|re(?:open|e|ad|xp(?:f|l)?)|m(?:in(?:f|l)?|od(?:f|l)?|a(?:f|l|x(?:f|l)?)?))|l(?:d(?:iv|exp(?:f|l)?)|o(?:ngjmp|cal(?:time|econv)|g(?:1(?:p(?:f|l)?|0(?:f|l)?)|2(?:f|l)?|f|l|b(?:f|l)?)?)|abs|l(?:div|abs|r(?:int(?:f|l)?|ound(?:f|l)?))|r(?:int(?:f|l)?|ound(?:f|l)?)|gamma(?:f|l)?)|w(?:scanf|c(?:s(?:s(?:tr|pn)|nc(?:py|at|mp)|c(?:spn|hr|oll|py|at|mp)|to(?:imax|d|u(?:l(?:l)?|max)|k|f|l(?:d|l)?|mbs)|pbrk|ftime|len|r(?:chr|tombs)|xfrm)|to(?:b|mb)|rtomb)|printf|mem(?:set|c(?:hr|py|mp)|move))|a(?:s(?:sert|ctime|in(?:h(?:f|l)?|f|l)?)|cos(?:h(?:f|l)?|f|l)?|t(?:o(?:i|f|l(?:l)?)|exit|an(?:h(?:f|l)?|2(?:f|l)?|f|l)?)|b(?:s|ort))|g(?:et(?:s|c(?:har)?|env|wc(?:har)?)|mtime)|r(?:int(?:f|l)?|ound(?:f|l)?|e(?:name|alloc|wind|m(?:ove|quo(?:f|l)?|ainder(?:f|l)?))|a(?:nd|ise))|b(?:search|towc)|m(?:odf(?:f|l)?|em(?:set|c(?:hr|py|mp)|move)|ktime|alloc|b(?:s(?:init|towcs|rtowcs)|towc|len|r(?:towc|len)))\\b"
  8. var c_cppHighlightRules = function() {
  9. var keywordControls = (
  10. "break|case|continue|default|do|else|for|goto|if|_Pragma|" +
  11. "return|switch|while|catch|operator|try|throw|using"
  12. );
  13. var storageType = (
  14. "asm|__asm__|auto|bool|_Bool|char|_Complex|double|enum|float|" +
  15. "_Imaginary|int|long|short|signed|struct|typedef|union|unsigned|void|" +
  16. "class|wchar_t|template"
  17. );
  18. var storageModifiers = (
  19. "const|extern|register|restrict|static|volatile|inline|private:|" +
  20. "protected:|public:|friend|explicit|virtual|export|mutable|typename"
  21. );
  22. var keywordOperators = (
  23. "and|and_eq|bitand|bitor|compl|not|not_eq|or|or_eq|typeid|xor|xor_eq" +
  24. "const_cast|dynamic_cast|reinterpret_cast|static_cast|sizeof|namespace"
  25. );
  26. var builtinConstants = (
  27. "NULL|true|false|TRUE|FALSE"
  28. );
  29. var keywordMapper = this.$keywords = this.createKeywordMapper({
  30. "keyword.control" : keywordControls,
  31. "storage.type" : storageType,
  32. "storage.modifier" : storageModifiers,
  33. "keyword.operator" : keywordOperators,
  34. "variable.language": "this",
  35. "constant.language": builtinConstants
  36. }, "identifier");
  37. var identifierRe = "[a-zA-Z\\$_\u00a1-\uffff][a-zA-Z\d\\$_\u00a1-\uffff]*\\b";
  38. // regexp must not have capturing parentheses. Use (?:) instead.
  39. // regexps are ordered -> the first match is used
  40. this.$rules = {
  41. "start" : [
  42. {
  43. token : "comment",
  44. regex : "\\/\\/.*$"
  45. },
  46. DocCommentHighlightRules.getStartRule("doc-start"),
  47. {
  48. token : "comment", // multi line comment
  49. regex : "\\/\\*",
  50. next : "comment"
  51. }, {
  52. token : "string", // single line
  53. regex : '["](?:(?:\\\\.)|(?:[^"\\\\]))*?["]'
  54. }, {
  55. token : "string", // multi line string start
  56. regex : '["].*\\\\$',
  57. next : "qqstring"
  58. }, {
  59. token : "string", // single line
  60. regex : "['](?:(?:\\\\.)|(?:[^'\\\\]))*?[']"
  61. }, {
  62. token : "string", // multi line string start
  63. regex : "['].*\\\\$",
  64. next : "qstring"
  65. }, {
  66. token : "constant.numeric", // hex
  67. regex : "0[xX][0-9a-fA-F]+(L|l|UL|ul|u|U|F|f|ll|LL|ull|ULL)?\\b"
  68. }, {
  69. token : "constant.numeric", // float
  70. regex : "[+-]?\\d+(?:(?:\\.\\d*)?(?:[eE][+-]?\\d+)?)?(L|l|UL|ul|u|U|F|f|ll|LL|ull|ULL)?\\b"
  71. }, {
  72. token : "keyword", // pre-compiler directives
  73. regex : "#\\s*(?:include|import|pragma|line|define|undef|if|ifdef|else|elif|ifndef)\\b",
  74. next : "directive"
  75. }, {
  76. token : "keyword", // special case pre-compiler directive
  77. regex : "(?:#\\s*endif)\\b"
  78. }, {
  79. token : "support.function.C99.c",
  80. regex : cFunctions
  81. }, {
  82. token : keywordMapper,
  83. regex : "[a-zA-Z_$][a-zA-Z0-9_$]*\\b"
  84. }, {
  85. token : "keyword.operator",
  86. regex : "!|\\$|%|&|\\*|\\-\\-|\\-|\\+\\+|\\+|~|==|=|!=|<=|>=|<<=|>>=|>>>=|<>|<|>|!|&&|\\|\\||\\?\\:|\\*=|%=|\\+=|\\-=|&=|\\^=|\\b(?:in|new|delete|typeof|void)"
  87. }, {
  88. token : "punctuation.operator",
  89. regex : "\\?|\\:|\\,|\\;|\\."
  90. }, {
  91. token : "paren.lparen",
  92. regex : "[[({]"
  93. }, {
  94. token : "paren.rparen",
  95. regex : "[\\])}]"
  96. }, {
  97. token : "text",
  98. regex : "\\s+"
  99. }
  100. ],
  101. "comment" : [
  102. {
  103. token : "comment", // closing comment
  104. regex : ".*?\\*\\/",
  105. next : "start"
  106. }, {
  107. token : "comment", // comment spanning whole line
  108. regex : ".+"
  109. }
  110. ],
  111. "qqstring" : [
  112. {
  113. token : "string",
  114. regex : '(?:(?:\\\\.)|(?:[^"\\\\]))*?"',
  115. next : "start"
  116. }, {
  117. token : "string",
  118. regex : '.+'
  119. }
  120. ],
  121. "qstring" : [
  122. {
  123. token : "string",
  124. regex : "(?:(?:\\\\.)|(?:[^'\\\\]))*?'",
  125. next : "start"
  126. }, {
  127. token : "string",
  128. regex : '.+'
  129. }
  130. ],
  131. "directive" : [
  132. {
  133. token : "constant.other.multiline",
  134. regex : /\\/
  135. },
  136. {
  137. token : "constant.other.multiline",
  138. regex : /.*\\/
  139. },
  140. {
  141. token : "constant.other",
  142. regex : "\\s*<.+?>",
  143. next : "start"
  144. },
  145. {
  146. token : "constant.other", // single line
  147. regex : '\\s*["](?:(?:\\\\.)|(?:[^"\\\\]))*?["]',
  148. next : "start"
  149. },
  150. {
  151. token : "constant.other", // single line
  152. regex : "\\s*['](?:(?:\\\\.)|(?:[^'\\\\]))*?[']",
  153. next : "start"
  154. },
  155. // "\" implies multiline, while "/" implies comment
  156. {
  157. token : "constant.other",
  158. regex : /[^\\\/]+/,
  159. next : "start"
  160. }
  161. ]
  162. };
  163. this.embedRules(DocCommentHighlightRules, "doc-",
  164. [ DocCommentHighlightRules.getEndRule("start") ]);
  165. };
  166. oop.inherits(c_cppHighlightRules, TextHighlightRules);
  167. exports.c_cppHighlightRules = c_cppHighlightRules;
  168. });