Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
XOSplicer committed Jul 29, 2017
1 parent 8c3e99d commit 985c60b
Show file tree
Hide file tree
Showing 9 changed files with 488 additions and 362 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "comment-strip"
version = "0.1.2"
version = "0.1.3"
authors = ["Felix Stegmaier <stegmaier.felix@gmail.com>"]
license = "MIT"
description = "Remove comments out of text files"
Expand All @@ -15,3 +15,4 @@ travis-ci = { repository = "XOSplicer/comment-strip", branch = "master" }

[dependencies]
clap = { version = "2.25.0", features = ["yaml"] }
quick-error = "1.2.0"
69 changes: 40 additions & 29 deletions src/blanklines/mod.rs → src/blanklines.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::CommentMatch;
use super::{CommentMatch, Start, End, find_comments_impl};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum ParseState {
Expand All @@ -13,6 +13,18 @@ enum ParseState {
End
}

impl Start for ParseState {
fn start() -> Self {
ParseState::Start
}
}

impl End for ParseState {
fn end() -> Self {
ParseState::End
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum ParseAction {
Nothing,
Expand Down Expand Up @@ -77,38 +89,37 @@ enum MultiBlanklineState {
InMultiBlankline(usize)
}

pub fn find_blanklines(input: &str) -> Result<Vec<CommentMatch>, &'static str> {
let mut matches = Vec::new();
let mut current_state = ParseState::Start;
let mut blankline_state = MultiBlanklineState::NotInMultiBlankline;
let mut chars = input.chars();
let mut position = 0;
while current_state != ParseState::End {
let current_char = chars.next();
// println!("parsing state {:?} with input '{:?}'", &current_state, &current_char );
let (next_state, action) = state_transition(current_state, current_char);
match action {
ParseAction::Nothing => {},
ParseAction::MultiBlanklineStart => {
blankline_state = MultiBlanklineState::InMultiBlankline(position);
},
ParseAction::MultiBlanklineEnd => {
match blankline_state {
MultiBlanklineState::NotInMultiBlankline => {
return Err(" blankline parser error");
},
MultiBlanklineState::InMultiBlankline(from) => {
matches.push(CommentMatch{from: from, to: position});
blankline_state = MultiBlanklineState::NotInMultiBlankline;
}
impl Start for MultiBlanklineState {
fn start() -> Self {
MultiBlanklineState::NotInMultiBlankline
}
}

fn do_action(action: ParseAction, mut blankline_state: MultiBlanklineState,
position: usize, mut matches: Vec<CommentMatch>)
-> Result<(MultiBlanklineState, Vec<CommentMatch>), &'static str> {
match action {
ParseAction::Nothing => {},
ParseAction::MultiBlanklineStart => {
blankline_state = MultiBlanklineState::InMultiBlankline(position);
},
ParseAction::MultiBlanklineEnd => {
match blankline_state {
MultiBlanklineState::NotInMultiBlankline => {
return Err(" blankline parser error");
},
MultiBlanklineState::InMultiBlankline(from) => {
matches.push(CommentMatch{from: from, to: position});
blankline_state = MultiBlanklineState::NotInMultiBlankline;
}
}
}
// println!("action {:?}", &action);
current_state = next_state;
position += 1;
}
Ok(matches)
Ok((blankline_state, matches))
}

pub fn find_blanklines(input: &str) -> Result<Vec<CommentMatch>, &'static str> {
find_comments_impl(input, state_transition, do_action)
}

#[cfg(test)]
Expand Down
115 changes: 63 additions & 52 deletions src/c/mod.rs → src/c.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::CommentMatch;
use super::{CommentMatch, Start, End, find_comments_impl};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum ParseState {
Expand All @@ -16,6 +16,18 @@ enum ParseState {
End
}

impl Start for ParseState {
fn start() -> Self {
ParseState::Start
}
}

impl End for ParseState {
fn end() -> Self {
ParseState::End
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum ParseAction {
Nothing,
Expand Down Expand Up @@ -106,64 +118,63 @@ enum CommentState {
InComment(usize)
}

pub fn find_comments(input: &str) -> Result<Vec<CommentMatch>, &'static str> {
let mut matches = Vec::new();
let mut current_state = ParseState::Start;
let mut comment_state = CommentState::NotInComment;
let mut chars = input.chars();
let mut position = 0;
while current_state != ParseState::End {
let current_char = chars.next();
// println!("parsing state {:?} with input '{:?}'", &current_state, &current_char );
let (next_state, action) = state_transition(current_state, current_char);
match action {
ParseAction::Nothing => {},
ParseAction::CommentMightStart => {
comment_state = CommentState::MaybeInComment(position);
},
ParseAction::CommentConfirmed => {
match comment_state {
CommentState::MaybeInComment(from) => {
comment_state = CommentState::InComment(from);
},
_ => {
// println!("{:?}", (&comment_state, &current_state, &next_state, &current_char, &position));
return Err("c style parser error");
}
impl Start for CommentState {
fn start() -> Self {
CommentState::NotInComment
}
}

fn do_action(action: ParseAction, mut comment_state: CommentState,
position: usize, mut matches: Vec<CommentMatch>)
-> Result<(CommentState, Vec<CommentMatch>), &'static str> {
match action {
ParseAction::Nothing => {},
ParseAction::CommentMightStart => {
comment_state = CommentState::MaybeInComment(position);
},
ParseAction::CommentConfirmed => {
match comment_state {
CommentState::MaybeInComment(from) => {
comment_state = CommentState::InComment(from);
},
_ => {
// println!("{:?}", (&comment_state, &current_state, &next_state, &current_char, &position));
return Err("c style parser error");
}
},
ParseAction::CommentDismissed => {
comment_state = CommentState::NotInComment
},
ParseAction::CommentEnds => {
match comment_state {
CommentState::InComment(from) => {
matches.push(CommentMatch{from: from, to: position});
comment_state = CommentState::NotInComment;
},
_ => {
return Err("c style parser error");
}

}
},
ParseAction::CommentDismissed => {
comment_state = CommentState::NotInComment
},
ParseAction::CommentEnds => {
match comment_state {
CommentState::InComment(from) => {
matches.push(CommentMatch{from: from, to: position});
comment_state = CommentState::NotInComment;
},
_ => {
return Err("c style parser error");
}
},
ParseAction::CommentEndsAndCommentMightStart => {
match comment_state {
CommentState::InComment(from) => {
matches.push(CommentMatch{from: from, to: position});
comment_state = CommentState::MaybeInComment(position);
},
_ => {
return Err("c style parser error");
}
}
},
ParseAction::CommentEndsAndCommentMightStart => {
match comment_state {
CommentState::InComment(from) => {
matches.push(CommentMatch{from: from, to: position});
comment_state = CommentState::MaybeInComment(position);
},
_ => {
return Err("c style parser error");
}
}
}
// println!("action {:?}", &action);
current_state = next_state;
position += 1;
}
Ok(matches)
Ok((comment_state, matches))
}

pub fn find_comments(input: &str) -> Result<Vec<CommentMatch>, &'static str> {
find_comments_impl(input, state_transition, do_action)
}

#[cfg(test)]
Expand Down
2 changes: 1 addition & 1 deletion src/cli.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: comment-strip
version: "0.1.2"
version: "0.1.3"
author: Felix Stegmaier <stegmaier.felix@gmail.com>
about: Strip comments away, default style is Shell comment style
args:
Expand Down
100 changes: 100 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
extern crate clap;

use std::fs;
use std::io;
use std::path::Path;
use self::clap::ArgMatches;
use super::CommentStyle;

#[derive(Debug)]
pub enum Input {
Standard(io::Stdin),
File(fs::File)
}

impl Input {
fn stdin() -> Input {
Input::Standard(io::stdin())
}
fn file<P: AsRef<Path>>(path: P) -> io::Result<Input> {
Ok(Input::File(try!(fs::File::open(path))))
}
fn from_arg<P: AsRef<Path>>(arg: Option<P>) -> io::Result<Input> {
Ok(match arg {
None => Input::stdin(),
Some(path) => try!(Input::file(path))
})
}
}

impl io::Read for Input {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
match *self {
Input::Standard(ref mut s) => s.read(buf),
Input::File(ref mut f) => f.read(buf),
}
}
}

#[derive(Debug)]
pub enum Output {
Standard(io::Stdout),
File(fs::File)
}

impl Output {
fn stdout() -> Output {
Output::Standard(io::stdout())
}
fn file<P: AsRef<Path>>(path: P) -> io::Result<Output> {
Ok(Output::File(try!(fs::File::create(path))))
}
fn from_arg<P: AsRef<Path>>(arg: Option<P>) -> io::Result<Output> {
Ok(match arg {
None => Output::stdout(),
Some(path) => try!(Output::file(path))
})
}
}

impl io::Write for Output {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
match *self {
Output::Standard(ref mut s) => s.write(buf),
Output::File(ref mut f) => f.write(buf),
}
}
fn flush(&mut self) -> io::Result<()> {
match *self {
Output::Standard(ref mut s) => s.flush(),
Output::File(ref mut f) => f.flush(),
}
}
}

pub struct Config {
pub input: Input,
pub output: Output,
pub style: CommentStyle,
pub remove_blanks: bool
}

impl Config {
pub fn from_matches(matches: &ArgMatches) -> io::Result<Self> {
let style_arg = (matches.is_present("c-style"),
matches.is_present("xml-style"),
matches.is_present("shell-style"));
let comment_style = match style_arg {
(true, _, _) => CommentStyle::C,
(_, true, _) => CommentStyle::XML,
(_, _, true) => CommentStyle::Shell,
_ => CommentStyle::Shell
};
Ok(Config {
input: Input::from_arg(matches.value_of("INPUT"))?,
output: Output::from_arg(matches.value_of("output"))?,
style: comment_style,
remove_blanks: !matches.is_present("no-remove-blank-lines")
})
}
}
Loading

0 comments on commit 985c60b

Please sign in to comment.