Skip to content

Commit 0e9fb35

Browse files
authored
(chore) Removes beforeMatch (#3137)
* removes beforeMatch compiler "sugar" in favor of multi-match
1 parent 8358762 commit 0e9fb35

File tree

7 files changed

+58
-50
lines changed

7 files changed

+58
-50
lines changed

docs/mode-reference.rst

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -250,29 +250,6 @@ This callback is triggered the moment an end match is detected. ``matchData`` in
250250
For an example of usage see ``END_SAME_AS_BEGIN`` in ``modes.js``.
251251

252252

253-
beforeMatch
254-
^^^^^^^^^^^
255-
256-
- **type**: string
257-
258-
Used to qualify a match by the content that immediately precedes it. This is syntactic sugar that generates a much more complex mode that first matches the entire sequence (using look-ahead), then glosses over the ``beforeMatch`` portion and ``starts`` a new rule to handle the actual match.
259-
260-
Notes:
261-
262-
- Any ``keywords`` specified are applied to the ``beforeMatch`` text as well (as shown in the example below)
263-
- Do not get this confused with any type of look-behind. We are always looking forward.
264-
- This is incompatible with ``starts``.
265-
266-
::
267-
268-
{
269-
beforeMatch: /\b(enum|class|struct|union)\s+/,
270-
keywords: "enum class struct union",
271-
begin: /\w+/,
272-
className: "title.class"
273-
}
274-
275-
276253
beginKeywords
277254
^^^^^^^^^^^^^
278255

src/languages/actionscript.js

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -96,16 +96,26 @@ export default function(hljs) {
9696
hljs.C_BLOCK_COMMENT_MODE,
9797
hljs.C_NUMBER_MODE,
9898
{
99-
beforeMatch: /\b(package)\s+/,
100-
keywords: "package",
101-
match: PKG_NAME_RE,
102-
className: "title.class"
99+
match: [
100+
/\bpackage/,
101+
/\s+/,
102+
PKG_NAME_RE
103+
],
104+
className: {
105+
1: "keyword",
106+
3: "title.class"
107+
}
103108
},
104109
{
105-
beforeMatch: /\b(class|interface|extends|implements)\s+/,
106-
keywords: "class interface extends implements",
107-
match: IDENT_RE,
108-
className: "title.class"
110+
match: [
111+
/\b(?:class|interface|extends|implements)/,
112+
/\s+/,
113+
IDENT_RE
114+
],
115+
className: {
116+
1: "keyword",
117+
3: "title.class"
118+
}
109119
},
110120
{
111121
className: 'meta',

src/languages/cpp.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -403,10 +403,16 @@ export default function(hljs) {
403403
keywords: CPP_KEYWORDS
404404
},
405405
{
406-
beforeMatch: /\b(enum|class|struct|union)\s+/,
407-
keywords: "enum class struct union",
408-
match: /\w+/,
409-
className: "title.class"
406+
match: [
407+
// extra complexity to deal with `enum class` and `enum struct`
408+
/\b(?:enum(?:\s+(?:class|struct))?|class|struct|union)/,
409+
/\s+/,
410+
/\w+/
411+
],
412+
className: {
413+
1: "keyword",
414+
3: "title.class"
415+
}
410416
}
411417
])
412418
};

