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

Rewrite tokenization with proc-macro2 tokens #146

Merged
merged 1 commit into from
May 26, 2017
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
13 changes: 9 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,22 @@ include = ["Cargo.toml", "src/**/*.rs", "README.md", "LICENSE-APACHE", "LICENSE-
default = ["parsing", "printing", "clone-impls"]
aster = []
full = []
parsing = ["unicode-xid", "synom"]
printing = ["quote"]
parsing = ["unicode-xid", "synom/parsing"]
printing = ["quote", "synom/printing"]
visit = []
fold = []
clone-impls = []
extra-traits = []

[dependencies]
quote = { version = "0.3.7", optional = true }
proc-macro2 = { git = 'https://github.com/alexcrichton/proc-macro2' }
unicode-xid = { version = "0.0.4", optional = true }
synom = { version = "0.11", path = "synom", optional = true }
synom = { version = "0.11", path = "synom", default-features = true }

[dependencies.quote]
git = 'https://github.com/alexcrichton/quote'
branch = 'new-tokens'
optional = true

[dev-dependencies]
syntex_pos = "0.58"
Expand Down
33 changes: 20 additions & 13 deletions src/aster/generics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use aster::path::IntoPath;
use aster::ty_param::TyParamBuilder;
use aster::where_predicate::WherePredicateBuilder;

use delimited::Delimited;

pub struct GenericsBuilder<F = Identity> {
callback: F,
lifetimes: Vec<LifetimeDef>,
Expand Down Expand Up @@ -37,16 +39,16 @@ impl<F> GenericsBuilder<F>
pub fn from_generics_with_callback(generics: Generics, callback: F) -> Self {
GenericsBuilder {
callback: callback,
lifetimes: generics.lifetimes,
ty_params: generics.ty_params,
predicates: generics.where_clause.predicates,
lifetimes: generics.lifetimes.into_vec(),
ty_params: generics.ty_params.into_vec(),
predicates: generics.where_clause.predicates.into_vec(),
}
}

pub fn with(self, generics: Generics) -> Self {
self.with_lifetimes(generics.lifetimes.into_iter())
.with_ty_params(generics.ty_params.into_iter())
.with_predicates(generics.where_clause.predicates.into_iter())
self.with_lifetimes(generics.lifetimes.into_vec().into_iter())
.with_ty_params(generics.ty_params.into_vec().into_iter())
.with_predicates(generics.where_clause.predicates.into_vec().into_iter())
}

pub fn with_lifetimes<I, L>(mut self, iter: I) -> Self
Expand Down Expand Up @@ -141,7 +143,7 @@ impl<F> GenericsBuilder<F>
let lifetime = lifetime.into_lifetime();

for lifetime_def in &mut self.lifetimes {
lifetime_def.bounds.push(lifetime.clone());
lifetime_def.bounds.push_default(lifetime.clone());
}

for ty_param in &mut self.ty_params {
Expand Down Expand Up @@ -174,14 +176,14 @@ impl<F> GenericsBuilder<F>

pub fn strip_lifetimes(mut self) -> Self {
for lifetime in &mut self.lifetimes {
lifetime.bounds = vec![];
lifetime.bounds = Delimited::new();
}
self
}

pub fn strip_ty_params(mut self) -> Self {
for ty_param in &mut self.ty_params {
ty_param.bounds = vec![];
ty_param.bounds = Delimited::new();
}
self
}
Expand All @@ -193,10 +195,15 @@ impl<F> GenericsBuilder<F>

pub fn build(self) -> F::Result {
self.callback.invoke(Generics {
lifetimes: self.lifetimes,
ty_params: self.ty_params,
where_clause: WhereClause { predicates: self.predicates },
})
lifetimes: self.lifetimes.into(),
ty_params: self.ty_params.into(),
where_clause: WhereClause {
predicates: self.predicates.into(),
where_token: Default::default(),
},
lt_token: Default::default(),
gt_token: Default::default(),
})
}
}

