laps 0.1.7

Build lexers and parsers by deriving traits.
Documentation
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
//! Implementations for constructing lexers.
//!
//! This module contains:
//!
//! * [`Tokenize`]: trait for tokenizing token kinds. With feature `macros`
//!   enabled, you can derive this trait for token kinds.
//! * [`Lexer`]: a lexer implementation for token kinds that implemented
//!   [`Tokenize`] trait.
//! * Some helper functions for constructing lexers.

use crate::input::InputStream;
use crate::token::{Token, Tokenizer};
use std::marker::PhantomData;
use std::num::ParseIntError;

#[cfg(feature = "macros")]
pub use laps_macros::Tokenize;

/// Trait for token kinds that can be tokenized from an input stream.
pub trait Tokenize: Sized {
  /// The type of the character produced by the input stream.
  type CharType;

  /// Reads the next token from the given input stream.
  ///
  /// Returns the token ([`Token<Self>`]) if successful, otherwise [`Err`].
  fn next_token<I>(input: &mut I) -> crate::span::Result<Token<Self>>
  where
    I: InputStream<CharType = Self::CharType>;

  /// Creates a lexer from the given input stream that
  /// produces the current token kind.
  fn lexer<I>(input: I) -> Lexer<I, Self> {
    Lexer {
      input,
      token: PhantomData,
    }
  }
}

/// A lexer with input stream type `I` and token kind type `K`.
///
/// This lexer will produce tokens of type [`Token<K>`].
pub struct Lexer<I, K> {
  input: I,
  token: PhantomData<K>,
}

impl<I, K> Lexer<I, K> {
  /// Converts the lexer into its inner input stream.
  pub fn into_input(self) -> I {
    self.input
  }

  /// Returns a reference to the inner input stream.
  pub fn input(&self) -> &I {
    &self.input
  }

  /// Returns a mutable reference to the inner input stream.
  pub fn input_mut(&mut self) -> &mut I {
    &mut self.input
  }
}

impl<I, K, C> Tokenizer for Lexer<I, K>
where
  I: InputStream<CharType = C>,
  K: Tokenize<CharType = C>,
{
  type Token = Token<K>;

  fn next_token(&mut self) -> crate::span::Result<Self::Token> {
    K::next_token(&mut self.input)
  }
}

/// Parses integer literals from the given string.
/// Supports decimal, binary, hexadecimal and octal.
///
/// Returns the integer if successful, otherwise returns [`None`].
///
/// # Examples
///
/// ```
/// use laps::lexer::int_literal;
///
/// assert_eq!(int_literal("0"), Some(0));
/// assert_eq!(int_literal("00"), Some(0));
/// assert_eq!(int_literal("42"), Some(42));
/// assert_eq!(int_literal("0x1a"), Some(26));
/// assert_eq!(int_literal("0b0110"), Some(6));
/// assert_eq!(int_literal("0o777"), Some(511));
/// assert_eq!(int_literal::<i32>("z"), None);
/// assert_eq!(int_literal::<i32>("0f"), None);
/// assert_eq!(int_literal::<i32>("0b777"), None);
/// ```
pub fn int_literal<T>(s: &str) -> Option<T>
where
  T: IntLiteral,
{
  // check if is a valid integer literal
  let mut chars = s.chars();
  let (radix, starts_from) = match (chars.next(), chars.next()) {
    (Some('0'), Some(c)) if "box".contains(c) => (
      match c {
        'b' => 2,
        'o' => 8,
        'x' => 16,
        _ => unreachable!(),
      },
      2,
    ),
    (Some(c), None) if c.is_ascii_digit() => (10, 0),
    (Some(c1), Some(c2)) if c1.is_ascii_digit() && c2.is_ascii_digit() => (10, 0),
    _ => return None,
  };
  if !chars.all(|c| c.is_digit(radix)) {
    return None;
  }
  // convert to integer
  T::from_str_radix(&s[starts_from..], radix).ok()
}

