Skip to content
This repository was archived by the owner on Sep 8, 2025. It is now read-only.

Commit ef7bdf5

Browse files
authored
cranelift: ISLE printer (#10962)
* skeleton * setup printer tests * get one test passing * tests pass * support remaining tests * basic pretty printing * cleanup * simplification to fits() * missing docs * new_without_pos_tracking constructor * move printer tests to existing ISLE tests * destructure self
1 parent 7f8370a commit ef7bdf5

5 files changed

Lines changed: 698 additions & 4 deletions

File tree

cranelift/isle/isle/build.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ fn main() {
1515
emit_tests(&mut out, "isle_examples/link", "run_link");
1616
emit_tests(&mut out, "isle_examples/run", "run_run");
1717

18+
emit_tests(&mut out, "isle_examples/pass", "run_print");
19+
emit_tests(&mut out, "isle_examples/link", "run_print");
20+
emit_tests(&mut out, "isle_examples/run", "run_print");
21+
1822
let output = out_dir.join("isle_tests.rs");
1923
std::fs::write(output, out).unwrap();
2024
}

cranelift/isle/isle/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ pub mod lexer;
2828
mod log;
2929
pub mod overlap;
3030
pub mod parser;
31+
pub mod printer;
3132
pub mod sema;
3233
pub mod serialize;
3334
pub mod stablemapset;

cranelift/isle/isle/src/parser.rs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,20 @@ pub fn parse(lexer: Lexer) -> Result<Vec<Def>> {
1212
parser.parse_defs()
1313
}
1414

15+
/// Parse without positional information. Provided mainly to support testing, to
16+
/// enable equality testing on structure alone.
17+
pub fn parse_without_pos(lexer: Lexer) -> Result<Vec<Def>> {
18+
let parser = Parser::new_without_pos_tracking(lexer);
19+
parser.parse_defs()
20+
}
21+
1522
/// The ISLE parser.
1623
///
1724
/// Takes in a lexer and creates an AST.
1825
#[derive(Clone, Debug)]
1926
struct Parser<'a> {
2027
lexer: Lexer<'a>,
28+
disable_pos: bool,
2129
}
2230

2331
/// Used during parsing a `(rule ...)` to encapsulate some form that
@@ -31,7 +39,17 @@ enum IfLetOrExpr {
3139
impl<'a> Parser<'a> {
3240
/// Construct a new parser from the given lexer.
3341
pub fn new(lexer: Lexer<'a>) -> Parser<'a> {
34-
Parser { lexer }
42+
Parser {
43+
lexer,
44+
disable_pos: false,
45+
}
46+
}
47+
48+
fn new_without_pos_tracking(lexer: Lexer<'a>) -> Parser<'a> {
49+
Parser {
50+
lexer,
51+
disable_pos: true,
52+
}
3553
}
3654

3755
fn error(&self, pos: Pos, msg: String) -> Error {
@@ -72,9 +90,13 @@ impl<'a> Parser<'a> {
7290
}
7391

7492
fn pos(&self) -> Pos {
75-
self.lexer
76-
.peek()
77-
.map_or_else(|| self.lexer.pos(), |(pos, _)| *pos)
93+
if !self.disable_pos {
94+
self.lexer
95+
.peek()
96+
.map_or_else(|| self.lexer.pos(), |(pos, _)| *pos)
97+
} else {
98+
Pos::default()
99+
}
78100
}
79101

80102
fn is_lparen(&self) -> bool {

0 commit comments

Comments
 (0)