Skip to content

Commit

Permalink
Updated refa + fixed 2 cases of exp backtracking (#2774)
Browse files Browse the repository at this point in the history
  • Loading branch information
RunDevelopment authored Mar 5, 2021
1 parent 42d24fa commit d85e30d
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 14 deletions.
4 changes: 2 additions & 2 deletions components/prism-asciidoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@
'function': /^[a-z\d-]+(?=:)/,
'punctuation': /^::?/,
'attributes': {
pattern: /(?:\[(?:[^\]\\"]|(["'])(?:(?!\1)[^\\]|\\.)*\1|\\.)*\])/,
pattern: /(?:\[(?:[^\]\\"']|(["'])(?:(?!\1)[^\\]|\\.)*\1|\\.)*\])/,
inside: attributes.inside
}
}
Expand All @@ -149,7 +149,7 @@
Those do not have the restrictions of the constrained quotes.
They are, in order: __emphasis__, **strong**, ++monospace++, +++passthrough+++, ##unquoted##, $$passthrough$$, ~subscript~, ^superscript^, {attribute-reference}, [[anchor]], [[[bibliography anchor]]], <<xref>>, (((indexes))) and ((indexes))
*/
pattern: /(^|[^\\])(?:(?:\B\[(?:[^\]\\"]|(["'])(?:(?!\2)[^\\]|\\.)*\2|\\.)*\])?(?:\b_(?!\s)(?: _|[^_\\\r\n]|\\.)+(?:(?:\r?\n|\r)(?: _|[^_\\\r\n]|\\.)+)*_\b|\B``(?!\s).+?(?:(?:\r?\n|\r).+?)*''\B|\B`(?!\s)(?:[^`'\s]|\s+\S)+['`]\B|\B(['*+#])(?!\s)(?: \3|(?!\3)[^\\\r\n]|\\.)+(?:(?:\r?\n|\r)(?: \3|(?!\3)[^\\\r\n]|\\.)+)*\3\B)|(?:\[(?:[^\]\\"]|(["'])(?:(?!\4)[^\\]|\\.)*\4|\\.)*\])?(?:(__|\*\*|\+\+\+?|##|\$\$|[~^]).+?(?:(?:\r?\n|\r).+?)*\5|\{[^}\r\n]+\}|\[\[\[?.+?(?:(?:\r?\n|\r).+?)*\]?\]\]|<<.+?(?:(?:\r?\n|\r).+?)*>>|\(\(\(?.+?(?:(?:\r?\n|\r).+?)*\)?\)\)))/m,
pattern: /(^|[^\\])(?:(?:\B\[(?:[^\]\\"']|(["'])(?:(?!\2)[^\\]|\\.)*\2|\\.)*\])?(?:\b_(?!\s)(?: _|[^_\\\r\n]|\\.)+(?:(?:\r?\n|\r)(?: _|[^_\\\r\n]|\\.)+)*_\b|\B``(?!\s).+?(?:(?:\r?\n|\r).+?)*''\B|\B`(?!\s)(?:[^`'\s]|\s+\S)+['`]\B|\B(['*+#])(?!\s)(?: \3|(?!\3)[^\\\r\n]|\\.)+(?:(?:\r?\n|\r)(?: \3|(?!\3)[^\\\r\n]|\\.)+)*\3\B)|(?:\[(?:[^\]\\"']|(["'])(?:(?!\4)[^\\]|\\.)*\4|\\.)*\])?(?:(__|\*\*|\+\+\+?|##|\$\$|[~^]).+?(?:(?:\r?\n|\r).+?)*\5|\{[^}\r\n]+\}|\[\[\[?.+?(?:(?:\r?\n|\r).+?)*\]?\]\]|<<.+?(?:(?:\r?\n|\r).+?)*>>|\(\(\(?.+?(?:(?:\r?\n|\r).+?)*\)?\)\)))/m,
lookbehind: true,
inside: {
'attributes': attributes,
Expand Down
2 changes: 1 addition & 1 deletion components/prism-asciidoc.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion components/prism-perl.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ Prism.languages.perl = {
// ${...}
/[&*$@%]#?(?=\{)/,
// $foo
/[&*$@%]#?(?:(?:::)*'?(?!\d)[\w$]+)+(?:::)*/i,
/[&*$@%]#?(?:(?:::)*'?(?!\d)[\w$]+(?![\w$]))+(?:::)*/i,
// $1
/[&*$@%]\d+/,
// $_, @_, %!
Expand Down
2 changes: 1 addition & 1 deletion components/prism-perl.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"jsdom": "^13.0.0",
"mocha": "^6.2.0",
"pump": "^3.0.0",
"refa": "0.5.0",
"refa": "^0.7.0",
"regexpp": "^2.0.1",
"scslre": "^0.1.1",
"simple-git": "^1.107.0",
Expand Down
30 changes: 25 additions & 5 deletions tests/pattern-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const PrismLoader = require('./helper/prism-loader');
const { BFS, parseRegex } = require('./helper/util');
const { languages } = require('../components.json');
const { visitRegExpAST } = require('regexpp');
const { JS, Words, NFA } = require('refa');
const { transform, combineTransformers, JS, Words, NFA, Transformers } = require('refa');
const scslre = require('scslre');

/**
Expand Down Expand Up @@ -383,6 +383,22 @@ function testPatterns(Prism) {
});

it('- should not cause exponential backtracking', function () {
/** @type {Transformers.CreationOptions} */
const options = {
ignoreOrder: true,
ignoreAmbiguity: true
};
const transformer = combineTransformers([
Transformers.inline(options),
Transformers.removeDeadBranches(options),
Transformers.unionCharacters(options),
Transformers.moveUpEmpty(options),
Transformers.nestedQuantifiers(options),
Transformers.sortAssertions(options),
Transformers.removeUnnecessaryAssertions(options),
Transformers.applyAssertions(options),
]);

forEachPattern(({ pattern, ast, tokenPath }) => {
const patternStr = String(pattern);
if (expoSafeRegexes.has(patternStr)) {
Expand All @@ -398,11 +414,15 @@ function testPatterns(Prism) {
* @returns {NFA}
*/
function toNFA(element) {
const { expression, maxCharacter } = parser.parseElement(element, {
backreferences: "resolve",
lookarounds: "disable",
let { expression, maxCharacter } = parser.parseElement(element, {
maxBackreferenceWords: 1000,
backreferences: "disable"
});
return NFA.fromRegex(expression, { maxCharacter });

// try to remove assertions
expression = transform(transformer, expression);

return NFA.fromRegex(expression, { maxCharacter }, { assertions: "disable" });
}

/**
Expand Down

0 comments on commit d85e30d

Please sign in to comment.