/// Parses integer literals with an optional sign from the given string.
/// Supports decimal, binary, hexadecimal and octal.
///
/// Returns the integer if successful, otherwise returns [`None`].
///
/// # Examples
///
/// ```
/// use laps::lexer::signed_int_literal;
///
/// assert_eq!(signed_int_literal("0"), Some(0));
/// assert_eq!(signed_int_literal("+00"), Some(0));
/// assert_eq!(signed_int_literal("-42"), Some(-42));
/// assert_eq!(signed_int_literal("-0x1a"), Some(-26));
/// assert_eq!(signed_int_literal("0b0110"), Some(6));
/// assert_eq!(signed_int_literal("+0o777"), Some(511));
/// assert_eq!(signed_int_literal::<u32>("-1"), Some(u32::MAX));
/// assert_eq!(signed_int_literal::<i32>("+"), None);
/// assert_eq!(signed_int_literal::<i32>("--1"), None);
/// assert_eq!(signed_int_literal::<i32>("-0b777"), None);
/// ```
pub fn signed_int_literal<T>(s: &str) -> Option<T>
where
  T: IntLiteral,
{
  let first = s.chars().next()?;
  if first == '+' || first == '-' {
    int_literal(&s[1..]).map(|n: T| if first == '-' { n.wrapping_neg() } else { n })
  } else {
    int_literal(s)
  }
}

/// A helper trait for function [`int_literal`].
///
/// Users are not allowed to implement this trait for other types.
pub trait IntLiteral: Sized + sealed_traits::SealedIntLiteral {
  /// Converts a string slice in a given base to an integer.
  ///
  /// This is identical to `from_str_radix` method of primitive integer types,
  /// such as [`i32::from_str_radix`](i32#method.from_str_radix).
  fn from_str_radix(s: &str, radix: u32) -> Result<Self, ParseIntError>;

  /// Wrapping negates the current number.
  fn wrapping_neg(self) -> Self;
}

/// Helper macro for implementing `IntLiteral` for integers.
macro_rules! impl_int_literal {
  ($ty:ty) => {
    impl IntLiteral for $ty {
      fn from_str_radix(s: &str, radix: u32) -> Result<Self, ParseIntError> {
        <$ty>::from_str_radix(s, radix)
      }

      fn wrapping_neg(self) -> Self {
        self.wrapping_neg()
      }
    }
  };
}

impl_int_literal!(i8);
impl_int_literal!(i16);
impl_int_literal!(i32);
impl_int_literal!(i64);
impl_int_literal!(i128);
impl_int_literal!(isize);
impl_int_literal!(u8);
impl_int_literal!(u16);
impl_int_literal!(u32);
impl_int_literal!(u64);
impl_int_literal!(u128);
impl_int_literal!(usize);

/// Sealed trait for trait `IntLiteral`.
mod sealed_traits {
  pub trait SealedIntLiteral {}
  impl SealedIntLiteral for i8 {}
  impl SealedIntLiteral for i16 {}
  impl SealedIntLiteral for i32 {}
  impl SealedIntLiteral for i64 {}
  impl SealedIntLiteral for i128 {}
  impl SealedIntLiteral for isize {}
  impl SealedIntLiteral for u8 {}
  impl SealedIntLiteral for u16 {}
  impl SealedIntLiteral for u32 {}
  impl SealedIntLiteral for u64 {}
  impl SealedIntLiteral for u128 {}
  impl SealedIntLiteral for usize {}
}

/// Parses string literals (`"..."`) from the given string.
///
/// Supported escapes:
/// * `\r`, `\n`, `\t`, `\0`, `\\`.
/// * `\'`, `\"`.
/// * `\x00`-`\xff` (`\xFF`).
/// * `\u{0}`-`\u{d7ff}` and `\u{e000}`-`\u{10ffff}` (`\u{10FFFF}`).
///
/// Returns the string if successful, otherwise returns [`None`].
///
/// # Examples
///
/// ```
/// use laps::lexer::str_literal;
///
/// assert_eq!(str_literal(r#""hello""#), Some("hello".into()));
/// assert_eq!(str_literal(r#""你好""#), Some("你好".into()));
/// assert_eq!(str_literal(r#""""#), Some("".into()));
/// assert_eq!(str_literal(r#""\"\n\t\\""#), Some("\"\n\t\\".into()));
/// assert_eq!(str_literal(r#""#), None);
/// assert_eq!(str_literal(r#""hello"#), None);
/// ```
pub fn str_literal(s: &str) -> Option<String> {
  let mut chars = s.chars();
  // check the first quote
  if chars.next()? != '"' {
    return None;
  }
  // get string literal
  let mut s = String::new();
  loop {
    match parse_char_literal(&mut chars, '"') {
      ParseResult::Char(c) => s.push(c),
      ParseResult::Quote => break,
      ParseResult::Error => return None,
    }
  }
  // check the last quote
  chars.next().is_none().then_some(s)
}

