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

Greedy matching bug #1492

Closed
RunDevelopment opened this issue Jul 21, 2018 · 1 comment · Fixed by #2032
Closed

Greedy matching bug #1492

RunDevelopment opened this issue Jul 21, 2018 · 1 comment · Fixed by #2032

Comments

@RunDevelopment
Copy link
Member

Consider the following code:

{
	'template-string-pattern': /`(?:\\[\s\S]|\${[^}]+}|[^\\`])*`/,
	'a': 0,
	'b': 1
};
{
	'string' : /(`|')(?:\\.|(?!\1)[^\\\r\n])*\1/m,
	'c': 0
};

Highlighted using Test Drive:

image

What's happening is quite simple:

  1. template-string matches (?:\\[\s\S]|\${[^}]+}|[^\\ and /, 'a': 0 ....... 'string': /( (plus the grave accents).
  2. string matches 'template-string-pattern' and 'c'. The rest is blocked by template-string.
  3. regex breaks the matches of template-string and highlights the regexes.
  4. string gets a chance from regex to match and does so matching 'a'. But that's it, because oneshot=true.

Btw: You can see this bug all over the place when you look at the Prism code with lots of language definitions.

@RunDevelopment
Copy link
Member Author

Here is a minimal example showing the bug in action:

Language definition

I used aliases for styling.

Prism.languages['greedy-test'] = {
	'a': {
		pattern: /'[^'\r\n]*'/,
		alias: 'comment'
	},
	'b': {
		pattern: /"[^"\r\n]*"/,
		greedy: true,
		alias: 'string'
	},
	'c': {
		pattern: /<[^>\r\n]*>/,
		greedy: true,
		alias: 'keyword'
	}
}

Code:

<'> '' ''
<"> "" ""

(Each line is one example.)

Result:
image

Notes:

  1. The order of a and b is not significant. They just have to be before c.
  2. Both example lines of code are independent of each other.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants