-
Notifications
You must be signed in to change notification settings - Fork 85
/
token_kind.rs
68 lines (54 loc) · 1.6 KB
/
token_kind.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
60
61
62
63
64
65
66
67
68
use alloc::borrow::Cow;
use core::fmt::{self, Display, Write as _};
use json_number::Number;
use logos::Logos;
use super::{error::LexingError, syntax_kind::SyntaxKind};
use crate::lexer::parse::{parse_number, parse_string};
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Logos)]
#[logos(error = LexingError)]
#[logos(source = [u8])]
#[logos(skip r"[ \t\r\n\f]+")]
pub(crate) enum TokenKind<'source> {
#[token("false", |_| false)]
#[token("true", |_| true)]
Bool(bool),
#[token("null")]
Null,
#[token("{")]
LBrace,
#[token("}")]
RBrace,
#[token("[")]
LBracket,
#[token("]")]
RBracket,
#[token(":")]
Colon,
#[token(",")]
Comma,
#[regex(r#"[0-9-]"#, parse_number)]
Number(Cow<'source, Number>),
#[token(r#"""#, parse_string)]
String(Cow<'source, str>),
}
impl TokenKind<'_> {
pub(crate) fn syntax(&self) -> SyntaxKind {
SyntaxKind::from(self)
}
}
impl Display for TokenKind<'_> {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Bool(bool) => Display::fmt(bool, fmt),
Self::Null => fmt.write_str("null"),
Self::LBrace => fmt.write_char('{'),
Self::RBrace => fmt.write_char('}'),
Self::LBracket => fmt.write_char('['),
Self::RBracket => fmt.write_char(']'),
Self::Colon => fmt.write_char(':'),
Self::Comma => fmt.write_char(','),
Self::Number(number) => Display::fmt(number, fmt),
Self::String(string) => write!(fmt, "\"{string}\""),
}
}
}