/// Parses character literals (`'...'`) from the given string.
///
/// Supported escapes:
/// * `\r`, `\n`, `\t`, `\0`, `\\`.
/// * `\'`, `\"`.
/// * `\x00`-`\xff` (`\xFF`).
/// * `\u{0}`-`\u{d7ff}` and `\u{e000}`-`\u{10ffff}` (`\u{10FFFF}`).
///
/// Returns the character if successful, otherwise returns [`None`].
///
/// # Examples
///
/// ```
/// use laps::lexer::char_literal;
///
/// assert_eq!(char_literal(r#"'a'"#), Some('a'));
/// assert_eq!(char_literal(r#"'😂'"#), Some('😂'));
/// assert_eq!(char_literal(r#"'\n'"#), Some('\n'));
/// assert_eq!(char_literal(r#""#), None);
/// assert_eq!(char_literal(r#"''"#), None);
/// assert_eq!(char_literal(r#"'a"#), None);
/// ```
pub fn char_literal(s: &str) -> Option<char> {
  let mut chars = s.chars();
  // check the first quote
  if chars.next()? != '\'' {
    return None;
  }
  // get character literal
  match parse_char_literal(&mut chars, '\'') {
    ParseResult::Char(c) => ((chars.next(), chars.next()) == (Some('\''), None)).then_some(c),
    _ => None,
  }
}

/// Parses a char literal (do not include quotes)
/// from the given character iterator.
fn parse_char_literal<I>(iter: &mut I, quote: char) -> ParseResult
where
  I: Iterator<Item = char>,
{
  match iter.next() {
    Some('\n') | Some('\r') | Some('\t') => ParseResult::Error,
    Some('\\') => match iter.next() {
      Some('r') => ParseResult::Char('\r'),
      Some('n') => ParseResult::Char('\n'),
      Some('t') => ParseResult::Char('\t'),
      Some('0') => ParseResult::Char('\0'),
      Some('\\') => ParseResult::Char('\\'),
      Some('\'') => ParseResult::Char('\''),
      Some('\"') => ParseResult::Char('\"'),
      Some('x') => {
        // get escaped char
        let c = iter
          .next()
          .and_then(|c| c.to_digit(16))
          .zip(iter.next().and_then(|c| c.to_digit(16)))
          .map(|(c1, c2)| (c1 * 16 + c2) as u8 as char);
        match c {
          Some(c) => ParseResult::Char(c),
          None => ParseResult::Error,
        }
      }
      Some('u') => {
        // check '{'
        if iter.next() != Some('{') {
          return ParseResult::Error;
        }
        // get hex value
        let mut hex = 0u32;
        for c in iter {
          match c.to_digit(16) {
            Some(h) => match hex.checked_mul(16) {
              Some(h16) => hex = h16 + h,
              None => break,
            },
            None if c == '}' => match char::from_u32(hex) {
              Some(c) => return ParseResult::Char(c),
              None => break,
            },
            None => break,
          }
        }
        ParseResult::Error
      }
      _ => ParseResult::Error,
    },
    Some(c) if c == quote => ParseResult::Quote,
    Some(c) => ParseResult::Char(c),
    None => ParseResult::Error,
  }
}

/// Result type of `parse_char_literal`.
enum ParseResult {
  Char(char),
  Quote,
  Error,
}

#[cfg(test)]
mod test {
  use super::*;

  #[test]
  fn parse_int() {
    assert_eq!(int_literal("123"), Some(123));
    assert_eq!(int_literal("0"), Some(0));
    assert_eq!(int_literal("000"), Some(0));
    assert_eq!(int_literal("0x0"), Some(0x0));
    assert_eq!(int_literal("0xFf"), Some(0xff));
    assert_eq!(int_literal("0b110"), Some(0b110));
    assert_eq!(int_literal("0o765"), Some(0o765));
    assert_eq!(int_literal::<i32>(""), None);
    assert_eq!(int_literal::<i32>("?"), None);
    assert_eq!(int_literal::<i32>("0x?"), None);
    assert_eq!(int_literal::<i32>("99999999999999999999999999999999"), None);
  }

