1- use std:: collections:: HashMap ;
1+ use std:: collections:: { HashMap , HashSet } ;
22
33use trie:: Trie ;
44
5- use crate :: parser:: { dots_to_unicode, fallback, AnchoredRule , Braille , Direction , Rule } ;
5+ use crate :: parser:: { dots_to_unicode, fallback, AnchoredRule , Attribute , Braille , Direction , Rule } ;
66
77use self :: trie:: Boundary ;
88
@@ -115,10 +115,28 @@ fn resolve_implicit_dots(
115115 . collect ( )
116116}
117117
118+ #[ derive( Debug ) ]
119+ struct CharacterAttributes ( HashSet < ( char , Attribute ) > ) ;
120+
121+ impl CharacterAttributes {
122+ fn new ( ) -> Self {
123+ Self ( HashSet :: new ( ) )
124+ }
125+
126+ fn insert ( & mut self , c : char , attribute : Attribute ) {
127+ self . 0 . insert ( ( c, attribute) ) ;
128+ }
129+
130+ fn contains ( & self , c : char , attribute : Attribute ) -> bool {
131+ self . 0 . contains ( & ( c, attribute) )
132+ }
133+ }
134+
118135#[ derive( Debug ) ]
119136pub struct TranslationTable {
120137 undefined : Option < String > ,
121138 character_definitions : CharacterDefinition ,
139+ character_attributes : CharacterAttributes ,
122140 translations : Trie ,
123141 direction : Direction ,
124142}
@@ -130,6 +148,7 @@ impl TranslationTable {
130148 ) -> Result < Self , TranslationError > {
131149 let mut undefined = None ;
132150 let mut character_definitions = CharacterDefinition :: new ( ) ;
151+ let mut character_attributes = CharacterAttributes :: new ( ) ;
133152 let mut translations = Trie :: new ( ) ;
134153
135154 let rules: Vec < AnchoredRule > = rules
@@ -144,35 +163,71 @@ impl TranslationTable {
144163 }
145164 Rule :: Space {
146165 character, dots, ..
166+ } => {
167+ let translation =
168+ Translation :: new ( character. to_string ( ) , dots_to_unicode ( dots) , 1 ) ;
169+ character_definitions. insert ( * character, translation) ;
170+ character_attributes. insert ( * character, Attribute :: Space ) ;
147171 }
148- | Rule :: Punctuation {
172+ Rule :: Punctuation {
149173 character, dots, ..
174+ } => {
175+ let translation =
176+ Translation :: new ( character. to_string ( ) , dots_to_unicode ( dots) , 1 ) ;
177+ character_definitions. insert ( * character, translation) ;
178+ character_attributes. insert ( * character, Attribute :: Punctuation ) ;
150179 }
151- | Rule :: Digit {
180+ Rule :: Digit {
152181 character, dots, ..
153182 }
154- | Rule :: Letter {
183+ | Rule :: Litdigit {
155184 character, dots, ..
185+ } => {
186+ let translation =
187+ Translation :: new ( character. to_string ( ) , dots_to_unicode ( dots) , 1 ) ;
188+ character_definitions. insert ( * character, translation) ;
189+ character_attributes. insert ( * character, Attribute :: Digit ) ;
156190 }
157- | Rule :: Lowercase {
191+ Rule :: Letter {
158192 character, dots, ..
193+ } => {
194+ let translation =
195+ Translation :: new ( character. to_string ( ) , dots_to_unicode ( dots) , 1 ) ;
196+ character_definitions. insert ( * character, translation) ;
197+ character_attributes. insert ( * character, Attribute :: Letter ) ;
159198 }
160- | Rule :: Uppercase {
199+ Rule :: Lowercase {
161200 character, dots, ..
201+ } => {
202+ let translation =
203+ Translation :: new ( character. to_string ( ) , dots_to_unicode ( dots) , 1 ) ;
204+ character_definitions. insert ( * character, translation) ;
205+ character_attributes. insert ( * character, Attribute :: Lowercase ) ;
162206 }
163- | Rule :: Litdigit {
207+ Rule :: Uppercase {
164208 character, dots, ..
209+ } => {
210+ let translation =
211+ Translation :: new ( character. to_string ( ) , dots_to_unicode ( dots) , 1 ) ;
212+ character_definitions. insert ( * character, translation) ;
213+ character_attributes. insert ( * character, Attribute :: Uppercase ) ;
165214 }
166- | Rule :: Sign {
215+ Rule :: Sign {
167216 character, dots, ..
217+ } => {
218+ let translation =
219+ Translation :: new ( character. to_string ( ) , dots_to_unicode ( dots) , 1 ) ;
220+ character_definitions. insert ( * character, translation) ;
221+ character_attributes. insert ( * character, Attribute :: Sign ) ;
168222 }
169- | Rule :: Math {
223+ Rule :: Math {
170224 character, dots, ..
171225 } => {
172226 character_definitions. insert (
173227 * character,
174228 Translation :: new ( character. to_string ( ) , dots_to_unicode ( dots) , 1 ) ,
175229 ) ;
230+ // TODO: should the math opcode not also define a CharacterAttribute?
176231 }
177232 Rule :: Comp6 {
178233 chars,
@@ -452,6 +507,7 @@ impl TranslationTable {
452507 undefined,
453508 direction,
454509 character_definitions,
510+ character_attributes,
455511 translations,
456512 } )
457513 }
0 commit comments