refactor main

This commit is contained in:
notafile 2022-01-04 17:24:21 +01:00
parent ed3ff17c89
commit 76d24042ce
2 changed files with 21 additions and 2 deletions

View File

@ -3,14 +3,29 @@ mod parser;
mod rtlil; mod rtlil;
use nom::error::{convert_error, VerboseError}; use nom::error::{convert_error, VerboseError};
use std::path::PathBuf;
use std::fs::File;
use std::io::prelude::*;
use structopt::StructOpt; use structopt::StructOpt;
#[derive(Debug, StructOpt)]
#[structopt(name = "example", about = "An example of StructOpt usage.")]
struct Opt {
/// Input file
#[structopt(parse(from_os_str))]
input: PathBuf,
}
// custom IResult type for verboseerror // custom IResult type for verboseerror
pub type IResult<I, O, E = VerboseError<I>> = nom::IResult<I, O, E>; pub type IResult<I, O, E = VerboseError<I>> = nom::IResult<I, O, E>;
fn main() { fn main() {
let input = include_str!("../identity.fut"); let opt = Opt::from_args();
let parsed = parser::module(input); let mut infile = File::open(opt.input).expect("could not open file");
let mut input = String::new();
infile.read_to_string(&mut input).expect("error reading file");
let input: &str = input.as_str();
let parsed = parser::parse(&input);
match parsed { match parsed {
Err(nom::Err::Error(err) | nom::Err::Failure(err)) => { Err(nom::Err::Error(err) | nom::Err::Failure(err)) => {
print!("{}", convert_error(input, err)) print!("{}", convert_error(input, err))

View File

@ -204,6 +204,10 @@ pub fn module(input: &str) -> IResult<&str, Module> {
)(input) )(input)
} }
fn parse(input: &str) -> IResult<&str, Module> {
module(input)
}
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::*; use super::*;