java.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. // https://docs.oracle.com/javase/specs/jls/se15/html/jls-3.html#jls-3.10
  2. var decimalDigits = '[0-9](_*[0-9])*';
  3. var frac = `\\.(${decimalDigits})`;
  4. var hexDigits = '[0-9a-fA-F](_*[0-9a-fA-F])*';
  5. var NUMERIC = {
  6. className: 'number',
  7. variants: [
  8. // DecimalFloatingPointLiteral
  9. // including ExponentPart
  10. { begin: `(\\b(${decimalDigits})((${frac})|\\.)?|(${frac}))` +
  11. `[eE][+-]?(${decimalDigits})[fFdD]?\\b` },
  12. // excluding ExponentPart
  13. { begin: `\\b(${decimalDigits})((${frac})[fFdD]?\\b|\\.([fFdD]\\b)?)` },
  14. { begin: `(${frac})[fFdD]?\\b` },
  15. { begin: `\\b(${decimalDigits})[fFdD]\\b` },
  16. // HexadecimalFloatingPointLiteral
  17. { begin: `\\b0[xX]((${hexDigits})\\.?|(${hexDigits})?\\.(${hexDigits}))` +
  18. `[pP][+-]?(${decimalDigits})[fFdD]?\\b` },
  19. // DecimalIntegerLiteral
  20. { begin: '\\b(0|[1-9](_*[0-9])*)[lL]?\\b' },
  21. // HexIntegerLiteral
  22. { begin: `\\b0[xX](${hexDigits})[lL]?\\b` },
  23. // OctalIntegerLiteral
  24. { begin: '\\b0(_*[0-7])*[lL]?\\b' },
  25. // BinaryIntegerLiteral
  26. { begin: '\\b0[bB][01](_*[01])*[lL]?\\b' },
  27. ],
  28. relevance: 0
  29. };
  30. /*
  31. Language: Java
  32. Author: Vsevolod Solovyov <vsevolod.solovyov@gmail.com>
  33. Category: common, enterprise
  34. Website: https://www.java.com/
  35. */
  36. function java(hljs) {
  37. var JAVA_IDENT_RE = '[\u00C0-\u02B8a-zA-Z_$][\u00C0-\u02B8a-zA-Z_$0-9]*';
  38. var GENERIC_IDENT_RE = JAVA_IDENT_RE + '(<' + JAVA_IDENT_RE + '(\\s*,\\s*' + JAVA_IDENT_RE + ')*>)?';
  39. var KEYWORDS = 'false synchronized int abstract float private char boolean var static null if const ' +
  40. 'for true while long strictfp finally protected import native final void ' +
  41. 'enum else break transient catch instanceof byte super volatile case assert short ' +
  42. 'package default double public try this switch continue throws protected public private ' +
  43. 'module requires exports do';
  44. var ANNOTATION = {
  45. className: 'meta',
  46. begin: '@' + JAVA_IDENT_RE,
  47. contains: [
  48. {
  49. begin: /\(/,
  50. end: /\)/,
  51. contains: ["self"] // allow nested () inside our annotation
  52. },
  53. ]
  54. };
  55. const NUMBER = NUMERIC;
  56. return {
  57. name: 'Java',
  58. aliases: ['jsp'],
  59. keywords: KEYWORDS,
  60. illegal: /<\/|#/,
  61. contains: [
  62. hljs.COMMENT(
  63. '/\\*\\*',
  64. '\\*/',
  65. {
  66. relevance: 0,
  67. contains: [
  68. {
  69. // eat up @'s in emails to prevent them to be recognized as doctags
  70. begin: /\w+@/, relevance: 0
  71. },
  72. {
  73. className: 'doctag',
  74. begin: '@[A-Za-z]+'
  75. }
  76. ]
  77. }
  78. ),
  79. // relevance boost
  80. {
  81. begin: /import java\.[a-z]+\./,
  82. keywords: "import",
  83. relevance: 2
  84. },
  85. hljs.C_LINE_COMMENT_MODE,
  86. hljs.C_BLOCK_COMMENT_MODE,
  87. hljs.APOS_STRING_MODE,
  88. hljs.QUOTE_STRING_MODE,
  89. {
  90. className: 'class',
  91. beginKeywords: 'class interface enum', end: /[{;=]/, excludeEnd: true,
  92. // TODO: can this be removed somehow?
  93. // an extra boost because Java is more popular than other languages with
  94. // this same syntax feature (this is just to preserve our tests passing
  95. // for now)
  96. relevance: 1,
  97. keywords: 'class interface enum',
  98. illegal: /[:"\[\]]/,
  99. contains: [
  100. { beginKeywords: 'extends implements' },
  101. hljs.UNDERSCORE_TITLE_MODE
  102. ]
  103. },
  104. {
  105. // Expression keywords prevent 'keyword Name(...)' from being
  106. // recognized as a function definition
  107. beginKeywords: 'new throw return else',
  108. relevance: 0
  109. },
  110. {
  111. className: 'class',
  112. begin: 'record\\s+' + hljs.UNDERSCORE_IDENT_RE + '\\s*\\(',
  113. returnBegin: true,
  114. excludeEnd: true,
  115. end: /[{;=]/,
  116. keywords: KEYWORDS,
  117. contains: [
  118. { beginKeywords: "record" },
  119. {
  120. begin: hljs.UNDERSCORE_IDENT_RE + '\\s*\\(',
  121. returnBegin: true,
  122. relevance: 0,
  123. contains: [hljs.UNDERSCORE_TITLE_MODE]
  124. },
  125. {
  126. className: 'params',
  127. begin: /\(/, end: /\)/,
  128. keywords: KEYWORDS,
  129. relevance: 0,
  130. contains: [
  131. hljs.C_BLOCK_COMMENT_MODE
  132. ]
  133. },
  134. hljs.C_LINE_COMMENT_MODE,
  135. hljs.C_BLOCK_COMMENT_MODE
  136. ]
  137. },
  138. {
  139. className: 'function',
  140. begin: '(' + GENERIC_IDENT_RE + '\\s+)+' + hljs.UNDERSCORE_IDENT_RE + '\\s*\\(', returnBegin: true, end: /[{;=]/,
  141. excludeEnd: true,
  142. keywords: KEYWORDS,
  143. contains: [
  144. {
  145. begin: hljs.UNDERSCORE_IDENT_RE + '\\s*\\(', returnBegin: true,
  146. relevance: 0,
  147. contains: [hljs.UNDERSCORE_TITLE_MODE]
  148. },
  149. {
  150. className: 'params',
  151. begin: /\(/, end: /\)/,
  152. keywords: KEYWORDS,
  153. relevance: 0,
  154. contains: [
  155. ANNOTATION,
  156. hljs.APOS_STRING_MODE,
  157. hljs.QUOTE_STRING_MODE,
  158. NUMBER,
  159. hljs.C_BLOCK_COMMENT_MODE
  160. ]
  161. },
  162. hljs.C_LINE_COMMENT_MODE,
  163. hljs.C_BLOCK_COMMENT_MODE
  164. ]
  165. },
  166. NUMBER,
  167. ANNOTATION
  168. ]
  169. };
  170. }
  171. module.exports = java;