1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#![cfg_attr(not(feature = "with-syntex"), feature(rustc_private))]
#![cfg_attr(feature = "unstable-testing", feature(plugin))]
#![cfg_attr(feature = "unstable-testing", plugin(clippy))]
#![cfg_attr(feature = "unstable-testing", allow(wrong_self_convention))]

#[cfg(feature = "with-syntex")]
extern crate syntex_syntax as syntax;

#[cfg(not(feature = "with-syntex"))]
extern crate syntax;

use syntax::ast;
use syntax::codemap::{DUMMY_SP, Span};
use syntax::parse::token;

pub mod arm;
pub mod attr;
pub mod block;
pub mod constant;
pub mod expr;
pub mod fn_decl;
pub mod generics;
pub mod ident;
pub mod invoke;
pub mod item;
pub mod lifetime;
pub mod lit;
pub mod mac;
pub mod method;
pub mod name;
pub mod pat;
pub mod path;
pub mod qpath;
pub mod self_;
pub mod stmt;
pub mod str;
pub mod struct_field;
pub mod ty;
pub mod ty_param;
pub mod variant;
pub mod variant_data;
pub mod where_predicate;

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

#[derive(Copy, Clone)]
pub struct AstBuilder {
    span: Span,
}

impl AstBuilder {
    pub fn new() -> AstBuilder {
        AstBuilder {
            span: DUMMY_SP,
        }
    }

    pub fn span(mut self, span: Span) -> Self {
        self.span = span;
        self
    }

    pub fn interned_string<S>(&self, s: S) -> token::InternedString
        where S: str::ToInternedString
    {
        s.to_interned_string()
    }

    pub fn id<I>(&self, id: I) -> ast::Ident
        where I: ident::ToIdent
    {
        id.to_ident()
    }

    pub fn name<N>(&self, name: N) -> ast::Name
        where N: name::ToName
    {
        name.to_name()
    }

    pub fn lifetime<L>(&self, lifetime: L) -> ast::Lifetime
        where L: lifetime::IntoLifetime
    {
        lifetime.into_lifetime()
    }

    pub fn arm(&self) -> arm::ArmBuilder {
        arm::ArmBuilder::new().span(self.span)
    }

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

    pub fn mac(&self) -> mac::MacBuilder {
        mac::MacBuilder::new().span(self.span)
    }

    pub fn path(&self) -> path::PathBuilder {
        path::PathBuilder::new().span(self.span)
    }

    pub fn qpath(&self) -> qpath::QPathBuilder {
        qpath::QPathBuilder::new().span(self.span)
    }

    pub fn ty(&self) -> ty::TyBuilder {
        ty::TyBuilder::new().span(self.span)
    }

    pub fn lifetime_def<N>(&self, name: N) -> lifetime::LifetimeDefBuilder
        where N: name::ToName,
    {
        lifetime::LifetimeDefBuilder::new(name)
    }

    pub fn ty_param<I>(&self, id: I) -> ty_param::TyParamBuilder
        where I: ident::ToIdent,
    {
        ty_param::TyParamBuilder::new(id).span(self.span)
    }

    pub fn ty_param_bound(&self) -> ty_param::TyParamBoundBuilder {
        ty_param::TyParamBoundBuilder::new().span(self.span)
    }

    pub fn from_ty_param(&self, ty_param: ast::TyParam) -> ty_param::TyParamBuilder {
        ty_param::TyParamBuilder::from_ty_param(ty_param)
    }

    pub fn generics(&self) -> generics::GenericsBuilder {
        generics::GenericsBuilder::new().span(self.span)
    }

    pub fn where_predicate(&self) -> where_predicate::WherePredicateBuilder {
        where_predicate::WherePredicateBuilder::new().span(self.span)
    }

    pub fn from_generics(&self, generics: ast::Generics) -> generics::GenericsBuilder {
        generics::GenericsBuilder::from_generics(generics).span(self.span)
    }

    pub fn lit(&self) -> lit::LitBuilder {
        lit::LitBuilder::new().span(self.span)
    }

    pub fn expr(&self) -> expr::ExprBuilder {
        expr::ExprBuilder::new().span(self.span)
    }

    pub fn stmt(&self) -> stmt::StmtBuilder {
        stmt::StmtBuilder::new().span(self.span)
    }

    pub fn block(&self) -> block::BlockBuilder {
        block::BlockBuilder::new().span(self.span)
    }

    pub fn pat(&self) -> pat::PatBuilder {
        pat::PatBuilder::new().span(self.span)
    }

    pub fn fn_decl(&self) -> fn_decl::FnDeclBuilder {
        fn_decl::FnDeclBuilder::new().span(self.span)
    }

    pub fn method_sig(&self) -> method::MethodSigBuilder {
        method::MethodSigBuilder::new().span(self.span)
    }

    pub fn self_(&self) -> self_::SelfBuilder {
        self_::SelfBuilder::new().span(self.span)
    }

    pub fn arg(&self) -> fn_decl::ArgBuilder {
        fn_decl::ArgBuilder::new().span(self.span)
    }

    pub fn variant_data(&self) -> variant_data::VariantDataBuilder {
        variant_data::VariantDataBuilder::new().span(self.span)
    }

    pub fn variant<T>(&self, id: T) -> variant::VariantBuilder
        where T: ident::ToIdent,
    {
        variant::VariantBuilder::new(id).span(self.span)
    }

    pub fn struct_field<T>(&self, id: T) -> struct_field::StructFieldBuilder
        where T: ident::ToIdent,
    {
        struct_field::StructFieldBuilder::named(id).span(self.span)
    }

    pub fn tuple_field(&self) -> struct_field::StructFieldBuilder {
        struct_field::StructFieldBuilder::unnamed().span(self.span)
    }

    pub fn item(&self) -> item::ItemBuilder {
        item::ItemBuilder::new().span(self.span)
    }

    pub fn trait_item<T>(&self, id: T) -> item::ItemTraitItemBuilder
        where T: ident::ToIdent,
    {
        item::ItemTraitItemBuilder::new(id).span(self.span)
    }

    pub fn impl_item<T>(&self, id: T) -> item::ItemImplItemBuilder
        where T: ident::ToIdent,
    {
        item::ItemImplItemBuilder::new(id).span(self.span)
    }

    pub fn const_(&self) -> constant::ConstBuilder {
        constant::ConstBuilder::new().span(self.span)
    }
}

impl Default for AstBuilder {
    fn default() -> Self {
        AstBuilder::new()
    }
}