Skip to content

Offline, privacy-first grammar checker. Fast, open-source, Rust-powered

perf(core): prefer `FirstMatchOf` when possible (#1576)

Steve Kurch9bbe9b7051d0
diff --git a/harper-core/src/document.rs b/harper-core/src/document.rs
index e6de8f97c..48bd4aec1 100644
--- a/harper-core/src/document.rs
+++ b/harper-core/src/document.rs
@@ -5,7 +5,7 @@ use std::fmt::Display;
use harper_brill::{Chunker, Tagger, brill_chunker, brill_tagger};
use paste::paste;
-use crate::expr::{Expr, ExprExt, LongestMatchOf, Repeating, SequenceExpr};
+use crate::expr::{Expr, ExprExt, FirstMatchOf, Repeating, SequenceExpr};
use crate::parsers::{Markdown, MarkdownOptions, Parser, PlainEnglish};
use crate::patterns::WordSet;
use crate::punctuation::Punctuation;
@@ -436,11 +436,11 @@ impl Document {
}
thread_local! {
- static LATIN_EXPR: Lrc<LongestMatchOf> = Document::uncached_latin_expr();
+ static LATIN_EXPR: Lrc<FirstMatchOf> = Document::uncached_latin_expr();
}
- fn uncached_latin_expr() -> Lrc<LongestMatchOf> {
- Lrc::new(LongestMatchOf::new(vec![
+ fn uncached_latin_expr() -> Lrc<FirstMatchOf> {
+ Lrc::new(FirstMatchOf::new(vec![
Box::new(
SequenceExpr::default()
.then(WordSet::new(&["etc", "vs"]))
diff --git a/harper-core/src/expr/first_match_of.rs b/harper-core/src/expr/first_match_of.rs
index fd6fb9581..6b0e144f6 100644
--- a/harper-core/src/expr/first_match_of.rs
+++ b/harper-core/src/expr/first_match_of.rs
@@ -15,8 +15,8 @@ impl FirstMatchOf {
Self { exprs }
}
- pub fn push(&mut self, expr: Box<dyn Expr>) {
- self.exprs.push(expr);
+ pub fn add(&mut self, expr: impl Expr + 'static) {
+ self.exprs.push(Box::new(expr));
}
}
@@ -24,7 +24,6 @@ impl Expr for FirstMatchOf {
fn run(&self, cursor: usize, tokens: &[Token], source: &[char]) -> Option<Span> {
self.exprs
.iter()
- .filter_map(|p| p.run(cursor, tokens, source))
- .next()
+ .find_map(|p| p.run(cursor, tokens, source))
}
}
diff --git a/harper-core/src/expr/mod.rs b/harper-core/src/expr/mod.rs
index ac65757ed..bb3dd249e 100644
--- a/harper-core/src/expr/mod.rs
+++ b/harper-core/src/expr/mod.rs
@@ -148,14 +148,23 @@ where
}
pub trait OwnedExprExt {
- fn or(self, other: impl Expr + 'static) -> LongestMatchOf;
+ fn or(self, other: impl Expr + 'static) -> FirstMatchOf;
+ fn or_longest(self, other: impl Expr + 'static) -> LongestMatchOf;
}
impl<E> OwnedExprExt for E
where
E: Expr + 'static,
{
- fn or(self, other: impl Expr + 'static) -> LongestMatchOf {
+ /// Returns an expression that matches either the current one or the expression contained in `other`.
+ fn or(self, other: impl Expr + 'static) -> FirstMatchOf {
+ FirstMatchOf::new(vec![Box::new(self), Box::new(other)])
+ }
+
+ /// Returns an expression that matches the longest of the current one or the expression contained in `other`.
+ ///
+ /// If you don't need the longest match, prefer using the short-circuiting [`Self::or()`] instead.
+ fn or_longest(self, other: impl Expr + 'static) -> LongestMatchOf {
LongestMatchOf::new(vec![Box::new(self), Box::new(other)])
}
}
diff --git a/harper-core/src/expr/reflexive_pronoun.rs b/harper-core/src/expr/reflexive_pronoun.rs
index e99955127..81c856203 100644
--- a/harper-core/src/expr/reflexive_pronoun.rs
+++ b/harper-core/src/expr/reflexive_pronoun.rs
@@ -1,6 +1,6 @@
use crate::{
Span, Token,
- expr::{Expr, LongestMatchOf},
+ expr::{Expr, FirstMatchOf},
patterns::WordSet,
};
@@ -53,7 +53,7 @@ impl ReflexivePronoun {
impl Expr for ReflexivePronoun {
fn run(&self, cursor: usize, tokens: &[Token], source: &[char]) -> Option<Span> {
let good_pronouns = |token: &Token, _: &[char]| token.kind.is_reflexive_pronoun();
- let mut expr = LongestMatchOf::new(vec![Box::new(good_pronouns)]);
+ let mut expr = FirstMatchOf::new(vec![Box::new(good_pronouns)]);
if self.include_common_errors {
expr.add(WordSet::new(BAD_REFLEXIVE_PRONOUNS));
}
diff --git a/harper-core/src/expr/sequence_expr.rs b/harper-core/src/expr/sequence_expr.rs
index 49fd6d1b4..bcf3d6b75 100644
--- a/harper-core/src/expr/sequence_expr.rs
+++ b/harper-core/src/expr/sequence_expr.rs
@@ -2,6 +2,7 @@ use paste::paste;
use crate::{
Span, Token, TokenKind,
+ expr::{FirstMatchOf, LongestMatchOf},
patterns::{AnyPattern, IndefiniteArticle, WhitespacePattern, Word, WordSet},
};
@@ -112,6 +113,25 @@ impl SequenceExpr {
self
}
+ /// Pushes an expression that will match any of the provided expressions.
+ ///
+ /// If more than one of the provided expressions match, this function provides no guarantee
+ /// as to which match will end up being used. If you need to get the longest of multiple
+ /// matches, use [`Self::then_longest_of()`] instead.
+ pub fn then_any_of(mut self, exprs: Vec<Box<dyn Expr>>) -> Self {
+ self.exprs.push(Box::new(FirstMatchOf::new(exprs)));
+ self
+ }
+
+ /// Pushes an expression that will match the longest of the provided expressions.
+ ///
+ /// If you don't need the longest match, prefer using the short-circuiting
+ /// [`Self::then_any_of()`] instead.
+ pub fn then_longest_of(mut self, exprs: Vec<Box<dyn Expr>>) -> Self {
+ self.exprs.push(Box::new(LongestMatchOf::new(exprs)));
+ self
+ }
+
/// Appends the steps in `other` onto the end of `self`.
/// This is more efficient than [`Self::then`] because it avoids pointer redirection.
pub fn then_seq(mut self, mut other: Self) -> Self {
diff --git a/harper-core/src/expr/spelled_number_expr.rs b/harper-core/src/expr/spelled_number_expr.rs
index 5ba2442ea..0d95d2a1e 100644
--- a/harper-core/src/expr/spelled_number_expr.rs
+++ b/harper-core/src/expr/spelled_number_expr.rs
@@ -53,10 +53,10 @@ impl Expr for SpelledNumberExpr {
let tens_units_compounds = SequenceExpr::default()
.then(WordSet::new(tens))
- .then(LongestMatchOf::new(vec![
+ .then_any_of(vec![
Box::new(|t: &Token, _s: &[char]| t.kind.is_hyphen()),
Box::new(WhitespacePattern),
- ]))
+ ])
.then(WordSet::new(units));
let expr =
diff --git a/harper-core/src/expr/word_expr_group.rs b/harper-core/src/expr/word_expr_group.rs
index f220427c5..f8fbd4361 100644
--- a/harper-core/src/expr/word_expr_group.rs
+++ b/harper-core/src/expr/word_expr_group.rs
@@ -17,24 +17,21 @@ where
}
impl WordExprGroup<FirstMatchOf> {
- pub fn add(&mut self, word: &str, expr: Box<dyn Expr>) {
+ pub fn add(&mut self, word: &str, expr: impl Expr + 'static) {
let chars = word.chars().collect();
if let Some(group) = self.exprs.get_mut(&chars) {
- group.push(expr);
+ group.add(expr);
} else {
let mut group = FirstMatchOf::default();
- group.push(expr);
+ group.add(expr);
self.exprs.insert(chars, group);
}
}
/// Add a pattern that matches just a word on its own, without anything else required to match.
pub fn add_word(&mut self, word: &'static str) {
- self.add(
- word,
- Box::new(SequenceExpr::default().then_exact_word(word)),
- );
+ self.add(word, SequenceExpr::default().then_exact_word(word));
}
}
diff --git a/harper-core/src/linting/a_part.rs b/harper-core/src/linting/a_part.rs
index 7963ddbca..6154faf4f 100644
--- a/harper-core/src/linting/a_part.rs
+++ b/harper-core/src/linting/a_part.rs
@@ -1,6 +1,6 @@
use crate::expr::Expr;
+use crate::expr::FirstMatchOf;
use crate::expr::FixedPhrase;
-use crate::expr::LongestMatchOf;
use crate::{
Token, TokenStringExt,
linting::{ExprLinter, Lint, LintKind, Suggestion},
@@ -12,7 +12,7 @@ pub struct APart {
impl Default for APart {
fn default() -> Self {
- let pattern = LongestMatchOf::new(vec![
+ let pattern = FirstMatchOf::new(vec![
Box::new(FixedPhrase::from_phrase("a part from")),
Box::new(FixedPhrase::from_phrase("apart of")),
Box::new(FixedPhrase::from_phrase("fall a part")),
diff --git a/harper-core/src/linting/am_in_the_morning.rs b/harper-core/src/linting/am_in_the_morning.rs
index df65f736a..b3a2478d8 100644
--- a/harper-core/src/linting/am_in_the_morning.rs
+++ b/harper-core/src/linting/am_in_the_morning.rs
@@ -1,5 +1,5 @@
use crate::{
- Lrc, Span, Token, TokenStringExt,
+ Span, Token, TokenStringExt,
expr::{Expr, FixedPhrase, LongestMatchOf, SequenceExpr},
linting::{ExprLinter, Lint, LintKind, Suggestion},
patterns::WordSet,
@@ -30,14 +30,8 @@ impl Default for AmInTheMorning {
let ws_at_periods = FixedPhrase::from_phrase(" at night");
let expr = SequenceExpr::default()
- .then(Lrc::new(LongestMatchOf::new(vec![
- Box::new(maybe_ws_am),
- Box::new(maybe_ws_pm),
- ])))
- .then(Lrc::new(LongestMatchOf::new(vec![
- Box::new(ws_in_periods),
- Box::new(ws_at_periods),
- ])));
+ .then_any_of(vec![Box::new(maybe_ws_am), Box::new(maybe_ws_pm)])
+ .then_any_of(vec![Box::new(ws_in_periods), Box::new(ws_at_periods)]);
Self {
expr: Box::new(expr),
diff --git a/harper-core/src/linting/amounts_for.rs b/harper-core/src/linting/amounts_for.rs
index da0e66b51..3167d93a5 100644
--- a/harper-core/src/linting/amounts_for.rs
+++ b/harper-core/src/linting/amounts_for.rs
@@ -1,6 +1,6 @@
use crate::expr::Expr;
+use crate::expr::FirstMatchOf;
use crate::expr::FixedPhrase;
-use crate::expr::LongestMatchOf;
use crate::expr::SequenceExpr;
use crate::{Lrc, Token, TokenStringExt, patterns::WordSet};
@@ -29,7 +29,7 @@ impl Default for AmountsFor {
.then(Lrc::new(FixedPhrase::from_phrase("amount for")));
Self {
- expr: Box::new(LongestMatchOf::new(vec![
+ expr: Box::new(FirstMatchOf::new(vec![
Box::new(singular_pattern),
Box::new(plural_pattern),
])),
diff --git a/harper-core/src/linting/back_in_the_day.rs b/harper-core/src/linting/back_in_the_day.rs
index 046b77a4d..aa04a538c 100644
--- a/harper-core/src/linting/back_in_the_day.rs
+++ b/harper-core/src/linting/back_in_the_day.rs
@@ -24,7 +24,7 @@ impl Default for BackInTheDay {
.then(phrase.clone())
.then_whitespace()
.then(exceptions.clone())
- .or(phrase);
+ .or_longest(phrase);
Self {
expr: Box::new(pattern),
diff --git a/harper-core/src/linting/discourse_markers.rs b/harper-core/src/linting/discourse_markers.rs
index 9e65a0b99..b474eeffb 100644
--- a/harper-core/src/linting/discourse_markers.rs
+++ b/harper-core/src/linting/discourse_markers.rs
@@ -1,6 +1,6 @@
use harper_brill::UPOS;
-use crate::expr::{Expr, FixedPhrase, LongestMatchOf, SequenceExpr};
+use crate::expr::{Expr, FirstMatchOf, FixedPhrase, SequenceExpr};
use crate::patterns::UPOSSet;
use crate::{Document, Token, TokenStringExt};
@@ -37,7 +37,7 @@ impl DiscourseMarkers {
"on the other hand",
];
- let phrases_expr = LongestMatchOf::new(
+ let phrases_expr = FirstMatchOf::new(
phrases
.iter()
.map(|text: &&str| Box::new(FixedPhrase::from_phrase(text)) as Box<dyn Expr>)
diff --git a/harper-core/src/linting/dot_initialisms.rs b/harper-core/src/linting/dot_initialisms.rs
index 8bd861bd0..2db513edb 100644
--- a/harper-core/src/linting/dot_initialisms.rs
+++ b/harper-core/src/linting/dot_initialisms.rs
@@ -24,7 +24,7 @@ impl Default for DotInitialisms {
.then_exact_word(target)
.then_punctuation();
- patterns.add(target, Box::new(pattern));
+ patterns.add(target, pattern);
}
Self {
diff --git a/harper-core/src/linting/expand_time_shorthands.rs b/harper-core/src/linting/expand_time_shorthands.rs
index 28edca919..67dfa7843 100644
--- a/harper-core/src/linting/expand_time_shorthands.rs
+++ b/harper-core/src/linting/expand_time_shorthands.rs
@@ -1,5 +1,4 @@
use crate::expr::Expr;
-use crate::expr::LongestMatchOf;
use crate::expr::SequenceExpr;
use std::sync::Arc;
@@ -19,21 +18,19 @@ impl ExpandTimeShorthands {
]));
Self {
- expr: Box::new(SequenceExpr::default().then(ImpliesQuantity).then(
- LongestMatchOf::new(vec![
+ expr: Box::new(
+ SequenceExpr::default()
+ .then(ImpliesQuantity)
+ .then_longest_of(vec![
Box::new(SequenceExpr::default().then(hotwords.clone())),
Box::new(
SequenceExpr::default()
.then_whitespace()
.then(hotwords.clone()),
),
- Box::new(
- SequenceExpr::default()
- .then_hyphen()
- .then(hotwords.clone()),
- ),
+ Box::new(SequenceExpr::default().then_hyphen().then(hotwords.clone())),
]),
- )),
+ ),
}
}
diff --git a/harper-core/src/linting/hedging.rs b/harper-core/src/linting/hedging.rs
index a89276da6..2acadc15a 100644
--- a/harper-core/src/linting/hedging.rs
+++ b/harper-core/src/linting/hedging.rs
@@ -1,6 +1,6 @@
use crate::expr::Expr;
+use crate::expr::FirstMatchOf;
use crate::expr::FixedPhrase;
-use crate::expr::LongestMatchOf;
use crate::linting::{ExprLinter, Lint, LintKind};
use crate::{Token, TokenStringExt};
@@ -18,7 +18,7 @@ impl Default for Hedging {
.map(|s| Box::new(FixedPhrase::from_phrase(s)) as Box<dyn Expr>)
.collect();
- let expr = Box::new(LongestMatchOf::new(patterns));
+ let expr = Box::new(FirstMatchOf::new(patterns));
Self { expr }
}
}
diff --git a/harper-core/src/linting/hyphenate_number_day.rs b/harper-core/src/linting/hyphenate_number_day.rs
index e3b02183a..6107fee29 100644
--- a/harper-core/src/linting/hyphenate_number_day.rs
+++ b/harper-core/src/linting/hyphenate_number_day.rs
@@ -1,5 +1,4 @@
use crate::expr::Expr;
-use crate::expr::LongestMatchOf;
use crate::expr::SequenceExpr;
use crate::{Token, patterns::NominalPhrase};
@@ -15,7 +14,7 @@ impl Default for HyphenateNumberDay {
.then_number()
.then_whitespace()
.t_aco("day")
- .then(LongestMatchOf::new(vec![
+ .then_longest_of(vec![
Box::new(
SequenceExpr::default()
.then_whitespace()
@@ -28,7 +27,7 @@ impl Default for HyphenateNumberDay {
.then_whitespace()
.then(NominalPhrase),
),
- ]));
+ ]);
Self {
expr: Box::new(pattern),
diff --git a/harper-core/src/linting/in_on_the_cards.rs b/harper-core/src/linting/in_on_the_cards.rs
index 2be497797..bfda21581 100644
--- a/harper-core/src/linting/in_on_the_cards.rs
+++ b/harper-core/src/linting/in_on_the_cards.rs
@@ -1,6 +1,6 @@
use crate::{
Dialect, Token,
- expr::{Expr, FixedPhrase, LongestMatchOf, SequenceExpr},
+ expr::{Expr, FirstMatchOf, FixedPhrase, SequenceExpr},
linting::{LintKind, Suggestion},
patterns::{InflectionOfBe, WordSet},
};
@@ -19,7 +19,7 @@ impl InOnTheCards {
_ => "on",
};
- let pre_context = LongestMatchOf::new(vec![
+ let pre_context = FirstMatchOf::new(vec![
Box::new(InflectionOfBe::new()),
Box::new(WordSet::new(&[
"isn't", "it's", "wasn't", "weren't", "not", "isnt", "its", "wasnt", "werent",
diff --git a/harper-core/src/linting/its_contraction.rs b/harper-core/src/linting/its_contraction.rs
index 48fa80554..fbc7838b6 100644
--- a/harper-core/src/linting/its_contraction.rs
+++ b/harper-core/src/linting/its_contraction.rs
@@ -33,12 +33,13 @@ impl Default for ItsContraction {
let inverted = SequenceExpr::default().then_unless(exceptions);
- let expr =
- All::new(vec![Box::new(positive), Box::new(inverted)]).or(SequenceExpr::aco("its")
+ let expr = All::new(vec![Box::new(positive), Box::new(inverted)]).or_longest(
+ SequenceExpr::aco("its")
.t_ws()
.then_adjective()
.t_ws()
- .t_aco("for"));
+ .t_aco("for"),
+ );
Self {
expr: Box::new(expr),
diff --git a/harper-core/src/linting/missing_preposition.rs b/harper-core/src/linting/missing_preposition.rs
index 95bfbea22..011115311 100644
--- a/harper-core/src/linting/missing_preposition.rs
+++ b/harper-core/src/linting/missing_preposition.rs
@@ -18,9 +18,11 @@ impl Default for MissingPreposition {
fn default() -> Self {
let expr = SequenceExpr::default()
.then(
- AnchorStart.or(SequenceExpr::default()
- .then(UPOSSet::new(&[UPOS::DET]))
- .t_ws()),
+ AnchorStart.or_longest(
+ SequenceExpr::default()
+ .then(UPOSSet::new(&[UPOS::DET]))
+ .t_ws(),
+ ),
)
.then(UPOSSet::new(&[UPOS::NOUN, UPOS::PRON, UPOS::PROPN]))
.t_ws()
diff --git a/harper-core/src/linting/noun_instead_of_verb.rs b/harper-core/src/linting/noun_instead_of_verb.rs
index bd16024e2..0d6fd6f82 100644
--- a/harper-core/src/linting/noun_instead_of_verb.rs
+++ b/harper-core/src/linting/noun_instead_of_verb.rs
@@ -1,6 +1,6 @@
use crate::expr::Expr;
-use crate::expr::LongestMatchOf;
use crate::expr::SequenceExpr;
+use crate::expr::{FirstMatchOf, LongestMatchOf};
use crate::{Lrc, Token, TokenStringExt, patterns::WordSet};
use super::{ExprLinter, Lint, LintKind, Suggestion};
@@ -55,7 +55,7 @@ pub struct NounInsteadOfVerb {
impl Default for NounInsteadOfVerb {
fn default() -> Self {
- let pre_context = LongestMatchOf::new(vec![
+ let pre_context = FirstMatchOf::new(vec![
Box::new(WordSet::new(PRONOUNS)),
Box::new(WordSet::new(MODAL_VERBS_ETC)),
Box::new(WordSet::new(ADVERBS)),
diff --git a/harper-core/src/linting/out_of_date.rs b/harper-core/src/linting/out_of_date.rs
index 9911b62fe..2e2d40eeb 100644
--- a/harper-core/src/linting/out_of_date.rs
+++ b/harper-core/src/linting/out_of_date.rs
@@ -1,6 +1,6 @@
use crate::expr::Expr;
+use crate::expr::FirstMatchOf;
use crate::expr::FixedPhrase;
-use crate::expr::LongestMatchOf;
use crate::{Token, TokenStringExt};
use super::{ExprLinter, Lint, LintKind, Suggestion};
@@ -11,7 +11,7 @@ pub struct OutOfDate {
impl Default for OutOfDate {
fn default() -> Self {
- let pattern = LongestMatchOf::new(vec![
+ let pattern = FirstMatchOf::new(vec![
Box::new(FixedPhrase::from_phrase("out of date")),
Box::new(FixedPhrase::from_phrase("out-of date")),
Box::new(FixedPhrase::from_phrase("out of-date")),
diff --git a/harper-core/src/linting/oxford_comma.rs b/harper-core/src/linting/oxford_comma.rs
index d1f2b84e4..23c6ab8c6 100644
--- a/harper-core/src/linting/oxford_comma.rs
+++ b/harper-core/src/linting/oxford_comma.rs
@@ -17,7 +17,7 @@ impl Default for OxfordComma {
.then_determiner()
.then_whitespace()
.then_nominal()
- .or(SequenceExpr::default().then_nominal()),
+ .or_longest(SequenceExpr::default().then_nominal()),
);
let item_chunk = SequenceExpr::default()
diff --git a/harper-core/src/linting/oxymorons.rs b/harper-core/src/linting/oxymorons.rs
index 387665100..045933c36 100644
--- a/harper-core/src/linting/oxymorons.rs
+++ b/harper-core/src/linting/oxymorons.rs
@@ -1,6 +1,6 @@
use crate::expr::Expr;
+use crate::expr::FirstMatchOf;
use crate::expr::FixedPhrase;
-use crate::expr::LongestMatchOf;
use crate::linting::{ExprLinter, Lint, LintKind};
use crate::{Token, TokenStringExt};
@@ -38,7 +38,7 @@ impl Oxymorons {
.map(|s| Box::new(FixedPhrase::from_phrase(s)) as Box<dyn Expr>)
.collect();
- let expr = Box::new(LongestMatchOf::new(exprs));
+ let expr = Box::new(FirstMatchOf::new(exprs));
Self { expr }
}
}
diff --git a/harper-core/src/linting/since_duration.rs b/harper-core/src/linting/since_duration.rs
index 715479d6d..2e542a05a 100644
--- a/harper-core/src/linting/since_duration.rs
+++ b/harper-core/src/linting/since_duration.rs
@@ -36,10 +36,10 @@ impl Default for SinceDuration {
SequenceExpr::default()
.then_any_capitalization_of("since")
.then_whitespace()
- .then(LongestMatchOf::new(vec![
+ .then_longest_of(vec![
Box::new(SpelledNumberExpr),
Box::new(SequenceExpr::default().then_number()),
- ]))
+ ])
.then_whitespace()
.then(units),
);
diff --git a/harper-core/src/linting/that_which.rs b/harper-core/src/linting/that_which.rs
index 5b73fb98f..add26ee32 100644
--- a/harper-core/src/linting/that_which.rs
+++ b/harper-core/src/linting/that_which.rs
@@ -22,8 +22,8 @@ impl Default for ThatWhich {
.then_any_capitalization_of("that"),
);
- pattern.add("that", Box::new(matching_pattern.clone()));
- pattern.add("That", Box::new(matching_pattern));
+ pattern.add("that", matching_pattern.clone());
+ pattern.add("That", matching_pattern);
Self {
expr: Box::new(pattern),
diff --git a/harper-core/src/linting/the_how_why.rs b/harper-core/src/linting/the_how_why.rs
index 9a883c7ec..45b6664c2 100644
--- a/harper-core/src/linting/the_how_why.rs
+++ b/harper-core/src/linting/the_how_why.rs
@@ -1,5 +1,5 @@
use crate::expr::Expr;
-use crate::expr::LongestMatchOf;
+use crate::expr::FirstMatchOf;
use crate::expr::SequenceExpr;
use crate::{
Token, TokenStringExt,
@@ -9,7 +9,7 @@ use crate::{
/// Suggests removing `the` when followed by how/why/who/when/what,
/// skipping cases like `how to` and `who's who`.
pub struct TheHowWhy {
- expr: LongestMatchOf,
+ expr: FirstMatchOf,
}
impl Default for TheHowWhy {
@@ -47,7 +47,7 @@ impl Default for TheHowWhy {
.then_whitespace()
.t_aco("what");
- let expr = LongestMatchOf::new(vec![
+ let expr = FirstMatchOf::new(vec![
Box::new(the_how),
Box::new(the_who),
Box::new(the_why),
diff --git a/harper-core/src/linting/the_my.rs b/harper-core/src/linting/the_my.rs
index e8d0fe44e..1d221a4fd 100644
--- a/harper-core/src/linting/the_my.rs
+++ b/harper-core/src/linting/the_my.rs
@@ -1,6 +1,6 @@
use super::{ExprLinter, Lint, LintKind, Suggestion};
use crate::expr::Expr;
-use crate::expr::LongestMatchOf;
+use crate::expr::FirstMatchOf;
use crate::expr::SequenceExpr;
use crate::{
CharStringExt, Token, TokenStringExt,
@@ -27,7 +27,7 @@ impl Default for TheMy {
.then(the);
Self {
- expr: Box::new(LongestMatchOf::new(vec![
+ expr: Box::new(FirstMatchOf::new(vec![
Box::new(the_poss),
Box::new(poss_the),
])),
diff --git a/harper-core/src/linting/then_than.rs b/harper-core/src/linting/then_than.rs
index 54e143093..7da759f1a 100644
--- a/harper-core/src/linting/then_than.rs
+++ b/harper-core/src/linting/then_than.rs
@@ -2,7 +2,7 @@ use super::{ExprLinter, Lint, LintKind};
use crate::Token;
use crate::expr::All;
use crate::expr::Expr;
-use crate::expr::LongestMatchOf;
+use crate::expr::FirstMatchOf;
use crate::expr::OwnedExprExt;
use crate::expr::SequenceExpr;
use crate::linting::Suggestion;
@@ -17,7 +17,7 @@ impl ThenThan {
pub fn new() -> Self {
Self {
expr: Box::new(All::new(vec![
- Box::new(LongestMatchOf::new(vec![
+ Box::new(FirstMatchOf::new(vec![
// Comparative form of adjective
Box::new(
SequenceExpr::default()
diff --git a/harper-core/src/linting/thing_think.rs b/harper-core/src/linting/thing_think.rs
index e60be913b..c02ced2e8 100644
--- a/harper-core/src/linting/thing_think.rs
+++ b/harper-core/src/linting/thing_think.rs
@@ -1,6 +1,6 @@
use crate::{
Token,
- expr::{Expr, FixedPhrase, LongestMatchOf, SequenceExpr},
+ expr::{Expr, FirstMatchOf, FixedPhrase, SequenceExpr},
linting::{ExprLinter, Lint, LintKind, Suggestion},
patterns::WordSet,
};
@@ -13,7 +13,7 @@ pub struct ThingThink {
impl Default for ThingThink {
fn default() -> Self {
let subject_pronouns = WordSet::new(&["I", "you", "we", "they"]);
- let indefinite_pronouns = LongestMatchOf::new(vec![
+ let indefinite_pronouns = FirstMatchOf::new(vec![
Box::new(WordSet::new(&[
"anybody",
"anyone",
@@ -23,7 +23,7 @@ impl Default for ThingThink {
// "Any one thing", "every one thing", "any body thing" cause false positives.
Box::new(FixedPhrase::from_phrase("every body")),
]);
- let pronoun = LongestMatchOf::new(vec![
+ let pronoun = FirstMatchOf::new(vec![
Box::new(subject_pronouns),
Box::new(indefinite_pronouns),
]);
@@ -58,7 +58,7 @@ impl Default for ThingThink {
let adverb_of_frequency =
WordSet::new(&["always", "sometimes", "often", "usually", "never"]);
- let pre_context = LongestMatchOf::new(vec![
+ let pre_context = FirstMatchOf::new(vec![
Box::new(pronoun),
Box::new(verb_to),
Box::new(modal),
diff --git a/harper-core/src/linting/use_genitive.rs b/harper-core/src/linting/use_genitive.rs
index a98eb08f3..ac57e5d6a 100644
--- a/harper-core/src/linting/use_genitive.rs
+++ b/harper-core/src/linting/use_genitive.rs
@@ -1,5 +1,5 @@
use crate::expr::Expr;
-use crate::expr::LongestMatchOf;
+use crate::expr::FirstMatchOf;
use crate::expr::SequenceExpr;
use crate::expr::WordExprGroup;
use crate::linting::{ExprLinter, LintKind, Suggestion};
@@ -14,8 +14,8 @@ pub struct UseGenitive {
impl UseGenitive {
fn new() -> Self {
// Define the environment in which the genitive case __should__ be used.
- let environment = Lrc::new(SequenceExpr::default().then_whitespace().then(
- LongestMatchOf::new(vec![
+ let environment = Lrc::new(SequenceExpr::default().then_whitespace().then_longest_of(
+ vec![
Box::new(
SequenceExpr::default()
.then_one_or_more_adjectives()
@@ -23,7 +23,7 @@ impl UseGenitive {
.then_noun(),
),
Box::new(SequenceExpr::default().then_noun()),
- ]),
+ ],
));
let trigger_words = ["there", "they're"];
@@ -33,17 +33,15 @@ impl UseGenitive {
for word in trigger_words {
primary_pattern.add(
word,
- Box::new(
- SequenceExpr::default()
- .then(Word::new_exact(word))
- .then(environment.clone()),
- ),
+ SequenceExpr::default()
+ .then(Word::new_exact(word))
+ .then(environment.clone()),
)
}
// Add a prelude to remove false-positives.
let full_pattern = SequenceExpr::default()
- .then_unless(LongestMatchOf::new(vec![
+ .then_unless(FirstMatchOf::new(vec![
Box::new(SequenceExpr::default().t_aco("is")),
Box::new(SequenceExpr::default().t_aco("were")),
Box::new(SequenceExpr::default().then_adjective()),
diff --git a/harper-core/src/linting/win_prize.rs b/harper-core/src/linting/win_prize.rs
index 03b892906..578a45e7d 100644
--- a/harper-core/src/linting/win_prize.rs
+++ b/harper-core/src/linting/win_prize.rs
@@ -21,10 +21,12 @@ impl Default for WinPrize {
.then_determiner()
.then_whitespace()
.then(miss.clone())
- .or(SequenceExpr::default()
- .then(verbs)
- .then_whitespace()
- .then(miss));
+ .or_longest(
+ SequenceExpr::default()
+ .then(verbs)
+ .then_whitespace()
+ .then(miss),
+ );
Self {
expr: Box::new(pattern),