-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathast.rs
226 lines (211 loc) · 7.48 KB
/
ast.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
use crate::{ast_macro::AllowedMacros, debug_eprintln};
use quote::ToTokens;
use syn::{
braced,
parse::{Parse, ParseStream},
spanned::Spanned,
token::Brace,
Block, Error, Ident, LitInt, Macro, Result, Stmt, Token,
};
pub(crate) enum MathExprs {
Unsigned(Vec<Stmt>),
Positive(Vec<Stmt>),
Negative(Vec<Stmt>),
}
enum Sign {
P,
N,
U,
}
impl Sign {
fn math_exprs(&self, stmts: Vec<Stmt>) -> Result<LitIntegerOrExprs> {
match self {
Self::P => Ok(LitIntegerOrExprs::Exprs(MathExprs::Positive(stmts))),
Self::N => Ok(LitIntegerOrExprs::Exprs(MathExprs::Negative(stmts))),
Self::U => Ok(LitIntegerOrExprs::Exprs(MathExprs::Unsigned(stmts))),
}
}
fn lit_integer(&self, litint: LitInt) -> Result<LitIntegerOrExprs> {
match self {
Self::P => Ok(LitIntegerOrExprs::LitInteger(LitInteger::Positive {
lit_integer: litint,
})),
Self::N => Ok(LitIntegerOrExprs::LitInteger(LitInteger::Negative {
lit_integer: litint,
})),
Self::U => Ok(LitIntegerOrExprs::LitInteger(LitInteger::Unsigned {
lit_integer: litint,
})),
}
}
}
fn which_lit_integer_or_exprs(input: ParseStream, sign: Sign) -> Result<LitIntegerOrExprs> {
let lookahead = input.lookahead1();
let result = if lookahead.peek(Brace) {
debug_eprintln!(
"`input` = {input}, `lookahead` = {}",
lookahead.error().to_string()
);
let content;
braced!(content in input);
if content.is_empty() {
return Err(content
.error("the content within the block delimited by `{...}` must not be empty"));
}
let stmts = content.call(Block::parse_within)?;
Ok(sign.math_exprs(stmts)?)
} else if lookahead.peek(Ident) {
let some_macro = input.parse::<Macro>()?;
let allowed_macro = AllowedMacros::which_macro(&some_macro)?;
let macro_result = &allowed_macro.invoke_macro()?;
let macro_result_as_isize = macro_result.parse::<isize>().map_err(|err| {
Error::new(
some_macro.span(),
format!(
"unable to parse the output of `{}` macro invocation to `isize`; error: {err}",
some_macro.path.to_token_stream()
),
)
})?;
match sign {
Sign::N => {
if macro_result_as_isize > 0 {
return Err(Error::new(
some_macro.span(),
format!(
"invocation of `{}` macro does not return a negative integer literal",
some_macro.path.to_token_stream()
),
));
}
}
Sign::P => {
if macro_result_as_isize < 0 {
return Err(Error::new(
some_macro.span(),
format!(
"invocation of `{}` macro does not return a positive integer literal",
some_macro.path.to_token_stream()
),
));
}
}
Sign::U => {
if macro_result_as_isize < 0 {
return Err(Error::new(
some_macro.span(),
format!(
"invocation of `{}` macro does not return a positive integer literal",
some_macro.path.to_token_stream()
),
));
}
}
}
let litint = LitInt::new(
format!("{}", macro_result_as_isize).as_str(),
some_macro.span(),
);
Ok(sign.lit_integer(litint)?)
} else {
Ok(sign.lit_integer(input.parse::<LitInt>()?)?)
};
if !input.is_empty() {
let _ = input.parse::<Token![;]>()?;
};
result
}
impl Parse for LitIntegerOrExprs {
fn parse(input: ParseStream) -> Result<Self> {
let lookahead = input.lookahead1();
if lookahead.peek(Token![+]) {
let _ = input.parse::<Token![+]>();
debug_eprintln!("`input` = {input}");
Ok(which_lit_integer_or_exprs(input, Sign::P)?)
} else if lookahead.peek(Token![-]) {
let _ = input.parse::<Token![-]>();
debug_eprintln!("`input` = {input}");
Ok(which_lit_integer_or_exprs(input, Sign::N)?)
} else {
debug_eprintln!("`input` = {input}");
Ok(which_lit_integer_or_exprs(input, Sign::U)?)
}
}
}
pub(crate) struct NegativeLitIntegerOrExprs(pub(crate) LitIntegerOrExprs);
impl Parse for NegativeLitIntegerOrExprs {
fn parse(input: ParseStream) -> Result<Self> {
let lookahead = input.lookahead1();
if lookahead.peek(Token![+]) || lookahead.peek(Token![-]) {
return Err(Error::new(
lookahead.error().span(),
"when using `nconst`, the first character passed cannot be a `-`",
));
}
Ok(NegativeLitIntegerOrExprs(which_lit_integer_or_exprs(
input,
Sign::N,
)?))
}
}
pub(crate) struct UnsignedLitIntegerOrExprs(pub(crate) LitIntegerOrExprs);
impl Parse for UnsignedLitIntegerOrExprs {
fn parse(input: ParseStream) -> Result<Self> {
let lookahead = input.lookahead1();
if lookahead.peek(Token![+]) || lookahead.peek(Token![-]) {
return Err(Error::new(
lookahead.error().span(),
"when using `uconst`, the first character passed cannot be a `-` or a `+`",
));
}
Ok(UnsignedLitIntegerOrExprs(which_lit_integer_or_exprs(
input,
Sign::U,
)?))
}
}
pub(crate) struct PositiveLitIntegerOrExprs(pub(crate) LitIntegerOrExprs);
impl Parse for PositiveLitIntegerOrExprs {
fn parse(input: ParseStream) -> Result<Self> {
let lookahead = input.lookahead1();
if lookahead.peek(Token![+]) || lookahead.peek(Token![-]) {
return Err(Error::new(
lookahead.error().span(),
"when using `pconst`, the first character passed cannot be a `+`",
));
}
Ok(PositiveLitIntegerOrExprs(which_lit_integer_or_exprs(
input,
Sign::P,
)?))
}
}
pub(crate) enum LitIntegerOrExprs {
Exprs(MathExprs),
LitInteger(LitInteger),
}
pub(crate) enum LitInteger {
Positive { lit_integer: LitInt },
Negative { lit_integer: LitInt },
Unsigned { lit_integer: LitInt },
}
impl Parse for LitInteger {
fn parse(input: ParseStream) -> Result<Self> {
let lookahead = input.lookahead1();
if lookahead.peek(Token![+]) {
let _ = input.parse::<Token![+]>();
let lit_integer: LitInt = input.parse()?;
debug_eprintln!("{:?}", lit_integer);
Ok(LitInteger::Positive { lit_integer })
} else if lookahead.peek(Token![-]) {
let _ = input.parse::<Token![-]>();
let lit_integer: LitInt = input.parse()?;
debug_eprintln!("{:?}", lit_integer);
Ok(LitInteger::Negative { lit_integer })
} else {
let lit_integer: LitInt = input.parse()?;
debug_eprintln!("{:?}", lit_integer);
Ok(LitInteger::Unsigned { lit_integer })
}
}
}