Skip to content

Commit bb083b0

Browse files
committed
Rewrite using proc-macro-hack
1 parent e9443dd commit bb083b0

28 files changed

+292
-165
lines changed

.travis.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,3 @@ rust:
88
script:
99
- cargo build --verbose
1010
- cargo test --verbose
11-
- cargo build --features with-syntex --verbose
12-
- cargo build --features clippy --verbose
13-
- cargo build --features 'clippy with-syntex' --verbose

Cargo.toml

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,19 @@
11
[package]
22
name = "indoc"
3-
version = "0.1.15"
3+
version = "0.2.0"
44
authors = ["David Tolnay <[email protected]>"]
55
license = "MIT/Apache-2.0"
66
description = "Indented document literals"
77
repository = "https://github.com/dtolnay/indoc"
88
documentation = "https://github.com/dtolnay/indoc"
99
keywords = ["heredoc", "nowdoc", "multiline", "string", "literal"]
10-
include = ["Cargo.toml", "src/**/*.rs"]
11-
12-
[lib]
13-
name = "indoc"
14-
plugin = true
15-
16-
[features]
17-
default = []
18-
with-syntex = ["syntex", "syntex_syntax"]
1910

2011
[dependencies]
21-
clippy = { version = "^0.*", optional = true }
22-
syntex = { version = "0.51.0", optional = true }
23-
syntex_syntax = { version = "0.51.0", optional = true }
24-
unindent = "0.1"
12+
indoc-impl = { version = "0.2", path = "impl" }
13+
proc-macro-hack = "0.3"
2514

2615
[dev-dependencies]
27-
compiletest_rs = "0.2.4"
16+
compiletest_rs = "0.3"
17+
18+
[workspace]
19+
members = ["impl", "unindent"]

impl/Cargo.toml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[package]
2+
name = "indoc-impl"
3+
version = "0.2.0"
4+
authors = ["David Tolnay <[email protected]>"]
5+
license = "MIT/Apache-2.0"
6+
description = "Indented document literals"
7+
repository = "https://github.com/dtolnay/indoc"
8+
documentation = "https://github.com/dtolnay/indoc"
9+
keywords = ["heredoc", "nowdoc", "multiline", "string", "literal"]
10+
11+
[lib]
12+
proc-macro = true
13+
14+
[dependencies]
15+
proc-macro-hack = "0.3"
16+
quote = "0.3"
17+
syn = "0.11"
18+
unindent = { version = "0.1", path = "../unindent" }

