Make generic over readers
This commit is contained in:
parent
bc73db5dba
commit
79300afd43
|
@ -11,7 +11,7 @@ mod scopes;
|
||||||
mod events;
|
mod events;
|
||||||
|
|
||||||
|
|
||||||
pub fn parse_vcd(file: File) -> Result<super::types::VCD, String> {
|
pub fn parse_vcd(file: impl std::io::Read) -> Result<super::types::VCD, String> {
|
||||||
let mut word_gen = super::reader::WordReader::new(file);
|
let mut word_gen = super::reader::WordReader::new(file);
|
||||||
|
|
||||||
let header = metadata::parse_metadata(&mut word_gen)?;
|
let header = metadata::parse_metadata(&mut word_gen)?;
|
||||||
|
|
|
@ -69,7 +69,10 @@ pub(super) fn tag<'a>(word: &'a str, pattern: &'a str) -> ParseResult<'a> {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) fn ident(word_reader: &mut WordReader, keyword: &str) -> Result<(), String> {
|
pub(super) fn ident<R: std::io::Read>(
|
||||||
|
word_reader: &mut WordReader<R>,
|
||||||
|
keyword: &str,
|
||||||
|
) -> Result<(), String> {
|
||||||
// let keyword = "module";
|
// let keyword = "module";
|
||||||
let (word, cursor) = next_word!(word_reader)?;
|
let (word, cursor) = next_word!(word_reader)?;
|
||||||
|
|
||||||
|
|
|
@ -12,8 +12,8 @@ use super::super::reader::{WordReader, Cursor, Line, Word, next_word};
|
||||||
use super::super::types::{SignalIdx, VCD};
|
use super::super::types::{SignalIdx, VCD};
|
||||||
|
|
||||||
|
|
||||||
pub(super) fn parse_events<'a>(
|
pub(super) fn parse_events<'a, R: std::io::Read>(
|
||||||
word_reader: &mut WordReader,
|
word_reader: &mut WordReader<R>,
|
||||||
vcd: &'a mut VCD,
|
vcd: &'a mut VCD,
|
||||||
signal_map: &mut HashMap<String, SignalIdx>,
|
signal_map: &mut HashMap<String, SignalIdx>,
|
||||||
) -> Result<(), String> {
|
) -> Result<(), String> {
|
||||||
|
|
|
@ -145,7 +145,7 @@ pub(super) fn parse_date(
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) fn parse_version(word_reader: &mut WordReader) -> Result<Version, String> {
|
pub(super) fn parse_version<R: std::io::Read>(word_reader: &mut WordReader<R>) -> Result<Version, String> {
|
||||||
let mut version = String::new();
|
let mut version = String::new();
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
|
@ -162,8 +162,8 @@ pub(super) fn parse_version(word_reader: &mut WordReader) -> Result<Version, Str
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) fn parse_timescale(
|
pub(super) fn parse_timescale<R: std::io::Read>(
|
||||||
word_reader: &mut WordReader,
|
word_reader: &mut WordReader<R>,
|
||||||
) -> Result<(Option<u32>, Timescale), String> {
|
) -> Result<(Option<u32>, Timescale), String> {
|
||||||
// we might see `1ps $end` or `1 ps $end`
|
// we might see `1ps $end` or `1 ps $end`
|
||||||
// first get timescale
|
// first get timescale
|
||||||
|
@ -220,7 +220,7 @@ pub(super) fn parse_timescale(
|
||||||
return Ok(timescale);
|
return Ok(timescale);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) fn parse_metadata(word_reader: &mut WordReader) -> Result<Metadata, String> {
|
pub(super) fn parse_metadata<R: std::io::Read>(word_reader: &mut WordReader<R>) -> Result<Metadata, String> {
|
||||||
let mut metadata = Metadata {
|
let mut metadata = Metadata {
|
||||||
date: None,
|
date: None,
|
||||||
version: None,
|
version: None,
|
||||||
|
|
|
@ -15,8 +15,8 @@ use super::super::signal::{SigType, SignalEnum};
|
||||||
use super::combinator_atoms::{tag, ident};
|
use super::combinator_atoms::{tag, ident};
|
||||||
use super::types::{ParseResult};
|
use super::types::{ParseResult};
|
||||||
|
|
||||||
pub(super) fn parse_var<'a>(
|
pub(super) fn parse_var<'a, R: std::io::Read>(
|
||||||
word_reader: &mut WordReader,
|
word_reader: &mut WordReader<R>,
|
||||||
parent_scope_idx: ScopeIdx,
|
parent_scope_idx: ScopeIdx,
|
||||||
vcd: &'a mut VCD,
|
vcd: &'a mut VCD,
|
||||||
signal_map: &mut HashMap<String, SignalIdx>,
|
signal_map: &mut HashMap<String, SignalIdx>,
|
||||||
|
@ -157,8 +157,8 @@ pub(super) fn parse_var<'a>(
|
||||||
|
|
||||||
/// Sometimes, variables can be listed outside of scopes.
|
/// Sometimes, variables can be listed outside of scopes.
|
||||||
/// We call these orphaned vars.
|
/// We call these orphaned vars.
|
||||||
fn parse_orphaned_vars<'a>(
|
fn parse_orphaned_vars<'a, R: std::io::Read>(
|
||||||
word_reader: &mut WordReader,
|
word_reader: &mut WordReader<R>,
|
||||||
vcd: &'a mut VCD,
|
vcd: &'a mut VCD,
|
||||||
signal_map: &mut HashMap<String, SignalIdx>,
|
signal_map: &mut HashMap<String, SignalIdx>,
|
||||||
) -> Result<(), String> {
|
) -> Result<(), String> {
|
||||||
|
@ -221,8 +221,8 @@ fn parse_orphaned_vars<'a>(
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_scopes_inner<'a>(
|
fn parse_scopes_inner<'a, R: std::io::Read>(
|
||||||
word_reader: &mut WordReader,
|
word_reader: &mut WordReader<R>,
|
||||||
parent_scope_idx: Option<ScopeIdx>,
|
parent_scope_idx: Option<ScopeIdx>,
|
||||||
vcd: &'a mut VCD,
|
vcd: &'a mut VCD,
|
||||||
signal_map: &mut HashMap<String, SignalIdx>,
|
signal_map: &mut HashMap<String, SignalIdx>,
|
||||||
|
@ -328,8 +328,8 @@ fn parse_scopes_inner<'a>(
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) fn parse_scopes<'a>(
|
pub(super) fn parse_scopes<'a, R: std::io::Read>(
|
||||||
word_reader: &mut WordReader,
|
word_reader: &mut WordReader<R>,
|
||||||
vcd: &'a mut VCD,
|
vcd: &'a mut VCD,
|
||||||
signal_map: &mut HashMap<String, SignalIdx>,
|
signal_map: &mut HashMap<String, SignalIdx>,
|
||||||
) -> Result<(), String> {
|
) -> Result<(), String> {
|
||||||
|
|
|
@ -3,7 +3,6 @@
|
||||||
// and the YEHOWSHUA license, both of which can be found at
|
// and the YEHOWSHUA license, both of which can be found at
|
||||||
// the root of the folder containing the sources for this program.
|
// the root of the folder containing the sources for this program.
|
||||||
use std::collections::VecDeque;
|
use std::collections::VecDeque;
|
||||||
use std::fs::File;
|
|
||||||
use std::io;
|
use std::io;
|
||||||
use std::io::BufRead;
|
use std::io::BufRead;
|
||||||
use std::slice;
|
use std::slice;
|
||||||
|
@ -16,8 +15,8 @@ pub(super) struct Word(pub(super) usize);
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub(super) struct Cursor(pub(super) Line, pub(super) Word);
|
pub(super) struct Cursor(pub(super) Line, pub(super) Word);
|
||||||
|
|
||||||
pub(super) struct WordReader {
|
pub(super) struct WordReader<R: io::Read> {
|
||||||
reader: io::BufReader<File>,
|
reader: io::BufReader<R>,
|
||||||
eof: bool,
|
eof: bool,
|
||||||
buffers: Vec<String>,
|
buffers: Vec<String>,
|
||||||
curr_line: usize,
|
curr_line: usize,
|
||||||
|
@ -25,11 +24,11 @@ pub(super) struct WordReader {
|
||||||
curr_slice: Option<(*const u8, usize, Cursor)>,
|
curr_slice: Option<(*const u8, usize, Cursor)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl WordReader {
|
impl<R: std::io::Read> WordReader<R> {
|
||||||
pub(super) fn new(file: File) -> WordReader {
|
pub(super) fn new(file: R) -> WordReader<R> {
|
||||||
let reader = io::BufReader::new(file);
|
let reader = io::BufReader::new(file);
|
||||||
WordReader {
|
WordReader {
|
||||||
reader: reader,
|
reader,
|
||||||
eof: false,
|
eof: false,
|
||||||
buffers: vec![],
|
buffers: vec![],
|
||||||
curr_line: 0,
|
curr_line: 0,
|
||||||
|
|
Loading…
Reference in a new issue