Skip to content

Commit 97651fb

Browse files
committed
syntax/hir: add a printer for HIR
This adds a printer for the high-level intermediate representation. The regex it prints is valid, and can be used as a way to turn it into a regex::Regex.
1 parent c230e59 commit 97651fb

File tree

2 files changed

+377
-0
lines changed

2 files changed

+377
-0
lines changed

regex-syntax/src/hir/mod.rs

+18
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ pub use hir::visitor::{Visitor, visit};
2525

2626
mod interval;
2727
pub mod literal;
28+
pub mod print;
2829
pub mod translate;
2930
mod visitor;
3031

@@ -152,6 +153,10 @@ impl fmt::Display for ErrorKind {
152153
/// and can be computed cheaply during the construction process. For
153154
/// example, one such attribute is whether the expression must match at the
154155
/// beginning of the text.
156+
///
157+
/// Also, an `Hir`'s `fmt::Display` implementation prints an HIR as a regular
158+
/// expression pattern string, and uses constant stack space and heap space
159+
/// proportional to the size of the `Hir`.
155160
#[derive(Clone, Debug, Eq, PartialEq)]
156161
pub struct Hir {
157162
/// The underlying HIR kind.
@@ -602,6 +607,19 @@ impl HirKind {
602607
}
603608
}
604609

610+
/// Print a display representation of this Hir.
611+
///
612+
/// The result of this is a valid regular expression pattern string.
613+
///
614+
/// This implementation uses constant stack space and heap space proportional
615+
/// to the size of the `Hir`.
616+
impl fmt::Display for Hir {
617+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
618+
use hir::print::Printer;
619+
Printer::new().print(self, f)
620+
}
621+
}
622+
605623
/// The high-level intermediate representation of a literal.
606624
///
607625
/// A literal corresponds to a single character, where a character is either

regex-syntax/src/hir/print.rs