Expand Down
13 changes: 8 additions & 5 deletions src/aster/lifetime.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use {Ident, Lifetime, LifetimeDef};
use aster::invoke::{Invoke, Identity};
use delimited::Delimited;

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

Expand Down Expand Up @@ -36,7 +37,8 @@ impl IntoLifetimeDef for Lifetime {
LifetimeDef {
attrs: vec![],
lifetime: self,
bounds: vec![],
bounds: Delimited::new(),
colon_token: Default::default(),
}
}
}
Expand Down Expand Up @@ -95,9 +97,10 @@ impl<F> LifetimeDefBuilder<F>

pub fn build(self) -> F::Result {
self.callback.invoke(LifetimeDef {
attrs: vec![],
lifetime: self.lifetime,
bounds: self.bounds,
})
attrs: vec![],
lifetime: self.lifetime,
bounds: self.bounds.into(),
colon_token: Default::default(),
})
}
}
42 changes: 25 additions & 17 deletions src/aster/path.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use {AngleBracketedParameterData, Generics, Ident, Lifetime, ParenthesizedParameterData, Path,
PathParameters, PathSegment, Ty, TypeBinding};
PathParameters, PathSegment, Ty, TypeBinding, FunctionRetTy};
use aster::ident::ToIdent;
use aster::invoke::{Invoke, Identity};
use aster::lifetime::IntoLifetime;
Expand Down Expand Up @@ -139,9 +139,10 @@ impl<F> PathSegmentsBuilder<F>

pub fn build(self) -> F::Result {
self.callback.invoke(Path {
global: self.global,
segments: self.segments,
})
global: self.global,
segments: self.segments.into(),
leading_colon: None,
})
}
}

Expand Down Expand Up @@ -181,10 +182,10 @@ impl<F> PathSegmentBuilder<F>

pub fn with_generics(self, generics: Generics) -> Self {
// Strip off the bounds.
let lifetimes = generics.lifetimes.iter().map(|lifetime_def| lifetime_def.lifetime.clone());
let lifetimes = generics.lifetimes.iter().map(|lifetime_def| lifetime_def.item().lifetime.clone());

let tys =
generics.ty_params.iter().map(|ty_param| TyBuilder::new().id(ty_param.ident.clone()));
generics.ty_params.iter().map(|ty_param| TyBuilder::new().id(ty_param.item().ident.clone()));

self.with_lifetimes(lifetimes).with_tys(tys)
}
Expand Down Expand Up @@ -252,23 +253,29 @@ impl<F> PathSegmentBuilder<F>

pub fn build_return(self, output: Option<Ty>) -> F::Result {
let data = ParenthesizedParameterData {
inputs: self.tys,
output: output,
inputs: self.tys.into(),
output: match output {
Some(ty) => FunctionRetTy::Ty(ty, Default::default()),
None => FunctionRetTy::Default,
},
paren_token: Default::default(),
};

let parameters = PathParameters::Parenthesized(data);

self.callback.invoke(PathSegment {
ident: self.id,
parameters: parameters,
})
ident: self.id,
parameters: parameters,
})
}

pub fn build(self) -> F::Result {
let data = AngleBracketedParameterData {
lifetimes: self.lifetimes,
types: self.tys,
bindings: self.bindings,
lifetimes: self.lifetimes.into(),
types: self.tys.into(),
bindings: self.bindings.into(),
gt_token: Default::default(),
lt_token: Default::default(),
};

let parameters = PathParameters::AngleBracketed(data);
Expand Down Expand Up @@ -306,9 +313,10 @@ impl<F> Invoke<Ty> for TypeBindingBuilder<F>
let id = self.id;

self.builder.with_binding(TypeBinding {
ident: id,
ty: ty,
})
ident: id,
ty: ty,
eq_token: Default::default(),
})
}
}

Expand Down
12 changes: 9 additions & 3 deletions src/aster/qpath.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use aster::ident::ToIdent;
use aster::invoke::{Invoke, Identity};
use aster::path::{PathBuilder, PathSegmentBuilder};
use aster::ty::TyBuilder;
use delimited::Delimited;

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

