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.
This commit is contained in:
Jakob Stoklund Olesen
2016-09-13 11:01:21 -07:00
parent c80934d084
commit fcec517fc5
7 changed files with 20 additions and 17 deletions

View File

@@ -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;

View File

@@ -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<Vec<Function>> {
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<Vec<Function>> {
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")

View File

@@ -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<String>) -> 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 {

View File

@@ -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<String>) -> 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() {

View File

@@ -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<u32>) {
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::<Vec<Ebb>>();

View File

@@ -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);

View File

@@ -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) => {