Skip to content

Commit

Permalink
Merge pull request #11 from dtolnay/hack
Browse files Browse the repository at this point in the history
Rewrite using proc-macro-hack
  • Loading branch information
dtolnay committed Sep 17, 2017
2 parents e9443dd + 4d3bc1b commit fb4764d
Show file tree
Hide file tree
Showing 29 changed files with 293 additions and 166 deletions.
3 changes: 0 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,3 @@ rust:
script:
- cargo build --verbose
- cargo test --verbose
- cargo build --features with-syntex --verbose
- cargo build --features clippy --verbose
- cargo build --features 'clippy with-syntex' --verbose
22 changes: 7 additions & 15 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,27 +1,19 @@
[package]
name = "indoc"
version = "0.1.15"
version = "0.2.0"
authors = ["David Tolnay <dtolnay@gmail.com>"]
license = "MIT/Apache-2.0"
description = "Indented document literals"
repository = "https://github.com/dtolnay/indoc"
documentation = "https://github.com/dtolnay/indoc"
keywords = ["heredoc", "nowdoc", "multiline", "string", "literal"]
include = ["Cargo.toml", "src/**/*.rs"]

[lib]
name = "indoc"
plugin = true

[features]
default = []
with-syntex = ["syntex", "syntex_syntax"]

[dependencies]
clippy = { version = "^0.*", optional = true }
syntex = { version = "0.51.0", optional = true }
syntex_syntax = { version = "0.51.0", optional = true }
unindent = "0.1"
indoc-impl = { version = "0.2", path = "impl" }
proc-macro-hack = "0.3"

[dev-dependencies]
compiletest_rs = "0.2.4"
compiletest_rs = "0.3"

[workspace]
members = ["impl", "unindent"]
18 changes: 18 additions & 0 deletions impl/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[package]
name = "indoc-impl"
version = "0.2.0"
authors = ["David Tolnay <dtolnay@gmail.com>"]
license = "MIT/Apache-2.0"
description = "Indented document literals"
repository = "https://github.com/dtolnay/indoc"
documentation = "https://github.com/dtolnay/indoc"
keywords = ["heredoc", "nowdoc", "multiline", "string", "literal"]

[lib]
proc-macro = true

[dependencies]
proc-macro-hack = "0.3"
quote = "0.3"
syn = "0.11"
unindent = { version = "0.1", path = "../unindent" }
55 changes: 55 additions & 0 deletions impl/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright 2016 Indoc Developers
//
// 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.

#[macro_use]
extern crate proc_macro_hack;

extern crate syn;

#[macro_use]
extern crate quote;

extern crate unindent;
use unindent::*;

use syn::{TokenTree, Token, Lit};

proc_macro_expr_impl! {
pub fn indoc_impl(input: &str) -> String {
let source = input.to_string();

let tts = syn::parse_token_trees(&source).unwrap();

if tts.len() != 1 {
panic!("argument must be a single string literal, but got {} arguments", tts.len());
}

let tt = tts.into_iter().next().unwrap();

let mut lit = match tt {
TokenTree::Token(Token::Literal(lit)) => lit,
_ => {
panic!("argument must be a single string literal");
}
};

match lit {
Lit::Str(ref mut s, _style) => {
*s = unindent(s);
}
Lit::ByteStr(ref mut v, _style) => {
*v = unindent_bytes(v);
}
_ => {
panic!("argument must be a single string literal");
}
}

quote!(#lit).parse().unwrap()
}
}
104 changes: 9 additions & 95 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,103 +6,17 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

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

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

#[cfg(not(feature = "with-syntex"))]
extern crate rustc_plugin;
#[cfg(not(feature = "with-syntex"))]
extern crate syntax;

#[cfg(feature = "with-syntex")]
extern crate syntex;
#[cfg(feature = "with-syntex")]
extern crate syntex_syntax as syntax;

extern crate unindent;
use unindent::*;

#[cfg(feature = "with-syntex")]
use std::path::Path;

use syntax::codemap::Span;
use syntax::parse;
use syntax::parse::token::{Lit, Literal};
use syntax::ast::{LitKind, StrStyle};
use syntax::ext::base::{DummyResult, ExtCtxt, MacEager, MacResult};
use syntax::ext::build::AstBuilder; // trait for expr_lit
use syntax::symbol::Symbol;
use syntax::tokenstream::TokenTree;

#[cfg(not(feature = "with-syntex"))]
#[plugin_registrar]
#[doc(hidden)]
pub fn register(reg: &mut rustc_plugin::Registry) {
reg.register_macro("indoc", expand_indoc);
}

#[cfg(feature = "with-syntex")]
#[allow(unused_imports)]
#[macro_use]
extern crate indoc_impl;
#[doc(hidden)]
pub fn register(reg: &mut syntex::Registry) {
reg.add_macro("indoc", expand_indoc);
}

#[cfg(feature = "with-syntex")]
#[doc(hidden)]
pub fn expand<S, D>(src: S, dst: D) -> Result<(), syntex::Error>
where S: AsRef<Path>,
D: AsRef<Path>,
{
let mut registry = syntex::Registry::new();
register(&mut registry);
registry.expand("", src.as_ref(), dst.as_ref())
}

fn expand_indoc<'a>(
cx: &'a mut ExtCtxt,
sp: Span,
args: &[TokenTree]
) -> Box<MacResult + 'a> {
if args.len() != 1 {
cx.span_err(sp,
&format!("argument must be a single string literal, but \
got {} arguments",
args.len()));
return DummyResult::any(sp);
}

