Debian Codegraph @

third_party/rust/uniffi_internal_macros/src/ir/parse.rs

raw
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5 //! Parsing code for the ast
6
7 use quote::format_ident;
8 use syn::{
9 braced, parenthesized,
10 parse::{Parse, ParseStream},
11 token, Attribute, Ident, Token,
12 };
13
14 use super::ast::*;
15
16 impl Parse for Node {
17 fn parse(input: ParseStream) -> syn::Result<Self> {
18 let attrs = input.parse()?;
19 let vis = input.parse()?;
20 let lookahead = input.lookahead1();
21 if lookahead.peek(Token![struct]) {
22 let _: Token![struct] = input.parse()?;
23 let ident = input.parse()?;
24 let generics = input.parse()?;
25 let fields: Fields = input.parse()?;
26 if fields.not_named() {
27 let _: Token![;] = input.parse()?;
28 }
29 let mut wraps_field = None;
30 for f in fields.iter() {
31 if f.attrs.wraps {
32 if wraps_field.is_some() {
33 return Err(syn::Error::new(f.var_name.span(), "Multiple wraps"));
34 } else {
35 wraps_field = Some(f.clone());
36 }
37 }
38 }
39 let st = Struct {
40 fields,
41 wraps_field,
42 };
43 Ok(Self {
44 attrs,
45 vis,
46 ident,
47 generics,
48 def: NodeDef::Struct(st),
49 })
50 } else if lookahead.peek(Token![enum]) {
51 let _: Token![enum] = input.parse()?;
52 let ident = input.parse()?;
53 let generics = input.parse()?;
54 let content;
55 braced!(content in input);
56 let variants = content.parse_terminated(Variant::parse, Token![,])?;
57 Ok(Self {
58 attrs,
59 vis,
60 ident,
61 generics,
62 def: NodeDef::Enum(variants.into_iter().collect()),
63 })
64 } else {
65 Err(lookahead.error())
66 }
67 }
68 }
69
70 impl Parse for Attributes {
71 fn parse(input: ParseStream) -> syn::Result<Self> {
72 let parsed_attrs = input.call(Attribute::parse_outer)?;
73 Self::from_list(&parsed_attrs)
74 }
75 }
76
77 impl Attributes {
78 pub fn from_list(attrs: &[Attribute]) -> syn::Result<Self> {
79 let mut result = Attributes::default();
80 for attr in attrs {
81 if attr.path().is_ident("node") {
82 attr.parse_nested_meta(|meta| {
83 if meta.path.is_ident("from") {
84 let content;
85 parenthesized!(content in meta.input);
86 result.from = Some(content.parse()?);
87 return Ok(());
88 } else if meta.path.is_ident("wraps") {
89 result.wraps = true;
90 return Ok(());
91 }
92 Err(meta.error("unrecognized node attr"))
93 })?;
94 }
95 }
96 Ok(result)
97 }
98 }
99
100 impl Parse for Fields {
101 fn parse(input: ParseStream) -> syn::Result<Self> {
102 let lookahead = input.lookahead1();
103 if lookahead.peek(Token![,]) || lookahead.peek(Token![;]) || lookahead.peek(Token![=]) {
104 Ok(Self::Unit)
105 } else if lookahead.peek(token::Brace) {
106 let content;
107 braced!(content in input);
108 Ok(Self::Named(
109 content
110 .parse_terminated(Field::parse_named, Token![,])?
111 .into_iter()
112 .collect(),
113 ))
114 } else if lookahead.peek(token::Paren) {
115 let content;
116 parenthesized!(content in input);
117 let mut counter = 0;
118 let mut fields = vec![];
119 while !content.is_empty() {
120 fields.push(Field::parse_unnamed(&content, counter)?);
121 counter += 1;
122 if content.is_empty() {
123 break;
124 }
125 let _: Token![,] = content.parse()?;
126 }
127 Ok(Self::Unnamed(fields))
128 } else {
129 Err(lookahead.error())
130 }
131 }
132 }
133
134 impl Parse for Variant {
135 fn parse(input: ParseStream) -> syn::Result<Self> {
136 let attrs = input.parse()?;
137 let vis = input.parse()?;
138 let ident = input.parse()?;
139 let fields = input.parse()?;
140 let mut discriminant = None;
141 if input.peek(Token![=]) {
142 let _: Token![=] = input.parse()?;
143 discriminant = Some(input.parse()?);
144 }
145 Ok(Self {
146 attrs,
147 vis,
148 ident,
149 fields,
150 discriminant,
151 })
152 }
153 }
154
155 impl Field {
156 fn parse_named(input: ParseStream) -> syn::Result<Self> {
157 let attrs = input.parse()?;
158 let vis = input.parse()?;
159 let ident: Ident = input.parse()?;
160 let _ = input.parse::<Token![:]>()?;
161 let ty = input.parse()?;
162 Ok(Self {
163 attrs,
164 vis,
165 ident: Some(ident.clone()),
166 ty,
167 var_name: ident,
168 })
169 }
170
171 fn parse_unnamed(input: ParseStream, index: usize) -> syn::Result<Self> {
172 Ok(Self {
173 attrs: input.parse()?,
174 vis: input.parse()?,
175 ident: None,
176 ty: input.parse()?,
177 var_name: format_ident!("var{index}"),
178 })
179 }
180 }