Skip to content

Commit

Permalink
Collect the character attributes of a table
Browse files Browse the repository at this point in the history
  • Loading branch information
egli committed Dec 11, 2024
1 parent 07fe348 commit 973fed5
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use self::{

pub use braille::dots_to_unicode;
pub use braille::fallback;
pub use match_rule::{Pattern, Patterns};
pub use match_rule::{Attribute, Pattern, Patterns};

mod braille;
mod match_rule;
Expand Down
76 changes: 66 additions & 10 deletions src/translator.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::collections::HashMap;
use std::collections::{HashMap, HashSet};

use trie::Trie;

use crate::parser::{dots_to_unicode, fallback, AnchoredRule, Braille, Direction, Rule};
use crate::parser::{dots_to_unicode, fallback, AnchoredRule, Attribute, Braille, Direction, Rule};

use self::trie::Boundary;

Expand Down Expand Up @@ -115,10 +115,28 @@ fn resolve_implicit_dots(
.collect()
}

#[derive(Debug)]
struct CharacterAttributes(HashSet<(char, Attribute)>);

impl CharacterAttributes {
fn new() -> Self {
Self(HashSet::new())
}

fn insert(&mut self, c: char, attribute: Attribute) {
self.0.insert((c, attribute));
}

fn contains(&self, c: char, attribute: Attribute) -> bool {
self.0.contains(&(c, attribute))
}
}

#[derive(Debug)]
pub struct TranslationTable {
undefined: Option<String>,
character_definitions: CharacterDefinition,
character_attributes: CharacterAttributes,
translations: Trie,
direction: Direction,
}
Expand All @@ -130,6 +148,7 @@ impl TranslationTable {
) -> Result<Self, TranslationError> {
let mut undefined = None;
let mut character_definitions = CharacterDefinition::new();
let mut character_attributes = CharacterAttributes::new();
let mut translations = Trie::new();

let rules: Vec<AnchoredRule> = rules
Expand All @@ -144,35 +163,71 @@ impl TranslationTable {
}
Rule::Space {
character, dots, ..
} => {
let translation =
Translation::new(character.to_string(), dots_to_unicode(dots), 1);
character_definitions.insert(*character, translation);
character_attributes.insert(*character, Attribute::Space);
}
| Rule::Punctuation {
Rule::Punctuation {
character, dots, ..
} => {
let translation =
Translation::new(character.to_string(), dots_to_unicode(dots), 1);
character_definitions.insert(*character, translation);
character_attributes.insert(*character, Attribute::Punctuation);
}
| Rule::Digit {
Rule::Digit {
character, dots, ..
}
| Rule::Letter {
| Rule::Litdigit {
character, dots, ..
} => {
let translation =
Translation::new(character.to_string(), dots_to_unicode(dots), 1);
character_definitions.insert(*character, translation);
character_attributes.insert(*character, Attribute::Digit);
}
| Rule::Lowercase {
Rule::Letter {
character, dots, ..
} => {
let translation =
Translation::new(character.to_string(), dots_to_unicode(dots), 1);
character_definitions.insert(*character, translation);
character_attributes.insert(*character, Attribute::Letter);
}
| Rule::Uppercase {
Rule::Lowercase {
character, dots, ..
} => {
let translation =
Translation::new(character.to_string(), dots_to_unicode(dots), 1);
character_definitions.insert(*character, translation);
character_attributes.insert(*character, Attribute::Lowercase);
}
| Rule::Litdigit {
Rule::Uppercase {
character, dots, ..
} => {
let translation =
Translation::new(character.to_string(), dots_to_unicode(dots), 1);
character_definitions.insert(*character, translation);
character_attributes.insert(*character, Attribute::Uppercase);
}
| Rule::Sign {
Rule::Sign {
character, dots, ..
} => {
let translation =
Translation::new(character.to_string(), dots_to_unicode(dots), 1);
character_definitions.insert(*character, translation);
character_attributes.insert(*character, Attribute::Sign);
}
| Rule::Math {
Rule::Math {
character, dots, ..
} => {
character_definitions.insert(
*character,
Translation::new(character.to_string(), dots_to_unicode(dots), 1),
);
// TODO: should the math opcode not also define a CharacterAttribute?
}
Rule::Comp6 {
chars,
Expand Down Expand Up @@ -452,6 +507,7 @@ impl TranslationTable {
undefined,
direction,
character_definitions,
character_attributes,
translations,
})
}
Expand Down

0 comments on commit 973fed5

Please sign in to comment.