-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.rs
79 lines (67 loc) · 1.79 KB
/
lib.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
//! 字句解析で取り出されるトークンとその付帯情報の定義
mod locatable;
mod pos;
mod range;
use std::str::FromStr;
use serde::{Deserialize, Serialize};
pub use locatable::Locatable;
pub use pos::Pos;
pub use range::Range;
/// [`Pos`] を簡単に生成するためのマクロ
#[macro_export]
macro_rules! pos {
($line:expr ; $char:expr) => {
mylang_token::Pos::new($line, $char)
};
}
/// [`Range`] を簡単に生成するためのマクロ
#[macro_export]
macro_rules! range {
($start_line:expr ; $start_char:expr, $end_line:expr ; $end_char:expr) => {
mylang_token::Range::new(
mylang_token::pos!($start_line;$start_char),
mylang_token::pos!($end_line;$end_char),
)
};
($start:expr, $end:expr) => {
mylang_token::Range::new($start, $end)
};
}
#[derive(Debug, Serialize, Deserialize)]
pub enum KeywordKind {
Let,
In,
PrintI32,
PrintStr,
}
impl FromStr for KeywordKind {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"let" => Ok(Self::Let),
"in" => Ok(Self::In),
"print_int" => Ok(Self::PrintI32),
"print_str" => Ok(Self::PrintStr),
_ => Err(format!("Invalid keyword: {s}")),
}
}
}
#[derive(Debug, Serialize, Deserialize)]
pub enum Token {
I32(Range, i32),
AddOp(Pos),
Str(Range, String),
Keyword(Range, KeywordKind),
Ident(Range, String),
Newline(Pos),
}
impl Locatable for Token {
fn locate(&self) -> Range {
match self {
Self::I32(r, _) | Self::Str(r, _) | Self::Keyword(r, _) | Self::Ident(r, _) => {
r.clone()
}
Self::AddOp(p) | Self::Newline(p) => p.clone().into(),
}
}
}