-
Notifications
You must be signed in to change notification settings - Fork 141
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
Check regular expressions for compile errors #165
Check regular expressions for compile errors #165
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good thanks, just some nits I'd prefer you fix but aren't actually important.
This is technically a breaking API change, but it is to a previously unused error, and searching all of Github brings up no uses of it, so I think it's fine to change without a major version bump.
Maybe just pinging @robinst since he's the only major non-open-source user I know.
src/parsing/yaml_load.rs
Outdated
assert!(def.is_err()); | ||
match def.unwrap_err() { | ||
ParseSyntaxError::RegexCompileError(ref regex, _) => assert_eq!("[a", regex), | ||
_ => assert!(false, "Got unexpedted ParseSyntaxError"), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/unexpedted/unexpected/
src/parsing/yaml_load.rs
Outdated
regex_str = regex_str.replace(&format!("\\{}", i), "placeholder"); | ||
} | ||
|
||
match Regex::new(®ex_str) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this may need to be a with_options
with ONIG_OPTION_CAPTURE_GROUP
like the other compiles in the library. From some Googling it appears this is the standard for textmate-like things and some syntaxes may rely on it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, good point. I saw Regex::new
somewhere and didn't check at the relevant places.
Oh, right - due to the
👍 Sounds good to me. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mostly looks good, just one suggestion.
Awesome that this caught another problem in the existing syntax :).
Regarding the API change, it's not a problem for us.
src/parsing/yaml_load.rs
Outdated
let mut regex_str = String::from(regex_str); | ||
for i in 0..10 { | ||
regex_str = regex_str.replace(&format!("\\{}", i), "placeholder"); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we make the regex_with_substitutes
function a bit more abstract (or extract another function), so that we can use the same logic here?:
syntect/src/parsing/syntax_definition.rs
Line 214 in af810cd
pub fn regex_with_substitutes(&self, region: &Region, s: &str) -> String { |
This is necessary to handle double backslashes like \\1 properly. While I was noticing things I also made the placeholders nicer for error messages and used a capacity when building the substitute result.
@sharkdp I realized that @robinst's suggestion was important not just for cleanliness but also to handle the case of I pushed my changes to the branch, they look okay? @sharkdp @robinst |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, looks good!
Looks great! |
Thank you! Very much looking forward to the next release 😄. |
This PR implements a basic version of regex syntax checking as proposed in #98 by @robinst. This would hopefully allow us to catch a lot of possible errors at
SyntaxSet
-generation time (and preventpanic!
s at syntax-highlighting time).I have also implemented
Error
forParseSyntaxError
(which includesRegexCompileError
). This allows user code to display nice error messages when loading syntax sets. For example, I get the following error when I try to loadCMake.sublime-syntax
:(this has already been addressed upstream: zyxar/Sublime-CMakeLists#32)
Note that the tests currently do not pass due to #164 (I believe this is a good thing!), so #164 should be fixed before merging this. All tests pass if
Rd (R Documentation).sublime-syntax
is removed.The backreference "rewriting" is very immature (and maybe even "unsafe"?). I ended up using this simple search-and-replace after trying two other approaches first:
Simply ignore regex compile errors with code
-208
(onig_sys::ONIGERR_INVALID_BACKREF
).However, using this approach, we wouldn't catch other syntax errors in these regexes.
Use
onig
sSyntaxBehavior
to disable backreference checking (because I thought thatSYNTAX_BEHAVIOR_STRICT_CHECK_BACKREF might help - it did not).