now parsing timelines with nom
This commit is contained in:
parent
21d84d46b5
commit
4478d02379
|
@ -10,5 +10,4 @@ num = "0.4"
|
||||||
clap = { version = "3.1.8", features = ["derive"] }
|
clap = { version = "3.1.8", features = ["derive"] }
|
||||||
chrono = "0.4"
|
chrono = "0.4"
|
||||||
nom = "7.1.1"
|
nom = "7.1.1"
|
||||||
genawaiter = "0.99.1"
|
|
||||||
nom-bufreader = "0.2.0"
|
nom-bufreader = "0.2.0"
|
124
src/main.rs
124
src/main.rs
|
@ -2,7 +2,6 @@ use std::io::prelude::*;
|
||||||
use std::io;
|
use std::io;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
|
|
||||||
use genawaiter::{sync::gen, yield_};
|
|
||||||
use num::*;
|
use num::*;
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use chrono::prelude::*;
|
use chrono::prelude::*;
|
||||||
|
@ -13,10 +12,15 @@ use nom_bufreader::{Error, Parse};
|
||||||
use nom::{
|
use nom::{
|
||||||
branch::alt,
|
branch::alt,
|
||||||
bytes::streaming::{tag, take_until, take_while1},
|
bytes::streaming::{tag, take_until, take_while1},
|
||||||
character::streaming::{space0,alpha1, multispace1, digit1},
|
character::is_alphanumeric,
|
||||||
|
character::complete::none_of,
|
||||||
|
character::streaming::{
|
||||||
|
space0, alpha1, multispace0,
|
||||||
|
multispace1, digit1, line_ending,
|
||||||
|
alphanumeric1},
|
||||||
combinator::map_res,
|
combinator::map_res,
|
||||||
IResult,
|
IResult,
|
||||||
sequence::tuple
|
sequence::{tuple, delimited, terminated, preceded}
|
||||||
};
|
};
|
||||||
|
|
||||||
// TODO : Remove
|
// TODO : Remove
|
||||||
|
@ -29,11 +33,6 @@ struct Cli {
|
||||||
path: std::path::PathBuf,
|
path: std::path::PathBuf,
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Timestamp{
|
|
||||||
file_offset: u64,
|
|
||||||
timestamp: BigInt
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: implement any timescales greater than a second
|
// TODO: implement any timescales greater than a second
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
enum Timescale {ps, ns, us, ms, s, unit}
|
enum Timescale {ps, ns, us, ms, s, unit}
|
||||||
|
@ -78,16 +77,6 @@ struct VCD {
|
||||||
// the root scope should always be placed at index 0
|
// the root scope should always be placed at index 0
|
||||||
all_scopes : Vec<Scope>}
|
all_scopes : Vec<Scope>}
|
||||||
|
|
||||||
fn method(i: &[u8]) -> IResult<&[u8], String, ()> {
|
|
||||||
map_res(alt((tag("GET"), tag("POST"), tag("HEAD"))), |s| {
|
|
||||||
from_utf8(s).map(|s| s.to_string())
|
|
||||||
})(i)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn path(i: &[u8]) -> IResult<&[u8], String, ()> {
|
|
||||||
map_res(take_until(" "), |s| from_utf8(s).map(|s| s.to_string()))(i)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn date(i: &[u8]) -> IResult<&[u8], DateTime<Utc>, ()> {
|
fn date(i: &[u8]) -> IResult<&[u8], DateTime<Utc>, ()> {
|
||||||
let (i, _) = tag("$date")(i)?;
|
let (i, _) = tag("$date")(i)?;
|
||||||
|
|
||||||
|
@ -139,34 +128,97 @@ fn date(i: &[u8]) -> IResult<&[u8], DateTime<Utc>, ()> {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn space(i: &[u8]) -> IResult<&[u8], (), ()> {
|
fn version(i: &[u8]) -> IResult<&[u8], String, ()> {
|
||||||
let (i, _) = space0(i)?;
|
let (i, _) = tag("$version")(i)?;
|
||||||
|
let (mut i, _) = multispace1(i)?;
|
||||||
|
|
||||||
|
let mut version = "".to_string();
|
||||||
|
let mut reached_end = false;
|
||||||
|
|
||||||
|
while (reached_end == false) {
|
||||||
|
let (_i, next_word) = take_while1(
|
||||||
|
|c|
|
||||||
|
c != 36 && // not $
|
||||||
|
c >= 33 && // between ! and ~
|
||||||
|
c <= 126
|
||||||
|
)(i)?;
|
||||||
|
i = _i;
|
||||||
|
let next_word = from_utf8(next_word).unwrap();
|
||||||
|
|
||||||
|
version = format!("{version} {next_word}");
|
||||||
|
let (_i, _) = multispace1(i)?;
|
||||||
|
i = _i;
|
||||||
|
|
||||||
|
match tag::<&str, &[u8], ()>("$end")(i) {
|
||||||
|
Ok((_i, _)) => {
|
||||||
|
i = _i;
|
||||||
|
reached_end = true;
|
||||||
|
}
|
||||||
|
Err(_) => {}
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// strip the initial space
|
||||||
|
version = version[1..].to_string();
|
||||||
|
|
||||||
|
Ok((i, version))
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
fn timescale(i: &[u8]) -> IResult<&[u8], (Option<u32>, Timescale), ()> {
|
||||||
|
let (i, _) = tag("$timescale")(i)?;
|
||||||
|
dbg!("here");
|
||||||
|
let (i, _) = multispace1(i)?;
|
||||||
|
|
||||||
|
let (i, scale) = digit1(i)?;
|
||||||
|
let (i, _) = multispace1(i)?;
|
||||||
|
|
||||||
|
let (i, unit) = alpha1(i)?;
|
||||||
|
let (i, _) = multispace1(i)?;
|
||||||
|
|
||||||
|
let (i, _) = tag("$end")(i)?;
|
||||||
|
|
||||||
|
let scale = from_utf8(scale).unwrap().to_string();
|
||||||
|
let scale = scale.parse::<u32>().unwrap();
|
||||||
|
|
||||||
|
let unit = match from_utf8(unit).unwrap() {
|
||||||
|
"ps" => Ok(Timescale::ps),
|
||||||
|
"ns" => Ok(Timescale::ns),
|
||||||
|
"us" => Ok(Timescale::us),
|
||||||
|
"ms" => Ok(Timescale::ms),
|
||||||
|
"s" => Ok(Timescale::s),
|
||||||
|
_ => Err(())
|
||||||
|
}.unwrap();
|
||||||
|
|
||||||
|
|
||||||
|
Ok((i, (Some(scale), unit)))
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
fn f_multispace0(i: &[u8]) -> IResult<&[u8], (), ()> {
|
||||||
|
let (i, _) = multispace0(i)?;
|
||||||
Ok((i, ()))
|
Ok((i, ()))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() -> std::io::Result<()> {
|
fn main() -> Result<(), Error<()>> {
|
||||||
let args = Cli::parse();
|
let args = Cli::parse();
|
||||||
|
|
||||||
let file = File::open(&args.path)?;
|
let file = File::open(&args.path)?;
|
||||||
let mut reader = BufReader::new(file);
|
let mut reader = BufReader::new(file);
|
||||||
let m = reader.parse(date).expect("failed to parse date");
|
|
||||||
dbg!(m.to_rfc2822());
|
|
||||||
|
|
||||||
// let mut file_by_line = gen!({
|
reader.parse(f_multispace0).unwrap();
|
||||||
// while {
|
let date = reader.parse(date).expect("failed to parse date");
|
||||||
// let bytes_read = reader.read_line(&mut buffer).unwrap();
|
|
||||||
// bytes_read > 0
|
|
||||||
// } {
|
|
||||||
// yield_!(buffer.as_bytes());
|
|
||||||
// buffer.clear()
|
|
||||||
// }
|
|
||||||
// });
|
|
||||||
|
|
||||||
// for line in file_by_line {
|
reader.parse(f_multispace0).unwrap();
|
||||||
// dbg!(&line);
|
let version = reader.parse(version).expect("failed to parse version");
|
||||||
// }
|
|
||||||
|
|
||||||
|
reader.parse(f_multispace0).unwrap();
|
||||||
|
let timescale = reader.parse(timescale).expect("failed to parse timescale");
|
||||||
|
|
||||||
|
dbg!(date.to_rfc2822());
|
||||||
|
dbg!(version);
|
||||||
|
dbg!(timescale);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
Loading…
Reference in a new issue