From 9885125e367bbbb4f1321ecbdc1f1ba06c4130c7 Mon Sep 17 00:00:00 2001 From: Aleksey Yakovlev Date: Thu, 9 May 2024 16:30:14 +0700 Subject: [PATCH] fixed main --- src/compiler/compiler.rs | 44 ++++++++++++++++++++++++++++-------- src/compiler/rst/compiler.rs | 22 ++++++++++++++++++ src/compiler/rst/mod.rs | 6 ++++- src/compiler/rst/node.rs | 4 +++- src/main.rs | 6 ++--- 5 files changed, 67 insertions(+), 15 deletions(-) create mode 100644 src/compiler/rst/compiler.rs diff --git a/src/compiler/compiler.rs b/src/compiler/compiler.rs index f9b32b3..26699c8 100644 --- a/src/compiler/compiler.rs +++ b/src/compiler/compiler.rs @@ -1,12 +1,14 @@ -use crate::compiler::ast::{AstParser, AstParserError}; +use crate::compiler::ast::{AstNode, AstParser, AstParserError}; use crate::compiler::{Compilation, CompilerSource, HexoCompilerContext}; -use crate::compiler::cst::{CstParser, CstParserError}; +use crate::compiler::cst::{CstFile, CstParser, CstParserError}; +use crate::compiler::rst::{HexoFile, RstCompiler, RstCompilerError}; #[derive(Debug)] pub(crate) enum CompilerError { IO(std::io::Error), AST(AstParserError), CST(CstParserError), + RST(RstCompilerError), } pub(crate) struct HexoCompiler { @@ -21,20 +23,42 @@ impl HexoCompiler { } } - pub(crate) fn compile(&self, source: TSource) -> Result { + pub(crate) fn compile_ast(&self, source: &TSource) -> Result { let ast_parser = AstParser::new(); - let cst_parser = CstParser::new(); - let source_text = source.read() .map_err(|e| CompilerError::IO(e))?; - let ast = ast_parser.parse(source_text) - .map_err(|e| CompilerError::AST(e))?; + return Ok( + ast_parser.parse(source_text) + .map_err(|e| CompilerError::AST(e))? + ); + } + + pub(crate) fn compile_cst(&self, source: &TSource) -> Result { + let ast = self.compile_ast(source)?; + let cst_parser = CstParser::new(); + + return Ok( + cst_parser.parse(source.path(), ast) + .map_err(|e| CompilerError::CST(e))? + ); + } + + pub(crate) fn compile_rst(&self, source: &TSource) -> Result { + let cst = self.compile_cst(source)?; + let rst_compiler = RstCompiler::new(self); + + return Ok( + rst_compiler.compile(&cst) + .map_err(|e| CompilerError::RST(e))? + ); + } - println!("{:#?}", ast); + pub(crate) fn compile(&self, source: &TSource) -> Result { + let cst = self.compile_cst(source)?; + let rst_compiler = RstCompiler::new(self); + let rst = rst_compiler.compile(&cst); - let cst = cst_parser.parse(source.path(), ast) - .map_err(|e| CompilerError::CST(e))?; println!("{:#?}", cst); return Ok(Compilation::empty()); diff --git a/src/compiler/rst/compiler.rs b/src/compiler/rst/compiler.rs new file mode 100644 index 0000000..b640fb0 --- /dev/null +++ b/src/compiler/rst/compiler.rs @@ -0,0 +1,22 @@ +use crate::compiler::cst::CstFile; +use crate::compiler::HexoCompiler; +use crate::compiler::rst::node::HexoFile; + +#[derive(Debug)] +pub(crate) enum RstCompilerError {} + +pub(crate) struct RstCompiler<'a> { + parent: &'a HexoCompiler, +} + +impl RstCompiler<'_> { + pub(crate) fn new(parent: &HexoCompiler) -> RstCompiler { + RstCompiler { + parent: parent + } + } + + pub(crate) fn compile(&self, cst: &CstFile) -> Result { + return todo!(); + } +} \ No newline at end of file diff --git a/src/compiler/rst/mod.rs b/src/compiler/rst/mod.rs index 20e5d7f..f3647bc 100644 --- a/src/compiler/rst/mod.rs +++ b/src/compiler/rst/mod.rs @@ -1 +1,5 @@ -mod node; \ No newline at end of file +mod node; +mod compiler; + +pub(crate) use compiler::{RstCompiler, RstCompilerError}; +pub(crate) use node::*; \ No newline at end of file diff --git a/src/compiler/rst/node.rs b/src/compiler/rst/node.rs index 843d837..2dc56dd 100644 --- a/src/compiler/rst/node.rs +++ b/src/compiler/rst/node.rs @@ -1 +1,3 @@ -pub(crate) struct HexoFile {} +pub(crate) struct HexoFile { + +} diff --git a/src/main.rs b/src/main.rs index 8208e27..6de8c78 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,7 +4,7 @@ use std::io::Read; use std::path::PathBuf; use compiler::CompilerSource; -use crate::cli::run_build; +use crate::cli::{run_build, run_cli}; use crate::compiler::{FileCompilerSource, HexoCompiler, HexoCompilerContext, StringCompilerSource}; mod cli; @@ -15,7 +15,7 @@ mod resolver_legacy; mod compiler; fn main() { - let compiler = HexoCompiler::new(HexoCompilerContext::new()); + run_cli() } #[test] @@ -27,7 +27,7 @@ fn new_compiler() { PathBuf::from("sample.hexo"), ); - let compilation_result = compiler.compile(source).unwrap(); + let compilation_result = compiler.compile(&source).unwrap(); } // list files in directory test cases