Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add HIR printer #458

Merged
merged 3 commits into from
Mar 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions regex-syntax/src/ast/print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
// except according to those terms.

/*!
This module provides a regular expression printer.
This module provides a regular expression printer for `Ast`.
*/

use std::fmt;
Expand Down Expand Up @@ -422,7 +422,7 @@ mod tests {
}

fn roundtrip_with<F>(mut f: F, given: &str)
where F: FnMut(&mut ParserBuilder) -> &mut ParserBuilder
where F: FnMut(&mut ParserBuilder) -> &mut ParserBuilder
{
let mut builder = ParserBuilder::new();
f(&mut builder);
Expand All @@ -434,11 +434,6 @@ mod tests {
assert_eq!(given, dst);
}

#[test]
fn scratch() {
roundtrip(".");
}

#[test]
fn print_literal() {
roundtrip("a");
Expand Down
23 changes: 19 additions & 4 deletions regex-syntax/src/hir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub use hir::visitor::{Visitor, visit};

mod interval;
pub mod literal;
pub mod print;
pub mod translate;
mod visitor;

Expand Down Expand Up @@ -152,6 +153,10 @@ impl fmt::Display for ErrorKind {
/// and can be computed cheaply during the construction process. For
/// example, one such attribute is whether the expression must match at the
/// beginning of the text.
///
/// Also, an `Hir`'s `fmt::Display` implementation prints an HIR as a regular
/// expression pattern string, and uses constant stack space and heap space
/// proportional to the size of the `Hir`.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Hir {
/// The underlying HIR kind.
Expand Down Expand Up @@ -302,10 +307,7 @@ impl Hir {
// A negated word boundary matches the empty string, but a normal
// word boundary does not!
info.set_match_empty(word_boundary.is_negated());
// ASCII word boundaries can match invalid UTF-8.
if let WordBoundary::Ascii = word_boundary {
info.set_always_utf8(false);
}
// Negated ASCII word boundaries can match invalid UTF-8.
if let WordBoundary::AsciiNegate = word_boundary {
info.set_always_utf8(false);
}
Expand Down Expand Up @@ -605,6 +607,19 @@ impl HirKind {
}
}

/// Print a display representation of this Hir.
///
/// The result of this is a valid regular expression pattern string.
///
/// This implementation uses constant stack space and heap space proportional
/// to the size of the `Hir`.
impl fmt::Display for Hir {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use hir::print::Printer;
Printer::new().print(self, f)
}
}

/// The high-level intermediate representation of a literal.
///
/// A literal corresponds to a single character, where a character is either
Expand Down
Loading