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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
use crate::core::area::Area;
use crate::core::code::UnOptCode;

pub(crate) const COMMANDS: &'static [char] = &['형', '항', '핫', '흣', '흡', '흑'];
const HEARTS: &'static [char] = &[
    '♥', '❤', '💕', '💖', '💗', '💘', '💙', '💚', '💛', '💜', '💝', '♡',
];

/// Check if the character is hangul
///
/// # Example
///
/// ```
/// use hyeong::core::parse;
///
/// assert_eq!(true, parse::is_hangul_syllable('가'));
/// assert_eq!(true, parse::is_hangul_syllable('힣'));
/// assert_eq!(false, parse::is_hangul_syllable('a'));
/// assert_eq!(false, parse::is_hangul_syllable('م'));
/// assert_eq!(false, parse::is_hangul_syllable('ý'));
/// assert_eq!(false, parse::is_hangul_syllable('ם'));
/// assert_eq!(false, parse::is_hangul_syllable('न'));
/// assert_eq!(false, parse::is_hangul_syllable('こ'));
/// assert_eq!(false, parse::is_hangul_syllable('你'));
/// assert_eq!(false, parse::is_hangul_syllable('д'));
/// ```
pub fn is_hangul_syllable(c: char) -> bool {
    '\u{AC00}' <= c && c <= '\u{D7A3}'
}

