From 76d24042ced05c8f12708b3f2a9607de3f4bbb3b Mon Sep 17 00:00:00 2001 From: notafile Date: Tue, 4 Jan 2022 17:24:21 +0100 Subject: [PATCH] refactor main --- src/main.rs | 19 +++++++++++++++++-- src/parser.rs | 4 ++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index de13e38..a31568b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,14 +3,29 @@ mod parser; mod rtlil; use nom::error::{convert_error, VerboseError}; +use std::path::PathBuf; +use std::fs::File; +use std::io::prelude::*; 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 pub type IResult> = nom::IResult; fn main() { - let input = include_str!("../identity.fut"); - let parsed = parser::module(input); + let opt = Opt::from_args(); + 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 { Err(nom::Err::Error(err) | nom::Err::Failure(err)) => { print!("{}", convert_error(input, err)) diff --git a/src/parser.rs b/src/parser.rs index 5322469..93bbb35 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -204,6 +204,10 @@ pub fn module(input: &str) -> IResult<&str, Module> { )(input) } +fn parse(input: &str) -> IResult<&str, Module> { + module(input) +} + #[cfg(test)] mod test { use super::*;