From fcec517fc5cfa0d3e90f32f021be0ed83e11bfb9 Mon Sep 17 00:00:00 2001 From: Jakob Stoklund Olesen Date: Tue, 13 Sep 2016 11:01:21 -0700 Subject: [PATCH] Simplify the interface to cretonne-reader. Export a single function: parse_functions() which results a vector of functions parsed from the source string. Hide the parser and lexer modules. They are not useful to external clients. --- cranelift/src/libreader/lib.rs | 5 +++-- cranelift/src/libreader/parser.rs | 12 +++++++----- cranelift/src/tools/cat.rs | 4 ++-- cranelift/src/tools/print_cfg.rs | 4 ++-- cranelift/src/tools/tests/cfg_traversal.rs | 4 ++-- cranelift/src/tools/tests/dominator_tree.rs | 4 ++-- cranelift/src/tools/tests/verifier.rs | 4 ++-- 7 files changed, 20 insertions(+), 17 deletions(-) diff --git a/cranelift/src/libreader/lib.rs b/cranelift/src/libreader/lib.rs index 4c53c79fa0..519fee7eed 100644 --- a/cranelift/src/libreader/lib.rs +++ b/cranelift/src/libreader/lib.rs @@ -5,8 +5,9 @@ extern crate cretonne; +pub use parser::{Result, parse_functions}; pub use testcommand::{TestCommand, TestOption}; -pub mod lexer; -pub mod parser; +mod lexer; +mod parser; mod testcommand; diff --git a/cranelift/src/libreader/parser.rs b/cranelift/src/libreader/parser.rs index fa6a445158..2ae1629c80 100644 --- a/cranelift/src/libreader/parser.rs +++ b/cranelift/src/libreader/parser.rs @@ -21,6 +21,13 @@ use cretonne::ir::instructions::{InstructionFormat, InstructionData, VariableArg pub use lexer::Location; +/// Parse the entire `text` into a list of functions. +/// +/// Any test commands or ISA declarations are ignored. +pub fn parse_functions(text: &str) -> Result> { + Parser::new(text).parse_function_list() +} + /// A parse error is returned when the parse failed. #[derive(Debug)] pub struct Error { @@ -256,11 +263,6 @@ impl<'a> Parser<'a> { } } - /// Parse the entire string into a list of functions. - pub fn parse(text: &'a str) -> Result> { - Self::new(text).parse_function_list() - } - // Consume the current lookahead token and return it. fn consume(&mut self) -> Token<'a> { self.lookahead.take().expect("No token to consume") diff --git a/cranelift/src/tools/cat.rs b/cranelift/src/tools/cat.rs index 6d740da701..a41b5b5f27 100644 --- a/cranelift/src/tools/cat.rs +++ b/cranelift/src/tools/cat.rs @@ -8,7 +8,7 @@ use std::io::{self, Read}; use CommandResult; -use cton_reader::parser::Parser; +use cton_reader::parse_functions; use cretonne::write::write_function; pub fn run(files: Vec) -> CommandResult { @@ -26,7 +26,7 @@ fn cat_one(filename: String) -> CommandResult { let mut buffer = String::new(); try!(file.read_to_string(&mut buffer) .map_err(|e| format!("Couldn't read {}: {}", filename, e))); - let items = try!(Parser::parse(&buffer).map_err(|e| format!("{}: {}", filename, e))); + let items = try!(parse_functions(&buffer).map_err(|e| format!("{}: {}", filename, e))); for (idx, func) in items.into_iter().enumerate() { if idx != 0 { diff --git a/cranelift/src/tools/print_cfg.rs b/cranelift/src/tools/print_cfg.rs index dd1aa3de18..649d87fb9f 100644 --- a/cranelift/src/tools/print_cfg.rs +++ b/cranelift/src/tools/print_cfg.rs @@ -9,7 +9,7 @@ use CommandResult; use cretonne::ir::Function; use cretonne::cfg::ControlFlowGraph; use cretonne::ir::instructions::InstructionData; -use cton_reader::parser::Parser; +use cton_reader::parse_functions; pub fn run(files: Vec) -> CommandResult { for (i, f) in files.into_iter().enumerate() { @@ -160,7 +160,7 @@ fn print_cfg(filename: String) -> CommandResult { let mut buffer = String::new(); try!(file.read_to_string(&mut buffer) .map_err(|e| format!("Couldn't read {}: {}", filename, e))); - let items = try!(Parser::parse(&buffer).map_err(|e| format!("{}: {}", filename, e))); + let items = try!(parse_functions(&buffer).map_err(|e| format!("{}: {}", filename, e))); let mut cfg_printer = CFGPrinter::new(stdout()); for (idx, func) in items.into_iter().enumerate() { diff --git a/cranelift/src/tools/tests/cfg_traversal.rs b/cranelift/src/tools/tests/cfg_traversal.rs index 03ed5bd148..b766f9d96c 100644 --- a/cranelift/src/tools/tests/cfg_traversal.rs +++ b/cranelift/src/tools/tests/cfg_traversal.rs @@ -1,13 +1,13 @@ extern crate cretonne; extern crate cton_reader; -use self::cton_reader::parser::Parser; +use self::cton_reader::parse_functions; use self::cretonne::ir::Ebb; use self::cretonne::cfg::ControlFlowGraph; use self::cretonne::entity_map::EntityMap; fn test_reverse_postorder_traversal(function_source: &str, ebb_order: Vec) { - let func = &Parser::parse(function_source).unwrap()[0]; + let func = &parse_functions(function_source).unwrap()[0]; let cfg = ControlFlowGraph::new(&func); let ebbs = ebb_order.iter().map(|n| Ebb::with_number(*n).unwrap()) .collect::>(); diff --git a/cranelift/src/tools/tests/dominator_tree.rs b/cranelift/src/tools/tests/dominator_tree.rs index c1be367137..71c02dc652 100644 --- a/cranelift/src/tools/tests/dominator_tree.rs +++ b/cranelift/src/tools/tests/dominator_tree.rs @@ -9,7 +9,7 @@ use regex::Regex; use std::fs::File; use std::io::Read; use self::cretonne::ir::Ebb; -use self::cton_reader::parser::Parser; +use self::cton_reader::parse_functions; use self::cretonne::ir::function::Function; use self::cretonne::entity_map::EntityMap; use self::cretonne::ir::entities::NO_INST; @@ -69,7 +69,7 @@ fn dominator_tree_from_source(func: &Function, function_source: &str) -> Dominat fn test_dominator_tree(function_source: &str) { - let func = &Parser::parse(function_source).unwrap()[0]; + let func = &parse_functions(function_source).unwrap()[0]; let src_dtree = dominator_tree_from_source(&func, function_source); let cfg = ControlFlowGraph::new(&func); diff --git a/cranelift/src/tools/tests/verifier.rs b/cranelift/src/tools/tests/verifier.rs index 28bb1bbba5..c26056b8ba 100644 --- a/cranelift/src/tools/tests/verifier.rs +++ b/cranelift/src/tools/tests/verifier.rs @@ -8,7 +8,7 @@ use glob::glob; use regex::Regex; use std::fs::File; use std::io::Read; -use self::cton_reader::parser::Parser; +use self::cton_reader::parse_functions; use self::cretonne::verifier::Verifier; /// Compile a function and run verifier tests based on specially formatted @@ -37,7 +37,7 @@ fn verifier_tests_from_source(function_source: &str) { // Run the verifier against each function and compare the output // with the expected result (as determined above). - for (i, func) in Parser::parse(function_source).unwrap().into_iter().enumerate() { + for (i, func) in parse_functions(function_source).unwrap().into_iter().enumerate() { let result = Verifier::new(&func).run(); match verifier_results[i] { Some(ref re) => {