Skip to content

Commit 637fbe6

Browse files
committed
Format the code
1 parent d281f4e commit 637fbe6

File tree

5 files changed

+114
-104
lines changed

5 files changed

+114
-104
lines changed

src/parser/match_rule.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -120,22 +120,23 @@ impl<'a> PatternParser<'a> {
120120
fn set(&mut self) -> Result<Pattern, ParseError> {
121121
self.consume('[')?;
122122
let mut characters = HashSet::new();
123-
while let Some(&c) = self.chars.peek() {
124-
if c == ']' { break; }
125-
else if c == '\\' {
126-
// Handle escape sequence
127-
self.chars.next(); // consume the backslash
128-
if let Some(escaped_char) = self.chars.next_if(|&c| c == ']' || c == '\\') {
123+
while let Some(&c) = self.chars.peek() {
124+
if c == ']' {
125+
break;
126+
} else if c == '\\' {
127+
// Handle escape sequence
128+
self.chars.next(); // consume the backslash
129+
if let Some(escaped_char) = self.chars.next_if(|&c| c == ']' || c == '\\') {
129130
characters.insert(escaped_char);
130-
} else {
131+
} else {
131132
return Err(ParseError::InvalidEscape);
132-
}
133+
}
133134
} else {
134-
// Regular character
135-
characters.insert(c);
136-
self.chars.next();
135+
// Regular character
136+
characters.insert(c);
137+
self.chars.next();
137138
}
138-
}
139+
}
139140
self.consume(']')?;
140141
if characters.is_empty() {
141142
Err(ParseError::EmptyPattern)

src/translator.rs

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -104,15 +104,11 @@ impl CharacterDefinition {
104104
self.0.get(c)
105105
}
106106

107-
fn resolve_implicit_dots(
108-
&self,
109-
chars: &str,
110-
) -> Result<String, TranslationError> {
111-
chars
107+
fn resolve_implicit_dots(&self, chars: &str) -> Result<String, TranslationError> {
108+
chars
112109
.chars()
113110
.map(|c| {
114-
self
115-
.get(&c)
111+
self.get(&c)
116112
.ok_or(TranslationError::ImplicitCharacterNotDefined(c))
117113
.map(|t| t.output.to_string())
118114
})
@@ -128,16 +124,12 @@ impl CharacterDefinition {
128124
///
129125
/// Returns the braille Unicode characters or `TranslationError` if the implicit characters could
130126
/// not be converted.
131-
fn braille_to_unicode(
132-
&self,
133-
dots: &Braille,
134-
chars: &str,
135-
) -> Result<String, TranslationError> {
136-
let dots = match dots {
127+
fn braille_to_unicode(&self, dots: &Braille, chars: &str) -> Result<String, TranslationError> {
128+
let dots = match dots {
137129
Braille::Implicit => self.resolve_implicit_dots(&chars)?,
138130
Braille::Explicit(dots) => dots_to_unicode(&dots),
139-
};
140-
Ok(dots)
131+
};
132+
Ok(dots)
141133
}
142134
}
143135

@@ -366,7 +358,7 @@ impl TranslationTable {
366358
..
367359
} => {
368360
let dots = character_definitions.braille_to_unicode(dots, chars)?;
369-
match_patterns.insert(pre, chars.to_string(), post, dots);
361+
match_patterns.insert(pre, chars.to_string(), post, dots);
370362
}
371363

372364
_ => (),
@@ -379,7 +371,7 @@ impl TranslationTable {
379371
character_definitions,
380372
character_attributes,
381373
translations,
382-
match_patterns,
374+
match_patterns,
383375
})
384376
}
385377

@@ -408,15 +400,16 @@ impl TranslationTable {
408400
// no longer applicable
409401
.partition(|t| t.offset == 0);
410402
delayed_translations.extend(delayed);
411-
// then search for matching match patterns. Unless they have empty pre patterns they will all have
412-
// an offset. Split those off.
413-
let (match_candidates, match_delayed): (Vec<Translation>, Vec<Translation>) = self
414-
.match_patterns
415-
.find_translations(chars.as_str())
416-
.into_iter().partition(|t| t.offset == 0);
403+
// then search for matching match patterns. Unless they have empty pre patterns they will all have
404+
// an offset. Split those off.
405+
let (match_candidates, match_delayed): (Vec<Translation>, Vec<Translation>) = self
406+
.match_patterns
407+
.find_translations(chars.as_str())
408+
.into_iter()
409+
.partition(|t| t.offset == 0);
417410
delayed_translations.extend(match_delayed);
418-
candidates.extend(match_candidates);
419-
// merge the candidates from the match patters with the candidates from the plain translations
411+
candidates.extend(match_candidates);
412+
// merge the candidates from the match patters with the candidates from the plain translations
420413
let (current, delayed): (Vec<Translation>, Vec<Translation>) = delayed_translations
421414
.into_iter()
422415
.partition(|t| t.offset == 0);

src/translator/match_pattern.rs

Lines changed: 56 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -43,40 +43,55 @@ impl From<&Pattern> for AST {
4343
Pattern::Optional(pattern) => AST::Optional(Box::new(AST::from(pattern))),
4444
Pattern::ZeroOrMore(pattern) => AST::ZeroOrMore(Box::new(AST::from(pattern))),
4545
Pattern::OneOrMore(pattern) => AST::OneOrMore(Box::new(AST::from(pattern))),
46-
Pattern::Either(left, right) => AST::Either(Box::new(AST::from(left)), Box::new(AST::from(right)))
46+
Pattern::Either(left, right) => {
47+
AST::Either(Box::new(AST::from(left)), Box::new(AST::from(right)))
48+
}
4749
}
4850
}
4951
}
5052

5153
impl From<&HashSet<Attribute>> for AST {
5254
fn from(items: &HashSet<Attribute>) -> Self {
53-
let mut chars = HashSet::new();
54-
let digits = HashSet::from(['1', '2', '3', '4', '5', '6', '7', '8', '9', '0']);
55-
let letters = HashSet::from(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']);
56-
for attr in items {
57-
match attr {
58-
Attribute::Space => {chars.insert(' ');},
59-
Attribute::Digit => {chars.extend(&digits);},
60-
Attribute::Letter => {chars.extend(&letters)},
61-
Attribute::Uppercase => todo!(),
62-
Attribute::Lowercase => chars.extend(&letters),
63-
Attribute::Punctuation => todo!(),
64-
Attribute::Sign => todo!(),
65-
Attribute::Seqdelimiter => todo!(),
66-
Attribute::Seqbeforechars => todo!(),
67-
Attribute::Seqafterchars => todo!(),
68-
Attribute::Boundary => todo!(),
69-
Attribute::UserDefined(_) => todo!(),
70-
}
71-
}
72-
AST::Set(chars)
55+
let mut chars = HashSet::new();
56+
let digits = HashSet::from(['1', '2', '3', '4', '5', '6', '7', '8', '9', '0']);
57+
let letters = HashSet::from([
58+
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q',
59+
'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
60+
]);
61+
for attr in items {
62+
match attr {
63+
Attribute::Space => {
64+
chars.insert(' ');
65+
}
66+
Attribute::Digit => {
67+
chars.extend(&digits);
68+
}
69+
Attribute::Letter => chars.extend(&letters),
70+
Attribute::Uppercase => todo!(),
71+
Attribute::Lowercase => chars.extend(&letters),
72+
Attribute::Punctuation => todo!(),
73+
Attribute::Sign => todo!(),
74+
Attribute::Seqdelimiter => todo!(),
75+
Attribute::Seqbeforechars => todo!(),
76+
Attribute::Seqafterchars => todo!(),
77+
Attribute::Boundary => todo!(),
78+
Attribute::UserDefined(_) => todo!(),
79+
}
80+
}
81+
AST::Set(chars)
7382
}
7483
}
7584

7685
impl AST {
7786
/// Combine the pre and post patterns with the match characters into one big regexp AST by joining them with concat
7887
fn from_match_rule(pre: &Patterns, chars: String, post: &Patterns) -> Self {
79-
AST::Concat(Box::new(AST::Concat(Box::new(AST::Concat(Box::new(AST::from(pre)), Box::new(AST::Offset))), Box::new(AST::String(chars)))), Box::new(AST::from(post)))
88+
AST::Concat(
89+
Box::new(AST::Concat(
90+
Box::new(AST::Concat(Box::new(AST::from(pre)), Box::new(AST::Offset))),
91+
Box::new(AST::String(chars)),
92+
)),
93+
Box::new(AST::from(post)),
94+
)
8095
}
8196
}
8297

@@ -93,13 +108,13 @@ impl MatchPatterns {
93108
}
94109

95110
pub fn insert(&mut self, pre: &Patterns, chars: String, post: &Patterns, to: String) {
96-
let translation = Translation::new(chars.clone(), to, 0);
97-
let ast = AST::from_match_rule(pre, chars, post);
98-
self.nfa.merge_accepting_fragment(&ast, translation);
111+
let translation = Translation::new(chars.clone(), to, 0);
112+
let ast = AST::from_match_rule(pre, chars, post);
113+
self.nfa.merge_accepting_fragment(&ast, translation);
99114
}
100115

101116
pub fn find_translations(&self, input: &str) -> Vec<Translation> {
102-
self.nfa.find_translations(input)
117+
self.nfa.find_translations(input)
103118
}
104119
}
105120

@@ -112,7 +127,7 @@ mod tests {
112127
fn find_pattern() {
113128
let patterns = PatternParser::new("abc").pattern().unwrap();
114129
let translation = Translation::default();
115-
let ast = AST::from(&patterns);
130+
let ast = AST::from(&patterns);
116131
let nfa = NFA::from(&ast);
117132
assert_eq!(nfa.find_translations("abc"), vec![translation]);
118133
assert!(nfa.find_translations("def").is_empty());
@@ -122,7 +137,7 @@ mod tests {
122137
fn find_character_class() {
123138
let patterns = PatternParser::new("[abc]").pattern().unwrap();
124139
let translation = Translation::default();
125-
let ast = AST::from(&patterns);
140+
let ast = AST::from(&patterns);
126141
let nfa = NFA::from(&ast);
127142
assert_eq!(nfa.find_translations("a"), vec![translation.clone()]);
128143
assert_eq!(nfa.find_translations("b"), vec![translation.clone()]);
@@ -134,7 +149,7 @@ mod tests {
134149
fn find_character_class_one_or_more() {
135150
let patterns = PatternParser::new("[abc]+").pattern().unwrap();
136151
let translation = Translation::default();
137-
let ast = AST::from(&patterns);
152+
let ast = AST::from(&patterns);
138153
let nfa = NFA::from(&ast);
139154
assert_eq!(nfa.find_translations("a"), vec![translation.clone()]);
140155
assert_eq!(nfa.find_translations("b"), vec![translation.clone()]);
@@ -146,16 +161,16 @@ mod tests {
146161
fn find_match() {
147162
let pre = PatternParser::new("[abc]+").pattern().unwrap();
148163
let post = PatternParser::new("[123]+").pattern().unwrap();
149-
let ast = AST::from_match_rule(&pre, "foo".into(), &post);
164+
let ast = AST::from_match_rule(&pre, "foo".into(), &post);
150165
let nfa = NFA::from(&ast);
151-
let translation = Translation::new("".into(), "".into(), 5).with_offset(1);
166+
let translation = Translation::new("".into(), "".into(), 5).with_offset(1);
152167
assert_eq!(nfa.find_translations("afoo1"), vec![translation.clone()]);
153168
assert_eq!(nfa.find_translations("bfoo2"), vec![translation.clone()]);
154-
let translations = vec![
155-
Translation::new("".into(), "".into(), 9).with_offset(3),
156-
Translation::new("".into(), "".into(), 8).with_offset(3),
157-
Translation::new("".into(), "".into(), 7).with_offset(3),
158-
];
169+
let translations = vec![
170+
Translation::new("".into(), "".into(), 9).with_offset(3),
171+
Translation::new("".into(), "".into(), 8).with_offset(3),
172+
Translation::new("".into(), "".into(), 7).with_offset(3),
173+
];
159174
assert_eq!(nfa.find_translations("cccfoo333"), translations);
160175
assert!(nfa.find_translations("def").is_empty());
161176
}
@@ -164,14 +179,13 @@ mod tests {
164179
fn find_multiple_match() {
165180
let pre = PatternParser::new("[abc]+").pattern().unwrap();
166181
let post = PatternParser::new("[1234567890]").pattern().unwrap();
167-
let mut match_patterns = MatchPatterns::new();
168-
match_patterns.insert(&pre, "foo".into(), &post, "FOO".into());
169-
match_patterns.insert(&pre, "bar".into(), &post, "BAR".into());
170-
let translation = vec![Translation::new("foo".into(), "FOO".into(), 7).with_offset(3)];
182+
let mut match_patterns = MatchPatterns::new();
183+
match_patterns.insert(&pre, "foo".into(), &post, "FOO".into());
184+
match_patterns.insert(&pre, "bar".into(), &post, "BAR".into());
185+
let translation = vec![Translation::new("foo".into(), "FOO".into(), 7).with_offset(3)];
171186
assert_eq!(match_patterns.find_translations("aaafoo333"), translation);
172-
let translation = vec![Translation::new("bar".into(), "BAR".into(), 7).with_offset(3)];
187+
let translation = vec![Translation::new("bar".into(), "BAR".into(), 7).with_offset(3)];
173188
assert_eq!(match_patterns.find_translations("aaabar333"), translation);
174189
assert_ne!(match_patterns.find_translations("aaabaz333"), translation);
175190
}
176-
177191
}

0 commit comments

Comments
 (0)