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

fix(syntax/#3384): Make invalid-pattern error non-fatal #3386

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions src/reason-textmate/RegExp.re
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,10 @@ let toString = (v: t) => v.raw;
let emptyMatches = [||];

let create = (str: string) => {
let regexp =
switch (OnigRegExp.create(str)) {
| Ok(v) => v
| Error(msg) => failwith(msg)
};

{cachedResult: NotEvaluated, raw: str, regexp};
str
|> OnigRegExp.create
|> Result.to_option
|> Option.map(regexp => {{cachedResult: NotEvaluated, raw: str, regexp}});
};

let search = (stringToEvaluate: string, positionToEvaluate: int, v: t) => {
Expand Down
19 changes: 12 additions & 7 deletions src/reason-textmate/RegExpFactory.re
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,11 @@ let _createCompiledAnchorCache = (ac: option(anchorCache)) => {
let a0_G1 = RegExp.create(v.raw_A0_G1);
let a1_G1 = RegExp.create(v.raw_A1_G1);
let a1_G0 = RegExp.create(v.raw_A1_G0);
Some({a0_G0, a0_G1, a1_G1, a1_G0});
switch (a0_G0, a0_G1, a1_G1, a1_G0) {
| (Some(a0_G0), Some(a0_G1), Some(a1_G1), Some(a1_G0)) =>
Some({a0_G0, a0_G1, a1_G1, a1_G0})
| _ => None
};
};
};

Expand Down Expand Up @@ -111,7 +115,7 @@ let create = (~allowBackReferences=true, str) => {
// If no back-references, and no anchors, we can just cache the regex
let regex =
if (!hasUnresolvedBackReferences && !anchorA && !anchorG) {
Some(RegExp.create(str));
RegExp.create(str);
} else {
None;
};
Expand Down Expand Up @@ -182,16 +186,17 @@ let compile = (allowA, allowG, v: t) =>
RegExp.create(rawStr);
} else {
switch (v.regex) {
| Some(v) => v
| Some(v) => Some(v)
| None =>
switch (v.compiledAnchorCache, allowA, allowG) {
| (None, _, _) => failwith("Should never hit this!")

| (Some({a1_G1, _}), allowA, allowG)
when allowA == true && allowG == true => a1_G1
| (Some({a1_G0, _}), allowA, _) when allowA == true => a1_G0
| (Some({a0_G1, _}), _, allowG) when allowG == true => a0_G1
| (Some({a0_G0, _}), _, _) => a0_G0
when allowA == true && allowG == true =>
Some(a1_G1)
| (Some({a1_G0, _}), allowA, _) when allowA == true => Some(a1_G0)
| (Some({a0_G1, _}), _, allowG) when allowG == true => Some(a0_G1)
| (Some({a0_G0, _}), _, _) => Some(a0_G0)
}
};
};
Expand Down
66 changes: 40 additions & 26 deletions src/reason-textmate/Rule.re
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,42 @@ let show = (v: t) => {
};

let ofMatch = (allowA, allowG, match: Pattern.patternMatch) => {
Some({
regex: RegExpFactory.compile(allowA, allowG, match.matchRegex),
name: match.matchName,
captures: match.captures,
popStack: None,
pushStack: None,
});
RegExpFactory.compile(allowA, allowG, match.matchRegex)
|> Option.map(regex =>
{
regex,
name: match.matchName,
captures: match.captures,
popStack: None,
pushStack: None,
}
);
};

let ofMatchRangeBegin = (allowA, allowG, matchRange: Pattern.matchRange) => {
Some({
regex: RegExpFactory.compile(allowA, allowG, matchRange.beginRegex),
name: matchRange.name,
captures: matchRange.beginCaptures,
popStack: None,
pushStack: Some(matchRange),
});
RegExpFactory.compile(allowA, allowG, matchRange.beginRegex)
|> Option.map(regex =>
{
regex,
name: matchRange.name,
captures: matchRange.beginCaptures,
popStack: None,
pushStack: Some(matchRange),
}
);
};

let ofMatchRangeEnd = (allowA, allowG, matchRange: Pattern.matchRange) => {
regex: RegExpFactory.compile(allowA, allowG, matchRange.endRegex),
name: matchRange.name,
captures: matchRange.endCaptures,
popStack: Some(matchRange),
pushStack: None,
RegExpFactory.compile(allowA, allowG, matchRange.endRegex)
|> Option.map(regex =>
{
regex,
name: matchRange.name,
captures: matchRange.endCaptures,
popStack: Some(matchRange),
pushStack: None,
}
);
};

let ofPatterns =
Expand Down Expand Up @@ -80,19 +91,22 @@ let ofPatterns =

let initialRules =
switch (activeRange) {
| Some(v) when v.applyEndPatternLast == true => [
ofMatchRangeEnd(isFirstLine, isAnchorPos, v),
]
| Some(v) when v.applyEndPatternLast == true =>
switch (ofMatchRangeEnd(isFirstLine, isAnchorPos, v)) {
| Some(v) => [v]
| None => []
}
| _ => []
};

let rules = List.fold_left(f, initialRules, patterns);

switch (activeRange) {
| Some(v) when v.applyEndPatternLast == false => [
ofMatchRangeEnd(isFirstLine, isAnchorPos, v),
...rules,
]
| Some(v) when v.applyEndPatternLast == false =>
switch (ofMatchRangeEnd(isFirstLine, isAnchorPos, v)) {
| Some(v) => [v, ...rules]
| None => rules
}
| _ => rules
};
};