scala.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. Language: Scala
  3. Category: functional
  4. Author: Jan Berkel <jan.berkel@gmail.com>
  5. Contributors: Erik Osheim <d_m@plastic-idolatry.com>
  6. Website: https://www.scala-lang.org
  7. */
  8. function scala(hljs) {
  9. const regex = hljs.regex;
  10. const ANNOTATION = {
  11. className: 'meta',
  12. begin: '@[A-Za-z]+'
  13. };
  14. // used in strings for escaping/interpolation/substitution
  15. const SUBST = {
  16. className: 'subst',
  17. variants: [
  18. { begin: '\\$[A-Za-z0-9_]+' },
  19. {
  20. begin: /\$\{/,
  21. end: /\}/
  22. }
  23. ]
  24. };
  25. const STRING = {
  26. className: 'string',
  27. variants: [
  28. {
  29. begin: '"""',
  30. end: '"""'
  31. },
  32. {
  33. begin: '"',
  34. end: '"',
  35. illegal: '\\n',
  36. contains: [ hljs.BACKSLASH_ESCAPE ]
  37. },
  38. {
  39. begin: '[a-z]+"',
  40. end: '"',
  41. illegal: '\\n',
  42. contains: [
  43. hljs.BACKSLASH_ESCAPE,
  44. SUBST
  45. ]
  46. },
  47. {
  48. className: 'string',
  49. begin: '[a-z]+"""',
  50. end: '"""',
  51. contains: [ SUBST ],
  52. relevance: 10
  53. }
  54. ]
  55. };
  56. const TYPE = {
  57. className: 'type',
  58. begin: '\\b[A-Z][A-Za-z0-9_]*',
  59. relevance: 0
  60. };
  61. const NAME = {
  62. className: 'title',
  63. begin: /[^0-9\n\t "'(),.`{}\[\]:;][^\n\t "'(),.`{}\[\]:;]+|[^0-9\n\t "'(),.`{}\[\]:;=]/,
  64. relevance: 0
  65. };
  66. const CLASS = {
  67. className: 'class',
  68. beginKeywords: 'class object trait type',
  69. end: /[:={\[\n;]/,
  70. excludeEnd: true,
  71. contains: [
  72. hljs.C_LINE_COMMENT_MODE,
  73. hljs.C_BLOCK_COMMENT_MODE,
  74. {
  75. beginKeywords: 'extends with',
  76. relevance: 10
  77. },
  78. {
  79. begin: /\[/,
  80. end: /\]/,
  81. excludeBegin: true,
  82. excludeEnd: true,
  83. relevance: 0,
  84. contains: [ TYPE ]
  85. },
  86. {
  87. className: 'params',
  88. begin: /\(/,
  89. end: /\)/,
  90. excludeBegin: true,
  91. excludeEnd: true,
  92. relevance: 0,
  93. contains: [ TYPE ]
  94. },
  95. NAME
  96. ]
  97. };
  98. const METHOD = {
  99. className: 'function',
  100. beginKeywords: 'def',
  101. end: regex.lookahead(/[:={\[(\n;]/),
  102. contains: [ NAME ]
  103. };
  104. const EXTENSION = {
  105. begin: [
  106. /^\s*/, // Is first token on the line
  107. 'extension',
  108. /\s+(?=[[(])/, // followed by at least one space and `[` or `(`
  109. ],
  110. beginScope: { 2: "keyword", }
  111. };
  112. const END = {
  113. begin: [
  114. /^\s*/, // Is first token on the line
  115. /end/,
  116. /\s+/,
  117. /(extension\b)?/, // `extension` is the only marker that follows an `end` that cannot be captured by another rule.
  118. ],
  119. beginScope: {
  120. 2: "keyword",
  121. 4: "keyword",
  122. }
  123. };
  124. // TODO: use negative look-behind in future
  125. // /(?<!\.)\binline(?=\s)/
  126. const INLINE_MODES = [
  127. { match: /\.inline\b/ },
  128. {
  129. begin: /\binline(?=\s)/,
  130. keywords: 'inline'
  131. }
  132. ];
  133. const USING_PARAM_CLAUSE = {
  134. begin: [
  135. /\(\s*/, // Opening `(` of a parameter or argument list
  136. /using/,
  137. /\s+(?!\))/, // Spaces not followed by `)`
  138. ],
  139. beginScope: { 2: "keyword", }
  140. };
  141. return {
  142. name: 'Scala',
  143. keywords: {
  144. literal: 'true false null',
  145. keyword: 'type yield lazy override def with val var sealed abstract private trait object if then forSome for while do throw finally protected extends import final return else break new catch super class case package default try this match continue throws implicit export enum given transparent'
  146. },
  147. contains: [
  148. hljs.C_LINE_COMMENT_MODE,
  149. hljs.C_BLOCK_COMMENT_MODE,
  150. STRING,
  151. TYPE,
  152. METHOD,
  153. CLASS,
  154. hljs.C_NUMBER_MODE,
  155. EXTENSION,
  156. END,
  157. ...INLINE_MODES,
  158. USING_PARAM_CLAUSE,
  159. ANNOTATION
  160. ]
  161. };
  162. }
  163. export { scala as default };