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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
use crate::{
    attribute::{Dimension, DimensionUnit, Glue},
    event::DelimiterType,
};

use super::{tables::token_to_delim, Argument, CharToken, ErrorKind, InnerResult, Token};

/// Parse the right-hand side of a definition (TeXBook p. 271).
///
/// In this case, a definition is any of `def`, `edef`, `gdef`, or `xdef`.
///
/// Returns the control sequence, the parameter text, and the replacement text.
pub fn definition<'a>(input: &mut &'a str) -> InnerResult<(&'a str, &'a str, &'a str)> {
    let control_sequence = control_sequence(input)?;
    let (parameter_text, rest) = input.split_once('{').ok_or(ErrorKind::EndOfInput)?;

    if let Some(idx) = parameter_text.find(|c: char| c == '%' || c == '}') {
        return Err(if parameter_text.as_bytes()[idx] == b'%' {
            ErrorKind::CommentInParamText
        } else {
            ErrorKind::BracesInParamText
        })
    }
    
    *input = rest;
    let replacement_text = group_content(input, "{", "}")?;

    Ok((control_sequence, parameter_text, replacement_text))
}

/// Parse an argument to a control sequence, and return it.
pub fn argument<'a>(input: &mut &'a str) -> InnerResult<Argument<'a>> {
    if let Some(rest) = input.trim_start().strip_prefix('{') {
        *input = rest;
        let content = group_content(input, "{", "}")?;
        Ok(Argument::Group(content))
    } else {
        Ok(Argument::Token(token(input)?))
    }
}

pub fn optional_argument<'a>(input: &mut &'a str) -> InnerResult<Option<&'a str>> {
    if let Some(rest) = input.trim_start().strip_prefix('[') {
        *input = rest;
        let content = group_content(input, "[", "]")?;
        Ok(Some(content))
    } else {
        Ok(None)
    }
}

pub fn brace_argument<'a>(input: &mut &'a str) -> InnerResult<&'a str> {
    if let Some(rest) = input.trim_start().strip_prefix('{') {
        *input = rest;
        group_content(input, "{", "}")
    } else {
        Err(ErrorKind::GroupArgument)
    }
}

/// Parses the inside of a group, when the first opening tag is already parsed.
///
/// The output is the content within the group without the surrounding `start` and `end`.
/// This content is guaranteed to be balanced.
pub fn group_content<'a>(input: &mut &'a str, start: &str, end: &str) -> InnerResult<&'a str> {
    let mut escaped = false;
    let mut index = 0;
    let mut depth = 0u32;
    let bytes = input.as_bytes();
    while escaped || depth > 0 || !bytes[index..].starts_with(end.as_bytes()) {
        if index + end.len() > input.len() {
            *input = &input[input.len()..];
            return Err(ErrorKind::UnbalancedGroup(None));
        }
        if !escaped && bytes[index..].starts_with(start.as_bytes()) {
            depth += 1;
            index += start.len();
            continue;
        }
        if !escaped && bytes[index..].starts_with(end.as_bytes()) {
            if depth.checked_sub(1).is_none() {
                break;
            }
            depth -= 1;
            index += end.len();
            continue;
        }
        match bytes[index] {
            b'\\' => escaped = !escaped,
            b'%' if !escaped => {
                let rest_pos = bytes[index..]
                    .iter()
                    .position(|&c| c == b'\n')
                    .unwrap_or(bytes.len());
                index += rest_pos;
            }
            _ => escaped = false,
        }
        index += 1;
    }
    let (argument, rest) = input.split_at(index);
    *input = &rest[end.len()..];
    Ok(argument)
}

/// Finds the shortest prefix of `input` that contains balanced groups and ends with the given `suffix`.
pub fn content_with_suffix<'a>(input: &mut &'a str, suffix: &str) -> InnerResult<&'a str> {
    let mut escaped = false;
    let mut index = 0;
    let bytes = input.as_bytes();
    while escaped || !bytes[index..].starts_with(suffix.as_bytes()) {
        if index + suffix.len() > input.len() {
            *input = &input[input.len()..];
            return Err(ErrorKind::EndOfInput);
        }
        match bytes[index] {
            b'\\' => escaped = !escaped,
            b'%' if !escaped => {
                let rest_pos = bytes[index..]
                    .iter()
                    .position(|&c| c == b'\n')
                    .unwrap_or(bytes.len());
                index += rest_pos;
            }
            b'{' if !escaped => {
                let conetent = group_content(&mut &input[index + 1..], "{", "}")?;
                index += conetent.len() + 1;
            }
            _ => escaped = false,
        }
        index += 1;
    }
    let (argument, rest) = input.split_at(index);
    *input = &rest[suffix.len()..];
    Ok(argument)
}

