Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
feat: refactored sequence patterns
Elijah Potter0778d5a8b2a9
diff --git a/harper-core/src/linting/multiple_sequential_pronouns.rs b/harper-core/src/linting/multiple_sequential_pronouns.rsindex 0ca3ea728..6663113c3 100644--- a/harper-core/src/linting/multiple_sequential_pronouns.rs+++ b/harper-core/src/linting/multiple_sequential_pronouns.rs@@ -1,6 +1,7 @@use hashbrown::HashSet;use super::pattern_linter::PatternLinter;+use super::Suggestion;use crate::linting::LintKind;use crate::patterns::{Pattern, SequencePattern};use crate::{Lint, Lrc, Token, TokenStringExt};@@ -42,13 +43,24 @@ impl PatternLinter for MultipleSequentialPronouns {self.pattern.as_ref()}- fn match_to_lint(&self, matched_tokens: &[Token], _source: &[char]) -> Lint {+ fn match_to_lint(&self, matched_tokens: &[Token], source: &[char]) -> Lint {+ let mut suggestions = Vec::new();++ if matched_tokens.len() == 3 {+ suggestions.push(Suggestion::ReplaceWith(+ matched_tokens[0].span.get_content(source).to_vec()+ ));+ suggestions.push(Suggestion::ReplaceWith(+ matched_tokens[2].span.get_content(source).to_vec()+ ));+ }+Lint {span: matched_tokens.span().unwrap(),lint_kind: LintKind::Repetition,message: "There are too many personal pronouns in sequence here.".to_owned(),priority: 63,- ..Default::default()+ suggestions}}}diff --git a/harper-core/src/linting/repeated_words.rs b/harper-core/src/linting/repeated_words.rsindex 8e58c171d..b9a808952 100644--- a/harper-core/src/linting/repeated_words.rs+++ b/harper-core/src/linting/repeated_words.rs@@ -1,4 +1,4 @@-use super::{Lint, LintKind, Linter, PatternLinter, Suggestion};+use super::{Lint, LintKind, PatternLinter, Suggestion};use crate::patterns::{Pattern, SequencePattern, WordPatternGroup};use crate::token::{Token, TokenStringExt};@@ -48,11 +48,13 @@ impl PatternLinter for RepeatedWords {self.pattern.as_ref()}- fn match_to_lint(&self, matched_tokens: &[Token], _source: &[char]) -> Lint {+ fn match_to_lint(&self, matched_tokens: &[Token], source: &[char]) -> Lint {Lint {- span: matched_tokens[1..].span().unwrap(),+ span: matched_tokens.span().unwrap(),lint_kind: LintKind::Repetition,- suggestions: vec![Suggestion::Remove],+ suggestions: vec![Suggestion::ReplaceWith(+ matched_tokens[0].span.get_content(source).to_vec()+ )],message: "Did you mean to repeat this word?".to_string(),..Default::default()}diff --git a/harper-core/src/patterns/any_pattern.rs b/harper-core/src/patterns/any_pattern.rsnew file mode 100644index 000000000..21d50c928--- /dev/null+++ b/harper-core/src/patterns/any_pattern.rs@@ -0,0 +1,14 @@+use super::Pattern;++/// A [`Pattern`] that will match any single token.+pub struct AnyPattern;++impl Pattern for AnyPattern {+ fn matches(&self, tokens: &[crate::Token], _source: &[char]) -> usize {+ if tokens.is_empty() {+ 0+ } else {+ 1+ }+ }+}diff --git a/harper-core/src/patterns/mod.rs b/harper-core/src/patterns/mod.rsindex 9cd709d97..93aa7227b 100644--- a/harper-core/src/patterns/mod.rs+++ b/harper-core/src/patterns/mod.rs@@ -1,17 +1,19 @@use crate::Token;+mod any_pattern;mod naive_pattern_group;mod repeating_pattern;+mod sequence_pattern;mod token_kind_pattern_group;-mod token_pattern;-mod token_sequence_pattern;+mod whitespace_pattern;mod word_pattern_group;+pub use any_pattern::AnyPattern;pub use naive_pattern_group::NaivePatternGroup;pub use repeating_pattern::RepeatingPattern;+pub use sequence_pattern::SequencePattern;pub use token_kind_pattern_group::TokenKindPatternGroup;-pub use token_pattern::TokenPattern;-pub use token_sequence_pattern::SequencePattern;+pub use whitespace_pattern::WhitespacePattern;pub use word_pattern_group::WordPatternGroup;#[cfg(not(feature = "concurrent"))]@@ -23,3 +25,44 @@ pub trait Pattern {pub trait Pattern: Send + Sync {fn matches(&self, tokens: &[Token], source: &[char]) -> usize;}++#[cfg(feature = "concurrent")]+impl<F> Pattern for F+where+ F: Fn(&Token, &[char]) -> bool,+ F: Send + Sync+{+ fn matches(&self, tokens: &[Token], source: &[char]) -> usize {+ if tokens.is_empty() {+ return 0;+ }++ let tok = &tokens[0];++ if self(tok, source) {+ 1+ } else {+ 0+ }+ }+}++#[cfg(not(feature = "concurrent"))]+impl<F> Pattern for F+where+ F: Fn(&Token, &[char]) -> bool+{+ fn matches(&self, tokens: &[Token], source: &[char]) -> usize {+ if tokens.is_empty() {+ return 0;+ }++ let tok = &tokens[0];++ if self(tok, source) {+ 1+ } else {+ 0+ }+ }+}diff --git a/harper-core/src/patterns/repeating_pattern.rs b/harper-core/src/patterns/repeating_pattern.rsindex 3f4971b2c..a066e89ee 100644--- a/harper-core/src/patterns/repeating_pattern.rs+++ b/harper-core/src/patterns/repeating_pattern.rs@@ -33,13 +33,15 @@ impl Pattern for RepeatingPattern {#[cfg(test)]mod tests {use super::RepeatingPattern;- use crate::patterns::{Pattern, TokenPattern};+ use crate::patterns::{AnyPattern, Pattern};use crate::Document;#[test]fn matches_anything() {- let doc = Document::new_plain_english_curated("This matcher will match anything!");- let pat = RepeatingPattern::new(Box::new(TokenPattern::Any));+ let doc = Document::new_plain_english_curated(+ "This matcher will match the entirety of any document!"+ );+ let pat = RepeatingPattern::new(Box::new(AnyPattern));assert_eq!(pat.matches(doc.get_tokens(), doc.get_source()),diff --git a/harper-core/src/patterns/token_sequence_pattern.rs b/harper-core/src/patterns/sequence_pattern.rssimilarity index 64%rename from harper-core/src/patterns/token_sequence_pattern.rsrename to harper-core/src/patterns/sequence_pattern.rsindex e897ffa00..f769769de 100644--- a/harper-core/src/patterns/token_sequence_pattern.rs+++ b/harper-core/src/patterns/sequence_pattern.rs@@ -1,6 +1,7 @@use hashbrown::HashSet;+use paste::paste;-use super::token_pattern::TokenPattern;+use super::whitespace_pattern::WhitespacePattern;use super::{Pattern, RepeatingPattern};use crate::{Lrc, Token, TokenKind};@@ -10,39 +11,81 @@ pub struct SequencePattern {token_patterns: Vec<Box<dyn Pattern>>}+macro_rules! gen_then_from_is {+ ($quality:ident) => {+ paste! {+ fn [< then_$quality >] (&mut self){+ self.token_patterns.push(Box::new(|tok: &Token, _source: &[char]| {+ tok.kind.[< is_$quality >]()+ }))+ }+ }+ };+}+impl SequencePattern {+ gen_then_from_is!(noun);+ gen_then_from_is!(verb);+ gen_then_from_is!(linking_verb);+pub fn then_exact_word(&mut self, word: &'static str) -> &mut Self {self.token_patterns- .push(Box::new(TokenPattern::WordExact(word)));+ .push(Box::new(|tok: &Token, source: &[char]| {+ if !tok.kind.is_word() {+ return false;+ }++ let tok_chars = tok.span.get_content(source);++ let mut w_char_count = 0;+ for (i, w_char) in word.chars().enumerate() {+ w_char_count += 1;++ if tok_chars.get(i).cloned() != Some(w_char) {+ return false;+ }+ }++ w_char_count == tok_chars.len()+ }));self}pub fn then_loose(&mut self, kind: TokenKind) -> &mut Self {self.token_patterns- .push(Box::new(TokenPattern::KindLoose(kind)));+ .push(Box::new(move |tok: &Token, _source: &[char]| {+ kind.with_default_data() == tok.kind.with_default_data()+ }));+self}pub fn then_any_word(&mut self) -> &mut Self {self.token_patterns- .push(Box::new(TokenPattern::KindLoose(TokenKind::blank_word())));+ .push(Box::new(|tok: &Token, _source: &[char]| tok.kind.is_word()));self}pub fn then_strict(&mut self, kind: TokenKind) -> &mut Self {self.token_patterns- .push(Box::new(TokenPattern::KindStrict(kind)));+ .push(Box::new(move |tok: &Token, _source: &[char]| {+ tok.kind == kind+ }));self}pub fn then_whitespace(&mut self) -> &mut Self {- self.token_patterns.push(Box::new(TokenPattern::WhiteSpace));+ self.token_patterns.push(Box::new(WhitespacePattern));self}pub fn then_any_word_in(&mut self, word_set: Lrc<HashSet<&'static str>>) -> &mut Self {self.token_patterns- .push(Box::new(TokenPattern::WordInSet(word_set)));+ .push(Box::new(move |tok: &Token, source: &[char]| {+ let tok_chars = tok.span.get_content(source);+ let word: String = tok_chars.iter().collect();+ word_set.contains(word.as_str())+ }));self}diff --git a/harper-core/src/patterns/token_pattern.rs b/harper-core/src/patterns/token_pattern.rsdeleted file mode 100644index 1c4219532..000000000--- a/harper-core/src/patterns/token_pattern.rs+++ /dev/null@@ -1,81 +0,0 @@-use hashbrown::HashSet;-use is_macro::Is;--use super::Pattern;-use crate::{Lrc, Token, TokenKind};--/// A pattern that matches a single token.-#[derive(Debug, Clone, Is)]-pub enum TokenPattern {- /// Checks that the overall kind of token matches, not whether the inner- /// data does.- KindLoose(TokenKind),- /// Checks that the kind matches exactly.- KindStrict(TokenKind),- WhiteSpace,- WordExact(&'static str),- WordInSet(Lrc<HashSet<&'static str>>),- /// Matches any single token.- Any-}--impl Pattern for TokenPattern {- fn matches(&self, tokens: &[Token], source: &[char]) -> usize {- let Some(tok) = tokens.first() else {- return 0;- };-- match self {- TokenPattern::KindLoose(kind) => {- if kind.with_default_data() == tok.kind.with_default_data() {- 1- } else {- 0- }- }- TokenPattern::KindStrict(kind) => {- if *kind == tok.kind {- 1- } else {- 0- }- }- TokenPattern::WhiteSpace => tokens- .iter()- .position(|t| !t.kind.is_whitespace())- .unwrap_or(tokens.len()),- TokenPattern::WordExact(word) => {- if !tok.kind.is_word() {- return 0;- }-- let tok_chars = tok.span.get_content(source);-- let mut w_char_count = 0;- for (i, w_char) in word.chars().enumerate() {- w_char_count += 1;-- if tok_chars.get(i).cloned() != Some(w_char) {- return 0;- }- }-- if w_char_count == tok_chars.len() {- 1- } else {- 0- }- }- TokenPattern::WordInSet(set) => {- let tok_chars = tok.span.get_content(source);- let word: String = tok_chars.iter().collect();- if set.contains(word.as_str()) {- 1- } else {- 0- }- }- TokenPattern::Any => 1- }- }-}diff --git a/harper-core/src/patterns/whitespace_pattern.rs b/harper-core/src/patterns/whitespace_pattern.rsnew file mode 100644index 000000000..b3348abe6--- /dev/null+++ b/harper-core/src/patterns/whitespace_pattern.rs@@ -0,0 +1,12 @@+use super::Pattern;++pub struct WhitespacePattern;++impl Pattern for WhitespacePattern {+ fn matches(&self, tokens: &[crate::Token], _source: &[char]) -> usize {+ tokens+ .iter()+ .position(|t| !t.kind.is_whitespace())+ .unwrap_or(tokens.len())+ }+}diff --git a/harper-core/src/token.rs b/harper-core/src/token.rsindex c12e00566..835deead7 100644--- a/harper-core/src/token.rs+++ b/harper-core/src/token.rs@@ -204,6 +204,30 @@ impl TokenKind {matches!(self, TokenKind::Punctuation(Punctuation::At))}+ pub fn is_verb(&self) -> bool {+ let TokenKind::Word(metadata) = self else {+ return false;+ };++ metadata.is_verb()+ }++ pub fn is_linking_verb(&self) -> bool {+ let TokenKind::Word(metadata) = self else {+ return false;+ };++ metadata.is_linking_verb()+ }++ pub fn is_noun(&self) -> bool {+ let TokenKind::Word(metadata) = self else {+ return false;+ };++ metadata.is_noun()+ }+/// Checks whether the token is whitespace.pub fn is_whitespace(&self) -> bool {matches!(self, TokenKind::Space(_) | TokenKind::Newline(_))