Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pretty print Expr::RawAddr #82

Merged
merged 1 commit into from
Oct 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ verbatim = ["syn/parsing"]

[dependencies]
proc-macro2 = { version = "1.0.80", default-features = false }
syn = { version = "2.0.76", default-features = false, features = ["full"] }
syn = { version = "2.0.80", default-features = false, features = ["full"] }

[dev-dependencies]
indoc = "2"
proc-macro2 = { version = "1.0.80", default-features = false }
quote = { version = "1.0.35", default-features = false }
syn = { version = "2.0.76", default-features = false, features = ["parsing"] }
syn = { version = "2.0.80", default-features = false, features = ["parsing"] }

[lib]
doc-scrape-examples = false
Expand Down
56 changes: 25 additions & 31 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ use syn::{
token, Arm, Attribute, BinOp, Block, Expr, ExprArray, ExprAssign, ExprAsync, ExprAwait,
ExprBinary, ExprBlock, ExprBreak, ExprCall, ExprCast, ExprClosure, ExprConst, ExprContinue,
ExprField, ExprForLoop, ExprGroup, ExprIf, ExprIndex, ExprInfer, ExprLet, ExprLit, ExprLoop,
ExprMacro, ExprMatch, ExprMethodCall, ExprParen, ExprPath, ExprRange, ExprReference,
ExprRepeat, ExprReturn, ExprStruct, ExprTry, ExprTryBlock, ExprTuple, ExprUnary, ExprUnsafe,
ExprWhile, ExprYield, FieldValue, Index, Label, Member, RangeLimits, ReturnType, Stmt, Token,
UnOp,
ExprMacro, ExprMatch, ExprMethodCall, ExprParen, ExprPath, ExprRange, ExprRawAddr,
ExprReference, ExprRepeat, ExprReturn, ExprStruct, ExprTry, ExprTryBlock, ExprTuple, ExprUnary,
ExprUnsafe, ExprWhile, ExprYield, FieldValue, Index, Label, Member, PointerMutability,
RangeLimits, ReturnType, Stmt, Token, UnOp,
};