Expand Down Expand Up @@ -74,7 +75,8 @@ impl<F> QPathTyBuilder<F>
{
let path = Path {
global: false,
segments: vec![],
segments: Delimited::new(),
leading_colon: None,
};
self.as_().build(path).id(id)
}
Expand All @@ -84,7 +86,8 @@ impl<F> QPathTyBuilder<F>
{
let path = Path {
global: false,
segments: vec![],
segments: Delimited::new(),
leading_colon: None,
};
self.as_().build(path).segment(id)
}
Expand All @@ -101,6 +104,9 @@ impl<F> Invoke<Path> for QPathTyBuilder<F>
qself: QSelf {
ty: Box::new(self.ty),
position: path.segments.len(),
as_token: Default::default(),
gt_token: Default::default(),
lt_token: Default::default(),
},
path: path,
}
Expand Down Expand Up @@ -137,7 +143,7 @@ impl<F> Invoke<PathSegment> for QPathQSelfBuilder<F>
type Result = F::Result;

fn invoke(mut self, segment: PathSegment) -> F::Result {
self.path.segments.push(segment);
self.path.segments.push_default(segment);
self.builder.build(self.qself, self.path)
}
}
29 changes: 22 additions & 7 deletions src/aster/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,10 @@ impl<F> TyBuilder<F>
}

pub fn build_slice(self, ty: Ty) -> F::Result {
self.build(Ty::Slice(TySlice { ty: Box::new(ty) }))
self.build(Ty::Slice(TySlice {
ty: Box::new(ty),
bracket_token: Default::default(),
}))
}

pub fn slice(self) -> TyBuilder<TySliceBuilder<F>> {
Expand All @@ -132,11 +135,15 @@ impl<F> TyBuilder<F>
}

pub fn never(self) -> F::Result {
self.build(Ty::Never(TyNever {}))
self.build(Ty::Never(TyNever {
bang_token: Default::default(),
}))
}

pub fn infer(self) -> F::Result {
self.build(Ty::Infer(TyInfer {}))
self.build(Ty::Infer(TyInfer {
underscore_token: Default::default()
}))
}

pub fn option(self) -> TyBuilder<TyOptionBuilder<F>> {
Expand Down Expand Up @@ -221,7 +228,7 @@ impl<F> TyRefBuilder<F>
where F: Invoke<Ty>
{
pub fn mut_(mut self) -> Self {
self.mutability = Mutability::Mutable;
self.mutability = Mutability::Mutable(Default::default());
self
}

Expand All @@ -240,6 +247,7 @@ impl<F> TyRefBuilder<F>
self.builder.build(Ty::Rptr(TyRptr {
lifetime: self.lifetime,
ty: Box::new(ty),
and_token: Default::default(),
}))
}

Expand Down Expand Up @@ -414,7 +422,9 @@ impl<F> TyImplTraitTyBuilder<F>
}

pub fn with_generics(self, generics: Generics) -> Self {
self.with_lifetimes(generics.lifetimes.into_iter().map(|def| def.lifetime))
self.with_lifetimes(generics.lifetimes.iter().map(|def| {
Lifetime { ident: def.item().lifetime.ident.clone() }
}))
}

pub fn with_lifetimes<I, L>(mut self, lifetimes: I) -> Self
Expand All @@ -437,7 +447,8 @@ impl<F> TyImplTraitTyBuilder<F>
pub fn build(self) -> F::Result {
let bounds = self.bounds;
self.builder.build(Ty::ImplTrait(TyImplTrait {
bounds: bounds,
bounds: bounds.into(),
impl_token: Default::default(),
}))
}
}
Expand Down Expand Up @@ -479,7 +490,11 @@ impl<F> TyTupleBuilder<F>
}

pub fn build(self) -> F::Result {
self.builder.build(Ty::Tup(TyTup { tys: self.tys }))
self.builder.build(Ty::Tup(TyTup {
tys: self.tys.into(),
paren_token: Default::default(),
lone_comma: None,
}))
}
}

Expand Down
Loading