Skip to content

Commit d794611

Browse files
committed
Work in progress:
- begin gets the general lexeme regexp - terminators get the general lexeme regexp - subMode checks if the found lexeme is indeed a known keyword - problem: if lexeme is found it prevents more specific subMode begins from working even if it's false positive (i.e., not a known lexeme)
1 parent 4bb70ad commit d794611

File tree

3 files changed

+29
-22
lines changed

3 files changed

+29
-22
lines changed

src/highlight.js

+21-15
Original file line numberDiff line numberDiff line change
@@ -170,34 +170,39 @@ function() {
170170
return;
171171
mode.compiled = true;
172172

173-
mode.keywords = mode.keywords || mode.beginKeywords;
174-
if (mode.keywords) {
175-
var compiled_keywords = {};
173+
function compileKeywords(keywords) {
174+
var result = {};
176175

177176
function flatten(className, str) {
178177
if (language.case_insensitive) {
179178
str = str.toLowerCase();
180179
}
181180
str.split(' ').forEach(function(kw) {
182181
var pair = kw.split('|');
183-
compiled_keywords[pair[0]] = [className, pair[1] ? Number(pair[1]) : 1];
182+
result[pair[0]] = [className, pair[1] ? Number(pair[1]) : 1];
184183
});
185184
}
186185

187-
mode.lexemesRe = langRe(mode.lexemes || /[A-Za-z0-9_\.]+/, true);
188-
if (typeof mode.keywords == 'string') { // string
189-
flatten('keyword', mode.keywords);
186+
if (typeof keywords == 'string') { // string
187+
flatten('keyword', keywords);
190188
} else {
191-
Object.keys(mode.keywords).forEach(function (className) {
192-
flatten(className, mode.keywords[className]);
189+
Object.keys(keywords).forEach(function (className) {
190+
flatten(className, keywords[className]);
193191
});
194192
}
195-
mode.keywords = compiled_keywords;
193+
return result;
194+
}
195+
196+
mode.lexemesRe = langRe(mode.lexemes || /[A-Za-z0-9_\.]+/, true);
197+
mode.keywords = mode.keywords || mode.beginKeywords;
198+
if (mode.keywords) {
199+
mode.keywords = compileKeywords(mode.keywords);
196200
}
197201

198202
if (parent) {
199203
if (mode.beginKeywords) {
200-
mode.begin = '\\b(' + mode.beginKeywords.split(/\s+/).join('|') + ')\\b(?!\\.)\\s*';
204+
mode.beginKeywords = compileKeywords(mode.beginKeywords);
205+
mode.begin = mode.lexemes || /[A-Za-z0-9_\.]+/;
201206
}
202207
mode.beginRe = langRe(mode.begin ? mode.begin : '\\B|\\b');
203208
if (!mode.end && !mode.endsWithParent)
@@ -231,9 +236,9 @@ function() {
231236
}
232237

233238
var terminators = [];
234-
for (var i = 0; i < mode.contains.length; i++) {
235-
terminators.push(reStr(mode.contains[i].begin));
236-
}
239+
mode.contains.forEach(function(c) {
240+
terminators.push(reStr(c.begin));
241+
});
237242
if (mode.terminator_end) {
238243
terminators.push(reStr(mode.terminator_end));
239244
}
@@ -261,7 +266,7 @@ function() {
261266

262267
function subMode(lexeme, mode) {
263268
for (var i = 0; i < mode.contains.length; i++) {
264-
if (testRe(mode.contains[i].beginRe, lexeme)) {
269+
if (testRe(mode.contains[i].beginRe, lexeme) && (!mode.contains[i].beginKeywords || mode.contains[i].beginKeywords.hasOwnProperty(lexeme))) {
265270
return mode.contains[i];
266271
}
267272
}
@@ -356,6 +361,7 @@ function() {
356361
}
357362

358363
function processLexeme(buffer, lexeme) {
364+
359365
mode_buffer += buffer;
360366
if (lexeme === undefined) {
361367
result += processBuffer();

src/languages/java.js

+7-6
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,16 @@ function(hljs) {
3535
begin: '\\.' + hljs.UNDERSCORE_IDENT_RE, relevance: 0
3636
},
3737
{
38+
className: 'container',
3839
beginKeywords: 'protected public private', end: /[{;=]/,
3940
keywords: KEYWORDS,
4041
contains: [
42+
{
43+
begin: hljs.UNDERSCORE_IDENT_RE + '\\s*\\(', returnBegin: true,
44+
contains: [
45+
hljs.UNDERSCORE_TITLE_MODE
46+
]
47+
},
4148
{
4249
className: 'class',
4350
beginKeywords: 'class interface', endsWithParent: true,
@@ -49,12 +56,6 @@ function(hljs) {
4956
},
5057
hljs.UNDERSCORE_TITLE_MODE
5158
]
52-
},
53-
{
54-
begin: hljs.UNDERSCORE_IDENT_RE + '\\s*\\(', returnBegin: true,
55-
contains: [
56-
hljs.UNDERSCORE_TITLE_MODE
57-
]
5859
}
5960
]
6061
},

src/test.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -776,7 +776,7 @@ <h2>Automatically detected languages</h2>
776776
<pre><code>function $initHighlight(block, flags) {
777777
try {
778778
if (block.className.search(/\bno\-highlight\b/) != -1)
779-
return processBlock(block, true, 0x0F) + ' class=""';
779+
return processBlock(block.function, true, 0x0F) + ' class=""';
780780
} catch (e) {
781781
/* handle exception */
782782
var e4x =

0 commit comments

Comments
 (0)