-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
cranelift: ISLE printer #8263
base: main
Are you sure you want to change the base?
cranelift: ISLE printer #8263
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,12 +12,21 @@ pub fn parse(lexer: Lexer) -> Result<Defs> { | |
parser.parse_defs() | ||
} | ||
|
||
/// Parse without positional information. Provided mainly to support testing, to | ||
/// enable equality testing on structure alone. | ||
pub fn parse_without_pos(lexer: Lexer) -> Result<Defs> { | ||
let mut parser = Parser::new(lexer); | ||
parser.disable_pos(); | ||
parser.parse_defs() | ||
} | ||
|
||
Comment on lines
+15
to
+22
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I really don't like this, but I also couldn't think of a better way. Please let me know if you have an alternative suggestion.
If There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A final option: a helper that maps through all AST nodes and sets positions to e.g. zero? That seems easiest to keep up to date and least likely to be subtly incorrect (vs. hand-written equality predicates), especially given compile errors on new AST node kinds. |
||
/// The ISLE parser. | ||
/// | ||
/// Takes in a lexer and creates an AST. | ||
#[derive(Clone, Debug)] | ||
struct Parser<'a> { | ||
lexer: Lexer<'a>, | ||
disable_pos: bool, | ||
} | ||
|
||
/// Used during parsing a `(rule ...)` to encapsulate some form that | ||
|
@@ -31,7 +40,14 @@ enum IfLetOrExpr { | |
impl<'a> Parser<'a> { | ||
/// Construct a new parser from the given lexer. | ||
pub fn new(lexer: Lexer<'a>) -> Parser<'a> { | ||
Parser { lexer } | ||
Parser { | ||
lexer, | ||
disable_pos: false, | ||
} | ||
} | ||
|
||
pub fn disable_pos(&mut self) { | ||
self.disable_pos = true; | ||
} | ||
|
||
fn error(&self, pos: Pos, msg: String) -> Errors { | ||
|
@@ -76,9 +92,13 @@ impl<'a> Parser<'a> { | |
} | ||
|
||
fn pos(&self) -> Pos { | ||
self.lexer | ||
.peek() | ||
.map_or_else(|| self.lexer.pos(), |(pos, _)| *pos) | ||
if !self.disable_pos { | ||
self.lexer | ||
.peek() | ||
.map_or_else(|| self.lexer.pos(), |(pos, _)| *pos) | ||
} else { | ||
Pos::default() | ||
} | ||
} | ||
|
||
fn is_lparen(&self) -> bool { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Under this implementation, I believe the printer tests would not be run by default in CI.
It would be nice if cargo supported some kind of "default test features" configuration, but as far as I can tell this isn't possible. Nor is there much appetite for adding it according to rust-lang/cargo#2911.
Perhaps if we cared enough we could edit the Github actions workflows to invoke these tests with the right
--feature
argument. But having poked around the CI config a little, it's not clear how to do that without causing a mess.Another option is to have the printer tests in a different crate that can depend on this one with the right feature set.
All that said, I'm not sure it's worth it just for these tests, but I wanted to mention it in case you had a simple suggestion for how to run them?