2017-08-28 04:56:43 -05:00
|
|
|
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
// no-prefer-dynamic
|
2018-07-03 17:36:31 -05:00
|
|
|
|
2017-08-28 04:56:43 -05:00
|
|
|
#![crate_type = "proc-macro"]
|
2018-10-01 12:58:21 -05:00
|
|
|
#![feature(proc_macro_diagnostic, proc_macro_span, proc_macro_def_site)]
|
2017-08-28 04:56:43 -05:00
|
|
|
|
|
|
|
extern crate proc_macro;
|
|
|
|
|
2018-04-02 10:19:32 -05:00
|
|
|
use proc_macro::{TokenStream, TokenTree, Span, Diagnostic};
|
2017-08-28 04:56:43 -05:00
|
|
|
|
|
|
|
fn parse(input: TokenStream) -> Result<(), Diagnostic> {
|
|
|
|
let mut count = 0;
|
2017-11-14 23:44:43 -06:00
|
|
|
let mut last_span = Span::def_site();
|
2017-08-28 04:56:43 -05:00
|
|
|
for tree in input {
|
2018-04-02 10:19:32 -05:00
|
|
|
let span = tree.span();
|
2017-08-28 04:56:43 -05:00
|
|
|
if count >= 3 {
|
|
|
|
return Err(span.error(format!("expected EOF, found `{}`.", tree))
|
|
|
|
.span_note(last_span, "last good input was here")
|
|
|
|
.help("input must be: `===`"))
|
|
|
|
}
|
|
|
|
|
2018-05-13 10:46:38 -05:00
|
|
|
if let TokenTree::Punct(ref tt) = tree {
|
2018-05-05 13:09:41 -05:00
|
|
|
if tt.as_char() == '=' {
|
2018-04-02 10:19:32 -05:00
|
|
|
count += 1;
|
|
|
|
last_span = span;
|
|
|
|
continue
|
|
|
|
}
|
2017-08-28 04:56:43 -05:00
|
|
|
}
|
2018-04-02 10:19:32 -05:00
|
|
|
return Err(span.error(format!("expected `=`, found `{}`.", tree)));
|
2017-08-28 04:56:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if count < 3 {
|
2017-11-14 23:44:43 -06:00
|
|
|
return Err(Span::def_site()
|
2017-08-28 04:56:43 -05:00
|
|
|
.error(format!("found {} equal signs, need exactly 3", count))
|
|
|
|
.help("input must be: `===`"))
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[proc_macro]
|
|
|
|
pub fn three_equals(input: TokenStream) -> TokenStream {
|
|
|
|
if let Err(diag) = parse(input) {
|
|
|
|
diag.emit();
|
2018-05-25 21:44:07 -05:00
|
|
|
return TokenStream::new();
|
2017-08-28 04:56:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
"3".parse().unwrap()
|
|
|
|
}
|