impl/src/lib.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright 2016 Indoc Developers
2+
//
3+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6+
// option. This file may not be copied, modified, or distributed
7+
// except according to those terms.
8+
9+
#[macro_use]
10+
extern crate proc_macro_hack;
11+
12+
extern crate syn;
13+
14+
#[macro_use]
15+
extern crate quote;
16+
17+
extern crate unindent;
18+
use unindent::*;
19+
20+
use syn::{TokenTree, Token, Lit};
21+
22+
proc_macro_expr_impl! {
23+
pub fn indoc_impl(input: &str) -> String {
24+
let source = input.to_string();
25+
26+
let tts = syn::parse_token_trees(&source).unwrap();
27+
28+
if tts.len() != 1 {
29+
panic!("argument must be a single string literal, but got {} arguments", tts.len());
30+
}
31+
32+
let tt = tts.into_iter().next().unwrap();
33+
34+
let mut lit = match tt {
35+
TokenTree::Token(Token::Literal(lit)) => lit,
36+
_ => {
37+
panic!("argument must be a single string literal");
38+
}
39+
};
40+
41+
match lit {
42+
Lit::Str(ref mut s, _style) => {
43+
*s = unindent(s);
44+
}
45+
Lit::ByteStr(ref mut v, _style) => {
46+
*v = unindent_bytes(v);
47+
}
48+
_ => {
49+
panic!("argument must be a single string literal");
50+
}
51+
}
52+
53+
quote!(#lit).parse().unwrap()
54+
}
55+
}

src/lib.rs

Lines changed: 9 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -6,103 +6,17 @@
66
// option. This file may not be copied, modified, or distributed
77
// except according to those terms.
88

9-
#![cfg_attr(feature="clippy", feature(plugin))]
10-
#![cfg_attr(feature="clippy", plugin(clippy))]
11-
#![cfg_attr(feature="clippy", deny(clippy))] // turn warnings into errors
9+
#![cfg_attr(feature = "cargo-clippy", allow(useless_attribute))]
1210

13-
#![cfg_attr(not(feature = "with-syntex"), feature(plugin_registrar, rustc_private))]
11+
#[macro_use]
12+
extern crate proc_macro_hack;
1413

15-
#[cfg(not(feature = "with-syntex"))]
16-
extern crate rustc_plugin;
17-
#[cfg(not(feature = "with-syntex"))]
18-
extern crate syntax;
19-
20-
#[cfg(feature = "with-syntex")]
21-
extern crate syntex;
22-
#[cfg(feature = "with-syntex")]
23-
extern crate syntex_syntax as syntax;
24-
25-
extern crate unindent;
26-
use unindent::*;
27-
28-
#[cfg(feature = "with-syntex")]
29-
use std::path::Path;
30-
31-
use syntax::codemap::Span;
32-
use syntax::parse;
33-
use syntax::parse::token::{Lit, Literal};
34-
use syntax::ast::{LitKind, StrStyle};
35-
use syntax::ext::base::{DummyResult, ExtCtxt, MacEager, MacResult};
36-
use syntax::ext::build::AstBuilder; // trait for expr_lit
37-
use syntax::symbol::Symbol;
38-
use syntax::tokenstream::TokenTree;
39-
40-
#[cfg(not(feature = "with-syntex"))]
41-
#[plugin_registrar]
42-
#[doc(hidden)]
43-
pub fn register(reg: &mut rustc_plugin::Registry) {
44-
reg.register_macro("indoc", expand_indoc);
45-
}
46-
47-
#[cfg(feature = "with-syntex")]
14+
#[allow(unused_imports)]
15+
#[macro_use]
16+
extern crate indoc_impl;
4817
#[doc(hidden)]
49-
pub fn register(reg: &mut syntex::Registry) {
50-
reg.add_macro("indoc", expand_indoc);
51-
}
52-
53-
#[cfg(feature = "with-syntex")]
54-
#[doc(hidden)]
55-
pub fn expand<S, D>(src: S, dst: D) -> Result<(), syntex::Error>
56-
where S: AsRef<Path>,
57-
D: AsRef<Path>,
58-
{
59-
let mut registry = syntex::Registry::new();
60-
register(&mut registry);
61-
registry.expand("", src.as_ref(), dst.as_ref())
62-
}
63-
64-
fn expand_indoc<'a>(
65-
cx: &'a mut ExtCtxt,
66-
sp: Span,
67-
args: &[TokenTree]
68-
) -> Box<MacResult + 'a> {
69-
if args.len() != 1 {
70-
cx.span_err(sp,
71-
&format!("argument must be a single string literal, but \
72-
got {} arguments",
73-
args.len()));
74-
return DummyResult::any(sp);
75-
}
76-
77-
let lit = match args[0] {
78-
TokenTree::Token(_, Literal(lit, _name)) => lit,
79-
_ => {
80-
cx.span_err(sp, "argument must be a single string literal");
81-
return DummyResult::any(sp);
82-
}
83-
};
84-
85-
let result = match lit {
86-
Lit::Str_(name) => {
87-
let unindented = parse::str_lit(&unindent(&name.as_str()));
88-
let interned = Symbol::intern(&unindented);
89-
LitKind::Str(interned, StrStyle::Cooked)
90-
}
91-
Lit::StrRaw(name, hashes) => {
92-
let unindented = parse::raw_str_lit(&unindent(&name.as_str()));
93-
let interned = Symbol::intern(&unindented);
94-
LitKind::Str(interned, StrStyle::Raw(hashes))
95-
}
96-
Lit::ByteStr(name) |
97-
Lit::ByteStrRaw(name, _) => {
98-
let unindented = parse::byte_str_lit(&unindent(&name.as_str()));
99-
LitKind::ByteStr(unindented)
100-
}
101-
_ => {
102-
cx.span_err(sp, "argument must be a single string literal");
103-
return DummyResult::any(sp);
104-
}
105-
};
18+
pub use indoc_impl::*;
10619

