objectivec.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. Language: Objective-C
  3. Author: Valerii Hiora <valerii.hiora@gmail.com>
  4. Contributors: Angel G. Olloqui <angelgarcia.mail@gmail.com>, Matt Diephouse <matt@diephouse.com>, Andrew Farmer <ahfarmer@gmail.com>, Minh Nguyễn <mxn@1ec5.org>
  5. Website: https://developer.apple.com/documentation/objectivec
  6. Category: common
  7. */
  8. function objectivec(hljs) {
  9. const API_CLASS = {
  10. className: 'built_in',
  11. begin: '\\b(AV|CA|CF|CG|CI|CL|CM|CN|CT|MK|MP|MTK|MTL|NS|SCN|SK|UI|WK|XC)\\w+'
  12. };
  13. const IDENTIFIER_RE = /[a-zA-Z@][a-zA-Z0-9_]*/;
  14. const TYPES = [
  15. "int",
  16. "float",
  17. "char",
  18. "unsigned",
  19. "signed",
  20. "short",
  21. "long",
  22. "double",
  23. "wchar_t",
  24. "unichar",
  25. "void",
  26. "bool",
  27. "BOOL",
  28. "id|0",
  29. "_Bool"
  30. ];
  31. const KWS = [
  32. "while",
  33. "export",
  34. "sizeof",
  35. "typedef",
  36. "const",
  37. "struct",
  38. "for",
  39. "union",
  40. "volatile",
  41. "static",
  42. "mutable",
  43. "if",
  44. "do",
  45. "return",
  46. "goto",
  47. "enum",
  48. "else",
  49. "break",
  50. "extern",
  51. "asm",
  52. "case",
  53. "default",
  54. "register",
  55. "explicit",
  56. "typename",
  57. "switch",
  58. "continue",
  59. "inline",
  60. "readonly",
  61. "assign",
  62. "readwrite",
  63. "self",
  64. "@synchronized",
  65. "id",
  66. "typeof",
  67. "nonatomic",
  68. "IBOutlet",
  69. "IBAction",
  70. "strong",
  71. "weak",
  72. "copy",
  73. "in",
  74. "out",
  75. "inout",
  76. "bycopy",
  77. "byref",
  78. "oneway",
  79. "__strong",
  80. "__weak",
  81. "__block",
  82. "__autoreleasing",
  83. "@private",
  84. "@protected",
  85. "@public",
  86. "@try",
  87. "@property",
  88. "@end",
  89. "@throw",
  90. "@catch",
  91. "@finally",
  92. "@autoreleasepool",
  93. "@synthesize",
  94. "@dynamic",
  95. "@selector",
  96. "@optional",
  97. "@required",
  98. "@encode",
  99. "@package",
  100. "@import",
  101. "@defs",
  102. "@compatibility_alias",
  103. "__bridge",
  104. "__bridge_transfer",
  105. "__bridge_retained",
  106. "__bridge_retain",
  107. "__covariant",
  108. "__contravariant",
  109. "__kindof",
  110. "_Nonnull",
  111. "_Nullable",
  112. "_Null_unspecified",
  113. "__FUNCTION__",
  114. "__PRETTY_FUNCTION__",
  115. "__attribute__",
  116. "getter",
  117. "setter",
  118. "retain",
  119. "unsafe_unretained",
  120. "nonnull",
  121. "nullable",
  122. "null_unspecified",
  123. "null_resettable",
  124. "class",
  125. "instancetype",
  126. "NS_DESIGNATED_INITIALIZER",
  127. "NS_UNAVAILABLE",
  128. "NS_REQUIRES_SUPER",
  129. "NS_RETURNS_INNER_POINTER",
  130. "NS_INLINE",
  131. "NS_AVAILABLE",
  132. "NS_DEPRECATED",
  133. "NS_ENUM",
  134. "NS_OPTIONS",
  135. "NS_SWIFT_UNAVAILABLE",
  136. "NS_ASSUME_NONNULL_BEGIN",
  137. "NS_ASSUME_NONNULL_END",
  138. "NS_REFINED_FOR_SWIFT",
  139. "NS_SWIFT_NAME",
  140. "NS_SWIFT_NOTHROW",
  141. "NS_DURING",
  142. "NS_HANDLER",
  143. "NS_ENDHANDLER",
  144. "NS_VALUERETURN",
  145. "NS_VOIDRETURN"
  146. ];
  147. const LITERALS = [
  148. "false",
  149. "true",
  150. "FALSE",
  151. "TRUE",
  152. "nil",
  153. "YES",
  154. "NO",
  155. "NULL"
  156. ];
  157. const BUILT_INS = [
  158. "dispatch_once_t",
  159. "dispatch_queue_t",
  160. "dispatch_sync",
  161. "dispatch_async",
  162. "dispatch_once"
  163. ];
  164. const KEYWORDS = {
  165. "variable.language": [
  166. "this",
  167. "super"
  168. ],
  169. $pattern: IDENTIFIER_RE,
  170. keyword: KWS,
  171. literal: LITERALS,
  172. built_in: BUILT_INS,
  173. type: TYPES
  174. };
  175. const CLASS_KEYWORDS = {
  176. $pattern: IDENTIFIER_RE,
  177. keyword: [
  178. "@interface",
  179. "@class",
  180. "@protocol",
  181. "@implementation"
  182. ]
  183. };
  184. return {
  185. name: 'Objective-C',
  186. aliases: [
  187. 'mm',
  188. 'objc',
  189. 'obj-c',
  190. 'obj-c++',
  191. 'objective-c++'
  192. ],
  193. keywords: KEYWORDS,
  194. illegal: '</',
  195. contains: [
  196. API_CLASS,
  197. hljs.C_LINE_COMMENT_MODE,
  198. hljs.C_BLOCK_COMMENT_MODE,
  199. hljs.C_NUMBER_MODE,
  200. hljs.QUOTE_STRING_MODE,
  201. hljs.APOS_STRING_MODE,
  202. {
  203. className: 'string',
  204. variants: [
  205. {
  206. begin: '@"',
  207. end: '"',
  208. illegal: '\\n',
  209. contains: [ hljs.BACKSLASH_ESCAPE ]
  210. }
  211. ]
  212. },
  213. {
  214. className: 'meta',
  215. begin: /#\s*[a-z]+\b/,
  216. end: /$/,
  217. keywords: { keyword:
  218. 'if else elif endif define undef warning error line '
  219. + 'pragma ifdef ifndef include' },
  220. contains: [
  221. {
  222. begin: /\\\n/,
  223. relevance: 0
  224. },
  225. hljs.inherit(hljs.QUOTE_STRING_MODE, { className: 'string' }),
  226. {
  227. className: 'string',
  228. begin: /<.*?>/,
  229. end: /$/,
  230. illegal: '\\n'
  231. },
  232. hljs.C_LINE_COMMENT_MODE,
  233. hljs.C_BLOCK_COMMENT_MODE
  234. ]
  235. },
  236. {
  237. className: 'class',
  238. begin: '(' + CLASS_KEYWORDS.keyword.join('|') + ')\\b',
  239. end: /(\{|$)/,
  240. excludeEnd: true,
  241. keywords: CLASS_KEYWORDS,
  242. contains: [ hljs.UNDERSCORE_TITLE_MODE ]
  243. },
  244. {
  245. begin: '\\.' + hljs.UNDERSCORE_IDENT_RE,
  246. relevance: 0
  247. }
  248. ]
  249. };
  250. }
  251. module.exports = objectivec;