/// Converts a control sequence or character into its corresponding delimiter unicode
/// character, and whether or not the delimiter is an opening.
///
/// Current delimiters supported are listed in TeXBook p. 146, and on https://temml.org/docs/en/supported ("delimiter" section).
pub fn delimiter(input: &mut &str) -> InnerResult<(char, DelimiterType)> {
    let maybe_delim = token(input)?;
    token_to_delim(maybe_delim).ok_or(ErrorKind::Delimiter)
}

/// Parse the right-hand side of a `futurelet` assignment (TeXBook p. 273).
///
/// Returns the control sequence, the token it should be assigned to, and the rest of the input
/// with both tokens not consumed.
pub fn futurelet_assignment<'a>(
    input: &mut &'a str,
) -> InnerResult<(&'a str, Token<'a>, &'a str)> {
    let control_sequence = control_sequence(input)?;

    let input_with_tokens = *input;

    let _ = token(input)?;
    let token = token(input)?;
    Ok((control_sequence, token, input_with_tokens))
}

/// Parse the right-hand side of a `let` assignment (TeXBook p. 273).
///
/// Returns the control sequence and the value it is assigned to.
pub fn let_assignment<'a>(input: &mut &'a str) -> InnerResult<(&'a str, Token<'a>)> {
    let control_sequence = control_sequence(input)?;
    if let Some(s) = input.trim_start().strip_prefix('=') {
        *input = s;
    }
    let token = token(input)?;
    Ok((control_sequence, token))
}

/// Parse a control_sequence, including the leading `\`.
pub fn control_sequence<'a>(input: &mut &'a str) -> InnerResult<&'a str> {
    if let Some(rest) = input.strip_prefix('\\') {
        *input = rest;
        rhs_control_sequence(input)
    } else {
        input
            .chars()
            .next()
            .map_or(Err(ErrorKind::EndOfInput), |_| {
                Err(ErrorKind::ControlSequence)
            })
    }
}

pub fn limit_modifiers(input: &mut &str) -> Option<bool> {
    let mut output = None;
    while let Some((rest, limits)) = input
        .trim_start()
        .strip_prefix(r"\limits")
        .map(|rest| (rest, true))
        .or_else(|| {
            input
                .trim_start()
                .strip_prefix(r"\nolimits")
                .map(|rest| (rest, false))
        })
    {
        *input = rest;
        output = Some(limits);
    }
    output
}

/// Parse the right side of a control sequence (`\` already being parsed).
///
/// A control sequence can be of the form `\controlsequence`, or `\#` (control symbol).
pub fn rhs_control_sequence<'a>(input: &mut &'a str) -> InnerResult<&'a str> {
    if input.is_empty() {
        return Err(ErrorKind::EmptyControlSequence);
    }

    let len = input
        .chars()
        .take_while(|c| c.is_ascii_alphabetic())
        .count()
        .max(1);

    let (control_sequence, rest) = input.split_at(len);
    *input = rest.trim_start();
    Ok(control_sequence)
}

/// Parse a glue (TeXBook p. 267).
pub fn glue(input: &mut &str) -> InnerResult<Glue> {
    let mut dimen = (dimension(input)?, None, None);
    if let Some(s) = input.trim_start().strip_prefix("plus") {
        *input = s;
        dimen.1 = Some(dimension(input)?);
    }
    if let Some(s) = input.trim_start().strip_prefix("minus") {
        *input = s;
        dimen.2 = Some(dimension(input)?);
    }
    Ok(dimen)
}

/// Parse a dimension (TeXBook p. 266).
pub fn dimension(input: &mut &str) -> InnerResult<Dimension> {
    let number = floating_point(input)?;
    let unit = dimension_unit(input)?;
    Ok((number, unit))
}

/// Parse a dimension unit (TeXBook p. 266).
pub fn dimension_unit(input: &mut &str) -> InnerResult<DimensionUnit> {
    *input = input.trim_start();
    if input.len() < 2 {
        return Err(ErrorKind::EndOfInput);
    }

    let unit = input.get(0..2).ok_or(ErrorKind::DimensionUnit)?;
    let unit = match unit {
        "em" => DimensionUnit::Em,
        "ex" => DimensionUnit::Ex,
        "pt" => DimensionUnit::Pt,
        "pc" => DimensionUnit::Pc,
        "in" => DimensionUnit::In,
        "bp" => DimensionUnit::Bp,
        "cm" => DimensionUnit::Cm,
        "mm" => DimensionUnit::Mm,
        "dd" => DimensionUnit::Dd,
        "cc" => DimensionUnit::Cc,
        "sp" => DimensionUnit::Sp,
        "mu" => DimensionUnit::Mu,
        _ => return Err(ErrorKind::DimensionUnit),
    };

    *input = &input[2..];
    one_optional_space(input);

    Ok(unit)
}

