Skip to content

Commit

Permalink
Merge #31
Browse files Browse the repository at this point in the history
31: Improve the minification function r=qryxip a=qryxip

bors r+


Co-authored-by: Ryo Yamashita <qryxip@gmail.com>
  • Loading branch information
bors[bot] and qryxip authored Oct 22, 2020
2 parents fea8e21 + 174b2ff commit 530cb59
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 37 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## [Unreleased]

### Added

- Improved the minification function.

## [0.5.1] - 2020-10-17Z

### Added
Expand Down
7 changes: 3 additions & 4 deletions README-ja.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,11 @@ fn main() {
cargo equip --remove docs test-items --minify libs --rustfmt --check -o ./bundled.rs
Bundling the code
warning: found `crate` paths. replacing them with `crate::__aclrs`
warning: could not minify the code. inserting spaces: `crate::__aclrs`
Checking cargo-equip-check-output-ea0pb2d6u0yda27w v0.1.0 (/tmp/cargo-equip-check-output-ea0pb2d6u0yda27w)
Finished dev [unoptimized + debuginfo] target(s) in 0.32s
Checking cargo-equip-check-output-miy9hfcb3nxljsw6 v0.1.0 (/tmp/cargo-equip-check-output-miy9hfcb3nxljsw6)
Finished dev [unoptimized + debuginfo] target(s) in 0.33s
```

[Submit Info #27189 - Library-Checker](https://judge.yosupo.jp/submission/27189)
[Submit Info #27728 - Library-Checker](https://judge.yosupo.jp/submission/27728)

## インストール

Expand Down
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,11 @@ fn main() {
cargo equip --remove docs test-items --minify libs --rustfmt --check -o ./bundled.rs
Bundling the code
warning: found `crate` paths. replacing them with `crate::__aclrs`
warning: could not minify the code. inserting spaces: `crate::__aclrs`
Checking cargo-equip-check-output-ea0pb2d6u0yda27w v0.1.0 (/tmp/cargo-equip-check-output-ea0pb2d6u0yda27w)
Finished dev [unoptimized + debuginfo] target(s) in 0.32s
Checking cargo-equip-check-output-miy9hfcb3nxljsw6 v0.1.0 (/tmp/cargo-equip-check-output-miy9hfcb3nxljsw6)
Finished dev [unoptimized + debuginfo] target(s) in 0.33s
```

[Submit Info #27189 - Library-Checker](https://judge.yosupo.jp/submission/27189)
[Submit Info #27728 - Library-Checker](https://judge.yosupo.jp/submission/27728)

## Installation

Expand Down
99 changes: 70 additions & 29 deletions src/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use fixedbitset::FixedBitSet;
use if_chain::if_chain;
use itertools::Itertools as _;
use maplit::{btreemap, btreeset, hashset};
use proc_macro2::{LineColumn, Span, TokenStream, TokenTree};
use proc_macro2::{LineColumn, Spacing, Span, TokenStream, TokenTree};
use quote::{quote, ToTokens as _};
use std::{
collections::{BTreeMap, BTreeSet, HashSet},
Expand Down Expand Up @@ -887,12 +887,20 @@ fn set_span(mask: &mut [FixedBitSet], span: Span, p: bool) {

pub(crate) fn minify(code: &str, shell: &mut Shell, name: Option<&str>) -> anyhow::Result<String> {
fn minify(acc: &mut String, token_stream: TokenStream) {
let mut space_on_ident = false;
let mut space_on_punct = false;
let mut space_on_literal = false;
#[derive(PartialEq)]
enum Prev {
None,
IdentOrLit,
Puncts(String, Spacing),
}

let mut prev = Prev::None;
for tt in token_stream {
match tt {
proc_macro2::TokenTree::Group(group) => {
TokenTree::Group(group) => {
if let Prev::Puncts(puncts, _) = mem::replace(&mut prev, Prev::None) {
*acc += &puncts;
}
let (left, right) = match group.delimiter() {
proc_macro2::Delimiter::Parenthesis => ('(', ')'),
proc_macro2::Delimiter::Brace => ('{', '}'),
Expand All @@ -902,39 +910,72 @@ pub(crate) fn minify(code: &str, shell: &mut Shell, name: Option<&str>) -> anyho
acc.push(left);
minify(acc, group.stream());
acc.push(right);
space_on_ident = false;
space_on_punct = false;
space_on_literal = false;
prev = Prev::None;
}
proc_macro2::TokenTree::Ident(ident) => {
if space_on_ident {
*acc += " ";
TokenTree::Ident(ident) => {
match mem::replace(&mut prev, Prev::IdentOrLit) {
Prev::IdentOrLit => *acc += " ",
Prev::Puncts(puncts, _) => *acc += &puncts,
_ => {}
}
*acc += &ident.to_string();
space_on_ident = true;
space_on_punct = false;
space_on_literal = true;
}
proc_macro2::TokenTree::Punct(punct) => {
if space_on_punct {
*acc += " ";
TokenTree::Literal(literal) => {
match mem::replace(&mut prev, Prev::IdentOrLit) {
Prev::IdentOrLit => *acc += " ",
Prev::Puncts(puncts, _) => *acc += &puncts,
_ => {}
}
acc.push(punct.as_char());
space_on_ident = false;
space_on_punct = punct.spacing() == proc_macro2::Spacing::Alone;
space_on_literal = false;
*acc += &literal.to_string();
}
proc_macro2::TokenTree::Literal(literal) => {
if space_on_literal {
*acc += " ";
TokenTree::Punct(punct) => {
if let Prev::Puncts(puncts, spacing) = &mut prev {
if *spacing == Spacing::Alone {
*acc += puncts;
if [
("&", '&'),
("|", '|'),
("<", '<'),
(">", '>'),
("+", '='),
("-", '='),
("*", '='),
("/", '='),
("%", '='),
("^", '='),
("&", '='),
("|", '='),
("<<", '='),
(">>", '='),
("=", '='),
("!", '='),
(">", '='),
("<", '='),
(".", '.'),
("..", '.'),
("..", '='),
(":", ':'),
("-", '>'),
("=", '>'),
]
.contains(&(&&*puncts, punct.as_char()))
{
*acc += " ";
}
prev = Prev::Puncts(punct.as_char().to_string(), punct.spacing());
} else {
puncts.push(punct.as_char());
*spacing = punct.spacing();
}
} else {
prev = Prev::Puncts(punct.as_char().to_string(), punct.spacing());
}
*acc += &literal.to_string();
space_on_ident = false;
space_on_punct = false;
space_on_literal = true;
}
}
}
if let Prev::Puncts(puncts, _) = prev {
*acc += &puncts;
}
}

let token_stream = syn::parse_file(code)
Expand All @@ -947,7 +988,7 @@ pub(crate) fn minify(code: &str, shell: &mut Shell, name: Option<&str>) -> anyho
let mut acc = "".to_owned();
minify(&mut acc, token_stream);

if matches!(syn::parse_file(&acc), Ok(f) if f.to_token_stream().to_string() == safe) {
if syn::parse_file(&acc).is_ok() {
Ok(acc)
} else {
shell.warn(format!(
Expand Down

0 comments on commit 530cb59

Please sign in to comment.