-
Notifications
You must be signed in to change notification settings - Fork 0
/
token.rs
190 lines (170 loc) · 4.98 KB
/
token.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
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
#![allow(dead_code)]
use std::ops::Range;
use logos::{Lexer, Logos};
pub struct APITokenizer<'a> {
source: &'a str,
lexer: Lexer<'a, APITokenKind>,
}
impl<'a> APITokenizer<'a> {
pub fn new(source: &'a str) -> Self {
Self {
source,
lexer: APITokenKind::lexer(source),
}
}
}
impl<'a> Iterator for APITokenizer<'a> {
type Item = APIToken<'a>;
fn next(&mut self) -> Option<Self::Item> {
match self.lexer.next() {
Some(Ok(kind)) => Some(APIToken {
source: self.source,
kind,
at: self.lexer.slice(),
span: self.lexer.span(),
}),
_ => None,
}
}
}
#[derive(PartialEq)]
pub struct APIToken<'a> {
pub source: &'a str,
pub kind: APITokenKind,
pub at: &'a str,
pub span: Range<usize>,
}
impl<'a> APIToken<'a> {
pub fn text(&self) -> &'a str {
&self.source[self.span.clone()]
}
}
impl std::fmt::Debug for APIToken<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{:?}[{:?}] @ {}..{}",
self.kind,
self.text(),
self.span.start,
self.span.end
)
}
}
pub fn tokenize(source: &str) -> Vec<APIToken> {
APITokenizer::new(source).collect::<Vec<_>>()
}
#[derive(Logos, Debug, PartialEq, Clone)]
pub enum APITokenKind {
#[regex(r"[ \t\r\n\f]+", logos::skip)]
Whitespace,
// basic tokens
#[token("{")]
OpenBrace,
#[token("}")]
CloseBrace,
#[token("(")]
OpenParen,
#[token(")")]
CloseParen,
#[token("[")]
OpenBracket,
#[token("]")]
CloseBracket,
#[token(":")]
Colon,
// data types
#[regex("int(8|16|32|64)?")]
IntDataType,
#[regex("float(32|64)")]
FloatDataType,
#[regex("string")]
StringDataType,
#[regex("bool")]
BoolDataType,
#[token("map")]
MapDataType,
// type modifiers
#[token("type")]
Type,
// #[regex(r#"[_a-zA-Z][_$a-zA-Z0-9]*"#)]
#[regex(r"[_a-zA-Z][_$a-zA-Z0-9]*|[0-9]+[_$a-zA-Z0-9]*")]
Identifier,
#[token("struct")]
Struct,
// #[regex(r#"`[a-zA-Z0-9_]+:"[^"]+(?:,[^"]+)*"`"#)]
#[regex(r#"`(?:[a-zA-Z0-9_]+:"[^"]*"(?:\s+)?)+`"#)]
TagAnnotation,
// service modifiers
#[token("@server")]
Server,
#[token("service")]
Service,
#[token("@handler")]
Handler,
#[regex("get|post")]
HttpMethod,
#[regex(r#"/([a-zA-Z0-9_]+/?)*"#)]
RoutePath,
#[token("returns")]
RespReturns,
}
impl std::fmt::Display for APITokenKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
APITokenKind::Whitespace => write!(f, "Whitespace"),
APITokenKind::OpenBrace => write!(f, "OpenBrace"),
APITokenKind::CloseBrace => write!(f, "CloseBrace"),
APITokenKind::OpenParen => write!(f, "OpenParen"),
APITokenKind::CloseParen => write!(f, "CloseParen"),
APITokenKind::OpenBracket => write!(f, "OpenBracket"),
APITokenKind::CloseBracket => write!(f, "CloseBracket"),
APITokenKind::Colon => write!(f, "Colon"),
APITokenKind::IntDataType => write!(f, "IntDataType"),
APITokenKind::FloatDataType => write!(f, "FloatDataType"),
APITokenKind::StringDataType => write!(f, "StringDataType"),
APITokenKind::BoolDataType => write!(f, "BoolDataType"),
APITokenKind::MapDataType => write!(f, "MapDataType"),
APITokenKind::Type => write!(f, "Type"),
APITokenKind::Identifier => write!(f, "Identifier"),
APITokenKind::Struct => write!(f, "Struct"),
APITokenKind::TagAnnotation => write!(f, "TagAnnotation"),
APITokenKind::Server => write!(f, "Server"),
APITokenKind::Service => write!(f, "Service"),
APITokenKind::Handler => write!(f, "Handler"),
APITokenKind::HttpMethod => write!(f, "HttpMethod"),
APITokenKind::RoutePath => write!(f, "RoutePath"),
APITokenKind::RespReturns => write!(f, "RespReturns"),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_tokenizer() {
let source = r#"
type GetFormReq struct {
Name string `form:"name,omitempty"`
Age int64 `form:"age" json:"age"`
Hobbits []string `form:"hobbits"`
}
type GetFormResp struct {
Total int64 `json:"total"`
}
@server (
group: json
jwt: Auth
timeout: 3m
)
service UserService {
@handler getForm
get /example/form (GetFormReq) returns (GetFormResp)
}
"#;
let tokenizer = APITokenizer::new(source);
for token in tokenizer {
println!("{:#?}", token);
}
}
}