mirror of
https://gitee.com/openharmony/third_party_rust_syn
synced 2024-11-30 03:10:24 +00:00
Static and const parsing
This commit is contained in:
parent
759d2ffd71
commit
47a877c90f
12
src/data.rs
12
src/data.rs
@ -192,9 +192,7 @@ mod printing {
|
||||
for attr in &self.attrs {
|
||||
attr.to_tokens(tokens);
|
||||
}
|
||||
if let Visibility::Public = self.vis {
|
||||
tokens.append("pub");
|
||||
}
|
||||
self.vis.to_tokens(tokens);
|
||||
if let Some(ref ident) = self.ident {
|
||||
ident.to_tokens(tokens);
|
||||
tokens.append(":");
|
||||
@ -203,6 +201,14 @@ mod printing {
|
||||
}
|
||||
}
|
||||
|
||||
impl ToTokens for Visibility {
|
||||
fn to_tokens(&self, tokens: &mut Tokens) {
|
||||
if let Visibility::Public = *self {
|
||||
tokens.append("pub");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ToTokens for Discriminant {
|
||||
fn to_tokens(&self, tokens: &mut Tokens) {
|
||||
Lit::Int(self.value, self.ty).to_tokens(tokens);
|
||||
|
42
src/expr.rs
42
src/expr.rs
@ -653,8 +653,48 @@ mod printing {
|
||||
impl ToTokens for Expr {
|
||||
fn to_tokens(&self, tokens: &mut Tokens) {
|
||||
match *self {
|
||||
Expr::Box(ref _inner) => unimplemented!(),
|
||||
Expr::Vec(ref _inner) => unimplemented!(),
|
||||
Expr::Call(ref _func, ref _args) => unimplemented!(),
|
||||
Expr::MethodCall(ref _ident, ref _ascript, ref _args) => unimplemented!(),
|
||||
Expr::Tup(ref fields) => {
|
||||
tokens.append("(");
|
||||
tokens.append_separated(fields, ",");
|
||||
if fields.len() == 1 {
|
||||
tokens.append(",");
|
||||
}
|
||||
tokens.append(")");
|
||||
}
|
||||
Expr::Binary(_op, ref _left, ref _right) => unimplemented!(),
|
||||
Expr::Unary(_op, ref _expr) => unimplemented!(),
|
||||
Expr::Lit(ref lit) => lit.to_tokens(tokens),
|
||||
_ => unimplemented!(),
|
||||
Expr::Cast(ref _expr, ref _ty) => unimplemented!(),
|
||||
Expr::Type(ref _expr, ref _ty) => unimplemented!(),
|
||||
Expr::If(ref _cond, ref _then_block, ref _else_block) => unimplemented!(),
|
||||
Expr::IfLet(ref _pat, ref _expr, ref _then_block, ref _else_block) => unimplemented!(),
|
||||
Expr::While(ref _cond, ref _body, ref _label) => unimplemented!(),
|
||||
Expr::WhileLet(ref _pat, ref _expr, ref _body, ref _label) => unimplemented!(),
|
||||
Expr::ForLoop(ref _pat, ref _expr, ref _body, ref _label) => unimplemented!(),
|
||||
Expr::Loop(ref _body, ref _label) => unimplemented!(),
|
||||
Expr::Match(ref _expr, ref _arms) => unimplemented!(),
|
||||
Expr::Closure(_capture, ref _decl, ref _body) => unimplemented!(),
|
||||
Expr::Block(ref _block) => unimplemented!(),
|
||||
Expr::Assign(ref _var, ref _expr) => unimplemented!(),
|
||||
Expr::AssignOp(_op, ref _var, ref _expr) => unimplemented!(),
|
||||
Expr::Field(ref _expr, ref _field) => unimplemented!(),
|
||||
Expr::TupField(ref _expr, _field) => unimplemented!(),
|
||||
Expr::Index(ref _expr, ref _index) => unimplemented!(),
|
||||
Expr::Range(ref _from, ref _to, _limits) => unimplemented!(),
|
||||
Expr::Path(ref _qself, ref _path) => unimplemented!(),
|
||||
Expr::AddrOf(_mutability, ref _expr) => unimplemented!(),
|
||||
Expr::Break(ref _label) => unimplemented!(),
|
||||
Expr::Continue(ref _label) => unimplemented!(),
|
||||
Expr::Ret(ref _expr) => unimplemented!(),
|
||||
Expr::Mac(ref _mac) => unimplemented!(),
|
||||
Expr::Struct(ref _path, ref _fields, ref _base) => unimplemented!(),
|
||||
Expr::Repeat(ref _expr, ref _times) => unimplemented!(),
|
||||
Expr::Paren(ref _inner) => unimplemented!(),
|
||||
Expr::Try(ref _expr) => unimplemented!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
78
src/item.rs
78
src/item.rs
@ -211,15 +211,19 @@ pub mod parsing {
|
||||
use super::*;
|
||||
use attr::parsing::outer_attr;
|
||||
use data::parsing::visibility;
|
||||
use expr::parsing::expr;
|
||||
use ident::parsing::ident;
|
||||
use macro_input::{Body, MacroInput};
|
||||
use macro_input::parsing::macro_input;
|
||||
use ty::parsing::{mutability, ty};
|
||||
|
||||
named!(pub item -> Item, alt!(
|
||||
item_extern_crate
|
||||
// TODO: Use
|
||||
// TODO: Static
|
||||
// TODO: Const
|
||||
|
|
||||
item_static
|
||||
|
|
||||
item_const
|
||||
// TODO: Fn
|
||||
// TODO: Mod
|
||||
// TODO: ForeignMod
|
||||
@ -258,6 +262,43 @@ pub mod parsing {
|
||||
})
|
||||
));
|
||||
|
||||
named!(item_static -> Item, do_parse!(
|
||||
attrs: many0!(outer_attr) >>
|
||||
vis: visibility >>
|
||||
keyword!("static") >>
|
||||
mutability: mutability >>
|
||||
id: ident >>
|
||||
punct!(":") >>
|
||||
ty: ty >>
|
||||
punct!("=") >>
|
||||
value: expr >>
|
||||
punct!(";") >>
|
||||
(Item {
|
||||
ident: id,
|
||||
vis: vis,
|
||||
attrs: attrs,
|
||||
node: ItemKind::Static(Box::new(ty), mutability, Box::new(value)),
|
||||
})
|
||||
));
|
||||
|
||||
named!(item_const -> Item, do_parse!(
|
||||
attrs: many0!(outer_attr) >>
|
||||
vis: visibility >>
|
||||
keyword!("const") >>
|
||||
id: ident >>
|
||||
punct!(":") >>
|
||||
ty: ty >>
|
||||
punct!("=") >>
|
||||
value: expr >>
|
||||
punct!(";") >>
|
||||
(Item {
|
||||
ident: id,
|
||||
vis: vis,
|
||||
attrs: attrs,
|
||||
node: ItemKind::Const(Box::new(ty), Box::new(value)),
|
||||
})
|
||||
));
|
||||
|
||||
named!(item_struct_or_enum -> Item, map!(
|
||||
macro_input,
|
||||
|def: MacroInput| Item {
|
||||
@ -280,7 +321,7 @@ pub mod parsing {
|
||||
mod printing {
|
||||
use super::*;
|
||||
use attr::FilterAttrs;
|
||||
use data::{VariantData, Visibility};
|
||||
use data::VariantData;
|
||||
use quote::{Tokens, ToTokens};
|
||||
|
||||
impl ToTokens for Item {
|
||||
@ -300,16 +341,33 @@ mod printing {
|
||||
tokens.append(";");
|
||||
}
|
||||
ItemKind::Use(ref _view_path) => unimplemented!(),
|
||||
ItemKind::Static(ref _ty, ref _mutability, ref _expr) => unimplemented!(),
|
||||
ItemKind::Const(ref _ty, ref _expr) => unimplemented!(),
|
||||
ItemKind::Static(ref ty, ref mutability, ref expr) => {
|
||||
self.vis.to_tokens(tokens);
|
||||
tokens.append("static");
|
||||
mutability.to_tokens(tokens);
|
||||
self.ident.to_tokens(tokens);
|
||||
tokens.append(":");
|
||||
ty.to_tokens(tokens);
|
||||
tokens.append("=");
|
||||
expr.to_tokens(tokens);
|
||||
tokens.append(";");
|
||||
}
|
||||
ItemKind::Const(ref ty, ref expr) => {
|
||||
self.vis.to_tokens(tokens);
|
||||
tokens.append("const");
|
||||
self.ident.to_tokens(tokens);
|
||||
tokens.append(":");
|
||||
ty.to_tokens(tokens);
|
||||
tokens.append("=");
|
||||
expr.to_tokens(tokens);
|
||||
tokens.append(";");
|
||||
}
|
||||
ItemKind::Fn(ref _decl, _unsafety, _constness, ref _abi, ref _generics, ref _block) => unimplemented!(),
|
||||
ItemKind::Mod(ref _items) => unimplemented!(),
|
||||
ItemKind::ForeignMod(ref _foreign_mod) => unimplemented!(),
|
||||
ItemKind::Ty(ref _ty, ref _generics) => unimplemented!(),
|
||||
ItemKind::Enum(ref variants, ref generics) => {
|
||||
if let Visibility::Public = self.vis {
|
||||
tokens.append("pub");
|
||||
}
|
||||
self.vis.to_tokens(tokens);
|
||||
tokens.append("enum");
|
||||
self.ident.to_tokens(tokens);
|
||||
generics.to_tokens(tokens);
|
||||
@ -322,9 +380,7 @@ mod printing {
|
||||
tokens.append("}");
|
||||
}
|
||||
ItemKind::Struct(ref variant_data, ref generics) => {
|
||||
if let Visibility::Public = self.vis {
|
||||
tokens.append("pub");
|
||||
}
|
||||
self.vis.to_tokens(tokens);
|
||||
tokens.append("struct");
|
||||
self.ident.to_tokens(tokens);
|
||||
generics.to_tokens(tokens);
|
||||
|
@ -56,7 +56,7 @@ pub mod parsing {
|
||||
mod printing {
|
||||
use super::*;
|
||||
use attr::FilterAttrs;
|
||||
use data::{Visibility, VariantData};
|
||||
use data::VariantData;
|
||||
use quote::{Tokens, ToTokens};
|
||||
|
||||
impl ToTokens for MacroInput {
|
||||
@ -64,9 +64,7 @@ mod printing {
|
||||
for attr in self.attrs.outer() {
|
||||
attr.to_tokens(tokens);
|
||||
}
|
||||
if let Visibility::Public = self.vis {
|
||||
tokens.append("pub");
|
||||
}
|
||||
self.vis.to_tokens(tokens);
|
||||
match self.body {
|
||||
Body::Enum(_) => tokens.append("enum"),
|
||||
Body::Struct(_) => tokens.append("struct"),
|
||||
|
14
src/ty.rs
14
src/ty.rs
@ -337,7 +337,7 @@ pub mod parsing {
|
||||
(Ty::Paren(Box::new(elem)))
|
||||
));
|
||||
|
||||
named!(mutability -> Mutability, alt!(
|
||||
named!(pub mutability -> Mutability, alt!(
|
||||
do_parse!(
|
||||
keyword!("mut") >>
|
||||
(Mutability::Mutable)
|
||||
@ -447,9 +447,7 @@ mod printing {
|
||||
Ty::Rptr(ref lifetime, ref target) => {
|
||||
tokens.append("&");
|
||||
lifetime.to_tokens(tokens);
|
||||
if let Mutability::Mutable = target.mutability {
|
||||
tokens.append("mut");
|
||||
}
|
||||
target.mutability.to_tokens(tokens);
|
||||
target.ty.to_tokens(tokens);
|
||||
}
|
||||
Ty::BareFn(ref func) => {
|
||||
@ -508,6 +506,14 @@ mod printing {
|
||||
}
|
||||
}
|
||||
|
||||
impl ToTokens for Mutability {
|
||||
fn to_tokens(&self, tokens: &mut Tokens) {
|
||||
if let Mutability::Mutable = *self {
|
||||
tokens.append("mut");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ToTokens for Path {
|
||||
fn to_tokens(&self, tokens: &mut Tokens) {
|
||||
for (i, segment) in self.segments.iter().enumerate() {
|
||||
|
9
tests/cases/global.rs
Normal file
9
tests/cases/global.rs
Normal file
@ -0,0 +1,9 @@
|
||||
static x: () = ();
|
||||
|
||||
pub static x: () = ();
|
||||
|
||||
pub static mut x: () = ();
|
||||
|
||||
const x: () = ();
|
||||
|
||||
pub const x: () = ();
|
Loading…
Reference in New Issue
Block a user