+359
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,359 @@
1+
/*!
2+
This module provides a regular expression printer for `Hir`.
3+
*/
4+
5+
use std::fmt;
6+
7+
use hir::{self, Hir, HirKind};
8+
use hir::visitor::{self, Visitor};
9+
use is_meta_character;
10+
11+
/// A builder for constructing a printer.
12+
///
13+
/// Note that since a printer doesn't have any configuration knobs, this type
14+
/// remains unexported.
15+
#[derive(Clone, Debug)]
16+
struct PrinterBuilder {
17+
_priv: (),
18+
}
19+
20+
impl Default for PrinterBuilder {
21+
fn default() -> PrinterBuilder {
22+
PrinterBuilder::new()
23+
}
24+
}
25+
26+
impl PrinterBuilder {
27+
fn new() -> PrinterBuilder {
28+
PrinterBuilder {
29+
_priv: (),
30+
}
31+
}
32+
33+
fn build(&self) -> Printer {
34+
Printer {
35+
_priv: (),
36+
}
37+
}
38+
}
39+
40+
/// A printer for a regular expression's high-level intermediate
41+
/// representation.
42+
///
43+
/// A printer converts a high-level intermediate representation (HIR) to a
44+
/// regular expression pattern string. This particular printer uses constant
45+
/// stack space and heap space proportional to the size of the HIR.
46+
///
47+
/// Since this printer is only using the HIR, the pattern it prints will likely
48+
/// not resemble the original pattern at all. For example, a pattern like
49+
/// `\pL` will have its entire class written out.
50+
///
51+
/// The purpose of this printer is to provide a means to mutate an HIR and then
52+
/// build a regular expression from the result of that mutation. (A regex
53+
/// library could provide a constructor from this HIR explicitly, but that
54+
/// creates an unnecessary public coupling between the regex library and this
55+
/// specific HIR representation.)
56+
#[derive(Debug)]
57+
pub struct Printer {
58+
_priv: (),
59+
}
60+
61+
impl Printer {
62+
/// Create a new printer.
63+
pub fn new() -> Printer {
64+
PrinterBuilder::new().build()
65+
}
66+
67+
/// Print the given `Ast` to the given writer. The writer must implement
68+
/// `fmt::Write`. Typical implementations of `fmt::Write` that can be used
69+
/// here are a `fmt::Formatter` (which is available in `fmt::Display`
70+
/// implementations) or a `&mut String`.
71+
pub fn print<W: fmt::Write>(&mut self, hir: &Hir, wtr: W) -> fmt::Result {
72+
visitor::visit(hir, Writer { printer: self, wtr: wtr })
73+
}
74+
}
75+
76+
#[derive(Debug)]
77+
struct Writer<'p, W> {
78+
printer: &'p mut Printer,
79+
wtr: W,
80+
}
81+
82+
impl<'p, W: fmt::Write> Visitor for Writer<'p, W> {
83+
type Output = ();
84+
type Err = fmt::Error;
85+
86+
fn finish(self) -> fmt::Result {
87+
Ok(())
88+
}
89+
90+
fn visit_pre(&mut self, hir: &Hir) -> fmt::Result {
91+
match *hir.kind() {
92+
HirKind::Empty
93+
| HirKind::Repetition(_)
94+
| HirKind::Concat(_)
95+
| HirKind::Alternation(_) => {}
96+
HirKind::Literal(hir::Literal::Unicode(c)) => {
97+
try!(self.write_literal_char(c));
98+
}
99+
HirKind::Literal(hir::Literal::Byte(b)) => {
100+
try!(self.write_literal_byte(b));
101+
}
102+
HirKind::Class(hir::Class::Unicode(ref cls)) => {
103+
try!(self.wtr.write_str("["));
104+
for range in cls.iter() {
105+
if range.start() == range.end() {
106+
try!(self.write_literal_char(range.start()));
107+
} else {
108+
try!(self.write_literal_char(range.start()));
109+
try!(self.wtr.write_str("-"));
110+
try!(self.write_literal_char(range.end()));
111+
}
112+
}
113+
try!(self.wtr.write_str("]"));
114+
}
115+
HirKind::Class(hir::Class::Bytes(ref cls)) => {
116+
try!(self.wtr.write_str("(?-u:["));
117+
for range in cls.iter() {
118+
if range.start() == range.end() {
119+
try!(self.write_literal_class_byte(range.start()));
120+
} else {
121+
try!(self.write_literal_class_byte(range.start()));
122+
try!(self.wtr.write_str("-"));
123+
try!(self.write_literal_class_byte(range.end()));
124+
}
125+
}
126+
try!(self.wtr.write_str("])"));
127+
}
128+
HirKind::Anchor(hir::Anchor::StartLine) => {
129+
try!(self.wtr.write_str("(?m:^)"));
130+
}
131+
HirKind::Anchor(hir::Anchor::EndLine) => {
132+
try!(self.wtr.write_str("(?m:$)"));
133+
}
134+
HirKind::Anchor(hir::Anchor::StartText) => {
135+
try!(self.wtr.write_str(r"\A"));
136+
}
137+
HirKind::Anchor(hir::Anchor::EndText) => {
138+
try!(self.wtr.write_str(r"\z"));
139+
}
140+
HirKind::WordBoundary(hir::WordBoundary::Unicode) => {
141+
try!(self.wtr.write_str(r"\b"));
142+
}
143+
HirKind::WordBoundary(hir::WordBoundary::UnicodeNegate) => {
144+
try!(self.wtr.write_str(r"\B"));
145+
}
146+
HirKind::WordBoundary(hir::WordBoundary::Ascii) => {
147+
try!(self.wtr.write_str(r"(?-u:\b)"));
148+
}
149+
HirKind::WordBoundary(hir::WordBoundary::AsciiNegate) => {
150+
try!(self.wtr.write_str(r"(?-u:\B)"));
151+
}
152+
HirKind::Group(ref x) => {
153+
match x.kind {
154+
hir::GroupKind::CaptureIndex(_) => {
155+
try!(self.wtr.write_str("("));
156+
}
157+
hir::GroupKind::CaptureName { ref name, .. } => {
158+
try!(write!(self.wtr, "(?P<{}>", name));
159+
}
160+
hir::GroupKind::NonCapturing => {
161+
try!(self.wtr.write_str("(?:"));
162+
}
163+
}
164+
}
165+
}
166+
Ok(())
167+
}
168+
169+
fn visit_post(&mut self, hir: &Hir) -> fmt::Result {
170+
match *hir.kind() {
171+
// Handled during visit_pre
172+
HirKind::Empty
173+
| HirKind::Literal(_)
174+
| HirKind::Class(_)
175+
| HirKind::Anchor(_)
176+
| HirKind::WordBoundary(_)
177+
| HirKind::Concat(_)
178+
| HirKind::Alternation(_) => {}
179+
HirKind::Repetition(ref x) => {
180+
match x.kind {
181+
hir::RepetitionKind::ZeroOrOne => {
182+
try!(self.wtr.write_str("?"));
183+
}
184+
hir::RepetitionKind::ZeroOrMore => {
185+
try!(self.wtr.write_str("*"));
186+
}
187+
hir::RepetitionKind::OneOrMore => {
188+
try!(self.wtr.write_str("+"));
189+
}
190+
hir::RepetitionKind::Range(ref x) => {
191+
match *x {
192+
hir::RepetitionRange::Exactly(m) => {
193+
try!(write!(self.wtr, "{{{}}}", m));
194+
}
195+
hir::RepetitionRange::AtLeast(m) => {
196+
try!(write!(self.wtr, "{{{},}}", m));
197+
}
198+
hir::RepetitionRange::Bounded(m, n) => {
199+
try!(write!(self.wtr, "{{{},{}}}", m, n));
200+
}
201+
}
202+
}
203+
}
204+
if !x.greedy {
205+
try!(self.wtr.write_str("?"));
206+
}
207+
}
208+
HirKind::Group(_) => {
209+
try!(self.wtr.write_str(")"));
210+
}
211+
}
212+
Ok(())
213+
}
214+
215+
fn visit_alternation_in(&mut self) -> fmt::Result {
216+
self.wtr.write_str("|")
217+
}
218+
}
219+
220+
impl<'p, W: fmt::Write> Writer<'p, W> {
221+
fn write_literal_char(&mut self, c: char) -> fmt::Result {
222+
if is_meta_character(c) {
223+
try!(self.wtr.write_str("\\"));
224+
}
225+
self.wtr.write_char(c)
226+
}
227+
228+
fn write_literal_byte(&mut self, b: u8) -> fmt::Result {
229+
let c = b as char;
230+
if c <= 0x7F as char && !c.is_control() && !c.is_whitespace() {
231+
self.wtr.write_char(c)
232+
} else {
233+
write!(self.wtr, "(?-u:\\x{:02X})", b)
234+
}
235+
}
236+
237+
fn write_literal_class_byte(&mut self, b: u8) -> fmt::Result {
238+
let c = b as char;
239+
if c <= 0x7F as char && !c.is_control() && !c.is_whitespace() {
240+
self.wtr.write_char(c)
241+
} else {
242+
write!(self.wtr, "\\x{:02X}", b)
243+
}
244+
}
245+
}
246+
247+
#[cfg(test)]
248+
mod tests {
249+
use ParserBuilder;
250+
use super::Printer;
251+
252+
fn roundtrip(given: &str, expected: &str) {
253+
roundtrip_with(|b| b, given, expected);
254+
}
255+
256+
fn roundtrip_bytes(given: &str, expected: &str) {
257+
roundtrip_with(|b| b.allow_invalid_utf8(true), given, expected);
258+
}
259+
260+
fn roundtrip_with<F>(mut f: F, given: &str, expected: &str)
261+
where F: FnMut(&mut ParserBuilder) -> &mut ParserBuilder
262+
{
263+
let mut builder = ParserBuilder::new();
264+
f(&mut builder);
265+
let hir = builder.build().parse(given).unwrap();
266+
267+
let mut printer = Printer::new();
268+
let mut dst = String::new();
269+
printer.print(&hir, &mut dst).unwrap();
270+
assert_eq!(expected, dst);
271+
}
272+
273+
#[test]
274+
fn print_literal() {
275+
roundtrip("a", "a");
276+
roundtrip(r"\xff", "\u{FF}");
277+
roundtrip_bytes(r"\xff", "\u{FF}");
278+
roundtrip_bytes(r"(?-u)\xff", r"(?-u:\xFF)");
279+
roundtrip("☃", "☃");
280+
}
281+
282+
#[test]
283+
fn print_class() {
284+
roundtrip(r"[a]", r"[a]");
285+
roundtrip(r"[a-z]", r"[a-z]");
286+
roundtrip(r"[a-z--b-c--x-y]", r"[ad-wz]");
287+
roundtrip(r"[^\x01-\u{10FFFF}]", "[\u{0}]");
288+
roundtrip(r"[-]", r"[\-]");
289+
roundtrip(r"[☃-⛄]", r"[☃-⛄]");
290+
291+
roundtrip(r"(?-u)[a]", r"(?-u:[a])");
292+
roundtrip(r"(?-u)[a-z]", r"(?-u:[a-z])");
293+
roundtrip_bytes(r"(?-u)[a-\xFF]", r"(?-u:[a-\xFF])");
294+
}
295+
296+
#[test]
297+
fn print_anchor() {
298+
roundtrip(r"^", r"\A");
299+
roundtrip(r"$", r"\z");
300+
roundtrip(r"(?m)^", r"(?m:^)");
301+
roundtrip(r"(?m)$", r"(?m:$)");
302+
}
303+
304+
#[test]
305+
fn print_word_boundary() {
306+
roundtrip(r"\b", r"\b");
307+
roundtrip(r"\B", r"\B");
308+
roundtrip(r"(?-u)\b", r"(?-u:\b)");
309+
roundtrip_bytes(r"(?-u)\B", r"(?-u:\B)");
310+
}
311+
312+
#[test]
313+
fn print_repetition() {
314+
roundtrip("a?", "a?");
315+
roundtrip("a??", "a??");
316+
roundtrip("(?U)a?", "a??");
317+
318+
roundtrip("a*", "a*");
319+
roundtrip("a*?", "a*?");
320+
roundtrip("(?U)a*", "a*?");
321+
322+
roundtrip("a+", "a+");
323+
roundtrip("a+?", "a+?");
324+
roundtrip("(?U)a+", "a+?");
325+
326+
roundtrip("a{1}", "a{1}");
327+
roundtrip("a{1,}", "a{1,}");
328+
roundtrip("a{1,5}", "a{1,5}");
329+
roundtrip("a{1}?", "a{1}?");
330+
roundtrip("a{1,}?", "a{1,}?");
331+
roundtrip("a{1,5}?", "a{1,5}?");
332+
roundtrip("(?U)a{1}", "a{1}?");
333+
roundtrip("(?U)a{1,}", "a{1,}?");
334+
roundtrip("(?U)a{1,5}", "a{1,5}?");
335+
}
336+
337+
#[test]
338+
fn print_group() {
339+
roundtrip("()", "()");
340+
roundtrip("(?P<foo>)", "(?P<foo>)");
341+
roundtrip("(?:)", "(?:)");
342+
343+
roundtrip("(a)", "(a)");
344+
roundtrip("(?P<foo>a)", "(?P<foo>a)");
345+
roundtrip("(?:a)", "(?:a)");
346+
347+
roundtrip("((((a))))", "((((a))))");
348+
}
349+
350+
#[test]
351+
fn print_alternation() {
352+
roundtrip("|", "|");
353+
roundtrip("||", "||");
354+
355+
roundtrip("a|b", "a|b");
356+
roundtrip("a|b|c", "a|b|c");
357+
roundtrip("foo|bar|quux", "foo|bar|quux");
358+
}
359+
}

0 commit comments

Comments
 (0)