java.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /**
  2. * @param {string} value
  3. * @returns {RegExp}
  4. * */
  5. /**
  6. * @param {RegExp | string } re
  7. * @returns {string}
  8. */
  9. function source(re) {
  10. if (!re) return null;
  11. if (typeof re === "string") return re;
  12. return re.source;
  13. }
  14. /**
  15. * @param {RegExp | string } re
  16. * @returns {string}
  17. */
  18. function optional(re) {
  19. return concat('(', re, ')?');
  20. }
  21. /**
  22. * @param {...(RegExp | string) } args
  23. * @returns {string}
  24. */
  25. function concat(...args) {
  26. const joined = args.map((x) => source(x)).join("");
  27. return joined;
  28. }
  29. /**
  30. * Any of the passed expresssions may match
  31. *
  32. * Creates a huge this | this | that | that match
  33. * @param {(RegExp | string)[] } args
  34. * @returns {string}
  35. */
  36. function either(...args) {
  37. const joined = '(' + args.map((x) => source(x)).join("|") + ")";
  38. return joined;
  39. }
  40. /*
  41. Language: Java
  42. Author: Vsevolod Solovyov <vsevolod.solovyov@gmail.com>
  43. Category: common, enterprise
  44. Website: https://www.java.com/
  45. */
  46. function java(hljs) {
  47. var JAVA_IDENT_RE = '[\u00C0-\u02B8a-zA-Z_$][\u00C0-\u02B8a-zA-Z_$0-9]*';
  48. var GENERIC_IDENT_RE = JAVA_IDENT_RE + '(<' + JAVA_IDENT_RE + '(\\s*,\\s*' + JAVA_IDENT_RE + ')*>)?';
  49. var KEYWORDS = 'false synchronized int abstract float private char boolean var static null if const ' +
  50. 'for true while long strictfp finally protected import native final void ' +
  51. 'enum else break transient catch instanceof byte super volatile case assert short ' +
  52. 'package default double public try this switch continue throws protected public private ' +
  53. 'module requires exports do';
  54. var ANNOTATION = {
  55. className: 'meta',
  56. begin: '@' + JAVA_IDENT_RE,
  57. contains: [
  58. {
  59. begin: /\(/,
  60. end: /\)/,
  61. contains: ["self"] // allow nested () inside our annotation
  62. },
  63. ]
  64. };
  65. /**
  66. * A given sequence, possibly with underscores
  67. * @type {(s: string | RegExp) => string} */
  68. var SEQUENCE_ALLOWING_UNDERSCORES = (seq) => concat('[', seq, ']+([', seq, '_]*[', seq, ']+)?');
  69. var JAVA_NUMBER_MODE = {
  70. className: 'number',
  71. variants: [
  72. { begin: `\\b(0[bB]${SEQUENCE_ALLOWING_UNDERSCORES('01')})[lL]?` }, // binary
  73. { begin: `\\b(0${SEQUENCE_ALLOWING_UNDERSCORES('0-7')})[dDfFlL]?` }, // octal
  74. {
  75. begin: concat(
  76. /\b0[xX]/,
  77. either(
  78. concat(SEQUENCE_ALLOWING_UNDERSCORES('a-fA-F0-9'), /\./, SEQUENCE_ALLOWING_UNDERSCORES('a-fA-F0-9')),
  79. concat(SEQUENCE_ALLOWING_UNDERSCORES('a-fA-F0-9'), /\.?/),
  80. concat(/\./, SEQUENCE_ALLOWING_UNDERSCORES('a-fA-F0-9'))
  81. ),
  82. /([pP][+-]?(\d+))?/,
  83. /[fFdDlL]?/ // decimal & fp mixed for simplicity
  84. )
  85. },
  86. // scientific notation
  87. { begin: concat(
  88. /\b/,
  89. either(
  90. concat(/\d*\./, SEQUENCE_ALLOWING_UNDERSCORES("\\d")), // .3, 3.3, 3.3_3
  91. SEQUENCE_ALLOWING_UNDERSCORES("\\d") // 3, 3_3
  92. ),
  93. /[eE][+-]?[\d]+[dDfF]?/)
  94. },
  95. // decimal & fp mixed for simplicity
  96. { begin: concat(
  97. /\b/,
  98. SEQUENCE_ALLOWING_UNDERSCORES(/\d/),
  99. optional(/\.?/),
  100. optional(SEQUENCE_ALLOWING_UNDERSCORES(/\d/)),
  101. /[dDfFlL]?/)
  102. }
  103. ],
  104. relevance: 0
  105. };
  106. return {
  107. name: 'Java',
  108. aliases: ['jsp'],
  109. keywords: KEYWORDS,
  110. illegal: /<\/|#/,
  111. contains: [
  112. hljs.COMMENT(
  113. '/\\*\\*',
  114. '\\*/',
  115. {
  116. relevance: 0,
  117. contains: [
  118. {
  119. // eat up @'s in emails to prevent them to be recognized as doctags
  120. begin: /\w+@/, relevance: 0
  121. },
  122. {
  123. className: 'doctag',
  124. begin: '@[A-Za-z]+'
  125. }
  126. ]
  127. }
  128. ),
  129. hljs.C_LINE_COMMENT_MODE,
  130. hljs.C_BLOCK_COMMENT_MODE,
  131. hljs.APOS_STRING_MODE,
  132. hljs.QUOTE_STRING_MODE,
  133. {
  134. className: 'class',
  135. beginKeywords: 'class interface', end: /[{;=]/, excludeEnd: true,
  136. keywords: 'class interface',
  137. illegal: /[:"\[\]]/,
  138. contains: [
  139. { beginKeywords: 'extends implements' },
  140. hljs.UNDERSCORE_TITLE_MODE
  141. ]
  142. },
  143. {
  144. // Expression keywords prevent 'keyword Name(...)' from being
  145. // recognized as a function definition
  146. beginKeywords: 'new throw return else',
  147. relevance: 0
  148. },
  149. {
  150. className: 'function',
  151. begin: '(' + GENERIC_IDENT_RE + '\\s+)+' + hljs.UNDERSCORE_IDENT_RE + '\\s*\\(', returnBegin: true, end: /[{;=]/,
  152. excludeEnd: true,
  153. keywords: KEYWORDS,
  154. contains: [
  155. {
  156. begin: hljs.UNDERSCORE_IDENT_RE + '\\s*\\(', returnBegin: true,
  157. relevance: 0,
  158. contains: [hljs.UNDERSCORE_TITLE_MODE]
  159. },
  160. {
  161. className: 'params',
  162. begin: /\(/, end: /\)/,
  163. keywords: KEYWORDS,
  164. relevance: 0,
  165. contains: [
  166. ANNOTATION,
  167. hljs.APOS_STRING_MODE,
  168. hljs.QUOTE_STRING_MODE,
  169. hljs.C_NUMBER_MODE,
  170. hljs.C_BLOCK_COMMENT_MODE
  171. ]
  172. },
  173. hljs.C_LINE_COMMENT_MODE,
  174. hljs.C_BLOCK_COMMENT_MODE
  175. ]
  176. },
  177. JAVA_NUMBER_MODE,
  178. ANNOTATION
  179. ]
  180. };
  181. }
  182. module.exports = java;