diff options
author | Mx Kookie <kookie@spacekookie.de> | 2020-12-13 12:08:55 +0000 |
---|---|---|
committer | Mx Kookie <kookie@spacekookie.de> | 2020-12-13 12:08:55 +0000 |
commit | bfe67afa8a9238bd2a3bfdc9079917c81c8cfef3 (patch) | |
tree | 95def4507da8b4def421efa8b868e4d3dfe1ffb2 /apps/cassiopeia/src/format/mod.rs | |
parent | 56212d7154b5e59f663af64b4b437d6ca44d7a8a (diff) |
cassiopeia: changing parser output to more generic IR structure
This allows a few things: a, it's a persistant format that we can
mirror to disk again, AND can adapt because all type information is
known, and it allows for new entries to be added to the IR more
easily, without having to worry about exact formatting, or
re-inferring order from the TimeFile abstraction.
Diffstat (limited to 'apps/cassiopeia/src/format/mod.rs')
-rw-r--r-- | apps/cassiopeia/src/format/mod.rs | 32 |
1 files changed, 25 insertions, 7 deletions
diff --git a/apps/cassiopeia/src/format/mod.rs b/apps/cassiopeia/src/format/mod.rs index e55658d515ff..814c08656dbe 100644 --- a/apps/cassiopeia/src/format/mod.rs +++ b/apps/cassiopeia/src/format/mod.rs @@ -1,17 +1,33 @@ //! cassiopeia file format +mod ir; mod lexer; mod parser; +pub(crate) use ir::{IrItem, IrStream, IrType, MakeIr}; pub(crate) use lexer::{LineLexer, LineToken, Token}; pub(crate) use parser::LineCfg; use crate::TimeFile; use std::{fs::File, io::Read}; +#[derive(Default)] +pub struct ParseOutput { + pub(crate) ir: IrStream, + pub(crate) tf: TimeFile, +} + +impl ParseOutput { + fn append(mut self, ir: IrItem) -> Self { + self.tf.append(ir.clone()); + self.ir.push(ir); + self + } +} + /// Load a file from disk and parse it into a /// [`TimeFile`](crate::TimeFile) -pub fn load_file(path: &str) -> Option<TimeFile> { +pub fn load_file(path: &str) -> Option<ParseOutput> { let mut f = File::open(path).ok()?; let mut content = String::new(); f.read_to_string(&mut content).ok()?; @@ -19,11 +35,13 @@ pub fn load_file(path: &str) -> Option<TimeFile> { let mut lines: Vec<String> = content.split("\n").map(|l| l.to_owned()).collect(); Some( - lines - .iter_mut() - .map(|line| lexer::lex(line)) - .map(|lex| parser::parse(lex)) - .filter(|line| line.valid()) - .fold(TimeFile::default(), |file, line| file.append(line)), + ir::generate_ir( + lines + .iter_mut() + .map(|line| lexer::lex(line)) + .map(|lex| parser::parse(lex)), + ) + .into_iter() + .fold(ParseOutput::default(), |output, ir| output.append(ir)), ) } |