jsonc_parser/
tokens.rs

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
use super::common::Range;
use super::common::Ranged;
use std::borrow::Cow;

/// A token found while scanning.
#[derive(Debug, PartialEq, Clone)]
pub enum Token<'a> {
  OpenBrace,
  CloseBrace,
  OpenBracket,
  CloseBracket,
  Comma,
  Colon,
  String(Cow<'a, str>),
  Word(&'a str),
  Boolean(bool),
  Number(&'a str),
  Null,
  CommentLine(&'a str),
  CommentBlock(&'a str),
}

impl<'a> Token<'a> {
  pub fn as_str(&self) -> &str {
    match self {
      Token::OpenBrace => "{",
      Token::CloseBrace => "}",
      Token::OpenBracket => "[",
      Token::CloseBracket => "]",
      Token::Comma => ",",
      Token::Colon => ":",
      Token::String(value) => value,
      Token::Word(value) => value,
      Token::Boolean(value) => {
        if *value {
          "true"
        } else {
          "false"
        }
      }
      Token::Number(value) => value,
      Token::Null => "null",
      Token::CommentLine(value) => value,
      Token::CommentBlock(value) => value,
    }
  }
}

/// A token with positional information.
pub struct TokenAndRange<'a> {
  pub range: Range,
  pub token: Token<'a>,
}

impl<'a> Ranged for TokenAndRange<'a> {
  fn range(&self) -> Range {
    self.range
  }
}