You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
67 lines
1.6 KiB
67 lines
1.6 KiB
//! cassiopeia file format
|
|
|
|
mod gen;
|
|
pub(crate) mod ir;
|
|
mod lexer;
|
|
mod parser;
|
|
|
|
pub(crate) use lexer::{LineLexer, LineToken, Token};
|
|
pub(crate) use parser::LineCfg;
|
|
|
|
use crate::TimeFile;
|
|
use ir::{IrItem, IrStream};
|
|
use std::{
|
|
fs::{File, OpenOptions},
|
|
io::{Read, Write},
|
|
};
|
|
|
|
#[derive(Default)]
|
|
pub(crate) 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(crate) 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()?;
|
|
|
|
let mut lines: Vec<String> = content.split("\n").map(|l| l.to_owned()).collect();
|
|
|
|
Some(
|
|
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)),
|
|
)
|
|
}
|
|
|
|
/// Write a file with the updated IR stream
|
|
pub(crate) fn write_file(path: &str, ir: &mut IrStream) -> Option<()> {
|
|
ir::update_header(ir);
|
|
let mut lines = ir.into_iter().map(|ir| gen::line(ir)).collect::<Vec<_>>();
|
|
lines.insert(0, gen::head_comment());
|
|
|
|
let mut f = OpenOptions::new()
|
|
.write(true)
|
|
.create(true)
|
|
.truncate(true)
|
|
.open(path)
|
|
.ok()?;
|
|
f.write_all(lines.join("\n").as_bytes()).ok()?;
|
|
Some(())
|
|
}
|
|
|