diff --git a/src/literals.rs b/src/literals.rs index 32fb6f0..1bfafb9 100644 --- a/src/literals.rs +++ b/src/literals.rs @@ -1,25 +1,37 @@ -use std::ops::RangeFrom; +use std::ops::{RangeFrom, RangeTo}; use nom::{ - bytes::complete::tag, character::complete::{char, one_of}, combinator::{map_res, recognize}, multi::{many0, many1}, sequence::{preceded, terminated}, + AsChar, FindToken, InputIter, InputLength, Offset, Slice }; use crate::parser::IResult; -pub fn hexadecimal(input: &str) -> IResult<&str, u64> { +pub fn hexadecimal<'a, I>(input: I) -> IResult +where + // TODO: ask if there is any way to avoid this hellish constraint + I: InputIter + + nom::Slice> + + InputLength + + Clone + + Offset + + Into<&'a str> + + Slice>, + ::Item: AsChar + Copy, + &'a str: FindToken<::Item> +{ map_res( preceded( - tag("h"), + char('h'), recognize(many1(terminated( one_of("0123456789abcdefABCDEF"), many0(char('_')), ))), ), - |out: &str| u64::from_str_radix(&str::replace(&out, "_", ""), 16), + |out: I| u64::from_str_radix(&str::replace(&out.into(), "_", ""), 16), )(input) } @@ -27,12 +39,6 @@ pub fn hexadecimal(input: &str) -> IResult<&str, u64> { mod test { use super::*; - #[test] - fn test_dec() { - assert_eq!(decimal("123").unwrap().1, 123); - assert_eq!(decimal("0123").unwrap().1, 123); - } - #[test] fn test_hex() { assert_eq!(hexadecimal("hfF").unwrap().1, 0xff); diff --git a/src/main.rs b/src/main.rs index b68eeec..d5051a9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,9 +3,9 @@ mod parser; mod rtlil; use nom::error::convert_error; -use std::path::PathBuf; use std::fs::File; use std::io::prelude::*; +use std::path::PathBuf; use structopt::StructOpt; #[derive(Debug, StructOpt)] @@ -20,7 +20,9 @@ fn main() { 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"); + infile + .read_to_string(&mut input) + .expect("error reading file"); let input: &str = input.as_str(); let parsed = parser::parse(&input); match parsed { diff --git a/src/parser.rs b/src/parser.rs index bfb69f2..e172098 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -16,7 +16,7 @@ type Span<'a> = LocatedSpan<&'a str>; // custom IResult type for VerboseError pub type IResult> = nom::IResult; -use crate::literals::{hexadecimal}; +use crate::literals::hexadecimal; fn ws0<'a, F: 'a, O, E: ParseError<&'a str>>( inner: F, diff --git a/src/rtlil.rs b/src/rtlil.rs index 961b56a..2dc180c 100644 --- a/src/rtlil.rs +++ b/src/rtlil.rs @@ -208,23 +208,23 @@ fn lower_expression(module: &mut Module, expr: &parser::Expression) -> String { let arg_b = args_resolved.next().unwrap(); let cell_id = module.make_genid("and"); builtin_binop_cell("$and", &cell_id, &arg_a, &arg_b, &output_gen_id) - }, + } "xor" => { let arg_a = args_resolved.next().unwrap(); let arg_b = args_resolved.next().unwrap(); let cell_id = module.make_genid("xor"); builtin_binop_cell("$xor", &cell_id, &arg_a, &arg_b, &output_gen_id) - }, + } "not" => { let arg_a = args_resolved.next().unwrap(); let cell_id = module.make_genid("not"); builtin_unop_cell("$not", &cell_id, &arg_a, &output_gen_id) - }, + } "reduce_or" => { let arg_a = args_resolved.next().unwrap(); let cell_id = module.make_genid("reduce_or"); builtin_unop_cell("$reduce_or", &cell_id, &arg_a, &output_gen_id) - }, + } _ => todo!("unknown function"), }; module.cells.push(cell);