impl Printer {
Expand Down Expand Up @@ -48,6 +48,7 @@ impl Printer {
Expr::Paren(expr) => self.expr_paren(expr),
Expr::Path(expr) => self.expr_path(expr),
Expr::Range(expr) => self.expr_range(expr),
Expr::RawAddr(expr) => self.expr_raw_addr(expr),
Expr::Reference(expr) => self.expr_reference(expr),
Expr::Repeat(expr) => self.expr_repeat(expr),
Expr::Return(expr) => self.expr_return(expr),
Expand Down Expand Up @@ -564,6 +565,14 @@ impl Printer {
}
}

fn expr_raw_addr(&mut self, expr: &ExprRawAddr) {
self.outer_attrs(&expr.attrs);
self.word("&raw ");
self.pointer_mutability(&expr.mutability);
self.nbsp();
self.expr(&expr.expr);
}

fn expr_reference(&mut self, expr: &ExprReference) {
self.outer_attrs(&expr.attrs);
self.word("&");
Expand Down Expand Up @@ -683,7 +692,6 @@ impl Printer {
Ellipsis,
Become(Become),
Builtin(Builtin),
RawReference(RawReference),
}

struct Become {
Expand All @@ -697,12 +705,6 @@ impl Printer {
args: TokenStream,
}

struct RawReference {
attrs: Vec<Attribute>,
mutable: bool,
expr: Expr,
}

mod kw {
syn::custom_keyword!(builtin);
syn::custom_keyword!(raw);
Expand All @@ -729,20 +731,6 @@ impl Printer {
parenthesized!(args in input);
let args: TokenStream = args.parse()?;
Ok(ExprVerbatim::Builtin(Builtin { attrs, name, args }))
} else if lookahead.peek(Token![&]) {
input.advance_to(&ahead);
input.parse::<Token![&]>()?;
input.parse::<kw::raw>()?;
let mutable = input.parse::<Option<Token![mut]>>()?.is_some();
if !mutable {
input.parse::<Token![const]>()?;
}
let expr: Expr = input.parse()?;
Ok(ExprVerbatim::RawReference(RawReference {
attrs,
mutable,
expr,
}))
} else if lookahead.peek(Token![...]) {
input.parse::<Token![...]>()?;
Ok(ExprVerbatim::Ellipsis)
Expand Down Expand Up @@ -785,12 +773,6 @@ impl Printer {
}
self.word(")");
}
ExprVerbatim::RawReference(expr) => {
self.outer_attrs(&expr.attrs);
self.word("&raw ");
self.word(if expr.mutable { "mut " } else { "const " });
self.expr(&expr.expr);
}
}
}

Expand Down Expand Up @@ -1013,6 +995,13 @@ impl Printer {
);
}

fn pointer_mutability(&mut self, mutability: &PointerMutability) {
match mutability {
PointerMutability::Const(_) => self.word("const"),
PointerMutability::Mut(_) => self.word("mut"),
}
}

fn zerobreak_unless_short_ident(&mut self, beginning_of_line: bool, expr: &Expr) {
if beginning_of_line && is_short_ident(expr) {
return;
Expand Down Expand Up @@ -1055,6 +1044,7 @@ fn requires_terminator(expr: &Expr) -> bool {
| Expr::Paren(_)
| Expr::Path(_)
| Expr::Range(_)
| Expr::RawAddr(_)
| Expr::Reference(_)
| Expr::Repeat(_)
| Expr::Return(_)
Expand Down Expand Up @@ -1090,6 +1080,7 @@ fn contains_exterior_struct_lit(expr: &Expr) -> bool {
| Expr::Group(ExprGroup { expr: e, .. })
| Expr::Index(ExprIndex { expr: e, .. })
| Expr::MethodCall(ExprMethodCall { receiver: e, .. })
| Expr::RawAddr(ExprRawAddr { expr: e, .. })
| Expr::Reference(ExprReference { expr: e, .. })
| Expr::Unary(ExprUnary { expr: e, .. }) => {
// &X { y: 1 }, X { y: 1 }.y
Expand Down Expand Up @@ -1172,6 +1163,7 @@ fn needs_newline_if_wrap(expr: &Expr) -> bool {
| Expr::Let(ExprLet { expr: e, .. })
| Expr::Paren(ExprParen { expr: e, .. })
| Expr::Range(ExprRange { end: Some(e), .. })
| Expr::RawAddr(ExprRawAddr { expr: e, .. })
| Expr::Reference(ExprReference { expr: e, .. })
| Expr::Return(ExprReturn { expr: Some(e), .. })
| Expr::Try(ExprTry { expr: e, .. })
Expand Down Expand Up @@ -1229,6 +1221,7 @@ fn is_blocklike(expr: &Expr) -> bool {
| Expr::Paren(_)
| Expr::Path(_)
| Expr::Range(_)
| Expr::RawAddr(_)
| Expr::Reference(_)
| Expr::Repeat(_)
| Expr::Return(_)
Expand Down Expand Up @@ -1266,6 +1259,7 @@ fn parseable_as_stmt(expr: &Expr) -> bool {
| Expr::Match(_)
| Expr::Paren(_)
| Expr::Path(_)
| Expr::RawAddr(_)
| Expr::Reference(_)
| Expr::Repeat(_)
| Expr::Return(_)
Expand Down
2 changes: 2 additions & 0 deletions src/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ pub fn add_semi(expr: &Expr) -> bool {
| Expr::Paren(_)
| Expr::Path(_)
| Expr::Range(_)
| Expr::RawAddr(_)
| Expr::Reference(_)
| Expr::Repeat(_)
| Expr::Struct(_)
Expand Down Expand Up @@ -203,6 +204,7 @@ fn remove_semi(expr: &Expr) -> bool {
| Expr::Paren(_)
| Expr::Path(_)
| Expr::Range(_)
| Expr::RawAddr(_)
| Expr::Reference(_)
| Expr::Repeat(_)
| Expr::Return(_)
Expand Down
Loading