easy-plugin 0.6.2

A compiler plugin that makes it easier to write compiler plugins.
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
// Copyright 2016 Kyle Mayes
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::cell::{RefCell};
use std::marker::{PhantomData};

use syntax::ext::tt::transcribe;
use syntax::parse::token;
use syntax::ast::*;
use syntax::codemap::{DUMMY_SP, Span, Spanned};
use syntax::errors::{FatalError, Level, RenderSpan};
use syntax::errors::emitter::{CoreEmitter};
use syntax::ext::base::{ExtCtxt};
use syntax::ext::build::{AstBuilder};
use syntax::parse::{ParseSess, PResult};
use syntax::parse::lexer::{Reader, TokenAndSpan};
use syntax::parse::parser::{Parser, PathStyle};
use syntax::parse::token::{BinOpToken, DelimToken, Token};
use syntax::ptr::{P};

use super::{PluginResult};

//================================================
// Macros
//================================================

// parse! _______________________________________

/// Defines a parsing method for `TransactionParser` that parses a particular AST entity.
macro_rules! parse {
    ($name:ident($($argument:expr), *)$(.$method:ident())*, $description:expr, $ty:ty) => {
        pub fn $name(&mut self, name: &str) -> PluginResult<$ty> {
            self.parse_expected($description, name, |p| p.$name($($argument), *))
        }
    };

    (OPTION: $name:ident($($argument:expr), *)$(.$method:ident())*, $description:expr, $ty:ty) => {
        pub fn $name(&mut self, name: &str) -> PluginResult<$ty> {
            let span = self.get_span();
            match self.apply(|p| p.$name($($argument), *)) {
                Ok(Some(value)) => return Ok(value),
                Err(mut db) => db.cancel(),
                _ => { },
            }
            span.to_error(format!("expected {}: '{}'", $description, name))
        }
    };
}

// to_error! _____________________________________

/// Defines a `ToError` implementation for the supplied type.
macro_rules! to_error {
    ($ty:ty) => (
        impl<T, S: Into<String>> ToError<T, S> for $ty {
            fn to_error(&self, message: S) -> PluginResult<T> {
                Err((self.span, message.into()))
            }
        }
    );
}

// token! ________________________________________

/// Prefixes a list of identifiers with `syntax`, `parse`, and `token`.
macro_rules! token {
    ($($ident:expr), +) => (&["syntax", "parse", "token", $($ident), *]);
}

//================================================
// Traits
//================================================

// PluginResultExt _______________________________

/// Extends `PluginResult<T>`.
pub trait PluginResultExt<T> {
    /// Returns this `PluginResult<T>` with a different span if it is an `Err`.
    fn map_err_span(self, span: Span) -> PluginResult<T>;

    /// Returns this `PluginResult<T>` with a different message if it is an `Err`.
    fn map_err_message<S: Into<String>>(self, message: S) -> PluginResult<T>;
}

impl<T> PluginResultExt<T> for PluginResult<T> {
    fn map_err_span(self, span: Span) -> PluginResult<T> {
        self.map_err(|(_, m)| (span, m))
    }

    fn map_err_message<S: Into<String>>(self, message: S) -> PluginResult<T> {
        self.map_err(|(s, _)| (s, message.into()))
    }
}

// ToError _______________________________________

/// A type that can be extended into a `PluginResult<T>`.
pub trait ToError<T, S> where S: Into<String> {
    /// Returns an `Err` value with the span of this value and the supplied message.
    fn to_error(&self, message: S) -> PluginResult<T>;
}

impl<T, S: Into<String>> ToError<T, S> for Span {
    fn to_error(&self, message: S) -> PluginResult<T> {
        Err((*self, message.into()))
    }
}

impl<T, S: Into<String>> ToError<T, S> for TokenTree {
    fn to_error(&self, message: S) -> PluginResult<T> {
        Err((self.get_span(), message.into()))
    }
}

impl<T, U, S:Into<String>> ToError<T, S> for Spanned<U> {
    fn to_error(&self, message: S) -> PluginResult<T> {
        Err((self.span, message.into()))
    }
}

to_error!(Block);
to_error!(Expr);
to_error!(Item);
to_error!(Pat);
to_error!(Path);
to_error!(Ty);

// ToExpr ________________________________________

/// A type whose values can be generated by evaluating exprs.
pub trait ToExpr {
    /// Returns an expr which would produce this value if evaluated.
    fn to_expr(&self, context: &mut ExtCtxt, span: Span) -> P<Expr>;
}

impl ToExpr for BinOpToken {
    fn to_expr(&self, context: &mut ExtCtxt, span: Span) -> P<Expr> {
        mk_expr_path(context, span, token!["BinOpToken", &format!("{:?}", self)])
    }
}