/// Parse the code to unoptimized code
/// Since the language itself has no compile error, it never returns error.
///
/// # State
///
/// This parsing algorithm is made with state.
/// - `0`: before command starts: hangul, dot, area can come
/// - `1`: when hangul part starts: hangul can come
/// - `2`: when area part starts: hangul, area can come
///
/// # Terms
///
/// - starting character: `혀`, `하` or `흐`
/// - ending character: `엉`, `앙`, `앗`, `읏`, `읍` or `윽`
/// - area character: `?`, `!`, `♥`, `❤`, `💕`, `💖`, `💗`, `💘`, `💙`, `💚`, `💛`, `💜`, `💝` or `♡`
/// - heart character: `♥`, `❤`, `💕`, `💖`, `💗`, `💘`, `💙`, `💚`, `💛`, `💜`, `💝` or `♡`
/// - hangul part, dot part, area part:
///   ```text
///   혀어어어어어어어어어어엉 .............. 💙?💕?♥!💝!!💘
///   <- hangul part -> <- dot part -> <- area part ->
///   ```
///
/// # Algorithm
///
/// ## Preprocessing
///
/// First, we have to preprocess the code to check if each character is valid.
/// In greedy method, if the corresponing character(`엉` for `혀`, `앙` or `앗` for `하`, etc.)
/// is not present after each starting character,
///
/// ## Main Algorithm
///
/// ### 0 State
///
/// In 0 state, we can have different scenarios.
///
/// 1. Before starting the whole command.
///    - goto 1 state when starting character appears.
/// 2. After finishing hangul part.
///    - count dot
/// 3. Before starting the area part. (Similar to 2)
///    - goto 2 state when area character appears.
///
/// ### 1 State
///
/// In 1 state, just count hangul syllables until ending character appears.
/// Then goto state 0(1)
///
/// ### 2 State
///
/// In 2 state, there are two binary operators: `?` and `!`
/// So, we will create two [binary tree](../code/enum.Area.html)s for each operators.
///
/// - `?` operator
///   1. if tree is empty, put `?` as root
///   2. if most right node is heart character, change to to `?` and put it to the left.
///   3. if most right node is `?`, add to the right.
/// - `!` operator
///   1. same as above.
/// - heart character
///   1. if tree is empty, put in
///   2. if most right node is heart character, ignore.
///   3. if most right node is operator, add to the right.
///
/// # Time Complexity
///
/// - `O(n)` where `n := code.len()`
/// - Iterates only twice: once for main loop, once for checking if the character is valid.
///
/// # Example
///
/// ```
/// use hyeong::core::parse;
///
/// let parsed = parse::parse(String::from("형...?💖?"));
///
/// assert_eq!("type: 0, cnt1: 1, cnt2: 3, area: \"?_?💖_\"", format!("{:?}", parsed[0]));
/// ```
pub fn parse(code: String) -> Vec<UnOptCode> {
    let mut res: Vec<UnOptCode> = Vec::new();

    let mut hangul_count = 0usize;
    let mut dot_count = 0usize;
    let mut type_ = 10u8;
    let mut loc = (1usize, 0usize);

    let mut state = 0u8;
    let mut area = Area::Nil;
    let mut leaf = &mut area;
    let mut qu_area = Area::Nil;
    let mut qu_leaf = &mut qu_area;

    let mut line_count = 0;
    let mut last_line_started = 0;
    let mut raw_command = String::new();

    let mut max_pos = [0usize, 0usize, 0usize];
    for (i, c) in code.chars().enumerate() {
        if let Some(t) = "엉앙앗읏읍윽".find(c) {
            max_pos[if t == 0 {
                0
            } else if t <= 6 {
                1
            } else {
                2
            }] = i;
        }
    }

    for (i, c) in code.chars().enumerate() {
        if c.is_whitespace() {
            if c == '\n' {
                line_count += 1;
                last_line_started = i + 1;
            }
            continue;
        }

        state = match state {
            0 | 2 => {
                if let Some(mut t) = "형항핫흣흡흑혀하흐".find(c) {
                    t /= 3;

                    if t >= 6 && max_pos[t - 6] <= i {
                        continue;
                    }

                    if type_ != 10 {
                        res.push(UnOptCode::new(
                            type_,
                            hangul_count,
                            dot_count,
                            loc,
                            match qu_leaf {
                                Area::Val {
                                    type_: _,
                                    left: _,
                                    ref mut right,
                                } => {
                                    *right = Box::new(area);
                                    qu_area
                                }
                                Area::Nil => area,
                            },
                            raw_command,
                        ));

                        area = Area::Nil;
                        leaf = &mut area;
                        qu_area = Area::Nil;
                        qu_leaf = &mut qu_area;
                    }

                    type_ = t as u8;
                    hangul_count = 1;
                    dot_count = 0;
                    loc = (line_count + 1, i - last_line_started);
                    raw_command = c.to_string();

                    if t < 6 {
                        0
                    } else {
                        1
                    }
                } else if ".…⋯⋮".contains(c) {
                    if state == 0 {
                        dot_count += if c == '.' { 1 } else { 3 };
                        raw_command.push(c);
                    }
                    state
                } else if c == '?' {
                    match qu_leaf {
                        Area::Val {
                            type_: _,
                            left: _,
                            ref mut right,
                        } => {
                            *right = Box::new(Area::Val {
                                type_: 0,
                                left: Box::new(area),
                                right: Box::new(Area::Nil),
                            });
                            qu_leaf = &mut *right;
                        }

                        Area::Nil => {
                            qu_area = Area::Val {
                                type_: 0,
                                left: Box::new(area),
                                right: Box::new(Area::Nil),
                            };
                            qu_leaf = &mut qu_area;
                        }
                    }

                    area = Area::Nil;
                    leaf = &mut area;
                    raw_command.push(c);
                    2
                } else if c == '!' {
                    match leaf {
                        Area::Val {
                            ref type_,
                            left: _,
                            ref mut right,
                        } => {
                            if *type_ <= 1 {
                                *right = match right.as_ref() {
                                    Area::Val {
                                        type_: t,
                                        left: _,
                                        right: _,
                                    } => Box::new(Area::Val {
                                        type_: 1,
                                        left: Box::new(Area::new(*t)),
                                        right: Box::new(Area::Nil),
                                    }),
                                    Area::Nil => Box::new(Area::new(1)),
                                };
                                leaf = &mut *right;
                            } else {
                                area = Area::Val {
                                    type_: 1,
                                    left: Box::new(Area::new(*type_)),
                                    right: Box::new(Area::Nil),
                                };
                                leaf = &mut area;
                            }
                        }
                        Area::Nil => {
                            area = Area::new(1);
                            leaf = &mut area;
                        }
                    }
                    raw_command.push(c);
                    2
                } else if let Some(mut t) = HEARTS.iter().position(|&x| x == c) {
                    t += 2;
                    match leaf {
                        Area::Val {
                            ref type_,
                            left: _,
                            ref mut right,
                        } => {
                            if *type_ <= 1 {
                                match right.as_ref() {
                                    Area::Nil => {
                                        *right = Box::new(Area::new(t as u8));
                                    }
                                    _ => {}
                                }
                            }
                        }
                        Area::Nil => {
                            area = Area::new(t as u8);
                            leaf = &mut area;
                        }
                    }
                    raw_command.push(c);
                    2
                } else {
                    continue;
                }
            }

            // 1
            _ => {
                if is_hangul_syllable(c) {
                    hangul_count += 1;
                    raw_command.push(c);
                }
                match type_ {
                    6 => {
                        if "엉".contains(c) {
                            type_ = 0;
                            dot_count = 0;
                            0
                        } else {
                            1
                        }
                    }

                    7 => {
                        if let Some(t) = "앙앗".find(c) {
                            type_ = (t / 3 + 1) as u8;
                            dot_count = 0;
                            0
                        } else {
                            1
                        }
                    }

                    // 8
                    _ => {
                        if let Some(t) = "읏읍윽".find(c) {
                            type_ = (t / 3 + 3) as u8;
                            dot_count = 0;
                            0
                        } else {
                            1
                        }
                    }
                }
            }
        };
    }

    if type_ != 10 {
        res.push(UnOptCode::new(
            type_,
            hangul_count,
            dot_count,
            loc,
            match qu_leaf {
                Area::Val {
                    type_: _,
                    left: _,
                    ref mut right,
                } => {
                    *right = Box::new(area);
                    qu_area
                }
                Area::Nil => area,
            },
            raw_command,
        ));
    }
    res
}