src/languages/java.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,15 @@ export default function(hljs) {
163163
hljs.APOS_STRING_MODE,
164164
hljs.QUOTE_STRING_MODE,
165165
{
166-
beforeMatch: /\b(class|interface|enum|extends|implements|new)\s+/,
167-
keywords: "class interface enum extends implements new",
168-
match: JAVA_IDENT_RE,
169-
className: "title.class"
166+
match: [
167+
/\b(?:class|interface|enum|extends|implements|new)/,
168+
/\s+/,
169+
JAVA_IDENT_RE
170+
],
171+
className: {
172+
1: "keyword",
173+
3: "title.class"
174+
}
170175
},
171176
{
172177
begin: [

src/languages/r.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -132,25 +132,35 @@ export default function(hljs) {
132132
],
133133
},
134134
{
135-
className: 'number',
136135
relevance: 0,
137-
beforeMatch: /([^a-zA-Z0-9._])/, // not part of an identifier
136+
className: {
137+
2: "number"
138+
},
138139
variants: [
139140
// TODO: replace with negative look-behind when available
140141
// { begin: /(?<![a-zA-Z0-9._])0[xX][0-9a-fA-F]+\.[0-9a-fA-F]*[pP][+-]?\d+i?/ },
141142
// { begin: /(?<![a-zA-Z0-9._])0[xX][0-9a-fA-F]+([pP][+-]?\d+)?[Li]?/ },
142143
// { begin: /(?<![a-zA-Z0-9._])(\d+(\.\d*)?|\.\d+)([eE][+-]?\d+)?[Li]?/ }
143144
{
144145
// Special case: only hexadecimal binary powers can contain fractions.
145-
match: /0[xX][0-9a-fA-F]+\.[0-9a-fA-F]*[pP][+-]?\d+i?/,
146+
match: [
147+
/[^a-zA-Z0-9._]/, // not part of an identifier
148+
/0[xX][0-9a-fA-F]+\.[0-9a-fA-F]*[pP][+-]?\d+i?/
149+
]
146150
},
147151
{
148-
match: /0[xX][0-9a-fA-F]+([pP][+-]?\d+)?[Li]?/
152+
match: [
153+
/[^a-zA-Z0-9._]/, // not part of an identifier
154+
/0[xX][0-9a-fA-F]+(?:[pP][+-]?\d+)?[Li]?/
155+
]
149156
},
150157
{
151-
match: /(\d+(\.\d*)?|\.\d+)([eE][+-]?\d+)?[Li]?/,
158+
match: [
159+
/[^a-zA-Z0-9._]/, // not part of an identifier
160+
/(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?[Li]?/
161+
]
152162
}
153-
],
163+
]
154164
},
155165
{
156166
// infix operator

src/languages/vbnet.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,15 +113,15 @@ export default function(hljs) {
113113
begin: /'/
114114
},
115115
{
116-
// TODO: Use `beforeMatch:` for leading spaces
116+
// TODO: Use multi-class for leading spaces
117117
begin: /([\t ]|^)REM(?=\s)/
118118
}
119119
]
120120
});
121121

122122
const DIRECTIVES = {
123123
className: 'meta',
124-
// TODO: Use `beforeMatch:` for indentation once available
124+
// TODO: Use multi-class for indentation once available
125125
begin: /[\t ]*#(const|disable|else|elseif|enable|end|externalsource|if|region)\b/,
126126
end: /$/,
127127
keywords: {

test/markup/cpp/template_complexity.expect.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ std::wostream &amp;<span class="hljs-keyword">operator</span> &lt;&lt;(std::wost
1313
<span class="hljs-keyword">return</span> stream &lt;&lt; <span class="hljs-keyword">static_cast</span>&lt;std::wstring_view&gt;(thing);
1414
}
1515

16-
<span class="hljs-keyword">enum</span> <span class="hljs-title hljs-class"><span class="hljs-keyword">struct</span></span> DataHolder { };
17-
<span class="hljs-keyword">enum</span> <span class="hljs-title hljs-class"><span class="hljs-keyword">class</span></span> DataThingy { };
18-
<span class="hljs-keyword">enum</span> <span class="hljs-title hljs-class"><span class="hljs-keyword">class</span></span> Boolean : <span class="hljs-keyword">char</span> {
16+
<span class="hljs-keyword">enum struct</span> <span class="hljs-title hljs-class">DataHolder</span> { };
17+
<span class="hljs-keyword">enum class</span> <span class="hljs-title hljs-class">DataThingy</span> { };
18+
<span class="hljs-keyword">enum class</span> <span class="hljs-title hljs-class">Boolean</span> : <span class="hljs-keyword">char</span> {
1919
True, False, FileNotFound
2020
};
2121

0 commit comments

Comments
 (0)