beating 'grep -E' on some things

This commit is contained in:
Andrew Gallant
2016-03-10 20:48:44 -05:00
parent 9d1e619ff3
commit 403bb72a4d
3 changed files with 117 additions and 18 deletions

55
src/nonl.rs Normal file
View File

@@ -0,0 +1,55 @@
use syntax::Expr;
use Result;
/// Returns a new expression that is guaranteed to never match `\n`.
///
/// If the expression contains a literal `\n`, then an error is returned.
pub fn remove(expr: Expr) -> Result<Expr> {
use syntax::Expr::*;
Ok(match expr {
Literal { chars, casei } => {
if chars.iter().position(|&c| c == '\n').is_some() {
return Err(format!("Literal '\\n' are not allowed.").into());
}
Literal { chars: chars, casei: casei }
}
LiteralBytes { bytes, casei } => {
if bytes.iter().position(|&b| b == b'\n').is_some() {
return Err(format!("Literal '\\n' are not allowed.").into());
}
LiteralBytes { bytes: bytes, casei: casei }
}
AnyChar => AnyCharNoNL,
AnyByte => AnyByteNoNL,
Class(mut cls) => {
cls.remove('\n');
Class(cls)
}
ClassBytes(mut cls) => {
cls.remove(b'\n');
ClassBytes(cls)
}
Group { e, i, name } => {
Group {
e: Box::new(try!(remove(*e))),
i: i,
name: name,
}
}
Repeat { e, r, greedy } => {
Repeat {
e: Box::new(try!(remove(*e))),
r: r,
greedy: greedy,
}
}
Concat(exprs) => {
Concat(try!(exprs.into_iter().map(remove).collect()))
}
Alternate(exprs) => {
Alternate(try!(exprs.into_iter().map(remove).collect()))
}
e => e,
})
}