let lit = match args[0] {
TokenTree::Token(_, Literal(lit, _name)) => lit,
_ => {
cx.span_err(sp, "argument must be a single string literal");
return DummyResult::any(sp);
}
};

let result = match lit {
Lit::Str_(name) => {
let unindented = parse::str_lit(&unindent(&name.as_str()));
let interned = Symbol::intern(&unindented);
LitKind::Str(interned, StrStyle::Cooked)
}
Lit::StrRaw(name, hashes) => {
let unindented = parse::raw_str_lit(&unindent(&name.as_str()));
let interned = Symbol::intern(&unindented);
LitKind::Str(interned, StrStyle::Raw(hashes))
}
Lit::ByteStr(name) |
Lit::ByteStrRaw(name, _) => {
let unindented = parse::byte_str_lit(&unindent(&name.as_str()));
LitKind::ByteStr(unindented)
}
_ => {
cx.span_err(sp, "argument must be a single string literal");
return DummyResult::any(sp);
}
};
pub use indoc_impl::*;

MacEager::expr(cx.expr_lit(sp, result))
proc_macro_expr_decl! {
indoc! => indoc_impl
}
4 changes: 2 additions & 2 deletions tests/run-pass/byte-string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(plugin)]
#![plugin(indoc)]
#[macro_use]
extern crate indoc;

fn main() {
let indoc = indoc!(b"
Expand Down
4 changes: 2 additions & 2 deletions tests/run-pass/carriage-return.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(plugin)]
#![plugin(indoc)]
#[macro_use]
extern crate indoc;

fn main() {
// Every line in the string ends with \r\n
Expand Down
4 changes: 2 additions & 2 deletions tests/run-pass/empty-string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(plugin)]
#![plugin(indoc)]
#[macro_use]
extern crate indoc;

fn main() {
let indoc = indoc!("");
Expand Down
4 changes: 2 additions & 2 deletions tests/run-pass/joined-first-line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(plugin)]
#![plugin(indoc)]
#[macro_use]
extern crate indoc;

fn main() {
let indoc = indoc!("\
Expand Down
4 changes: 2 additions & 2 deletions tests/run-pass/joined-lines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(plugin)]
#![plugin(indoc)]
#[macro_use]
extern crate indoc;

fn main() {
let indoc = indoc!("
Expand Down
4 changes: 2 additions & 2 deletions tests/run-pass/no-leading-newline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(plugin)]
#![plugin(indoc)]
#[macro_use]
extern crate indoc;

fn main() {
let indoc = indoc!("a
Expand Down
4 changes: 2 additions & 2 deletions tests/run-pass/one-line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(plugin)]
#![plugin(indoc)]
#[macro_use]
extern crate indoc;

fn main() {
let indoc = indoc!("a");
Expand Down
6 changes: 3 additions & 3 deletions tests/run-pass/raw-byte-string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(plugin)]
#![plugin(indoc)]
#[macro_use]
extern crate indoc;

fn main() {
let indoc = indoc!(br#"
"a"
\\b
c"#);
let expected = b"\"a\"\n\n \\b\nc";
let expected = b"\"a\"\n\n \\\\b\nc";
assert_eq!(indoc, expected);
}
4 changes: 2 additions & 2 deletions tests/run-pass/raw-string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(plugin)]
#![plugin(indoc)]
#[macro_use]
extern crate indoc;

fn main() {
let indoc = indoc!(r#"
Expand Down
4 changes: 2 additions & 2 deletions tests/run-pass/string-trailing-newline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(plugin)]
#![plugin(indoc)]
#[macro_use]
extern crate indoc;

fn main() {
let indoc = indoc!("
Expand Down
4 changes: 2 additions & 2 deletions tests/run-pass/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(plugin)]
#![plugin(indoc)]
#[macro_use]
extern crate indoc;

fn main() {
let indoc = indoc!("
Expand Down
4 changes: 2 additions & 2 deletions tests/run-pass/trailing-whitespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(plugin)]
#![plugin(indoc)]
#[macro_use]
extern crate indoc;

fn main() {
let indoc = indoc!("
Expand Down

0 comments on commit fb4764d

Please sign in to comment.