impl ToExpr for DelimToken {
    fn to_expr(&self, context: &mut ExtCtxt, span: Span) -> P<Expr> {
        mk_expr_path(context, span, token!["DelimToken", &format!("{:?}", self)])
    }
}

impl ToExpr for Ident {
    fn to_expr(&self, context: &mut ExtCtxt, span: Span) -> P<Expr> {
        let arguments = vec![context.expr_str(span, self.name.as_str())];
        mk_expr_call(context, span, token!["str_to_ident"], arguments)
    }
}

impl ToExpr for token::Lit {
    fn to_expr(&self, context: &mut ExtCtxt, span: Span) -> P<Expr> {
        macro_rules! expr {
            ($variant:expr, $name:expr) => ({
                let arguments = vec![$name.to_expr(context, span)];
                mk_expr_call(context, span, token!["Lit", $variant], arguments)
            });

            ($variant:expr, $name:expr, $size:expr) => ({
                let arguments = vec![$name.to_expr(context, span), context.expr_usize(span, $size)];
                mk_expr_call(context, span, token!["Lit", $variant], arguments)
            });
        }

        match *self {
            token::Lit::Byte(name) => expr!("Byte", name),
            token::Lit::Char(name) => expr!("Char", name),
            token::Lit::Integer(name) => expr!("Integer", name),
            token::Lit::Float(name) => expr!("Float", name),
            token::Lit::Str_(name) => expr!("Str_", name),
            token::Lit::StrRaw(name, size) => expr!("StrRaw", name, size),
            token::Lit::ByteStr(name) => expr!("ByteStr", name),
            token::Lit::ByteStrRaw(name, size) => expr!("ByteStrRaw", name, size),
        }
    }
}

impl ToExpr for Name {
    fn to_expr(&self, context: &mut ExtCtxt, span: Span) -> P<Expr> {
        mk_expr_call(context, span, token!["intern"], vec![context.expr_str(span, self.as_str())])
    }
}

impl ToExpr for String {
    fn to_expr(&self, context: &mut ExtCtxt, span: Span) -> P<Expr> {
        let name = context.expr_str(span, context.name_of(self).as_str());
        let into = context.ident_of("into");
        context.expr_method_call(span, name, into, vec![])
    }
}

impl ToExpr for Token {
    fn to_expr(&self, context: &mut ExtCtxt, span: Span) -> P<Expr> {
        macro_rules! expr {
            ($variant:expr, $($argument:expr), *) => ({
                let arguments = vec![$($argument.to_expr(context, span)), *];
                mk_expr_call(context, span, token!["Token", $variant], arguments)
            });
        }

        match *self {
            Token::BinOp(binop) => expr!("BinOp", binop),
            Token::BinOpEq(binop) => expr!("BinOpEq", binop),
            Token::Literal(lit, suffix) => expr!("Literal", lit, suffix),
            Token::Ident(ref ident) => expr!("Ident", ident),
            Token::Lifetime(ref lifetime) => expr!("Lifetime", lifetime),
            Token::DocComment(comment) => expr!("DocComment", comment),
            Token::OpenDelim(_) |
            Token::CloseDelim(_) |
            Token::Shebang(_) |
            Token::Interpolated(_) |
            Token::MatchNt(_, _) |
            Token::SubstNt(_) |
            Token::SpecialVarNt(_) => unreachable!(),
            _ => mk_expr_path(context, span, token!["Token", &format!("{:?}", self)]),
        }
    }
}

impl<T> ToExpr for Option<T> where T: ToExpr {
    fn to_expr(&self, context: &mut ExtCtxt, span: Span) -> P<Expr> {
        match *self {
            Some(ref some) => {
                let some = some.to_expr(context, span);
                context.expr_some(span, some)
            },
            None => context.expr_none(span),
        }
    }
}

impl<T> ToExpr for Vec<T> where T: ToExpr {
    fn to_expr(&self, context: &mut ExtCtxt, span: Span) -> P<Expr> {
        let exprs = self.iter().map(|i| i.to_expr(context, span)).collect();
        let slice = context.expr_vec_slice(span, exprs);
        context.expr_method_call(span, slice, context.ident_of("to_vec"), vec![])
    }
}

impl<T> ToExpr for [T] where T: ToExpr {
    fn to_expr(&self, context: &mut ExtCtxt, span: Span) -> P<Expr> {
        let exprs = self.iter().map(|i| i.to_expr(context, span)).collect();
        context.expr_vec_slice(span, exprs)
    }
}

