-
Notifications
You must be signed in to change notification settings - Fork 245
/
lex.rs
137 lines (118 loc) · 3.02 KB
/
lex.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
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
//! Lexing types
use std::fmt::Display;
pub use logos::Span;
/// A WAVE `logos::Lexer`
pub type Lexer<'source> = logos::Lexer<'source, Token>;
/// A WAVE token
#[derive(Clone, Copy, Debug, PartialEq, Eq, logos::Logos)]
#[logos(error = Option<Span>)]
#[logos(skip r"[ \t\n\r]+")]
#[logos(skip r"//[^\n]*")]
#[logos(subpattern label_word = r"[a-z][a-z0-9]*|[A-Z][A-Z0-9]*")]
#[logos(subpattern char_escape = r#"\\['"tnr\\]|\\u\{[0-9a-fA-F]{1,6}\}"#)]
pub enum Token {
/// The `{` symbol
#[token("{")]
BraceOpen,
/// The `}` symbol
#[token("}")]
BraceClose,
/// The `(` symbol
#[token("(")]
ParenOpen,
/// The `)` symbol
#[token(")")]
ParenClose,
/// The `[` symbol
#[token("[")]
BracketOpen,
/// The `]` symbol
#[token("]")]
BracketClose,
/// The `:` symbol
#[token(":")]
Colon,
/// The `,` symbol
#[token(",")]
Comma,
/// A number literal
#[regex(r"-?(0|([1-9][0-9]*))(\.[0-9]+)?([eE][-+]?[0-9]+)?")]
#[token("-inf")]
Number,
/// A label or keyword
#[regex(r"%?(?&label_word)(-(?&label_word))*")]
LabelOrKeyword,
/// A char literal
#[regex(r#"'([^\\'\n]{1,4}|(?&char_escape))'"#, validate_char)]
Char,
/// A string literal
#[regex(r#""([^\\"\n]|(?&char_escape))*""#)]
String,
/// A multi-line string literal
#[token(r#"""""#, lex_multiline_string)]
MultilineString,
}
impl Display for Token {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{self:?}")
}
}
fn validate_char(lex: &mut Lexer) -> Result<(), Option<Span>> {
let s = &lex.slice()[1..lex.slice().len() - 1];
if s.starts_with('\\') || s.chars().count() == 1 {
Ok(())
} else {
Err(Some(lex.span()))
}
}
fn lex_multiline_string(lex: &mut Lexer) -> bool {
if let Some(end) = lex.remainder().find(r#"""""#) {
lex.bump(end + 3);
true
} else {
false
}
}
/// A WAVE keyword
#[derive(Clone, Copy, Debug, PartialEq)]
#[allow(missing_docs)]
pub enum Keyword {
True,
False,
Some,
None,
Ok,
Err,
Inf,
Nan,
}
impl Keyword {
/// Returns any keyword exactly matching the given string.
pub fn decode(raw_label: &str) -> Option<Self> {
Some(match raw_label {
"true" => Self::True,
"false" => Self::False,
"some" => Self::Some,
"none" => Self::None,
"ok" => Self::Ok,
"err" => Self::Err,
"inf" => Self::Inf,
"nan" => Self::Nan,
_ => return None,
})
}
}
impl Display for Keyword {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(match self {
Keyword::True => "true",
Keyword::False => "false",
Keyword::Some => "some",
Keyword::None => "none",
Keyword::Ok => "ok",
Keyword::Err => "err",
Keyword::Inf => "inf",
Keyword::Nan => "nan",
})
}
}