]>
Commit | Line | Data |
---|---|---|
ea8adc8c XL |
1 | // Copyright 2017 The Rust Project Developers. See the COPYRIGHT |
2 | // file at the top-level directory of this distribution and at | |
3 | // http://rust-lang.org/COPYRIGHT. | |
4 | // | |
5 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | |
6 | // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | |
7 | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | |
8 | // option. This file may not be copied, modified, or distributed | |
9 | // except according to those terms. | |
10 | ||
11 | // no-prefer-dynamic | |
12 | #![feature(proc_macro)] | |
13 | #![crate_type = "proc-macro"] | |
14 | ||
15 | extern crate proc_macro; | |
16 | ||
17 | use proc_macro::{TokenStream, TokenNode, Span, Diagnostic}; | |
18 | ||
19 | fn parse(input: TokenStream) -> Result<(), Diagnostic> { | |
20 | let mut count = 0; | |
abe05a73 | 21 | let mut last_span = Span::def_site(); |
ea8adc8c XL |
22 | for tree in input { |
23 | let span = tree.span; | |
24 | if count >= 3 { | |
25 | return Err(span.error(format!("expected EOF, found `{}`.", tree)) | |
26 | .span_note(last_span, "last good input was here") | |
27 | .help("input must be: `===`")) | |
28 | } | |
29 | ||
30 | if let TokenNode::Op('=', _) = tree.kind { | |
31 | count += 1; | |
32 | } else { | |
33 | return Err(span.error(format!("expected `=`, found `{}`.", tree))); | |
34 | } | |
35 | ||
36 | last_span = span; | |
37 | } | |
38 | ||
39 | if count < 3 { | |
abe05a73 | 40 | return Err(Span::def_site() |
ea8adc8c XL |
41 | .error(format!("found {} equal signs, need exactly 3", count)) |
42 | .help("input must be: `===`")) | |
43 | } | |
44 | ||
45 | Ok(()) | |
46 | } | |
47 | ||
48 | #[proc_macro] | |
49 | pub fn three_equals(input: TokenStream) -> TokenStream { | |
50 | if let Err(diag) = parse(input) { | |
51 | diag.emit(); | |
52 | return TokenStream::empty(); | |
53 | } | |
54 | ||
55 | "3".parse().unwrap() | |
56 | } |