harper_core/expr/
sequence_expr.rs1use paste::paste;
2
3use crate::{
4 Span, Token, TokenKind,
5 patterns::{AnyPattern, IndefiniteArticle, WhitespacePattern, Word},
6};
7
8use super::{Expr, Repeating, Step, UnlessStep};
9
10#[derive(Default)]
11pub struct SequenceExpr {
12 exprs: Vec<Box<dyn Expr>>,
13}
14
15macro_rules! gen_then_from_is {
17 ($quality:ident) => {
18 paste! {
19 pub fn [< then_$quality >] (self) -> Self{
20 self.then(|tok: &Token, _source: &[char]| {
21 tok.kind.[< is_$quality >]()
22 })
23 }
24
25 pub fn [< then_one_or_more_$quality s >] (self) -> Self{
26 self.then_one_or_more(Box::new(|tok: &Token, _source: &[char]| {
27 tok.kind.[< is_$quality >]()
28 }))
29 }
30
31 pub fn [< then_anything_but_$quality >] (self) -> Self{
32 self.then(|tok: &Token, _source: &[char]| {
33 if tok.kind.[< is_$quality >](){
34 false
35 }else{
36 true
37 }
38 })
39 }
40 }
41 };
42}
43
44impl Expr for SequenceExpr {
45 fn run(&self, mut cursor: usize, tokens: &[Token], source: &[char]) -> Option<Span> {
49 let mut window = Span::new_with_len(cursor, 0);
50
51 for cur_expr in &self.exprs {
52 let out = cur_expr.run(cursor, tokens, source)?;
53
54 window.expand_to_include(out.start);
55 window.expand_to_include(out.end - 1);
56
57 if out.start < cursor {
58 cursor = out.start;
59 } else {
60 cursor = out.end;
61 }
62 }
63
64 Some(window)
65 }
66}
67
68impl SequenceExpr {
69 pub fn then(mut self, window: impl Expr + 'static) -> Self {
70 self.exprs.push(Box::new(window));
71 self
72 }
73
74 pub fn then_expr(mut self, mut other: Self) -> Self {
76 self.exprs.append(&mut other.exprs);
77 self
78 }
79
80 pub fn then_indefinite_article(self) -> Self {
81 self.then(IndefiniteArticle::default())
82 }
83
84 pub fn then_exact_word(self, word: &'static str) -> Self {
86 self.then(Word::new_exact(word))
87 }
88
89 pub fn aco(word: &'static str) -> Self {
91 Self::any_capitalization_of(word)
92 }
93
94 pub fn any_capitalization_of(word: &'static str) -> Self {
95 Self::default().then_any_capitalization_of(word)
96 }
97
98 pub fn t_aco(self, word: &'static str) -> Self {
100 self.then_any_capitalization_of(word)
101 }
102
103 pub fn then_any_capitalization_of(self, word: &'static str) -> Self {
105 self.then(Word::new(word))
106 }
107
108 pub fn then_any_word(self) -> Self {
110 self.then(|tok: &Token, _source: &[char]| tok.kind.is_word())
111 }
112
113 pub fn then_strict(self, kind: TokenKind) -> Self {
115 self.then(move |tok: &Token, _source: &[char]| tok.kind == kind)
116 }
117
118 pub fn t_ws(self) -> Self {
120 self.then_whitespace()
121 }
122
123 pub fn then_whitespace(self) -> Self {
125 self.then(WhitespacePattern)
126 }
127
128 pub fn then_one_or_more(self, expr: impl Expr + 'static) -> Self {
129 self.then(Repeating::new(Box::new(expr), 1))
130 }
131
132 pub fn if_not_then_step_one(self, condition: impl Expr + 'static) -> Self {
134 self.then(UnlessStep::new(condition, |_tok: &Token, _src: &[char]| {
135 true
136 }))
137 }
138
139 pub fn t_any(self) -> Self {
140 self.then_anything()
141 }
142
143 pub fn then_anything(self) -> Self {
144 self.then(AnyPattern)
145 }
146
147 gen_then_from_is!(nominal);
148 gen_then_from_is!(noun);
149 gen_then_from_is!(possessive_nominal);
150 gen_then_from_is!(plural_nominal);
151 gen_then_from_is!(verb);
152 gen_then_from_is!(auxiliary_verb);
153 gen_then_from_is!(linking_verb);
154 gen_then_from_is!(pronoun);
155 gen_then_from_is!(punctuation);
156 gen_then_from_is!(conjunction);
157 gen_then_from_is!(comma);
158 gen_then_from_is!(period);
159 gen_then_from_is!(number);
160 gen_then_from_is!(case_separator);
161 gen_then_from_is!(adverb);
162 gen_then_from_is!(adjective);
163 gen_then_from_is!(apostrophe);
164 gen_then_from_is!(hyphen);
165 gen_then_from_is!(determiner);
166 gen_then_from_is!(proper_noun);
167 gen_then_from_is!(preposition);
168 gen_then_from_is!(not_plural_nominal);
169}
170
171impl<S> From<S> for SequenceExpr
172where
173 S: Step + 'static,
174{
175 fn from(step: S) -> Self {
176 Self {
177 exprs: vec![Box::new(step)],
178 }
179 }
180}