/// A builder for constructing a printer. /// /// Note that since a printer doesn't have any configuration knobs, this type /// remains unexported. #[derive(Clone, Debug)] struct PrinterBuilder {
_priv: (),
}
/// A printer for a regular expression abstract syntax tree. /// /// A printer converts an abstract syntax tree (AST) to a regular expression /// pattern string. This particular printer uses constant stack space and heap /// space proportional to the size of the AST. /// /// This printer will not necessarily preserve the original formatting of the /// regular expression pattern string. For example, all whitespace and comments /// are ignored. #[derive(Debug)] pubstruct Printer {
_priv: (),
}
impl Printer { /// Create a new printer. pubfn new() -> Printer {
PrinterBuilder::new().build()
}
/// Print the given `Ast` to the given writer. The writer must implement /// `fmt::Write`. Typical implementations of `fmt::Write` that can be used /// here are a `fmt::Formatter` (which is available in `fmt::Display` /// implementations) or a `&mut String`. pubfn print<W: fmt::Write>(&mutself, ast: &Ast, wtr: W) -> fmt::Result {
visitor::visit(ast, Writer { wtr })
}
}
#[derive(Debug)] struct Writer<W> {
wtr: W,
}
impl<W: fmt::Write> Visitor for Writer<W> { type Output = (); type Err = fmt::Error;
fn fmt_class_perl(&mutself, ast: &ast::ClassPerl) -> fmt::Result { usecrate::ast::ClassPerlKind::*; match ast.kind {
Digit if ast.negated => self.wtr.write_str(r"\D"),
Digit => self.wtr.write_str(r"\d"),
Space if ast.negated => self.wtr.write_str(r"\S"),
Space => self.wtr.write_str(r"\s"),
Word if ast.negated => self.wtr.write_str(r"\W"),
Word => self.wtr.write_str(r"\w"),
}
}
fn fmt_class_ascii(&mutself, ast: &ast::ClassAscii) -> fmt::Result { usecrate::ast::ClassAsciiKind::*; match ast.kind {
Alnum if ast.negated => self.wtr.write_str("[:^alnum:]"),
Alnum => self.wtr.write_str("[:alnum:]"),
Alpha if ast.negated => self.wtr.write_str("[:^alpha:]"),
Alpha => self.wtr.write_str("[:alpha:]"),
Ascii if ast.negated => self.wtr.write_str("[:^ascii:]"),
Ascii => self.wtr.write_str("[:ascii:]"),
Blank if ast.negated => self.wtr.write_str("[:^blank:]"),
Blank => self.wtr.write_str("[:blank:]"),
Cntrl if ast.negated => self.wtr.write_str("[:^cntrl:]"),
Cntrl => self.wtr.write_str("[:cntrl:]"),
Digit if ast.negated => self.wtr.write_str("[:^digit:]"),
Digit => self.wtr.write_str("[:digit:]"),
Graph if ast.negated => self.wtr.write_str("[:^graph:]"),
Graph => self.wtr.write_str("[:graph:]"),
Lower if ast.negated => self.wtr.write_str("[:^lower:]"),
Lower => self.wtr.write_str("[:lower:]"),
Print if ast.negated => self.wtr.write_str("[:^print:]"),
Print => self.wtr.write_str("[:print:]"),
Punct if ast.negated => self.wtr.write_str("[:^punct:]"),
Punct => self.wtr.write_str("[:punct:]"),
Space if ast.negated => self.wtr.write_str("[:^space:]"),
Space => self.wtr.write_str("[:space:]"),
Upper if ast.negated => self.wtr.write_str("[:^upper:]"),
Upper => self.wtr.write_str("[:upper:]"),
Word if ast.negated => self.wtr.write_str("[:^word:]"),
Word => self.wtr.write_str("[:word:]"),
Xdigit if ast.negated => self.wtr.write_str("[:^xdigit:]"),
Xdigit => self.wtr.write_str("[:xdigit:]"),
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.