Skip to content

Commit 9b4e0f3

Browse files
Remove lookbehinds from hljs grammar, close scala#11771
1 parent 0195dff commit 9b4e0f3

File tree

1 file changed

+88
-36
lines changed

1 file changed

+88
-36
lines changed

scaladoc/resources/dotty_res/scripts/hljs-scala3.js

Lines changed: 88 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
function highlightDotty(hljs) {
22

33
// identifiers
4-
const camelCaseId = /[a-z][$\w]*/
54
const capitalizedId = /\b[A-Z][$\w]*\b/
65
const alphaId = /[a-zA-Z$_][$\w]*/
7-
const op = /[^\s\w\d,"'()[\]{}]+/
8-
const id = new RegExp(`(${alphaId.source}((?<=_)${op.source})?|${op.source}|\`.*?\`)`)
6+
const op1 = /[^\s\w\d,;"'()[\]{}=:]/
7+
const op2 = /[^\s\w\d,;"'()[\]{}]/
8+
const id = new RegExp(`(${alphaId.source}(_${op2.source}+)?|${op2.source}{2,}|${op1.source}+|\`.+?\`)`)
99

1010
// numbers
1111
const hexDigit = '[a-fA-F0-9]'
@@ -19,26 +19,18 @@ function highlightDotty(hljs) {
1919
// Regular Keywords
2020
// The "soft" keywords (e.g. 'using') are added later where necessary
2121
const alwaysKeywords = {
22-
$pattern: /(\w+|\?=>|\?{1,3}|=>>|=>|<:|>:|_|<-|\.nn)/,
22+
$pattern: /(\w+|\?=>|\?{1,3}|=>>|=>|<:|>:|_|#|<-|\.nn)/,
2323
keyword:
2424
'abstract case catch class def do else enum export extends final finally for given '+
2525
'if implicit import lazy match new object package private protected override return '+
26-
'sealed then throw trait true try type val var while with yield =>> => ?=> <: >: _ ? <-',
26+
'sealed then throw trait true try type val var while with yield =>> => ?=> <: >: _ ? <- #',
2727
literal: 'true false null this super',
28-
built_in: '??? asInstanceOf isInstanceOf assert implicitly locally summon .nn'
28+
built_in: '??? asInstanceOf isInstanceOf assert implicitly locally summon valueOf .nn'
2929
}
3030
const modifiers = 'abstract|final|implicit|override|private|protected|sealed'
3131

3232
// End of class, enum, etc. header
33-
const templateDeclEnd = /(\/[/*]|{|: *\n|\n(?! *(extends|with|derives)))/
34-
35-
// name <title>
36-
function titleFor(name) {
37-
return {
38-
className: 'title',
39-
begin: `(?<=${name} )${id.source}`
40-
}
41-
}
33+
const templateDeclEnd = /(\/[/*]|{|:(?= *\n)|\n(?! *(extends|with|derives)))/
4234

4335
// all the keywords + soft keywords, separated by spaces
4436
function withSoftKeywords(kwd) {
@@ -50,6 +42,43 @@ function highlightDotty(hljs) {
5042
}
5143
}
5244

45+
// title inside of a complex token made of several parts (e.g. class)
46+
const TITLE = {
47+
className: 'title',
48+
begin: id,
49+
returnEnd: true,
50+
keywords: alwaysKeywords.keyword,
51+
literal: alwaysKeywords.literal,
52+
built_in: alwaysKeywords.built_in
53+
}
54+
55+
// title that goes to the end of a simple token (e.g. val)
56+
const TITLE2 = {
57+
className: 'title',
58+
begin: id,
59+
excludeEnd: true,
60+
endsWithParent: true
61+
}
62+
63+
const TYPED = {
64+
begin: /: (?=[a-zA-Z()?])/,
65+
end: /\/\/|\/\*|\n/,
66+
endsWithParent: true,
67+
returnEnd: true,
68+
contains: [
69+
{
70+
// works better than the usual way of defining keyword,
71+
// in this specific situation
72+
className: 'keyword',
73+
begin: /\?\=>|=>>|[=:][><]|\?/,
74+
},
75+
{
76+
className: 'type',
77+
begin: alphaId
78+
}
79+
]
80+
}
81+
5382
const PROBABLY_TYPE = {
5483
className: 'type',
5584
begin: capitalizedId,
@@ -62,6 +91,7 @@ function highlightDotty(hljs) {
6291
relevance: 0
6392
}
6493

94+
// type parameters within [square brackets]
6595
const TPARAMS = {
6696
begin: /\[/, end: /\]/,
6797
keywords: {
@@ -121,6 +151,7 @@ function highlightDotty(hljs) {
121151
]
122152
}
123153

154+
// "string" or """string""", with or without interpolation
124155
const STRING = {
125156
className: 'string',
126157
variants: [
@@ -206,66 +237,78 @@ function highlightDotty(hljs) {
206237
begin: /- (?=\S)/, end: /\s/,
207238
},
208239
{
209-
className: 'link',
210-
begin: /(?<=\[.*?\])\(/, end: /\)/,
240+
begin: /\[.*?\]\(/, end: /\)/,
241+
contains: [
242+
{
243+
// mark as "link" only the URL
244+
className: 'link',
245+
begin: /.*?/,
246+
endsWithParent: true
247+
}
248+
]
211249
}
212250
]
213251
})
214252

215253
// Methods
216254
const METHOD = {
217255
className: 'function',
218-
begin: `((${modifiers}|transparent|inline) +)*def`, end: / =|\n/,
256+
begin: `((${modifiers}|transparent|inline|infix) +)*def`, end: / =\s|\n/,
219257
excludeEnd: true,
220258
relevance: 5,
221-
keywords: withSoftKeywords('inline transparent'),
259+
keywords: withSoftKeywords('inline infix transparent'),
222260
contains: [
223261
hljs.C_LINE_COMMENT_MODE,
224262
hljs.C_BLOCK_COMMENT_MODE,
225-
titleFor('def'),
226263
TPARAMS,
227264
CTX_PARAMS,
228265
PARAMS,
229-
PROBABLY_TYPE
266+
TYPED, // prevents the ":" (declared type) to become a title
267+
PROBABLY_TYPE,
268+
TITLE
230269
]
231270
}
232271

233272
// Variables & Constants
234273
const VAL = {
235-
beginKeywords: 'val var', end: /[=:;\n]/,
274+
beginKeywords: 'val var', end: /[=:;\n/]/,
236275
excludeEnd: true,
237276
contains: [
238277
hljs.C_LINE_COMMENT_MODE,
239278
hljs.C_BLOCK_COMMENT_MODE,
240-
titleFor('(val|var)')
279+
TITLE2
241280
]
242281
}
243282

244283
// Type declarations
245284
const TYPEDEF = {
246285
className: 'typedef',
247-
begin: `((${modifiers}|opaque) +)*type`, end: /[=;\n]/,
286+
begin: `((${modifiers}|opaque) +)*type`, end: /[=;\n]| ?[<>]:/,
248287
excludeEnd: true,
249288
keywords: withSoftKeywords('opaque'),
250289
contains: [
251290
hljs.C_LINE_COMMENT_MODE,
252291
hljs.C_BLOCK_COMMENT_MODE,
253-
titleFor('type'),
254-
PROBABLY_TYPE
292+
PROBABLY_TYPE,
293+
TITLE2,
255294
]
256295
}
257296

258297
// Given instances
259298
const GIVEN = {
260-
begin: /given/, end: /[=;\n]/,
299+
begin: /given/, end: / =|[=;\n]/,
261300
excludeEnd: true,
262301
keywords: 'given using with',
263302
contains: [
264303
hljs.C_LINE_COMMENT_MODE,
265304
hljs.C_BLOCK_COMMENT_MODE,
266-
titleFor('given'),
267305
PARAMS,
268-
PROBABLY_TYPE
306+
{
307+
begin: 'as',
308+
keywords: 'as'
309+
},
310+
PROBABLY_TYPE,
311+
TITLE
269312
]
270313
}
271314

@@ -318,20 +361,32 @@ function highlightDotty(hljs) {
318361
className: 'class',
319362
begin: `((${modifiers}|open|case|transparent) +)*(class|trait|enum|object|package object)`, end: templateDeclEnd,
320363
keywords: withSoftKeywords('open transparent'),
364+
excludeEnd: true,
321365
contains: [
322366
hljs.C_LINE_COMMENT_MODE,
323367
hljs.C_BLOCK_COMMENT_MODE,
324-
titleFor('(class|trait|object|enum)'),
325368
TPARAMS,
326369
CTX_PARAMS,
327370
PARAMS,
328371
EXTENDS_PARENT,
329372
WITH_MIXIN,
330373
DERIVES_TYPECLASS,
374+
TITLE,
331375
PROBABLY_TYPE
332376
]
333377
}
334378

379+
// package declaration with a content
380+
const PACKAGE = {
381+
className: 'package',
382+
begin: /package (?=\w+ *[:{\n])/, end: /[:{\n]/,
383+
excludeEnd: true,
384+
keywords: alwaysKeywords,
385+
contains: [
386+
TITLE
387+
]
388+
}
389+
335390
// Case in enum
336391
const ENUM_CASE = {
337392
begin: /case (?!.*=>)/, end: /\n/,
@@ -340,22 +395,18 @@ function highlightDotty(hljs) {
340395
contains: [
341396
hljs.C_LINE_COMMENT_MODE,
342397
hljs.C_BLOCK_COMMENT_MODE,
343-
{
344-
// case A, B, C
345-
className: 'title',
346-
begin: `(?<=(case|,) *)${id.source}`
347-
},
348398
PARAMS,
349399
EXTENDS_PARENT,
350400
WITH_MIXIN,
351401
DERIVES_TYPECLASS,
402+
TITLE,
352403
PROBABLY_TYPE
353404
]
354405
}
355406

356407
// Case in pattern matching
357408
const MATCH_CASE = {
358-
begin: /case/, end: /=>/,
409+
begin: /case/, end: /=>|\n/,
359410
keywords: 'case',
360411
excludeEnd: true,
361412
contains: [
@@ -393,6 +444,7 @@ function highlightDotty(hljs) {
393444
METHOD,
394445
VAL,
395446
TYPEDEF,
447+
PACKAGE,
396448
CLASS,
397449
GIVEN,
398450
EXTENSION,

0 commit comments

Comments
 (0)