interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / vendor / syn-2.0.119 / src / scan_expr.rs

270 lines · 9440 bytes Raw Blame History
1use self::{Action::*, Input::*};
2use proc_macro2::{Delimiter, Ident, Spacing, TokenTree};
3use syn::parse::{ParseStream, Result};
4#[allow(unused_imports)]
5//#[cfg_attr(not(test), expect(unused_imports))] // Rust 1.81+
6use syn::Token;
7use syn::{AngleBracketedGenericArguments, BinOp, Expr, ExprPath, Lifetime, Lit, Type};
8
9enum Input {
10 Keyword(&'static str),
11 Punct(&'static str),
12 ConsumeAny,
13 ConsumeBinOp,
14 ConsumeBrace,
15 ConsumeDelimiter,
16 ConsumeIdent,
17 ConsumeLifetime,
18 ConsumeLiteral,
19 ConsumeNestedBrace,
20 ExpectPath,
21 ExpectTurbofish,
22 ExpectType,
23 CanBeginExpr,
24 Otherwise,
25 Empty,
26}
27
28enum Action {
29 SetState(&'static [(Input, Action)]),
30 IncDepth,
31 DecDepth,
32 Finish,
33}
34
35static INIT: [(Input, Action); 29] = [
36 (ConsumeDelimiter, SetState(&POSTFIX)),
37 (Keyword("async"), SetState(&ASYNC)),
38 (Keyword("break"), SetState(&BREAK_LABEL)),
39 (Keyword("const"), SetState(&CONST)),
40 (Keyword("continue"), SetState(&CONTINUE)),
41 (Keyword("for"), SetState(&FOR)),
42 (Keyword("if"), IncDepth),
43 (Keyword("let"), SetState(&PATTERN)),
44 (Keyword("loop"), SetState(&BLOCK)),
45 (Keyword("match"), IncDepth),
46 (Keyword("move"), SetState(&CLOSURE)),
47 (Keyword("return"), SetState(&RETURN)),
48 (Keyword("static"), SetState(&CLOSURE)),
49 (Keyword("try"), SetState(&BLOCK)),
50 (Keyword("unsafe"), SetState(&BLOCK)),
51 (Keyword("while"), IncDepth),
52 (Keyword("yield"), SetState(&RETURN)),
53 (Keyword("_"), SetState(&POSTFIX)),
54 (Punct("!"), SetState(&INIT)),
55 (Punct("#"), SetState(&[(ConsumeDelimiter, SetState(&INIT))])),
56 (Punct("&"), SetState(&REFERENCE)),
57 (Punct("*"), SetState(&INIT)),
58 (Punct("-"), SetState(&INIT)),
59 (Punct("..="), SetState(&INIT)),
60 (Punct(".."), SetState(&RANGE)),
61 (Punct("|"), SetState(&CLOSURE_ARGS)),
62 (ConsumeLifetime, SetState(&[(Punct(":"), SetState(&INIT))])),
63 (ConsumeLiteral, SetState(&POSTFIX)),
64 (ExpectPath, SetState(&PATH)),
65];
66
67static POSTFIX: [(Input, Action); 10] = [
68 (Keyword("as"), SetState(&[(ExpectType, SetState(&POSTFIX))])),
69 (Punct("..="), SetState(&INIT)),
70 (Punct(".."), SetState(&RANGE)),
71 (Punct("."), SetState(&DOT)),
72 (Punct("?"), SetState(&POSTFIX)),
73 (ConsumeBinOp, SetState(&INIT)),
74 (Punct("="), SetState(&INIT)),
75 (ConsumeNestedBrace, SetState(&IF_THEN)),
76 (ConsumeDelimiter, SetState(&POSTFIX)),
77 (Empty, Finish),
78];
79
80static ASYNC: [(Input, Action); 3] = [
81 (Keyword("move"), SetState(&ASYNC)),
82 (Punct("|"), SetState(&CLOSURE_ARGS)),
83 (ConsumeBrace, SetState(&POSTFIX)),
84];
85
86static BLOCK: [(Input, Action); 1] = [(ConsumeBrace, SetState(&POSTFIX))];
87
88static BREAK_LABEL: [(Input, Action); 2] = [
89 (ConsumeLifetime, SetState(&BREAK_VALUE)),
90 (Otherwise, SetState(&BREAK_VALUE)),
91];
92
93static BREAK_VALUE: [(Input, Action); 3] = [
94 (ConsumeNestedBrace, SetState(&IF_THEN)),
95 (CanBeginExpr, SetState(&INIT)),
96 (Otherwise, SetState(&POSTFIX)),
97];
98
99static CLOSURE: [(Input, Action); 7] = [
100 (Keyword("async"), SetState(&CLOSURE)),
101 (Keyword("move"), SetState(&CLOSURE)),
102 (Punct(","), SetState(&CLOSURE)),
103 (Punct(">"), SetState(&CLOSURE)),
104 (Punct("|"), SetState(&CLOSURE_ARGS)),
105 (ConsumeLifetime, SetState(&CLOSURE)),
106 (ConsumeIdent, SetState(&CLOSURE)),
107];
108
109static CLOSURE_ARGS: [(Input, Action); 2] = [
110 (Punct("|"), SetState(&CLOSURE_RET)),
111 (ConsumeAny, SetState(&CLOSURE_ARGS)),
112];
113
114static CLOSURE_RET: [(Input, Action); 2] = [
115 (Punct("->"), SetState(&[(ExpectType, SetState(&BLOCK))])),
116 (Otherwise, SetState(&INIT)),
117];
118
119static CONST: [(Input, Action); 3] = [
120 (Keyword("move"), SetState(&CLOSURE)),
121 (Punct("|"), SetState(&CLOSURE_ARGS)),
122 (ConsumeBrace, SetState(&POSTFIX)),
123];
124
125static CONTINUE: [(Input, Action); 2] = [
126 (ConsumeLifetime, SetState(&POSTFIX)),
127 (Otherwise, SetState(&POSTFIX)),
128];
129
130static DOT: [(Input, Action); 3] = [
131 (Keyword("await"), SetState(&POSTFIX)),
132 (ConsumeIdent, SetState(&METHOD)),
133 (ConsumeLiteral, SetState(&POSTFIX)),
134];
135
136static FOR: [(Input, Action); 2] = [
137 (Punct("<"), SetState(&CLOSURE)),
138 (Otherwise, SetState(&PATTERN)),
139];
140
141static IF_ELSE: [(Input, Action); 2] = [(Keyword("if"), SetState(&INIT)), (ConsumeBrace, DecDepth)];
142static IF_THEN: [(Input, Action); 2] =
143 [(Keyword("else"), SetState(&IF_ELSE)), (Otherwise, DecDepth)];
144
145static METHOD: [(Input, Action); 1] = [(ExpectTurbofish, SetState(&POSTFIX))];
146
147static PATH: [(Input, Action); 4] = [
148 (Punct("!="), SetState(&INIT)),
149 (Punct("!"), SetState(&INIT)),
150 (ConsumeNestedBrace, SetState(&IF_THEN)),
151 (Otherwise, SetState(&POSTFIX)),
152];
153
154static PATTERN: [(Input, Action); 15] = [
155 (ConsumeDelimiter, SetState(&PATTERN)),
156 (Keyword("box"), SetState(&PATTERN)),
157 (Keyword("in"), IncDepth),
158 (Keyword("mut"), SetState(&PATTERN)),
159 (Keyword("ref"), SetState(&PATTERN)),
160 (Keyword("_"), SetState(&PATTERN)),
161 (Punct("!"), SetState(&PATTERN)),
162 (Punct("&"), SetState(&PATTERN)),
163 (Punct("..="), SetState(&PATTERN)),
164 (Punct(".."), SetState(&PATTERN)),
165 (Punct("="), SetState(&INIT)),
166 (Punct("@"), SetState(&PATTERN)),
167 (Punct("|"), SetState(&PATTERN)),
168 (ConsumeLiteral, SetState(&PATTERN)),
169 (ExpectPath, SetState(&PATTERN)),
170];
171
172static RANGE: [(Input, Action); 6] = [
173 (Punct("..="), SetState(&INIT)),
174 (Punct(".."), SetState(&RANGE)),
175 (Punct("."), SetState(&DOT)),
176 (ConsumeNestedBrace, SetState(&IF_THEN)),
177 (Empty, Finish),
178 (Otherwise, SetState(&INIT)),
179];
180
181static RAW: [(Input, Action); 3] = [
182 (Keyword("const"), SetState(&INIT)),
183 (Keyword("mut"), SetState(&INIT)),
184 (Otherwise, SetState(&POSTFIX)),
185];
186
187static REFERENCE: [(Input, Action); 3] = [
188 (Keyword("mut"), SetState(&INIT)),
189 (Keyword("raw"), SetState(&RAW)),
190 (Otherwise, SetState(&INIT)),
191];
192
193static RETURN: [(Input, Action); 2] = [
194 (CanBeginExpr, SetState(&INIT)),
195 (Otherwise, SetState(&POSTFIX)),
196];
197
198pub(crate) fn scan_expr(input: ParseStream) -> Result<()> {
199 let mut state = INIT.as_slice();
200 let mut depth = 0usize;
201 'table: loop {
202 for rule in state {
203 if match rule.0 {
204 Input::Keyword(expected) => input.step(|cursor| match cursor.ident() {
205 Some((ident, rest)) if ident == expected => Ok((true, rest)),
206 _ => Ok((false, *cursor)),
207 })?,
208 Input::Punct(expected) => input.step(|cursor| {
209 let begin = *cursor;
210 let mut cursor = begin;
211 for (i, ch) in expected.chars().enumerate() {
212 match cursor.punct() {
213 Some((punct, _)) if punct.as_char() != ch => break,
214 Some((_, rest)) if i == expected.len() - 1 => {
215 return Ok((true, rest));
216 }
217 Some((punct, rest)) if punct.spacing() == Spacing::Joint => {
218 cursor = rest;
219 }
220 _ => break,
221 }
222 }
223 Ok((false, begin))
224 })?,
225 Input::ConsumeAny => input.parse::<Option<TokenTree>>()?.is_some(),
226 Input::ConsumeBinOp => input.parse::<BinOp>().is_ok(),
227 Input::ConsumeBrace | Input::ConsumeNestedBrace => {
228 (matches!(rule.0, Input::ConsumeBrace) || depth > 0)
229 && input.step(|cursor| match cursor.group(Delimiter::Brace) {
230 Some((_inside, _span, rest)) => Ok((true, rest)),
231 None => Ok((false, *cursor)),
232 })?
233 }
234 Input::ConsumeDelimiter => input.step(|cursor| match cursor.any_group() {
235 Some((_inside, _delimiter, _span, rest)) => Ok((true, rest)),
236 None => Ok((false, *cursor)),
237 })?,
238 Input::ConsumeIdent => input.parse::<Option<Ident>>()?.is_some(),
239 Input::ConsumeLifetime => input.parse::<Option<Lifetime>>()?.is_some(),
240 Input::ConsumeLiteral => input.parse::<Option<Lit>>()?.is_some(),
241 Input::ExpectPath => {
242 input.parse::<ExprPath>()?;
243 true
244 }
245 Input::ExpectTurbofish => {
246 if input.peek(Token![::]) {
247 input.parse::<AngleBracketedGenericArguments>()?;
248 }
249 true
250 }
251 Input::ExpectType => {
252 Type::without_plus(input)?;
253 true
254 }
255 Input::CanBeginExpr => Expr::peek(input),
256 Input::Otherwise => true,
257 Input::Empty => input.is_empty() || input.peek(Token![,]),
258 } {
259 state = match rule.1 {
260 Action::SetState(next) => next,
261 Action::IncDepth => (depth += 1, &INIT).1,
262 Action::DecDepth => (depth -= 1, &POSTFIX).1,
263 Action::Finish => return if depth == 0 { Ok(()) } else { break },
264 };
265 continue 'table;
266 }
267 }
268 return Err(input.error("unsupported expression"));
269 }
270}