cargo fmt

This commit is contained in:
NotAFile 2022-02-01 19:46:06 +01:00
parent 220d827dbb
commit c32da018ad
7 changed files with 44 additions and 54 deletions

View File

@ -8,8 +8,8 @@ pub use callable::Callable;
pub use types::{Type, TypeStruct}; pub use types::{Type, TypeStruct};
mod callable; mod callable;
pub mod types;
pub mod typed_ir; pub mod typed_ir;
pub mod types;
/// lots of code is still not width-aware, this constant keeps track of that /// lots of code is still not width-aware, this constant keeps track of that
const TODO_WIDTH: u32 = 1; const TODO_WIDTH: u32 = 1;
@ -316,11 +316,7 @@ pub fn lower_module(pa_module: parser::Module) -> Result<String, CompileError> {
parser::PortDirection::Input => rtlil::PortOption::Input(idx as i32 + 1), parser::PortDirection::Input => rtlil::PortOption::Input(idx as i32 + 1),
parser::PortDirection::Output => rtlil::PortOption::Output(idx as i32 + 1), parser::PortDirection::Output => rtlil::PortOption::Output(idx as i32 + 1),
}; };
let wire = rtlil::Wire::new( let wire = rtlil::Wire::new(sig.il_id.to_owned(), TODO_WIDTH, Some(dir_option));
sig.il_id.to_owned(),
TODO_WIDTH,
Some(dir_option),
);
ir_module.add_wire(wire); ir_module.add_wire(wire);
} }
for item in pa_module.items { for item in pa_module.items {

View File

@ -9,8 +9,7 @@ struct Element<'ty> {
struct Signal<'ty> { struct Signal<'ty> {
pub id: u32, pub id: u32,
pub typ: Type<'ty> pub typ: Type<'ty>,
} }
struct Expression { struct Expression {}
}

View File

@ -8,9 +8,8 @@ use std::fs::File;
use std::io::prelude::*; use std::io::prelude::*;
use std::path::PathBuf; use std::path::PathBuf;
use structopt::StructOpt;
use nom_greedyerror::convert_error;
use ariadne::Source; use ariadne::Source;
use structopt::StructOpt;
#[derive(Debug, StructOpt)] #[derive(Debug, StructOpt)]
#[structopt(name = "example", about = "An example of StructOpt usage.")] #[structopt(name = "example", about = "An example of StructOpt usage.")]
@ -45,7 +44,12 @@ fn main() {
let parsed = parser::parse(input); 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)) => {
parser::error::convert_error(input, err).eprint(Source::from(input.fragment())).unwrap(); if opt.debug {
println!("{err:#?}");
}
parser::error::convert_error(input, err)
.eprint(Source::from(input.fragment()))
.unwrap();
} }
Err(_) => (unreachable!()), Err(_) => (unreachable!()),
Ok(res) => { Ok(res) => {

View File

@ -1,14 +1,14 @@
pub mod error;
mod literals;
pub mod module; pub mod module;
pub mod proc; pub mod proc;
mod literals;
pub mod error;
use nom::{ use nom::{
branch::alt, branch::alt,
bytes::complete::tag, bytes::complete::tag,
character::complete::{alpha1, alphanumeric1, char, multispace0, u64 as decimal}, character::complete::{alpha1, alphanumeric1, char, multispace0, u64 as decimal},
combinator::{map, opt, recognize}, combinator::{map, opt, recognize},
error::{ParseError, ErrorKind}, error::{ErrorKind, ParseError},
multi::{many0, separated_list0}, multi::{many0, separated_list0},
sequence::{delimited, pair, preceded, separated_pair, terminated, tuple}, sequence::{delimited, pair, preceded, separated_pair, terminated, tuple},
}; };
@ -22,8 +22,8 @@ pub type IErr<I> = GreedyError<I, ErrorKind>;
// custom IResult type for VerboseError // custom IResult type for VerboseError
pub type IResult<I, O, E = IErr<I>> = nom::IResult<I, O, E>; pub type IResult<I, O, E = IErr<I>> = nom::IResult<I, O, E>;
use literals::hexadecimal;
pub use crate::parser::module::{module, Module, ModuleItem, PortDirection}; pub use crate::parser::module::{module, Module, ModuleItem, PortDirection};
use literals::hexadecimal;
fn ws0<'a, F: 'a, O, E: ParseError<Span<'a>>>( fn ws0<'a, F: 'a, O, E: ParseError<Span<'a>>>(
inner: F, inner: F,
@ -46,14 +46,12 @@ fn typename(input: Span) -> IResult<Span, TypeName> {
map( map(
tuple(( tuple((
identifier, identifier,
opt(delimited(char('<'), ws0(expression), char('>'))) opt(delimited(char('<'), ws0(expression), char('>'))),
)), )),
|(ident, _)| { |(ident, _)| TypeName {
TypeName {
name: ident, name: ident,
generics: () generics: (),
} },
}
)(input) )(input)
} }

View File

@ -1,36 +1,29 @@
use std::fmt::Debug; use std::fmt::Debug;
use std::ops::Deref; use std::ops::Deref;
use super::{Span, IErr}; use super::{IErr, Span};
use ariadne::{Label, Report, ReportKind};
use nom::error::ErrorKind; use nom::error::ErrorKind;
use nom_greedyerror::{Position, GreedyErrorKind}; use nom_greedyerror::{GreedyErrorKind, Position};
use ariadne::{Report, ReportKind, Label};
fn span_to_range(input: Span) -> std::ops::Range<usize> { fn span_to_range(input: Span) -> std::ops::Range<usize> {
input.position()..(input.position() + input.len()) input.position()..(input.position() + input.len())
} }
pub fn convert_error( pub fn convert_error(input: Span, e: IErr<Span>) -> Report {
input: Span,
e: IErr<Span>,
) -> Report {
let mut labels = Vec::new(); let mut labels = Vec::new();
for err in e.errors { for err in e.errors {
let label = match err.1 { let label = match err.1 {
GreedyErrorKind::Context(ctx) => { GreedyErrorKind::Context(ctx) => {
Label::new(span_to_range(err.0)) Label::new(span_to_range(err.0)).with_message(format!("in {ctx}"))
.with_message(format!("in {ctx}")) }
}, GreedyErrorKind::Char(c) => Label::new(err.0.position()..err.0.position())
GreedyErrorKind::Char(c) => { .with_message(format!("expected {c:?}")),
Label::new(err.0.position()..err.0.position())
.with_message(format!("expected {c:?}"))
},
GreedyErrorKind::Nom(_) => todo!(), GreedyErrorKind::Nom(_) => todo!(),
}; };
labels.push(label); labels.push(label);
} }
let mut rep = Report::build(ReportKind::Error, (), 0) let mut rep = Report::build(ReportKind::Error, (), 0).with_message("Parse Error");
.with_message("Parse Error");
for lbl in labels { for lbl in labels {
rep = rep.with_label(lbl) rep = rep.with_label(lbl)
} }

View File

@ -17,7 +17,8 @@ pub fn hexadecimal(input: Span) -> IResult<Span, u64> {
))), ))),
), ),
|out: Span| { |out: Span| {
u64::from_str_radix(&str::replace(out.fragment(), "_", ""), 16).expect("error parsing literal") u64::from_str_radix(&str::replace(out.fragment(), "_", ""), 16)
.expect("error parsing literal")
}, },
)(input) )(input)
} }

