prism-java.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. (function (Prism) {
  2. var keywords = /\b(?:abstract|assert|boolean|break|byte|case|catch|char|class|const|continue|default|do|double|else|enum|exports|extends|final|finally|float|for|goto|if|implements|import|instanceof|int|interface|long|module|native|new|non-sealed|null|open|opens|package|permits|private|protected|provides|public|record(?!\s*[(){}[\]<>=%~.:,;?+\-*/&|^])|requires|return|sealed|short|static|strictfp|super|switch|synchronized|this|throw|throws|to|transient|transitive|try|uses|var|void|volatile|while|with|yield)\b/;
  3. // full package (optional) + parent classes (optional)
  4. var classNamePrefix = /(?:[a-z]\w*\s*\.\s*)*(?:[A-Z]\w*\s*\.\s*)*/.source;
  5. // based on the java naming conventions
  6. var className = {
  7. pattern: RegExp(/(^|[^\w.])/.source + classNamePrefix + /[A-Z](?:[\d_A-Z]*[a-z]\w*)?\b/.source),
  8. lookbehind: true,
  9. inside: {
  10. 'namespace': {
  11. pattern: /^[a-z]\w*(?:\s*\.\s*[a-z]\w*)*(?:\s*\.)?/,
  12. inside: {
  13. 'punctuation': /\./
  14. }
  15. },
  16. 'punctuation': /\./
  17. }
  18. };
  19. Prism.languages.java = Prism.languages.extend('clike', {
  20. 'string': {
  21. pattern: /(^|[^\\])"(?:\\.|[^"\\\r\n])*"/,
  22. lookbehind: true,
  23. greedy: true
  24. },
  25. 'class-name': [
  26. className,
  27. {
  28. // variables, parameters, and constructor references
  29. // this to support class names (or generic parameters) which do not contain a lower case letter (also works for methods)
  30. pattern: RegExp(/(^|[^\w.])/.source + classNamePrefix + /[A-Z]\w*(?=\s+\w+\s*[;,=()]|\s*(?:\[[\s,]*\]\s*)?::\s*new\b)/.source),
  31. lookbehind: true,
  32. inside: className.inside
  33. },
  34. {
  35. // class names based on keyword
  36. // this to support class names (or generic parameters) which do not contain a lower case letter (also works for methods)
  37. pattern: RegExp(/(\b(?:class|enum|extends|implements|instanceof|interface|new|record|throws)\s+)/.source + classNamePrefix + /[A-Z]\w*\b/.source),
  38. lookbehind: true,
  39. inside: className.inside
  40. }
  41. ],
  42. 'keyword': keywords,
  43. 'function': [
  44. Prism.languages.clike.function,
  45. {
  46. pattern: /(::\s*)[a-z_]\w*/,
  47. lookbehind: true
  48. }
  49. ],
  50. 'number': /\b0b[01][01_]*L?\b|\b0x(?:\.[\da-f_p+-]+|[\da-f_]+(?:\.[\da-f_p+-]+)?)\b|(?:\b\d[\d_]*(?:\.[\d_]*)?|\B\.\d[\d_]*)(?:e[+-]?\d[\d_]*)?[dfl]?/i,
  51. 'operator': {
  52. pattern: /(^|[^.])(?:<<=?|>>>?=?|->|--|\+\+|&&|\|\||::|[?:~]|[-+*/%&|^!=<>]=?)/m,
  53. lookbehind: true
  54. },
  55. 'constant': /\b[A-Z][A-Z_\d]+\b/
  56. });
  57. Prism.languages.insertBefore('java', 'string', {
  58. 'triple-quoted-string': {
  59. // http://openjdk.java.net/jeps/355#Description
  60. pattern: /"""[ \t]*[\r\n](?:(?:"|"")?(?:\\.|[^"\\]))*"""/,
  61. greedy: true,
  62. alias: 'string'
  63. },
  64. 'char': {
  65. pattern: /'(?:\\.|[^'\\\r\n]){1,6}'/,
  66. greedy: true
  67. }
  68. });
  69. Prism.languages.insertBefore('java', 'class-name', {
  70. 'annotation': {
  71. pattern: /(^|[^.])@\w+(?:\s*\.\s*\w+)*/,
  72. lookbehind: true,
  73. alias: 'punctuation'
  74. },
  75. 'generics': {
  76. pattern: /<(?:[\w\s,.?]|&(?!&)|<(?:[\w\s,.?]|&(?!&)|<(?:[\w\s,.?]|&(?!&)|<(?:[\w\s,.?]|&(?!&))*>)*>)*>)*>/,
  77. inside: {
  78. 'class-name': className,
  79. 'keyword': keywords,
  80. 'punctuation': /[<>(),.:]/,
  81. 'operator': /[?&|]/
  82. }
  83. },
  84. 'import': [
  85. {
  86. pattern: RegExp(/(\bimport\s+)/.source + classNamePrefix + /(?:[A-Z]\w*|\*)(?=\s*;)/.source),
  87. lookbehind: true,
  88. inside: {
  89. 'namespace': className.inside.namespace,
  90. 'punctuation': /\./,
  91. 'operator': /\*/,
  92. 'class-name': /\w+/
  93. }
  94. },
  95. {
  96. pattern: RegExp(/(\bimport\s+static\s+)/.source + classNamePrefix + /(?:\w+|\*)(?=\s*;)/.source),
  97. lookbehind: true,
  98. alias: 'static',
  99. inside: {
  100. 'namespace': className.inside.namespace,
  101. 'static': /\b\w+$/,
  102. 'punctuation': /\./,
  103. 'operator': /\*/,
  104. 'class-name': /\w+/
  105. }
  106. }
  107. ],
  108. 'namespace': {
  109. pattern: RegExp(
  110. /(\b(?:exports|import(?:\s+static)?|module|open|opens|package|provides|requires|to|transitive|uses|with)\s+)(?!<keyword>)[a-z]\w*(?:\.[a-z]\w*)*\.?/
  111. .source.replace(/<keyword>/g, function () { return keywords.source; })),
  112. lookbehind: true,
  113. inside: {
  114. 'punctuation': /\./,
  115. }
  116. }
  117. });
  118. }(Prism));