//================================================
// Structs
//================================================

// SaveEmitter ___________________________________

/// The most recent fatal error, if any.
thread_local! { static ERROR: RefCell<Option<(Span, String)>> = RefCell::default() }

/// A diagnostic emitter that saves fatal errors to a thread local variable.
pub struct SaveEmitter;

impl CoreEmitter for SaveEmitter {
    fn emit_message(
        &mut self, span: &RenderSpan, message: &str, _: Option<&str>, level: Level, _: bool, _: bool
    ) -> () {
        if level == Level::Fatal {
            if let RenderSpan::FullSpan(ref ms) = *span {
                let span = ms.primary_span().unwrap_or(DUMMY_SP);
                ERROR.with(|e| *e.borrow_mut() = Some((span, message.into())));
            }
        }
    }
}

// TokenReader ___________________________________

/// A token reader which wraps a `Vec<TokenAndSpan>`.
#[derive(Clone)]
struct TokenReader<'s> {
    session: &'s ParseSess,
    tokens: Vec<TokenAndSpan>,
    index: usize,
}

impl<'s> TokenReader<'s> {
    //- Constructors -----------------------------

    fn new(session: &'s ParseSess, tokens: Vec<TokenAndSpan>) -> TokenReader<'s> {
        TokenReader { session: session, tokens: tokens, index: 0 }
    }
}

impl<'s> Reader for TokenReader<'s> {
    fn is_eof(&self) -> bool {
        self.index + 1 == self.tokens.len()
    }

    fn try_next_token(&mut self) -> Result<TokenAndSpan, ()> {
        Ok(self.next_token())
    }

    fn fatal(&self, message: &str) -> FatalError {
        self.session.span_diagnostic.span_fatal(self.peek().sp, message)
    }

    fn err(&self, message: &str) {
        self.session.span_diagnostic.span_err(self.peek().sp, message);
    }

    fn emit_fatal_errors(&mut self) { }

    fn peek(&self) -> TokenAndSpan {
        self.tokens[self.index].clone()
    }

    fn next_token(&mut self) -> TokenAndSpan {
        let next = self.tokens[self.index].clone();
        if !self.is_eof() {
            self.index += 1;
        }
        next
    }
}

// TransactionParser _____________________________

/// A wrapper around a `Parser` which allows for rolling back parsing actions.
pub struct TransactionParser<'s> {
    session: &'s ParseSess,
    tokens: Vec<TokenAndSpan>,
    start: usize,
    position: usize,
}

impl<'s> TransactionParser<'s> {
    //- Constructors -----------------------------