/// Parse an integer that may be positive or negative and may be represented as octal, decimal,
/// hexadecimal, or a character code (TeXBook p. 265).
// For future use maybe.
#[allow(dead_code)]
pub fn integer(input: &mut &str) -> InnerResult<isize> {
    let signum = signs(input)?;

    let unsigned_int = unsigned_integer(input)?;

    Ok(unsigned_int as isize * signum)
}

pub fn unsigned_integer(input: &mut &str) -> InnerResult<usize> {
    // The following character must be ascii.
    let next_char = input.chars().next().ok_or(ErrorKind::EndOfInput)?;
    if next_char.is_ascii_digit() {
        return Ok(decimal(input));
    }
    *input = &input[1..];
    match next_char {
        '`' => {
            let mut next_byte = *input.as_bytes().first().ok_or(ErrorKind::EndOfInput)?;
            if next_byte == b'\\' {
                *input = &input[1..];
                next_byte = *input.as_bytes().first().ok_or(ErrorKind::EndOfInput)?;
            }
            if next_byte.is_ascii() {
                *input = &input[1..];
                Ok(next_byte as usize)
            } else {
                Err(ErrorKind::CharacterNumber)
            }
        }
        '\'' => Ok(octal(input)),
        '"' => Ok(hexadecimal(input)),
        _ => Err(ErrorKind::Number),
    }
}

/// Parse the signs in front of a number, returning the signum.
pub fn signs(input: &mut &str) -> InnerResult<isize> {
    let mut minus_count = 0;
    *input = input
        .trim_start_matches(|c: char| {
            if c == '-' {
                minus_count += 1;
                true
            } else {
                c == '+' || c.is_whitespace()
            }
        })
        .trim_start();
    Ok(if minus_count % 2 == 0 { 1 } else { -1 })
}

/// Parse a base 16 unsigned number.
pub fn hexadecimal(input: &mut &str) -> usize {
    let mut number = 0;
    *input = input.trim_start_matches(|c: char| {
        if c.is_ascii_alphanumeric() && c < 'G' {
            number =
                number * 16 + c.to_digit(16).expect("the character is a valid hex digit") as usize;
            true
        } else {
            false
        }
    });
    one_optional_space(input);

    number
}

/// Parse a floating point number (named `factor` in TeXBook p. 266).
pub fn floating_point(input: &mut &str) -> InnerResult<f32> {
    let signum = signs(input)?;

    let mut number = 0.;
    *input = input.trim_start_matches(|c: char| {
        if c.is_ascii_digit() {
            number = number * 10. + (c as u8 - b'0') as f32;
            true
        } else {
            false
        }
    });

    if let Some(stripped_decimal_point) = input.strip_prefix(|c| c == '.' || c == ',') {
        let mut decimal = 0.;
        let mut decimal_divisor = 1.;
        *input = stripped_decimal_point.trim_start_matches(|c: char| {
            if c.is_ascii_digit() {
                decimal = decimal * 10. + (c as u8 - b'0') as f32;
                decimal_divisor *= 10.;
                true
            } else {
                false
            }
        });
        number += decimal / decimal_divisor;
    };

    Ok(signum as f32 * number)
}

/// Parse a base 10 unsigned number.
pub fn decimal(input: &mut &str) -> usize {
    let mut number = 0;
    *input = input.trim_start_matches(|c: char| {
        if c.is_ascii_digit() {
            number = number * 10 + (c as u8 - b'0') as usize;
            true
        } else {
            false
        }
    });
    one_optional_space(input);

    number
}

/// Parse a base 8 unsigned number.
pub fn octal(input: &mut &str) -> usize {
    let mut number = 0;
    *input = input.trim_start_matches(|c: char| {
        if c.is_ascii_digit() {
            number = number * 8 + (c as u8 - b'0') as usize;
            true
        } else {
            false
        }
    });
    one_optional_space(input);

    number
}

/// Parse an optional space.
pub fn one_optional_space(input: &mut &str) -> bool {
    let mut chars = input.chars();
    if chars.next().is_some_and(|c| c.is_whitespace()) {
        *input = &input[1..];
        true
    } else {
        false
    }
}

