Skip to content

Commit 1e32a3f

Browse files
committed
Test compound tokens.
1 parent 20a9048 commit 1e32a3f

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
13+
#![feature(proc_macro)]
14+
#![crate_type = "proc-macro"]
15+
16+
extern crate proc_macro;
17+
18+
use proc_macro::{TokenStream, TokenKind, OpKind, Literal, quote};
19+
20+
#[proc_macro]
21+
pub fn count_compound_ops(input: TokenStream) -> TokenStream {
22+
assert_eq!(count_compound_ops_helper(quote!(++ (&&) 4@a)), 3);
23+
TokenKind::Literal(Literal::u32(count_compound_ops_helper(input))).into()
24+
}
25+
26+
fn count_compound_ops_helper(input: TokenStream) -> u32 {
27+
let mut count = 0;
28+
for token in input {
29+
match token.kind {
30+
TokenKind::Op(c, OpKind::Alone) => count += 1,
31+
TokenKind::Sequence(_, tokens) => count += count_compound_ops_helper(tokens),
32+
_ => {}
33+
}
34+
}
35+
count
36+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
// aux-build:count_compound_ops.rs
12+
13+
#![feature(proc_macro)]
14+
15+
extern crate count_compound_ops;
16+
use count_compound_ops::count_compound_ops;
17+
18+
fn main() {
19+
assert_eq!(count_compound_ops!(foo<=>bar <<<! -baz ++), 4);
20+
}

0 commit comments

Comments
 (0)