    pub fn new(session: &'s ParseSess, tts: &[TokenTree]) -> TransactionParser<'s> {
        let mut parser = TransactionParser {
            session: session, tokens: vec![], start: 0, position: 0
        };

        // Generate `TokenAndSpan`s from the supplied `TokenTree`s.
        let handler = &session.span_diagnostic;
        let mut reader = transcribe::new_tt_reader(handler, None, None, tts.into());
        while !reader.is_eof() {
            parser.tokens.push(reader.next_token());
        }
        parser.tokens.push(reader.next_token());

        parser
    }

    //- Accessors --------------------------------

    /// Returns the span of current token.
    pub fn get_span(&self) -> Span {
        if self.position == self.tokens.len() {
            self.tokens.get(self.tokens.len().saturating_sub(1)).expect("expected span").sp
        } else {
            self.tokens.get(self.position).expect("expected span").sp
        }
    }

    /// Returns the span of the last token processed.
    pub fn get_last_span(&self) -> Span {
        if self.position == self.tokens.len() {
            self.tokens.get(self.tokens.len().saturating_sub(1)).expect("expected span").sp
        } else {
            self.tokens.get(self.position.saturating_sub(1)).expect("expected span").sp
        }
    }

    /// Returns whether this parser has successfully processed all of its tokens.
    pub fn is_empty(&self) -> bool {
        self.position == self.tokens.len() - 1
    }

    //- Mutators ---------------------------------

    /// Sets the saved position to the current position.
    pub fn save(&mut self) {
        self.start = self.position;
    }

    /// Sets the position to the saved position.
    pub fn rollback(&mut self) {
        self.position = self.start;
    }

    /// Applies an action to this parser, returning the result of the action.
    fn apply<T, F: FnOnce(&mut Parser<'s>) -> T>(&mut self, f: F) -> T {
        // Construct a temporary `Parser` that reads from the unprocessed `TokenAndSpan`s.
        let reader = Box::new(TokenReader::new(self.session, self.tokens[self.position..].into()));
        let mut parser = Parser::new(self.session, vec![], reader);

        // Apply the action, incrementing the position by how many `TokenAndSpan`s were read.
        let result = f(&mut parser);
        self.position += parser.tokens_consumed;
        result
    }

    pub fn bump_and_get(&mut self) -> Token {
        self.apply(|p| p.bump_and_get())
    }

    pub fn eat(&mut self, token: &Token) -> bool {
        self.apply(|p| p.eat(token))
    }

    /// Applies a parsing action to this parser, returning the result of the action.
    ///
    /// If the parsing action fails, the reported error is the last fatal parsing error.
    pub fn parse<T, F: FnOnce(&mut Parser<'s>) -> PResult<'s, T>>(
        &mut self, f: F
    ) -> PluginResult<T> {
        self.apply(f).map_err(|mut db| {
            db.cancel();
            ERROR.with(|e| e.borrow().clone().unwrap_or_else(|| (DUMMY_SP, "no error".into())))
        })
    }

    /// Applies a parsing action to this parser, returning the result of the action.
    ///
    /// If the parsing action fails, the reported error describes what kind of AST entity was
    /// expected.
    fn parse_expected<T, F: FnOnce(&mut Parser<'s>) -> PResult<'s, T>>(
        &mut self, description: &str, name: &str, f: F
    ) -> PluginResult<T> {
        let span = self.get_span();
        self.apply(f).map_err(|mut db| {
            db.cancel();
            (span, format!("expected {}: '{}'", description, name))
        })
    }

    parse!(parse_attribute(true), "attribute", Attribute);
    parse!(parse_block(), "block", P<Block>);
    parse!(parse_expr(), "expression", P<Expr>);
    parse!(parse_ident(), "identifier", Ident);
    parse!(OPTION: parse_item(), "item", P<Item>);
    parse!(parse_lifetime(), "lifetime", Lifetime);
    parse!(parse_lit(), "literal", Lit);
    parse!(parse_meta_item(), "meta item", P<MetaItem>);
    parse!(parse_pat(), "pattern", P<Pat>);
    parse!(parse_path(PathStyle::Type), "path", Path);
    parse!(OPTION: parse_stmt(), "statement", Stmt);
    parse!(parse_ty(), "type", P<Ty>);
    parse!(parse_token_tree(), "token tree", TokenTree);
}

// TtsIterator ___________________________________

/// A token tree iterator which returns an error when the output does not match expectations.
pub struct TtsIterator<'i, I> where I: Iterator<Item=&'i TokenTree> {
    pub error: (Span, String),
    pub iterator: I,
    _marker: PhantomData<&'i ()>,
}

impl<'i, I> TtsIterator<'i, I> where I: Iterator<Item=&'i TokenTree> {
    //- Constructors -----------------------------

    pub fn new(iterator: I, span: Span, message: &str) -> TtsIterator<'i, I> {
        TtsIterator { error: (span, message.into()), iterator: iterator, _marker: PhantomData }
    }

    //- Mutators ---------------------------------

    pub fn expect(&mut self) -> PluginResult<&'i TokenTree> {
        self.iterator.next().ok_or_else(|| self.error.clone())
    }

    pub fn expect_token(&mut self, description: &str) -> PluginResult<(Span, Token)> {
        self.expect().and_then(|tt| {
            match *tt {
                TokenTree::Token(span, ref token) => Ok((span, token.clone())),
                _ => tt.to_error(format!("expected {}", description)),
            }
        })
    }

    pub fn expect_specific_token(&mut self, token: Token) -> PluginResult<()> {
        let description = Parser::token_to_string(&token);
        self.expect_token(&description).and_then(|(s, t)| {
            if t.mtwt_eq(&token) {
                Ok(())
            } else {
                s.to_error(format!("expected {}", description))
            }
        })
    }
}

impl<'i, I> Iterator for TtsIterator<'i, I> where I: Iterator<Item=&'i TokenTree> {
    type Item = &'i TokenTree;

    fn next(&mut self) -> Option<&'i TokenTree> {
        self.iterator.next()
    }
}

//================================================
// Functions
//================================================

pub fn mk_path(context: &ExtCtxt, idents: &[&str]) -> Vec<Ident> {
    idents.iter().map(|i| context.ident_of(i)).collect()
}

pub fn mk_expr_call(context: &ExtCtxt, span: Span, idents: &[&str], args: Vec<P<Expr>>) -> P<Expr> {
    context.expr_call_global(span, mk_path(context, idents), args)
}

pub fn mk_expr_path(context: &ExtCtxt, span: Span, idents: &[&str]) -> P<Expr> {
    context.expr_path(context.path_global(span, mk_path(context, idents)))
}