Skip to content
This repository has been archived by the owner on May 6, 2020. It is now read-only.

Commit

Permalink
Add support for doc comments, inner attrs, AstBuilder::attr()
Browse files Browse the repository at this point in the history
  • Loading branch information
erickt committed Apr 20, 2015
1 parent 5bfb85a commit 48008b3
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]

name = "aster"
version = "0.2.0"
version = "0.2.1"
authors = ["Erick Tryzelaar <[email protected]>"]
license = "MIT/Apache-2.0"
description = "A libsyntax ast builder"
Expand Down
35 changes: 31 additions & 4 deletions src/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,23 @@ use syntax::codemap::{DUMMY_SP, Span, respan};
use syntax::parse::token;
use syntax::ptr::P;

use invoke::Invoke;
use invoke::{Invoke, Identity};
use lit::LitBuilder;
use str::ToInternedString;

//////////////////////////////////////////////////////////////////////////////

pub struct AttrBuilder<F> {
pub struct AttrBuilder<F=Identity> {
callback: F,
span: Span,
style: ast::AttrStyle,
is_sugared_doc: bool,
}

impl AttrBuilder {
pub fn new() -> Self {
AttrBuilder::new_with_callback(Identity)
}
}

impl<F> AttrBuilder<F>
Expand All @@ -24,6 +32,8 @@ impl<F> AttrBuilder<F>
AttrBuilder {
callback: callback,
span: DUMMY_SP,
style: ast::AttrOuter,
is_sugared_doc: false,
}
}

Expand All @@ -32,12 +42,17 @@ impl<F> AttrBuilder<F>
self
}

pub fn inner(mut self) -> Self {
self.style = ast::AttrInner;
self
}

pub fn build_meta_item(self, item: P<ast::MetaItem>) -> F::Result {
let attr = respan(self.span, ast::Attribute_ {
id: attr::mk_attr_id(),
style: ast::AttrOuter,
style: self.style,
value: item,
is_sugared_doc: false,
is_sugared_doc: self.is_sugared_doc,
});
self.callback.invoke(attr)
}
Expand Down Expand Up @@ -114,6 +129,18 @@ impl<F> AttrBuilder<F>
{
self.list("plugin").words(iter).build()
}

/**
* Create a #[doc = "..."] node. Note that callers of this must make sure to prefix their
* comments with either "///" or "/\*\*" if an outer comment, or "//!" or "/\*!" if an inner
* comment.
*/
pub fn doc<T>(mut self, doc: T) -> F::Result
where T: ToInternedString,
{
self.is_sugared_doc = true;
self.name_value("doc").str(doc)
}
}

impl<F> Invoke<P<ast::MetaItem>> for AttrBuilder<F>
Expand Down
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ impl AstBuilder {
lifetime.into_lifetime()
}

pub fn attr(&self) -> attr::AttrBuilder {
attr::AttrBuilder::new()
}

pub fn path(&self) -> path::PathBuilder {
path::PathBuilder::new()
}
Expand Down
33 changes: 33 additions & 0 deletions tests/test_attr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#![feature(rustc_private)]

extern crate aster;
extern crate syntax;

use syntax::ast;
use syntax::codemap::{DUMMY_SP, respan};
use syntax::ptr::P;

use aster::AstBuilder;

#[test]
fn test_doc() {
let builder = AstBuilder::new();
assert_eq!(
builder.attr().doc("/// doc string"),
respan(
DUMMY_SP,
ast::Attribute_ {
id: ast::AttrId(0),
style: ast::AttrOuter,
value: P(respan(
DUMMY_SP,
ast::MetaNameValue(
builder.interned_string("doc"),
(*builder.lit().str("/// doc string")).clone(),
),
)),
is_sugared_doc: true,
}
)
);
}

0 comments on commit 48008b3

Please sign in to comment.