107-
MacEager::expr(cx.expr_lit(sp, result))
20+
proc_macro_expr_decl! {
21+
indoc! => indoc_impl
10822
}

tests/run-pass/byte-string.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
// option. This file may not be copied, modified, or distributed
77
// except according to those terms.
88

9-
#![feature(plugin)]
10-
#![plugin(indoc)]
9+
#[macro_use]
10+
extern crate indoc;
1111

1212
fn main() {
1313
let indoc = indoc!(b"

tests/run-pass/carriage-return.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
// option. This file may not be copied, modified, or distributed
77
// except according to those terms.
88

9-
#![feature(plugin)]
10-
#![plugin(indoc)]
9+
#[macro_use]
10+
extern crate indoc;
1111

1212
fn main() {
1313
// Every line in the string ends with \r\n

tests/run-pass/empty-string.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
// option. This file may not be copied, modified, or distributed
77
// except according to those terms.
88

9-
#![feature(plugin)]
10-
#![plugin(indoc)]
9+
#[macro_use]
10+
extern crate indoc;
1111

1212
fn main() {
1313
let indoc = indoc!("");

tests/run-pass/joined-first-line.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
// option. This file may not be copied, modified, or distributed
77
// except according to those terms.
88

9-
#![feature(plugin)]
10-
#![plugin(indoc)]
9+
#[macro_use]
10+
extern crate indoc;
1111

1212
fn main() {
1313
let indoc = indoc!("\

tests/run-pass/joined-lines.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
// option. This file may not be copied, modified, or distributed
77
// except according to those terms.
88

9-
#![feature(plugin)]
10-
#![plugin(indoc)]
9+
#[macro_use]
10+
extern crate indoc;
1111

1212
fn main() {
1313
let indoc = indoc!("

tests/run-pass/no-leading-newline.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
// option. This file may not be copied, modified, or distributed
77
// except according to those terms.
88

9-
#![feature(plugin)]
10-
#![plugin(indoc)]
9+
#[macro_use]
10+
extern crate indoc;
1111

1212
fn main() {
1313
let indoc = indoc!("a

tests/run-pass/one-line.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
// option. This file may not be copied, modified, or distributed
77
// except according to those terms.
88

9-
#![feature(plugin)]
10-
#![plugin(indoc)]
9+
#[macro_use]
10+
extern crate indoc;
1111

1212
fn main() {
1313
let indoc = indoc!("a");

tests/run-pass/raw-byte-string.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66
// option. This file may not be copied, modified, or distributed
77
// except according to those terms.
88

9-
#![feature(plugin)]
10-
#![plugin(indoc)]
9+
#[macro_use]
10+
extern crate indoc;
1111

1212
fn main() {
1313
let indoc = indoc!(br#"
1414
"a"
1515
1616
\\b
1717
c"#);
18-
let expected = b"\"a\"\n\n \\b\nc";
18+
let expected = b"\"a\"\n\n \\\\b\nc";
1919
assert_eq!(indoc, expected);
2020
}

tests/run-pass/raw-string.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
// option. This file may not be copied, modified, or distributed
77
// except according to those terms.
88

9-
#![feature(plugin)]
10-
#![plugin(indoc)]
9+
#[macro_use]
10+
extern crate indoc;
1111

1212
fn main() {
1313
let indoc = indoc!(r#"

tests/run-pass/string-trailing-newline.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
// option. This file may not be copied, modified, or distributed
77
// except according to those terms.
88

9-
#![feature(plugin)]
10-
#![plugin(indoc)]
9+
#[macro_use]
10+
extern crate indoc;
1111

1212
fn main() {
1313
let indoc = indoc!("

tests/run-pass/string.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
// option. This file may not be copied, modified, or distributed
77
// except according to those terms.
88

9-
#![feature(plugin)]
10-
#![plugin(indoc)]
9+
#[macro_use]
10+
extern crate indoc;
1111

1212
fn main() {
1313
let indoc = indoc!("

tests/run-pass/trailing-whitespace.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
// option. This file may not be copied, modified, or distributed
77
// except according to those terms.
88

9-
#![feature(plugin)]
10-
#![plugin(indoc)]
9+
#[macro_use]
10+
extern crate indoc;
1111

1212
fn main() {
1313
let indoc = indoc!("

0 commit comments

Comments
 (0)