View File

@ -5,13 +5,13 @@ use nom::{
combinator::{consumed, map}, combinator::{consumed, map},
error::context, error::context,
multi::{many0, separated_list0}, multi::{many0, separated_list0},
sequence::{delimited, terminated, tuple, preceded}, sequence::{delimited, preceded, terminated, tuple},
}; };
use crate::parser::{ use crate::parser::{
assign_statement, declaration, identifier, assign_statement, declaration, identifier,
proc::{proc_block, ProcBlock}, proc::{proc_block, ProcBlock},
ws0, Assign, IResult, NetDecl, Span, typename typename, ws0, Assign, IResult, NetDecl, Span,
}; };
#[derive(Debug)] #[derive(Debug)]
@ -41,16 +41,11 @@ pub enum ModuleItem<'a> {
} }
fn port_decl(i: Span) -> IResult<Span, PortDecl> { fn port_decl(i: Span) -> IResult<Span, PortDecl> {
map( map(consumed(declaration), |(pos, net)| PortDecl {
consumed(
declaration,
),
|(pos, net)| PortDecl {
pos, pos,
direction: PortDirection::Input, direction: PortDirection::Input,
net, net,
}, })(i)
)(i)
} }
fn inputs_list(input: Span) -> IResult<Span, Vec<PortDecl>> { fn inputs_list(input: Span) -> IResult<Span, Vec<PortDecl>> {
@ -85,7 +80,11 @@ pub fn module(input: Span) -> IResult<Span, Module> {
ws0(identifier), ws0(identifier),
ws0(delimited(char('('), ws0(inputs_list), char(')'))), ws0(delimited(char('('), ws0(inputs_list), char(')'))),
ws0(preceded(tag("->"), ws0(typename))), ws0(preceded(tag("->"), ws0(typename))),
ws0(delimited(char('{'), ws0(many0(ws0(module_item))), char('}'))), ws0(delimited(
char('{'),
ws0(many0(ws0(module_item))),
char('}'),
)),
)), )),
|(_, name, inputs, ret, items)| Module { |(_, name, inputs, ret, items)| Module {
name, name,