-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Core: Fixed greedy targeting bug (#1932)
The `target` parameter defines the token which causes rematching. Rematching is done by recursively calling `matchGrammar` where `target` will be the current token name. The rematching is done if either a match was found or we reached the target token, whatever comes first. The bug is that a token is identified by its name. But the name alone doesn't uniquely identify a token in a grammar because many tokens can have the same name (array). This fixes this bug.
- Loading branch information
1 parent
3509f3e
commit e864d51
Showing
7 changed files
with
114 additions
and
30 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,69 @@ | ||
"use strict"; | ||
|
||
const { assert } = require('chai'); | ||
const PrismLoader = require('../helper/prism-loader'); | ||
const TestCase = require('../helper/test-case'); | ||
|
||
|
||
function testTokens({ grammar, code, expected }) { | ||
const Prism = PrismLoader.createEmptyPrism(); | ||
Prism.languages.test = grammar; | ||
|
||
const simpleTokens = TestCase.simpleTokenize(Prism, code, 'test'); | ||
|
||
assert.deepStrictEqual(simpleTokens, expected); | ||
} | ||
|
||
describe('Greedy matching', function () { | ||
|
||
it('should correctly handle tokens with the same name', function () { | ||
testTokens({ | ||
grammar: { | ||
'comment': [ | ||
/\/\/.*/, | ||
{ | ||
pattern: /\/\*[\s\S]*?(?:\*\/|$)/, | ||
greedy: true | ||
} | ||
] | ||
}, | ||
code: '// /*\n/* comment */', | ||
expected: [ | ||
["comment", "// /*"], | ||
["comment", "/* comment */"] | ||
] | ||
}); | ||
}); | ||
|
||
// https://github.com/PrismJS/prism/issues/1492 | ||
/* | ||
it('should correctly rematch tokens', function () { | ||
testTokens({ | ||
grammar: { | ||
'a': { | ||
pattern: /'[^'\r\n]*'/, | ||
}, | ||
'b': { | ||
pattern: /"[^"\r\n]*"/, | ||
greedy: true, | ||
}, | ||
'c': { | ||
pattern: /<[^>\r\n]*>/, | ||
greedy: true, | ||
} | ||
}, | ||
code: `<'> '' ''\n<"> "" ""`, | ||
expected: [ | ||
["c", "<'>"], | ||
["a", "''"], | ||
["a", "''"], | ||
["c", "<\">"], | ||
["b", "\"\""], | ||
["b", "\"\""], | ||
] | ||
}); | ||
}); | ||
*/ | ||
}); | ||
|
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