1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
use crate::{characters::*, Token};
use super::{ParseAction, TengwarMode};
pub const CARRIER_DIPH_I: char = TENGWA_YANTA;
pub const CARRIER_DIPH_U: char = TENGWA_URE;
pub const fn consonant_char(slice: &[char]) -> Option<char> {
Some(match slice {
// Tincotéma.
['t'] /**/ => TEMA_TINCO.single_dn,
['d']
| ['n', 'd'] /**/ => TEMA_TINCO.double_dn,
['t', 'h']
| ['þ'] | ['θ'] /**/ => TEMA_TINCO.single_up,
['n', 't'] /**/ => TEMA_TINCO.double_up,
['n'] /**/ => TEMA_TINCO.double_sh,
['r'] /**/ => TEMA_TINCO.single_sh,
// Parmatéma.
['p'] /**/ => TEMA_PARMA.single_dn,
['b']
| ['m', 'b'] /**/ => TEMA_PARMA.double_dn,
['f'] | ['φ'] /**/ => TEMA_PARMA.single_up,
['m', 'p'] /**/ => TEMA_PARMA.double_up,
['m'] /**/ => TEMA_PARMA.double_sh,
['v'] /**/ => TEMA_PARMA.single_sh,
// Calmatéma.
['c'] | ['k'] /**/ => TEMA_CALMA.single_dn,
['g']
| ['n', 'g'] /**/ => TEMA_CALMA.double_dn,
['c', 'h']
| ['k', 'h'] /**/ => TEMA_CALMA.single_up,
['n', 'c'] /**/ => TEMA_CALMA.double_up,
['ñ'] /**/ => TEMA_CALMA.double_sh,
// ['y'] /**/ => TEMA_CALMA.single_sh, // Special case.
// NOTE: This sound vanished from Quenya very early. However, it may
// still be useful to have a way to write, either for representing
// very old texts or (more likely) to represent a "vanished"
// consonant (such as in "ʒalda").
// https://at.mansbjorkman.net/teng_quenya.htm#note_anna
['g', 'h']
| ['ɣ'] | ['ʒ'] /**/ => TEMA_CALMA.single_sh,
// Qessetéma.
['q']
| ['q', 'u']
| ['c', 'w']
| ['k', 'w'] /**/ => TEMA_QESSE.single_dn,
['n', 'g', 'w'] /**/ => TEMA_QESSE.double_dn,
['h', 'w'] /**/ => TEMA_QESSE.single_up,
['n', 'q', 'u']
| ['n', 'q'] /**/ => TEMA_QESSE.double_up,
['ñ', 'w'] /**/ => TEMA_QESSE.double_sh,
['w'] /**/ => TEMA_QESSE.single_sh,
// Irregulars.
// ['r'] /**/ => TENGWA_ROMEN, // Only with following vowel.
['r', 'd'] /**/ => TENGWA_ARDA,
['l'] /**/ => TENGWA_LAMBE,
['l', 'd'] /**/ => TENGWA_ALDA,
['s'] /**/ => TENGWA_SILME,
['s', 's']
| ['z'] | ['ß'] /**/ => TENGWA_ESSE,
['h'] /**/ => TENGWA_HYARMEN,
_ => { return None; }
})
}
pub const fn get_consonant(slice: &[char]) -> Option<Glyph> {
match consonant_char(slice) {
Some(cons) => Some(Glyph::new_base(cons)),
None => match slice {
&[a, b] if a == b => match consonant_char(&[a]) {
Some(cons) => Some(Glyph::new_base(cons).with_underline(true)),
None => None,
}
_ => None,
}
}
}
pub const fn get_diphthong(slice: &[char]) -> Option<Glyph> {
match slice {
['a', 'i'] => Some(Glyph::new_both(CARRIER_DIPH_I, TEHTA_A)),
['o', 'i'] => Some(Glyph::new_both(CARRIER_DIPH_I, TEHTA_O)),
['u', 'i'] => Some(Glyph::new_both(CARRIER_DIPH_I, TEHTA_U)),
['a', 'u'] => Some(Glyph::new_both(CARRIER_DIPH_U, TEHTA_A)),
['e', 'u'] => Some(Glyph::new_both(CARRIER_DIPH_U, TEHTA_E)),
['i', 'u'] => Some(Glyph::new_both(CARRIER_DIPH_U, TEHTA_I)),
_ => None,
}
}
pub const fn get_tehta(slice: &[char]) -> Option<(Tehta, bool)> {
match slice {
['a'] | ['ä'] => Some((TEHTA_A, false)),
['e'] | ['ë'] => Some((TEHTA_E, false)),
['i'] | ['ï'] => Some((TEHTA_I, false)),
['o'] | ['ö'] => Some((TEHTA_O, false)),
['u'] | ['ü'] => Some((TEHTA_U, false)),
['á'] | ['a', 'a'] => Some((TEHTA_A, true)),
['é'] | ['e', 'e'] => Some((TEHTA_E, true)),
['í'] | ['i', 'i'] => Some((TEHTA_I, true)),
['ó'] | ['o', 'o'] => Some((TEHTA_O, true)),
['ú'] | ['u', 'u'] => Some((TEHTA_U, true)),
_ => None,
}
}
pub const fn get_vowel_glyph(slice: &[char]) -> Option<Glyph> {
if let Some(glyph) = get_diphthong(slice) {
Some(glyph)
} else if let Some((tehta, alt)) = get_tehta(slice) {
Some(Glyph::new_vowel(tehta, alt))
} else {
None
}
}
/// The Classical Mode, developed by Fëanáro Finwion in Valinor, during the
/// Years of the Trees, for writing Quenya.
#[derive(Clone, Copy, Debug, Default)]
pub struct Quenya {
current: Option<Glyph>,
previous: Option<Glyph>,
}
impl TengwarMode for Quenya {
fn finish_current(&mut self) -> Option<Token> {
self.previous = self.current.take();
self.previous.map(Token::Glyph)
}
fn process(&mut self, chunk: &[char]) -> ParseAction {
macro_rules! finish {
($glyph:expr) => {finish!($glyph, 0)};
($glyph:expr, $len:expr) => {replace!($glyph, None, $len)}
}
macro_rules! replace {
($old:expr, $new:expr, $len:expr) => {{
let finished = $old;
self.current = $new;
self.previous = Some(finished);
ParseAction::MatchedToken {
token: Token::Glyph(finished),
len: $len,
}
}};
}
let initial: bool = self.previous.is_none();
if let [ESC, ESC_NOP, ..] = chunk {
self.previous = None;
ParseAction::matched_opt(self.current.take().map(Token::Glyph), 2)
} else if let [ESC, _, ..] = chunk {
ParseAction::ESC_BACKSLASH
} else if let Some(current) = &mut self.current {
// A glyph is currently being constructed. Try to continue it.
match ¤t.tehta {
Some(_) => ParseAction::MatchedNone,
None => match chunk {
['y', ..] if !current.palatal => {
current.palatal = true;
ParseAction::MatchedPart(1)
}
['s' | 'z'] if current.can_take_rince() => {
// FIXME: This is a POLICY LEVEL DECISION. It CANNOT be
// made at the Mode level while also assuming the
// Policy to be Standard.
// TODO: Make TengwarMode generic over Policy, or find
// a way to defer this decision to the TokenIter.
current.rince = true;
ParseAction::MatchedPart(1)
}
['s', 's'] => {
// This cannot modify a consonant, but if the window is
// allowed to narrow, it will become ['s'], which
// will modify it incorrectly. Need to output the
// current glyph immediately.
finish!(*current)
}
['l' | 'r'] => {
// The H in softened HL and HR is represented by Halla,
// not Hyarmen.
current.replace_base(TENGWA_HYARMEN, TENGWA_HALLA);
finish!(*current)
}
_ => {
if let Some(new) = get_diphthong(chunk) {
// A following vowel sound changes Órë to Rómen.
current.replace_base(TENGWA_ORE, TENGWA_ROMEN);
// Set the diphthong as the current glyph, while
// returning the glyph that was in progress.
replace!(*current, Some(new), chunk.len())
} else if let Some((tehta, alt)) = get_tehta(chunk) {
// A following vowel sound changes Órë to Rómen.
current.replace_base(TENGWA_ORE, TENGWA_ROMEN);
current.tehta = Some(tehta);
current.tehta_alt = alt;
// In the Classical mode, a vowel tehta is the last
// modification that can be made to a tengwa.
// Nothing else after the vowel can affect the
// glyph, so finish it now.
finish!(*current, chunk.len())
} else {
ParseAction::MatchedNone
}
}
}
}
} else {
// Try to find a new glyph.
// Check for special cases.
if initial && ['n', 'w'] == chunk {
self.current = Some(Glyph::new_base(TENGWA_NWALME));
ParseAction::MatchedPart(2)
} else if let ['x', ..] = chunk {
self.current = Some(Glyph::new_base(TENGWA_CALMA).with_rince(true));
ParseAction::MatchedPart(1)
} else if let ['y', ..] = chunk {
self.current = Some(Glyph::new_base(TENGWA_ANNA).with_palatal(true));
ParseAction::MatchedPart(1)
}
// Check for a consonant.
else if let Some(mut new) = get_consonant(chunk) {
if initial {
// TODO: These special cases allow for using basic ASCII
// `ng`, instead of needing to use `ñ`, to specify the
// archaic initial spellings. However, this approach
// will also be applied to detached suffixes, such as
// "-ngwë" (which is technically initial, but does not
// truly represent the initial variant of the tengwa).
// Checking one token previous would not fix it either,
// because that would then misspell hyphenated compound
// phrases like "etya-ngoldorin".
// Initial NG is represented by Ñoldo, not Anga.
new.replace_base(TENGWA_ANGA, TENGWA_NOLDO);
// Initial NGW is represented by Ñwalmë, not Ungwë.
new.replace_base(TENGWA_UNGWE, TENGWA_NWALME);
} else {
// Medial H is represented by Aha, not Hyarmen.
new.replace_base(TENGWA_HYARMEN, TENGWA_AHA);
}
self.current = Some(new);
ParseAction::MatchedPart(chunk.len())
}
// Check for a vowel or diphthong.
else if let Some(new) = get_vowel_glyph(chunk) {
self.previous = Some(new);
ParseAction::MatchedToken {
token: Token::Glyph(new),
len: chunk.len(),
}
} else {
ParseAction::MatchedNone
}
}
}
}