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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
use std::{num::ParseIntError, str::FromStr};

use crate::fullcontext_label::{
    AccentPhraseCurrent, AccentPhrasePrevNext, BreathGroupCurrent, BreathGroupPrevNext, Label,
    Mora, Phoneme, Utterance, Word,
};

/// Errors from jlabel parser.
#[derive(Debug, thiserror::Error)]
pub enum ParseError {
    /// The required symbol was not found.
    #[error("Symbol not found: expected {0}")]
    SymbolNotFound(&'static str),
    /// The position was supposed to be integer, but failed to parse it as integer.
    #[error("Parse int error: {0}")]
    ParseIntError(#[from] ParseIntError),
    /// The position was supposed to be boolean (0 or 1), but failed to parse it as boolean.
    #[error("Parse bool error")]
    ParseBoolError,
    /// The position must always be undefined.
    #[error("Not undefined")]
    NotUndefined,
}

#[derive(Debug)]
struct LabelTokenizer<'a> {
    input: &'a str,
    index: usize,
}

impl<'a> LabelTokenizer<'a> {
    fn new(input: &'a str) -> Self {
        Self { input, index: 0 }
    }

    fn until(&mut self, symbol: &'static str) -> Result<&'a str, ParseError> {
        match self.input[self.index..].find(symbol) {
            Some(i) => {
                let result = &self.input[self.index..(self.index + i)];
                self.index += i + symbol.len();
                Ok(result)
            }
            None => Err(ParseError::SymbolNotFound(symbol)),
        }
    }

    fn string_or_xx(input: &'a str) -> Option<String> {
        if input == "xx" {
            None
        } else {
            Some(input.to_string())
        }
    }

    fn parse_or_xx<T: FromStr>(input: &'a str) -> Result<Option<T>, T::Err> {
        if input == "xx" {
            Ok(None)
        } else {
            input.parse().map(Some)
        }
    }

    fn parse_bool_or_xx(input: &'a str) -> Result<Option<bool>, ParseError> {
        match input {
            "xx" => Ok(None),
            "0" => Ok(Some(false)),
            "1" => Ok(Some(true)),
            _ => Err(ParseError::ParseBoolError),
        }
    }

    fn assert_xx(input: &'a str) -> Result<(), ParseError> {
        if input == "xx" {
            Ok(())
        } else {
            Err(ParseError::NotUndefined)
        }
    }

    /// `p1ˆp2-p3+p4=p5`
    fn p(&mut self) -> Result<Phoneme, ParseError> {
        let p1 = Self::string_or_xx(self.until("^")?);
        let p2 = Self::string_or_xx(self.until("-")?);
        let p3 = Self::string_or_xx(self.until("+")?);
        let p4 = Self::string_or_xx(self.until("=")?);
        let p5 = Self::string_or_xx(self.until("/A:")?);
        Ok(Phoneme {
            p2: p1,
            p1: p2,
            c: p3,
            n1: p4,
            n2: p5,
        })
    }

    /// `/A:a1+a2+a3`
    fn a(&mut self) -> Result<Option<Mora>, ParseError> {
        let a1 = Self::parse_or_xx(self.until("+")?)?;
        let a2 = Self::parse_or_xx(self.until("+")?)?;
        let a3 = Self::parse_or_xx(self.until("/B:")?)?;

        if let (Some(a1), Some(a2), Some(a3)) = (a1, a2, a3) {
            Ok(Some(Mora {
                relative_accent_position: a1,
                position_forward: a2,
                position_backward: a3,
            }))
        } else {
            Ok(None)
        }
    }

    /// `/B:b1-b2_b3`
    fn b(&mut self) -> Result<Option<Word>, ParseError> {
        let b1 = Self::parse_or_xx(self.until("-")?)?;
        let b2 = Self::parse_or_xx(self.until("_")?)?;
        let b3 = Self::parse_or_xx(self.until("/C:")?)?;

        if [b1, b2, b3].iter().all(Option::is_none) {
            Ok(None)
        } else {
            Ok(Some(Word {
                pos: b1,
                ctype: b2,
                cform: b3,
            }))
        }
    }

    /// `/C:c1_c2+c3`
    fn c(&mut self) -> Result<Option<Word>, ParseError> {
        let c1 = Self::parse_or_xx(self.until("_")?)?;
        let c2 = Self::parse_or_xx(self.until("+")?)?;
        let c3 = Self::parse_or_xx(self.until("/D:")?)?;

        if [c1, c2, c3].iter().all(Option::is_none) {
            Ok(None)
        } else {
            Ok(Some(Word {
                pos: c1,
                ctype: c2,
                cform: c3,
            }))
        }
    }

    /// `/D:d1+d2_d3`
    fn d(&mut self) -> Result<Option<Word>, ParseError> {
        let d1 = Self::parse_or_xx(self.until("+")?)?;
        let d2 = Self::parse_or_xx(self.until("_")?)?;
        let d3 = Self::parse_or_xx(self.until("/E:")?)?;

        if [d1, d2, d3].iter().all(Option::is_none) {
            Ok(None)
        } else {
            Ok(Some(Word {
                pos: d1,
                ctype: d2,
                cform: d3,
            }))
        }
    }

    /// `/E:e1_e2!e3_e4-e5`
    fn e(&mut self) -> Result<Option<AccentPhrasePrevNext>, ParseError> {
        let e1 = Self::parse_or_xx(self.until("_")?)?;
        let e2 = Self::parse_or_xx(self.until("!")?)?;
        let e3 = Self::parse_bool_or_xx(self.until("_")?)?;
        Self::assert_xx(self.until("-")?)?;
        let e5 = Self::parse_bool_or_xx(self.until("/F:")?)?;

        if let (Some(e1), Some(e2), Some(e3)) = (e1, e2, e3) {
            Ok(Some(AccentPhrasePrevNext {
                mora_count: e1,
                accent_position: e2,
                is_interrogative: e3,
                is_pause_insertion: e5.map(|e5| !e5),
            }))
        } else {
            Ok(None)
        }
    }

    /// `/F:f1_f2#f3_f4@f5_f6|f7_f8`
    fn f(&mut self) -> Result<Option<AccentPhraseCurrent>, ParseError> {
        let f1 = Self::parse_or_xx(self.until("_")?)?;
        let f2 = Self::parse_or_xx(self.until("#")?)?;
        let f3 = Self::parse_bool_or_xx(self.until("_")?)?;
        Self::assert_xx(self.until("@")?)?;
        let f5 = Self::parse_or_xx(self.until("_")?)?;
        let f6 = Self::parse_or_xx(self.until("|")?)?;
        let f7 = Self::parse_or_xx(self.until("_")?)?;
        let f8 = Self::parse_or_xx(self.until("/G:")?)?;

        if let (Some(f1), Some(f2), Some(f3), Some(f5), Some(f6), Some(f7), Some(f8)) =
            (f1, f2, f3, f5, f6, f7, f8)
        {
            Ok(Some(AccentPhraseCurrent {
                mora_count: f1,
                accent_position: f2,
                is_interrogative: f3,
                accent_phrase_position_forward: f5,
                accent_phrase_position_backward: f6,
                mora_position_forward: f7,
                mora_position_backward: f8,
            }))
        } else {
            Ok(None)
        }
    }

    /// `/G:g1_g2%g3_g4_g5`
    fn g(&mut self) -> Result<Option<AccentPhrasePrevNext>, ParseError> {
        let g1 = Self::parse_or_xx(self.until("_")?)?;
        let g2 = Self::parse_or_xx(self.until("%")?)?;
        let g3 = Self::parse_bool_or_xx(self.until("_")?)?;
        Self::assert_xx(self.until("_")?)?;
        let g5 = Self::parse_bool_or_xx(self.until("/H:")?)?;

        if let (Some(g1), Some(g2), Some(g3)) = (g1, g2, g3) {
            Ok(Some(AccentPhrasePrevNext {
                mora_count: g1,
                accent_position: g2,
                is_interrogative: g3,
                is_pause_insertion: g5.map(|g5| !g5),
            }))
        } else {
            Ok(None)
        }
    }

    /// `/H:h1_h2`
    fn h(&mut self) -> Result<Option<BreathGroupPrevNext>, ParseError> {
        let h1 = Self::parse_or_xx(self.until("_")?)?;
        let h2 = Self::parse_or_xx(self.until("/I:")?)?;

        if let (Some(h1), Some(h2)) = (h1, h2) {
            Ok(Some(BreathGroupPrevNext {
                accent_phrase_count: h1,
                mora_count: h2,
            }))
        } else {
            Ok(None)
        }
    }

    /// `/I:i1-i2@i3+i4&i5-i6|i7+i8`
    fn i(&mut self) -> Result<Option<BreathGroupCurrent>, ParseError> {
        let i1 = Self::parse_or_xx(self.until("-")?)?;
        let i2 = Self::parse_or_xx(self.until("@")?)?;
        let i3 = Self::parse_or_xx(self.until("+")?)?;
        let i4 = Self::parse_or_xx(self.until("&")?)?;
        let i5 = Self::parse_or_xx(self.until("-")?)?;
        let i6 = Self::parse_or_xx(self.until("|")?)?;
        let i7 = Self::parse_or_xx(self.until("+")?)?;
        let i8 = Self::parse_or_xx(self.until("/J:")?)?;

        if let (Some(i1), Some(i2), Some(i3), Some(i4), Some(i5), Some(i6), Some(i7), Some(i8)) =
            (i1, i2, i3, i4, i5, i6, i7, i8)
        {
            Ok(Some(BreathGroupCurrent {
                accent_phrase_count: i1,
                mora_count: i2,
                breath_group_position_forward: i3,
                breath_group_position_backward: i4,
                accent_phrase_position_forward: i5,
                accent_phrase_position_backward: i6,
                mora_position_forward: i7,
                mora_position_backward: i8,
            }))
        } else {
            Ok(None)
        }
    }

    /// `/J:j1_j2`
    fn j(&mut self) -> Result<Option<BreathGroupPrevNext>, ParseError> {
        let j1 = Self::parse_or_xx(self.until("_")?)?;
        let j2 = Self::parse_or_xx(self.until("/K:")?)?;

        if let (Some(j1), Some(j2)) = (j1, j2) {
            Ok(Some(BreathGroupPrevNext {
                accent_phrase_count: j1,
                mora_count: j2,
            }))
        } else {
            Ok(None)
        }
    }

    /// `/K:k1+k2-k3`
    fn k(&mut self) -> Result<Utterance, ParseError> {
        let k1 = self.until("+")?.parse()?;
        let k2 = self.until("-")?.parse()?;
        let k3 = self.input[self.index..].parse()?;

        Ok(Utterance {
            breath_group_count: k1,
            accent_phrase_count: k2,
            mora_count: k3,
        })
    }

    fn consume(mut self) -> Result<Label, ParseError> {
        Ok(Label {
            phoneme: self.p()?,
            mora: self.a()?,
            word_prev: self.b()?,
            word_curr: self.c()?,
            word_next: self.d()?,
            accent_phrase_prev: self.e()?,
            accent_phrase_curr: self.f()?,
            accent_phrase_next: self.g()?,
            breath_group_prev: self.h()?,
            breath_group_curr: self.i()?,
            breath_group_next: self.j()?,
            utterance: self.k()?,
        })
    }
}

impl FromStr for Label {
    type Err = ParseError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        LabelTokenizer::new(s).consume()
    }
}