Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enh(parser) use negative look-ahead for beginKeywords #2813

Merged
merged 5 commits into from Nov 13, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGES.md
@@ -1,5 +1,9 @@
## Version 10.4.0 (work in process)

Parser:

- enh(parser) use negative look-ahead for `beginKeywords` support (#2813) [Josh Goebel][]

Language Improvements:

- enh(julia) Update keyword lists for Julia 1.x (#2781) [Fredrik Ekre][]
Expand Down
11 changes: 5 additions & 6 deletions src/lib/mode_compiler.js
Expand Up @@ -240,18 +240,17 @@ export function compileLanguage(language) {

// TODO: We need negative look-behind support to do this properly
/**
* Skip a match if it has a preceding or trailing dot
* Skip a match if it has a preceding dot
*
* This is used for `beginKeywords` to prevent matching expressions such as
* `bob.keyword.do()`. The mode compiler automatically wires this up as a
* special _internal_ 'on:begin' callback for modes with `beginKeywords`
* @param {RegExpMatchArray} match
* @param {CallbackResponse} response
*/
function skipIfhasPrecedingOrTrailingDot(match, response) {
function skipIfhasPrecedingDot(match, response) {
const before = match.input[match.index - 1];
const after = match.input[match.index + match[0].length];
if (before === "." || after === ".") {
if (before === ".") {
response.ignoreMatch();
}
}
Expand Down Expand Up @@ -331,8 +330,8 @@ export function compileLanguage(language) {
// or whitespace - this does no harm in any case since our keyword engine
// doesn't allow spaces in keywords anyways and we still check for the boundary
// first
mode.begin = '\\b(' + mode.beginKeywords.split(' ').join('|') + ')(?=\\b|\\s)';
mode.__beforeBegin = skipIfhasPrecedingOrTrailingDot;
mode.begin = '\\b(' + mode.beginKeywords.split(' ').join('|') + ')(?!\\.)(?=\\b|\\s)';
mode.__beforeBegin = skipIfhasPrecedingDot;
}
if (!mode.begin) mode.begin = /\B|\b/;
cmode.beginRe = langRe(mode.begin);
Expand Down
15 changes: 8 additions & 7 deletions test/api/index.js
@@ -1,16 +1,17 @@
'use strict';

describe('hljs', function() {
require('./ident');
require('./underscoreIdent');
require('./number');
require('./cNumber');
require('./autoDetection');
require('./beginKeywords');
joshgoebel marked this conversation as resolved.
Show resolved Hide resolved
require('./binaryNumber');
require('./starters');
require('./cNumber');
require('./fixmarkup');
require('./getLanguage');
require('./autoDetection');
require('./highlight');
require('./fixmarkup');
require('./ident');
require('./keywords');
require('./number');
require('./registerAlias');
require('./starters');
require('./underscoreIdent');
});