  #[test]
  fn parse_str() {
    assert_eq!(str_literal(r#""""#), Some("".into()));
    assert_eq!(str_literal(r#""a""#), Some("a".into()));
    assert_eq!(str_literal(r#""🤡👈""#), Some("🤡👈".into()));
    assert_eq!(str_literal(r#""\t""#), Some("\t".into()));
    assert_eq!(str_literal(r#""\n""#), Some("\n".into()));
    assert_eq!(str_literal(r#""\r""#), Some("\r".into()));
    assert_eq!(str_literal(r#""\\r""#), Some("\\r".into()));
    assert_eq!(str_literal(r#""\'""#), Some("\'".into()));
    assert_eq!(str_literal(r#""\"""#), Some("\"".into()));
    assert_eq!(str_literal(r#""\x4a""#), Some("\x4a".into()));
    assert_eq!(str_literal(r#""\u{1234}""#), Some("\u{1234}".into()));
    assert_eq!(
      str_literal(r#""\u{1234}\u{5678}""#),
      Some("\u{1234}\u{5678}".into())
    );
    assert_eq!(str_literal(r#""\u{10ffff}""#), Some("\u{10ffff}".into()));
    assert_eq!(str_literal(r#""a\x4aa""#), Some("a\x4aa".into()));
    assert_eq!(str_literal(r#""'""#), Some("'".into()));
    assert_eq!(str_literal(r#"?"#), None);
    assert_eq!(str_literal(r#"""#), None);
    assert_eq!(str_literal(r#""aa"#), None);
    assert_eq!(str_literal(r#""\"#), None);
    assert_eq!(
      str_literal(
        r#""
""#
      ),
      None,
    );
    assert_eq!(
      str_literal(
        r#""aa
""#
      ),
      None,
    );
    assert_eq!(str_literal(r#""\?""#), None);
    assert_eq!(str_literal(r#""\x""#), None);
    assert_eq!(str_literal(r#""\x4""#), None);
    assert_eq!(str_literal(r#""\u""#), None);
    assert_eq!(str_literal(r#""\u{""#), None);
    assert_eq!(str_literal(r#""\u{111111111""#), None);
    assert_eq!(str_literal(r#""\u{111111111}""#), None);
    assert_eq!(str_literal(r#""\u{d800}""#), None);
    assert_eq!(str_literal(r#""\u{dfff}""#), None);
  }

  #[test]
  fn parse_char() {
    assert_eq!(char_literal("'a'"), Some('a'));
    assert_eq!(char_literal("'🤔'"), Some('🤔'));
    assert_eq!(char_literal(r"'\t'"), Some('\t'));
    assert_eq!(char_literal(r"'\n'"), Some('\n'));
    assert_eq!(char_literal(r"'\r'"), Some('\r'));
    assert_eq!(char_literal(r"'\\'"), Some('\\'));
    assert_eq!(char_literal(r"'\''"), Some('\''));
    assert_eq!(char_literal(r#"'\"'"#), Some('\"'));
    assert_eq!(char_literal(r"'\x4a'"), Some('\x4a'));
    assert_eq!(char_literal(r"'\u{1234}'"), Some('\u{1234}'));
    assert_eq!(char_literal(r"'\u{10ffff}'"), Some('\u{10ffff}'));
    assert_eq!(char_literal(r#"'"'"#), Some('"'));
    assert_eq!(char_literal("?"), None);
    assert_eq!(char_literal("'"), None);
    assert_eq!(char_literal("''"), None);
    assert_eq!(char_literal("'a"), None);
    assert_eq!(char_literal("'ab"), None);
    assert_eq!(char_literal("'ab'"), None);
    assert_eq!(
      char_literal(
        r#"'
'"#
      ),
      None,
    );
    assert_eq!(
      char_literal(
        r#"'a
'"#
      ),
      None,
    );
    assert_eq!(char_literal(r"'\'"), None);
    assert_eq!(char_literal(r"'\?'"), None);
    assert_eq!(char_literal(r"'\x'"), None);
    assert_eq!(char_literal(r"'\x4'"), None);
    assert_eq!(char_literal(r"'\u'"), None);
    assert_eq!(char_literal(r"'\u{'"), None);
    assert_eq!(char_literal(r"'\u{111111111'"), None);
    assert_eq!(char_literal(r"'\u{111111111}'"), None);
    assert_eq!(str_literal(r"'\u{d800}'"), None);
    assert_eq!(str_literal(r"'\u{dfff}'"), None);
  }
}