Skip to content

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

feat: wrote simple pattern for slightly more complex ideas

Elijah Potter9767243c1ebd
diff --git a/harper-core/dictionary.dict b/harper-core/dictionary.dict
index 02fc35f01..6c8d07a80 100644
--- a/harper-core/dictionary.dict
+++ b/harper-core/dictionary.dict
@@ -49003,7 +49003,7 @@ wit/14+SM
witch/14MDSG
witchcraft/1M
witchery/1M
-with/+1~
+with/+~
withal/+
withdraw/41SG
withdrawal/1MS
diff --git a/harper-core/src/patterns/mod.rs b/harper-core/src/patterns/mod.rs
index e290a3146..39fd588b7 100644
--- a/harper-core/src/patterns/mod.rs
+++ b/harper-core/src/patterns/mod.rs
@@ -1,6 +1,6 @@
use std::collections::VecDeque;
-use crate::{Span, Token, VecExt};
+use crate::{document, Document, Span, Token, VecExt};
mod any_pattern;
mod consumes_remaining_pattern;
@@ -122,3 +122,13 @@ where
}
}
}
+
+trait DocPattern {
+ fn find_all_matches_in_doc(&self, document: &Document) -> Vec<Span>;
+}
+
+impl<P: PatternExt> DocPattern for P {
+ fn find_all_matches_in_doc(&self, document: &Document) -> Vec<Span> {
+ self.find_all_matches(document.get_tokens(), document.get_source())
+ }
+}
diff --git a/harper-core/src/patterns/sequence_pattern.rs b/harper-core/src/patterns/sequence_pattern.rs
index 39d538c32..8a75a186e 100644
--- a/harper-core/src/patterns/sequence_pattern.rs
+++ b/harper-core/src/patterns/sequence_pattern.rs
@@ -2,10 +2,10 @@ use hashbrown::HashSet;
use paste::paste;
use super::whitespace_pattern::WhitespacePattern;
-use super::{Pattern, RepeatingPattern};
+use super::{EitherPattern, Pattern, RepeatingPattern};
use crate::{CharStringExt, Lrc, Token, TokenKind};
-/// A pattern that checks that a sequence of others patterns match.
+/// A pattern that checks that a sequence of other patterns match.
#[derive(Default)]
pub struct SequencePattern {
token_patterns: Vec<Box<dyn Pattern>>,
@@ -58,6 +58,22 @@ impl SequencePattern {
gen_then_from_is!(adjective);
gen_then_from_is!(hyphen);
+ /// Add a pattern that looks for more complex ideas, like nouns with adjectives attached.
+ pub fn then_idea(mut self) -> Self {
+ self.then(Box::new(EitherPattern::new(vec![
+ Box::new(
+ SequencePattern::default()
+ .then_one_or_more(Box::new(
+ SequencePattern::default()
+ .then_adjective()
+ .then_whitespace(),
+ ))
+ .then_noun(),
+ ),
+ Box::new(SequencePattern::default().then_noun()),
+ ])))
+ }
+
pub fn then_exact_word(mut self, word: &'static str) -> Self {
self.token_patterns
.push(Box::new(|tok: &Token, source: &[char]| {
@@ -220,8 +236,8 @@ mod tests {
use hashbrown::HashSet;
use super::SequencePattern;
- use crate::patterns::Pattern;
- use crate::{Document, Lrc};
+ use crate::patterns::{DocPattern, Pattern};
+ use crate::{Document, Lrc, Span};
#[test]
fn matches_n_whitespace_tokens() {
@@ -271,4 +287,24 @@ mod tests {
doc.get_tokens().len()
);
}
+
+ #[test]
+ fn simple_idea_apple() {
+ let doc = Document::new_markdown_curated("A red apple");
+ let matches = SequencePattern::default()
+ .then_idea()
+ .find_all_matches_in_doc(&doc);
+
+ assert_eq!(matches, vec![Span::new(0, 5)])
+ }
+
+ #[test]
+ fn complex_idea_apple() {
+ let doc = Document::new_markdown_curated("A red apple with a long stem");
+ let matches = SequencePattern::default()
+ .then_idea()
+ .find_all_matches_in_doc(&doc);
+
+ assert_eq!(matches, vec![Span::new(0, 5), Span::new(8, 13)])
+ }
}