Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
refactor: introduce `SequenceExpr::word_seq()` (#3659)
Andrew Dunbar449184cff141
diff --git a/harper-core/src/expr/filter.rs b/harper-core/src/expr/filter.rsindex 10440f120..4141f6e6e 100644--- a/harper-core/src/expr/filter.rs+++ b/harper-core/src/expr/filter.rs@@ -17,7 +17,7 @@ use super::Expr;/// use harper_core::expr::{SequenceExpr, Filter, ExprExt};/// use harper_core::{Span, Document};///-/// let a = SequenceExpr::aco("chock").t_ws().t_aco("full");+/// let a = SequenceExpr::word_seq(&["chock", "full"]);/// let b = WhitespacePattern;////// let filter = Filter::new(vec![Box::new(a), Box::new(b)]);diff --git a/harper-core/src/expr/sequence_expr.rs b/harper-core/src/expr/sequence_expr.rsindex b1bdad7f9..a24aaf0ae 100644--- a/harper-core/src/expr/sequence_expr.rs+++ b/harper-core/src/expr/sequence_expr.rs@@ -127,6 +127,11 @@ impl SequenceExpr {Self::default().then_optional(expr)}+ /// Match a series of words separated by whitespace.+ pub fn word_seq(words: &'static [&'static str]) -> Self {+ Self::default().then_word_seq(words)+ }+/// Match a fixed phrase.pub fn fixed_phrase(phrase: &'static str) -> Self {Self::default().then_fixed_phrase(phrase)@@ -296,6 +301,19 @@ impl SequenceExpr {self.then(Word::new_exact(word))}+ /// Match a series of words separated by whitespace.+ pub fn then_word_seq(self, words: &'static [&'static str]) -> Self {+ if let Some((first, rest)) = words.split_first() {+ let mut expr = self.t_aco(first);+ for word in rest {+ expr = expr.t_ws().t_aco(word);+ }+ expr+ } else {+ self+ }+ }+/// Match a fixed phrase.pub fn then_fixed_phrase(self, phrase: &'static str) -> Self {self.then(FixedPhrase::from_phrase(phrase))diff --git a/harper-core/src/linting/a_some_time.rs b/harper-core/src/linting/a_some_time.rsindex 9fea5c0d5..9e4c89bb7 100644--- a/harper-core/src/linting/a_some_time.rs+++ b/harper-core/src/linting/a_some_time.rs@@ -12,9 +12,7 @@ pub struct ASomeTime {impl Default for ASomeTime {fn default() -> Self {Self {- expr: SequenceExpr::aco("a")- .t_ws()- .t_aco("some")+ expr: SequenceExpr::word_seq(&["a", "some"]).t_ws().then_any_of(vec![Box::new(Word::new("time")),diff --git a/harper-core/src/linting/all_hell_break_loose.rs b/harper-core/src/linting/all_hell_break_loose.rsindex ae8c65c21..424eed9f0 100644--- a/harper-core/src/linting/all_hell_break_loose.rs+++ b/harper-core/src/linting/all_hell_break_loose.rs@@ -11,9 +11,7 @@ pub struct AllHellBreakLoose {impl Default for AllHellBreakLoose {fn default() -> Self {Self {- expr: SequenceExpr::aco("all")- .t_ws()- .t_aco("hell")+ expr: SequenceExpr::word_seq(&["all", "hell"]).t_ws().then_word_set(&["break", "breaking", "breaks", "broke", "broken"]).t_ws()diff --git a/harper-core/src/linting/apart_from.rs b/harper-core/src/linting/apart_from.rsindex fc409cd9d..466ca2f01 100644--- a/harper-core/src/linting/apart_from.rs+++ b/harper-core/src/linting/apart_from.rs@@ -10,11 +10,9 @@ pub struct ApartFrom {impl Default for ApartFrom {fn default() -> Self {- let expr = SequenceExpr::any_capitalization_of("apart")- .t_ws()- .then_any_capitalization_of("form");-- Self { expr }+ Self {+ expr: SequenceExpr::word_seq(&["apart", "form"]),+ }}}diff --git a/harper-core/src/linting/as_how.rs b/harper-core/src/linting/as_how.rsindex bac35257a..8bfcfa6a4 100644--- a/harper-core/src/linting/as_how.rs+++ b/harper-core/src/linting/as_how.rs@@ -12,9 +12,7 @@ pub struct AsHow {impl Default for AsHow {fn default() -> Self {Self {- expr: SequenceExpr::any_capitalization_of("as")- .t_ws()- .t_aco("how"),+ expr: SequenceExpr::word_seq(&["as", "how"]),}}}diff --git a/harper-core/src/linting/change_tack.rs b/harper-core/src/linting/change_tack.rsindex dced30d86..c19e2f50c 100644--- a/harper-core/src/linting/change_tack.rs+++ b/harper-core/src/linting/change_tack.rs@@ -30,7 +30,7 @@ impl Default for ChangeTack {.t_ws().then_word_set(eggcorns),),- Box::new(SequenceExpr::aco("different").t_ws().t_aco("tact")),+ Box::new(SequenceExpr::word_seq(&["different", "tact"])),]),}}diff --git a/harper-core/src/linting/else_possessive.rs b/harper-core/src/linting/else_possessive.rsindex 1746fed9e..3c54b2a43 100644--- a/harper-core/src/linting/else_possessive.rs+++ b/harper-core/src/linting/else_possessive.rs@@ -23,7 +23,7 @@ impl Default for ElsePossessive {"everyone","nobody",])- .or(SequenceExpr::aco("no").then_whitespace().t_aco("one"));+ .or(SequenceExpr::word_seq(&["no", "one"]));let pattern = SequenceExpr::with(pronouns).then_whitespace()diff --git a/harper-core/src/linting/everyday.rs b/harper-core/src/linting/everyday.rsindex 516206192..4fc786217 100644--- a/harper-core/src/linting/everyday.rs+++ b/harper-core/src/linting/everyday.rs@@ -13,7 +13,7 @@ pub struct Everyday {impl Default for Everyday {fn default() -> Self {let everyday = Word::new("everyday");- let every_day = Lrc::new(SequenceExpr::aco("every").t_ws().t_aco("day"));+ let every_day = Lrc::new(SequenceExpr::word_seq(&["every", "day"]));let everyday_bad_after = All::new(vec![Box::new(SequenceExpr::with(everyday.clone()).t_ws().then_any_word()),diff --git a/harper-core/src/linting/fed_up_with.rs b/harper-core/src/linting/fed_up_with.rsindex 3a7753d8f..765225a35 100644--- a/harper-core/src/linting/fed_up_with.rs+++ b/harper-core/src/linting/fed_up_with.rs@@ -11,7 +11,7 @@ pub struct FedUpWith {impl FedUpWith {pub fn new(dialect: Dialect) -> Self {- let expr = SequenceExpr::fixed_phrase("fed up of");+ let expr = SequenceExpr::word_seq(&["fed", "up", "of"]);Self { expr, dialect }}diff --git a/harper-core/src/linting/for_free_of_charge.rs b/harper-core/src/linting/for_free_of_charge.rsindex b27a5514f..1b272d28d 100644--- a/harper-core/src/linting/for_free_of_charge.rs+++ b/harper-core/src/linting/for_free_of_charge.rs@@ -11,28 +11,21 @@ pub struct ForFreeOfCharge {impl Default for ForFreeOfCharge {fn default() -> Self {Self {- expr: SequenceExpr::aco("for")- .t_ws()- .t_aco("free")- .t_ws_h()- .t_aco("of")- .t_ws_h()- .t_aco("charge")- .then_any_of(vec![- Box::new(SequenceExpr::default().then_kind_any(&[- TokenKind::is_sentence_terminator,- TokenKind::is_comma,- TokenKind::is_quote,- ])),- Box::new(SequenceExpr::whitespace().then_kind_any_but_not(- &[- TokenKind::is_conjunction,- TokenKind::is_preposition,- TokenKind::is_verb,- ],- TokenKind::is_noun,- )),- ]),+ expr: SequenceExpr::word_seq(&["for", "free", "of", "charge"]).then_any_of(vec![+ Box::new(SequenceExpr::default().then_kind_any(&[+ TokenKind::is_sentence_terminator,+ TokenKind::is_comma,+ TokenKind::is_quote,+ ])),+ Box::new(SequenceExpr::whitespace().then_kind_any_but_not(+ &[+ TokenKind::is_conjunction,+ TokenKind::is_preposition,+ TokenKind::is_verb,+ ],+ TokenKind::is_noun,+ )),+ ]),}}}diff --git a/harper-core/src/linting/hereby.rs b/harper-core/src/linting/hereby.rsindex 643565ce4..3ce314a78 100644--- a/harper-core/src/linting/hereby.rs+++ b/harper-core/src/linting/hereby.rs@@ -14,9 +14,7 @@ impl Default for Hereby {// Require a verb that is not also a noun. Otherwise sentences like// "I got here by skill" — where "skill" is a noun object of "by" —// match because "skill" is tagged as both verb and noun.- let pattern = SequenceExpr::aco("here")- .then_whitespace()- .t_aco("by")+ let pattern = SequenceExpr::word_seq(&["here", "by"]).then_whitespace().then_kind_is_but_is_not(TokenKind::is_verb, TokenKind::is_noun);diff --git a/harper-core/src/linting/hope_youre.rs b/harper-core/src/linting/hope_youre.rsindex 024e9978a..a17f26d4e 100644--- a/harper-core/src/linting/hope_youre.rs+++ b/harper-core/src/linting/hope_youre.rs@@ -16,9 +16,7 @@ impl Default for HopeYoure {let prep = SequenceExpr::default().t_ws().then_preposition();- let expr = SequenceExpr::aco("hope")- .t_ws()- .t_aco("your")+ let expr = SequenceExpr::word_seq(&["hope", "your"]).t_ws().then_adjective().then_optional(prep)diff --git a/harper-core/src/linting/it_looks_like_that.rs b/harper-core/src/linting/it_looks_like_that.rsindex 4a0e2809a..4473c15e6 100644--- a/harper-core/src/linting/it_looks_like_that.rs+++ b/harper-core/src/linting/it_looks_like_that.rs@@ -11,7 +11,7 @@ pub struct ItLooksLikeThat {impl Default for ItLooksLikeThat {fn default() -> Self {Self {- expr: SequenceExpr::fixed_phrase("it looks like that")+ expr: SequenceExpr::word_seq(&["it", "looks", "like", "that"]).then_whitespace().then_kind_where(|kind| {// Heuristics on the word after "that" which show "that" was useddiff --git a/harper-core/src/linting/its_possessive.rs b/harper-core/src/linting/its_possessive.rsindex 03c09b5c0..99bbe23cb 100644--- a/harper-core/src/linting/its_possessive.rs+++ b/harper-core/src/linting/its_possessive.rs@@ -114,7 +114,7 @@ impl Default for ItsPossessive {map.insert(start_of_chunk_after_conjunction, 2);- let special = SequenceExpr::aco("it's").t_ws().t_aco("various");+ let special = SequenceExpr::word_seq(&["it's", "various"]);map.insert(special, 0);diff --git a/harper-core/src/linting/likewise.rs b/harper-core/src/linting/likewise.rsindex 08c8be093..e87220a3f 100644--- a/harper-core/src/linting/likewise.rs+++ b/harper-core/src/linting/likewise.rs@@ -13,7 +13,7 @@ impl Default for Likewise {fn default() -> Self {let mut expr = All::default();- expr.add(SequenceExpr::aco("like").then_whitespace().t_aco("wise"));+ expr.add(SequenceExpr::word_seq(&["like", "wise"]));expr.add(SequenceExpr::unless(SequenceExpr::anything().then_whitespace()diff --git a/harper-core/src/linting/no_longer.rs b/harper-core/src/linting/no_longer.rsindex fd78d7dcb..3a0c42180 100644--- a/harper-core/src/linting/no_longer.rs+++ b/harper-core/src/linting/no_longer.rs@@ -11,9 +11,7 @@ pub struct NoLonger {impl Default for NoLonger {fn default() -> Self {Self {- expr: SequenceExpr::aco("not")- .t_ws()- .t_aco("longer")+ expr: SequenceExpr::word_seq(&["not", "longer"]).then_optional(SequenceExpr::default().t_ws().then_kind_any(&[TokenKind::is_verb_lemma,diff --git a/harper-core/src/linting/nobody.rs b/harper-core/src/linting/nobody.rsindex 16cd5b5f8..6ae1d5e2e 100644--- a/harper-core/src/linting/nobody.rs+++ b/harper-core/src/linting/nobody.rs@@ -11,9 +11,7 @@ pub struct Nobody {impl Default for Nobody {fn default() -> Self {- let pattern = SequenceExpr::aco("no")- .then_whitespace()- .t_aco("body")+ let pattern = SequenceExpr::word_seq(&["no", "body"]).then_whitespace().then_verb();Self { expr: pattern }diff --git a/harper-core/src/linting/not_only_inversion.rs b/harper-core/src/linting/not_only_inversion.rsindex a7f144045..97d56823f 100644--- a/harper-core/src/linting/not_only_inversion.rs+++ b/harper-core/src/linting/not_only_inversion.rs@@ -11,9 +11,7 @@ pub struct NotOnlyInversion {impl Default for NotOnlyInversion {fn default() -> Self {Self {- expr: SequenceExpr::aco("not")- .t_ws()- .t_aco("only")+ expr: SequenceExpr::word_seq(&["not", "only"]).t_ws().then_word_set(&["I", "we", "you", "he", "she", "it", "they"]).t_ws()diff --git a/harper-core/src/linting/oldest_in_the_book.rs b/harper-core/src/linting/oldest_in_the_book.rsindex b6cb715f0..544596f82 100644--- a/harper-core/src/linting/oldest_in_the_book.rs+++ b/harper-core/src/linting/oldest_in_the_book.rs@@ -36,7 +36,8 @@ impl Default for OldestInTheBook {Self {expr: SequenceExpr::fixed_phrase("oldest ").then(noun_phrase)- .then_fixed_phrase(" in the books"),+ .t_ws()+ .then_word_seq(&["in", "the", "books"]),}}}diff --git a/harper-core/src/linting/once_or_twice.rs b/harper-core/src/linting/once_or_twice.rsindex 64f04a9b6..8a9cd3cc5 100644--- a/harper-core/src/linting/once_or_twice.rs+++ b/harper-core/src/linting/once_or_twice.rs@@ -11,13 +11,9 @@ pub struct OnceOrTwice {impl Default for OnceOrTwice {fn default() -> Self {- let pattern = SequenceExpr::aco("once")- .then_whitespace()- .t_aco("a")- .then_whitespace()- .t_aco("twice");-- Self { expr: pattern }+ Self {+ expr: SequenceExpr::word_seq(&["once", "a", "twice"]),+ }}}diff --git a/harper-core/src/linting/one_of_the_singular.rs b/harper-core/src/linting/one_of_the_singular.rsindex 206494354..bad802674 100644--- a/harper-core/src/linting/one_of_the_singular.rs+++ b/harper-core/src/linting/one_of_the_singular.rs@@ -39,7 +39,8 @@ impl<D: Dictionary + 'static> OneOfTheSingular<D> {.then_zero_or_more_spaced(SequenceExpr::default().then_my_noun_or_adjective());Self {- expr: SequenceExpr::fixed_phrase("one of the ")+ expr: SequenceExpr::word_seq(&["one", "of", "the"])+ .t_ws().then(SequenceExpr::optional(advs.t_ws()).then(adj_or_nouns)),dict,}diff --git a/harper-core/src/linting/pronoun_verb_agreement.rs b/harper-core/src/linting/pronoun_verb_agreement.rsindex e0d020d53..562c31261 100644--- a/harper-core/src/linting/pronoun_verb_agreement.rs+++ b/harper-core/src/linting/pronoun_verb_agreement.rs@@ -90,7 +90,7 @@ where.t_ws().then(verb_lemma),),- Box::new(SequenceExpr::aco("it").t_ws().t_aco("don't")),+ Box::new(SequenceExpr::word_seq(&["it", "don't"])),]),dict,}diff --git a/harper-core/src/linting/some_without_article.rs b/harper-core/src/linting/some_without_article.rsindex 02df8a1df..52e97a4da 100644--- a/harper-core/src/linting/some_without_article.rs+++ b/harper-core/src/linting/some_without_article.rs@@ -11,11 +11,9 @@ pub struct SomeWithoutArticle {impl Default for SomeWithoutArticle {fn default() -> Self {- let expr = SequenceExpr::any_capitalization_of("the")- .t_ws()- .then_any_capitalization_of("some");-- Self { expr }+ Self {+ expr: SequenceExpr::word_seq(&["the", "some"]),+ }}}diff --git a/harper-core/src/linting/somewhat_something.rs b/harper-core/src/linting/somewhat_something.rsindex edba9be5c..bd3c3404b 100644--- a/harper-core/src/linting/somewhat_something.rs+++ b/harper-core/src/linting/somewhat_something.rs@@ -10,13 +10,9 @@ pub struct SomewhatSomething {impl Default for SomewhatSomething {fn default() -> Self {- let pattern = SequenceExpr::aco("somewhat")- .then_whitespace()- .t_aco("of")- .then_whitespace()- .t_aco("a");-- Self { expr: pattern }+ Self {+ expr: SequenceExpr::word_seq(&["somewhat", "of", "a"]),+ }}}diff --git a/harper-core/src/linting/that_which.rs b/harper-core/src/linting/that_which.rsindex 91a6e1a66..f91749f4e 100644--- a/harper-core/src/linting/that_which.rs+++ b/harper-core/src/linting/that_which.rs@@ -17,11 +17,7 @@ impl Default for ThatWhich {fn default() -> Self {let mut pattern = WordExprGroup::default();- let matching_pattern = Lrc::new(- SequenceExpr::any_capitalization_of("that")- .then_whitespace()- .then_any_capitalization_of("that"),- );+ let matching_pattern = Lrc::new(SequenceExpr::word_seq(&["that", "that"]));pattern.add("that", matching_pattern.clone());pattern.add("That", matching_pattern);diff --git a/harper-core/src/linting/were_where.rs b/harper-core/src/linting/were_where.rsindex a4bc9af1b..4ca5f0799 100644--- a/harper-core/src/linting/were_where.rs+++ b/harper-core/src/linting/were_where.rs@@ -26,16 +26,12 @@ impl Default for WereWhere {// "you where" alone is ambiguous ("I'll show you where to go"), so only flag// it when followed by a verb, auxiliary, or adjective — confirming a verb slot.// e.g. "you where going" → "you were going"- let you_where_verb = SequenceExpr::aco("you")- .t_ws()- .t_aco("where")+ let you_where_verb = SequenceExpr::word_seq(&["you", "where"]).t_ws().then(UPOSSet::new(&[UPOS::VERB, UPOS::AUX, UPOS::ADJ]));// "where you ..." can be a typo for "were you ..." when it starts a question.- let where_you_verb = SequenceExpr::aco("where")- .t_ws()- .t_aco("you")+ let where_you_verb = SequenceExpr::word_seq(&["where", "you"]).t_ws().then(UPOSSet::new(&[UPOS::VERB, UPOS::AUX, UPOS::ADJ]));diff --git a/harper-core/src/linting/worth_to_do.rs b/harper-core/src/linting/worth_to_do.rsindex 13724dd42..ea57e543d 100644--- a/harper-core/src/linting/worth_to_do.rs+++ b/harper-core/src/linting/worth_to_do.rs@@ -20,9 +20,7 @@ where{pub fn new(dict: D) -> Self {Self {- expr: SequenceExpr::aco("worth")- .t_ws()- .t_aco("to")+ expr: SequenceExpr::word_seq(&["worth", "to"]).t_ws().then_verb_lemma(),dict,