/// Return the next token in the input.
///
/// A token will never be whitespace, and will never be inside of a comment.
pub fn token<'a>(input: &mut &'a str) -> InnerResult<Token<'a>> {
    *input = input.trim_start();
    match input.chars().next() {
        Some('\\') => {
            *input = &input[1..];
            Ok(Token::ControlSequence(rhs_control_sequence(input)?))
        }
        Some('%') => {
            let (_, rest) = input.split_once('\n').ok_or(ErrorKind::EndOfInput)?;
            *input = rest;
            token(input)
        }
        Some(c) => {
            let context = *input;
            *input = input.split_at(c.len_utf8()).1;
            Ok(Token::Character(CharToken::from_str(context)))
        }
        None => Err(ErrorKind::EndOfInput),
    }
}

#[cfg(test)]
mod tests {
    use crate::{
        attribute::DimensionUnit,
        parser::{lex, Token},
    };

    #[test]
    fn signs() {
        let mut input = "  +    +-   \\test";
        assert_eq!(lex::signs(&mut input).unwrap(), -1);
        assert_eq!(input, "\\test");
    }

    #[test]
    fn no_signs() {
        let mut input = "\\mycommand";
        assert_eq!(lex::signs(&mut input).unwrap(), 1);
        assert_eq!(input, "\\mycommand");
    }

    // A complex exanple from problem 20.7 in TeXBook (p. 205):
    // \def\cs AB#1#2C$#3\$ {#3{ab#1}#1 c##\x #2}
    #[test]
    fn definition_texbook() {
        let mut input = "\\cs AB#1#2C$#3\\$ {#3{ab#1}#1 c##\\x #2}";

        let (cs, param, repl) = lex::definition(&mut input).unwrap();
        assert_eq!(cs, "cs");
        assert_eq!(param, "AB#1#2C$#3\\$ ");
        assert_eq!(repl, "#3{ab#1}#1 c##\\x #2");
        assert_eq!(input, "");
    }

    #[test]
    fn complex_definition() {
        let mut input = r"\foo #1\test#2#{##\####2#2 \{{\}} \{\{\{} 5 + 5 = 10";
        let (cs, param, repl) = lex::definition(&mut input).unwrap();

        assert_eq!(cs, "foo");
        assert_eq!(param, r"#1\test#2#");
        assert_eq!(repl, r"##\####2#2 \{{\}} \{\{\{");
        assert_eq!(input, " 5 + 5 = 10");
    }

    #[test]
    fn let_assignment() {
        let mut input = r"\foo = \bar";
        let (cs, token) = lex::let_assignment(&mut input).unwrap();

        assert_eq!(cs, "foo");
        assert_eq!(token, Token::ControlSequence("bar"));
        assert_eq!(input, "");
    }

    #[test]
    fn futurelet_assignment() {
        let mut input = r"\foo\bar\baz blah";
        let (cs, token, rest) = lex::futurelet_assignment(&mut input).unwrap();

        assert_eq!(cs, "foo");
        assert_eq!(token, Token::ControlSequence("baz"));
        assert_eq!(rest, r"\bar\baz blah");
    }

    #[test]
    fn dimension() {
        let mut input = "1.2pt";
        let dim = lex::dimension(&mut input).unwrap();

        assert_eq!(dim, (1.2, DimensionUnit::Pt));
        assert_eq!(input, "");
    }

    #[test]
    fn complex_glue() {
        let mut input = "1.2 pt plus 3.4pt minus 5.6pt nope";
        let glue = lex::glue(&mut input).unwrap();

        assert_eq!(
            glue,
            (
                (1.2, DimensionUnit::Pt),
                Some((3.4, DimensionUnit::Pt)),
                Some((5.6, DimensionUnit::Pt))
            )
        );
        assert_eq!(input, "nope");
    }

    #[test]
    fn numbers() {
        let mut input = "123 -\"AEF24 --'3475 `\\a -.47";
        assert_eq!(lex::integer(&mut input).unwrap(), 123);
        assert_eq!(lex::integer(&mut input).unwrap(), -716580);
        assert_eq!(lex::integer(&mut input).unwrap(), 1853);
        assert_eq!(lex::integer(&mut input).unwrap(), 97);
        assert_eq!(lex::floating_point(&mut input).unwrap(), -0.47);
        assert_eq!(input, "");
    }

    #[test]
    fn group_content() {
        let mut input =
            "this { { is a test } to see if { the content parsing { of this } } } works }";
        let content = lex::group_content(&mut input, "{", "}").unwrap();
        assert_eq!(
            content,
            "this { { is a test } to see if { the content parsing { of this } } } works "
        );
    }
}