diff --git a/src/reason-textmate/RegExp.re b/src/reason-textmate/RegExp.re index b1c7165dd5..56e80f205b 100644 --- a/src/reason-textmate/RegExp.re +++ b/src/reason-textmate/RegExp.re @@ -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) => { diff --git a/src/reason-textmate/RegExpFactory.re b/src/reason-textmate/RegExpFactory.re index 9db10ca68c..2fcf3a8cff 100644 --- a/src/reason-textmate/RegExpFactory.re +++ b/src/reason-textmate/RegExpFactory.re @@ -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 + }; }; }; @@ -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; }; @@ -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) } }; }; diff --git a/src/reason-textmate/Rule.re b/src/reason-textmate/Rule.re index 1ab71f0155..6d5832fb0f 100644 --- a/src/reason-textmate/Rule.re +++ b/src/reason-textmate/Rule.re @@ -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 = @@ -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 }; };