-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
enh(parser) multi-class support in a single mode (#3081)
Co-authored-by: Vladimir Jimenez <allejo@me.com>
- Loading branch information
1 parent
3ef0f1b
commit 05a2770
Showing
10 changed files
with
187 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* eslint-disable no-throw-literal */ | ||
import * as logger from "../../lib/logger.js"; | ||
import * as regex from "../regex.js"; | ||
|
||
const MultiClassError = new Error(); | ||
|
||
/** | ||
* | ||
* @param {CompiledMode} mode | ||
*/ | ||
export function MultiClass(mode) { | ||
if (!Array.isArray(mode.begin)) return; | ||
|
||
if (mode.skip || mode.excludeBegin || mode.returnBegin) { | ||
logger.error("skip, excludeBegin, returnBegin not compatible with multi-class"); | ||
throw MultiClassError; | ||
} | ||
|
||
if (typeof mode.className !== "object") { | ||
logger.error("className must be object"); | ||
throw MultiClassError; | ||
} | ||
|
||
const items = mode.begin.map(x => regex.concat("(", x, ")")); | ||
mode.begin = regex.concat(...items); | ||
mode.isMultiClass = true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
'use strict'; | ||
|
||
const hljs = require('../../build'); | ||
|
||
describe('multi-class matchers', () => { | ||
before(() => { | ||
const grammar = function() { | ||
return { | ||
contains: [ | ||
{ | ||
begin: ["a", "b", "c"], | ||
className: { | ||
1: "a", | ||
3: "c" | ||
}, | ||
contains: [ | ||
{ | ||
match: "def", | ||
className: "def" | ||
} | ||
] | ||
}, | ||
{ | ||
className: "carrot", | ||
begin: /\^\^\^/, | ||
end: /\^\^\^/, | ||
contains: [ | ||
{ | ||
begin: ["a", "b", "c"], | ||
className: { | ||
1: "a", | ||
3: "c" | ||
} | ||
} | ||
] | ||
}, | ||
{ | ||
match: [ | ||
/func/, | ||
/\(\)/, | ||
/{.*}/ | ||
], | ||
className: { | ||
1: "keyword", | ||
2: "params", | ||
3: "body" | ||
} | ||
} | ||
] | ||
}; | ||
}; | ||
hljs.registerLanguage("test", grammar); | ||
}); | ||
after(() => { | ||
hljs.unregisterLanguage("test"); | ||
}); | ||
it('should support begin', () => { | ||
const code = "abcdef"; | ||
const result = hljs.highlight(code, { language: 'test' }); | ||
|
||
result.value.should.equal(`<span class="hljs-a">a</span>b<span class="hljs-c">c</span><span class="hljs-def">def</span>`); | ||
result.relevance.should.equal(2); | ||
}); | ||
it('basic functionality', () => { | ||
const code = "func(){ test }"; | ||
const result = hljs.highlight(code, { language: 'test' }); | ||
|
||
result.value.should.equal(`<span class="hljs-keyword">func</span><span class="hljs-params">()</span><span class="hljs-body">{ test }</span>`); | ||
result.relevance.should.equal(1); | ||
}); | ||
it('works inside a classified parent mode', () => { | ||
const code = "^^^what abc now^^^"; | ||
const result = hljs.highlight(code, { language: 'test' }); | ||
|
||
result.value.should.equal( | ||
`<span class="hljs-carrot">^^^what ` + | ||
`<span class="hljs-a">a</span>b<span class="hljs-c">c</span>` + | ||
` now^^^</span>`); | ||
}) | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters