New parser #2

Merged
ThePerfectComputer merged 51 commits from new_parser into main 2022-08-01 21:00:00 +00:00
5 changed files with 53 additions and 13 deletions
Showing only changes of commit 49d103fd56 - Show all commits

View file

@ -5,7 +5,7 @@ pub mod test;
use test::*; use test::*;
pub mod vcd; pub mod vcd;
use vcd::*; use vcd::parse_vcd;
#[derive(Parser)] #[derive(Parser)]
struct Cli { struct Cli {
@ -16,6 +16,7 @@ struct Cli {
fn main() -> std::io::Result<()> { fn main() -> std::io::Result<()> {
let args = Cli::parse(); let args = Cli::parse();
let file = File::open(&args.path)?; let file = File::open(&args.path)?;
parse_vcd(file); parse_vcd(file);

View file

@ -14,12 +14,43 @@ use types::*;
mod metadata; mod metadata;
use metadata::*; use metadata::*;
// use function_name::named;
#[named]
fn parse_signal_tree<'a>(
word_reader : &mut WordReader,
vcd : &'a mut VCD
) -> Result<&'a mut VCD, String> {
let err : Result<&'a mut VCD, String> = Err(format!("reached end of file without parser leaving {}", function_name!()));
// we assume we've already seen a `$scope` once
// by the time we reach this function
// let scope_name =
loop {
let word = word_reader.next_word();
// if there isn't another word left in the file, then we exit
if word.is_none() {
return err;
}
}
Ok(vcd)
}
pub fn parse_vcd(file : File) { pub fn parse_vcd(file : File) {
let mut word_gen = WordReader::new(file); let mut word_gen = WordReader::new(file);
let header = parse_metadata(&mut word_gen).unwrap(); let header = parse_metadata(&mut word_gen).unwrap();
dbg!(header); dbg!(&header);
// let (word, cursor) = word_gen.next_word().unwrap();
// cursor.error(word).unwrap();
let mut vcd = VCD{
metadata: header,
all_signals: vec![],
all_scopes: vec![]
};
} }
#[cfg(test)] #[cfg(test)]

View file

@ -1,7 +1,7 @@
use chrono::prelude::*; use chrono::prelude::*;
use itertools::Itertools; use itertools::Itertools;
use std::fs::File; use std::fs::File;
use ::function_name::named; use function_name::named;
use super::*; use super::*;

View file

@ -12,6 +12,14 @@ struct Word(usize);
#[derive(Debug)] #[derive(Debug)]
pub struct Cursor(Line, Word); pub struct Cursor(Line, Word);
impl Cursor {
pub(super) fn error(&self, word : &str) -> Result<(), String> {
let Cursor(Line(line_no), Word(word_no)) = self;
Err(format!("Error on word '{word}' {word_no} words into line {line_no}!"))
}
}
pub struct WordReader { pub struct WordReader {
reader : io::BufReader<File>, reader : io::BufReader<File>,
EOF : bool, EOF : bool,
@ -21,7 +29,7 @@ pub struct WordReader {
} }
impl WordReader { impl WordReader {
pub fn new(file : File) -> WordReader { pub(super) fn new(file : File) -> WordReader {
let mut reader = io::BufReader::new(file); let mut reader = io::BufReader::new(file);
WordReader { WordReader {
reader : reader, reader : reader,
@ -32,7 +40,7 @@ impl WordReader {
} }
} }
pub fn next_word(&mut self) -> Option<(&str, Cursor)> { pub(super) fn next_word(&mut self) -> Option<(&str, Cursor)> {
// if there are no more words, attempt to read more content // if there are no more words, attempt to read more content
// from the file // from the file
if self.str_slices.is_empty() { if self.str_slices.is_empty() {

View file

@ -15,13 +15,13 @@ pub(super) struct Metadata {
pub(super) timescale : (Option<u32>, Timescale)} pub(super) timescale : (Option<u32>, Timescale)}
#[derive(Debug)] #[derive(Debug)]
struct Scope_Idx(usize); pub(super) struct Scope_Idx(usize);
#[derive(Debug)] #[derive(Debug)]
struct Signal_Idx(usize); pub(super) struct Signal_Idx(usize);
#[derive(Debug)] #[derive(Debug)]
enum SignalGeneric{ pub(super) enum SignalGeneric{
Signal{ Signal{
name : String, name : String,
timeline : BTreeMap<BigInt, BigInt>, timeline : BTreeMap<BigInt, BigInt>,
@ -32,18 +32,18 @@ enum SignalGeneric{
} }
#[derive(Debug)] #[derive(Debug)]
struct Scope { pub(super) struct Scope {
name : String, name : String,
child_signals : Vec<Signal_Idx>, child_signals : Vec<Signal_Idx>,
child_scopes : Vec<Scope_Idx>} child_scopes : Vec<Scope_Idx>}
#[derive(Debug)] #[derive(Debug)]
struct VCD { pub struct VCD {
metadata : Metadata, pub(super) metadata : Metadata,
all_signals : Vec<SignalGeneric>, pub(super) all_signals : Vec<SignalGeneric>,
// the root scope should always be placed at index 0 // the root scope should always be placed at index 0
all_scopes : Vec<Scope>} pub(super) all_scopes : Vec<Scope>}
impl VCD { impl VCD {
pub fn new() -> Self { pub fn new() -> Self {