From 74aa701fd585651edd8694b8d411af11ef2542f5 Mon Sep 17 00:00:00 2001 From: Aleksey Yakovlev Date: Mon, 13 May 2024 16:00:31 +0700 Subject: [PATCH] reformat --- sample.hexo | 6 +- src/cli.rs | 21 +- src/compiler/ast/mod.rs | 4 +- src/compiler/ast/node.rs | 7 +- src/compiler/ast/parser.rs | 28 +-- src/compiler/compilation_result.rs | 9 +- src/compiler/compiler.rs | 54 ++--- src/compiler/compiler_context.rs | 5 +- src/compiler/compiler_source.rs | 13 +- src/compiler/cst/mod.rs | 2 +- src/compiler/cst/node.rs | 9 +- src/compiler/cst/parser.rs | 258 +++++++++++++----------- src/compiler/mod.rs | 20 +- src/compiler/rst/compilation_context.rs | 11 +- src/compiler/rst/compiler.rs | 73 ++++--- src/compiler/rst/mod.rs | 6 +- src/compiler/rst/node.rs | 2 +- src/compiler/source_finder.rs | 17 +- src/compiler/util/byte_buffer.rs | 9 +- src/compiler/util/encoding.rs | 2 +- src/compiler/util/mod.rs | 2 +- src/cst_legacy.rs | 29 +-- src/main.rs | 17 +- 23 files changed, 307 insertions(+), 297 deletions(-) diff --git a/sample.hexo b/sample.hexo index f0a0261..f3a6452 100644 --- a/sample.hexo +++ b/sample.hexo @@ -1,3 +1,5 @@ -$ hello 'world' +$ hello 'hello' +$ world 'world' +$ hello_world $hello $world -> $hello \ No newline at end of file +> $hello_world \ No newline at end of file diff --git a/src/cli.rs b/src/cli.rs index 768e44f..aadb48f 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -12,10 +12,10 @@ use notify::EventKind::Modify; use notify::{Event, RecursiveMode, Watcher}; use pest::Parser as PestParser; -use crate::resolver_legacy::resolve_cst; -use crate::{cst_legacy, render_legacy}; use crate::compiler::ast::{AstParser, AstParserError}; use crate::render_legacy::RenderError; +use crate::resolver_legacy::resolve_cst; +use crate::{cst_legacy, render_legacy}; #[derive(Parser)] #[command(version, about, long_about = None)] @@ -61,7 +61,7 @@ pub(crate) fn run_cli() { let cli = Cli::parse(); let cli_result: Result<_, CliError> = match cli.command { - None => { Err(CliError::UnknownCommand) } + None => Err(CliError::UnknownCommand), Some(Commands::Watch { source, output }) => run_watch(source, output), Some(Commands::Build { source, output }) => run_build(source, output), }; @@ -88,8 +88,7 @@ fn handle_cli_error(cli_result: Result<(), CliError>) { fn handle_render_error(error: RenderError) { match error { - RenderError::UnresolvedAtom { atom } => - eprintln!("unresolved atom: {:?}", atom) + RenderError::UnresolvedAtom { atom } => eprintln!("unresolved atom: {:?}", atom), } } @@ -102,9 +101,10 @@ fn run_watch(source: String, output: Option) -> Result<(), CliError> { let source_path_clone = source.clone(); let source_path = source_path_clone.as_ref(); - let mut watcher = notify::recommended_watcher( - move |event: Result| run_watch_loop(source.clone(), output.clone(), event) - ).map_err(|err| CliError::CantCreateWatcher(err))?; + let mut watcher = notify::recommended_watcher(move |event: Result| { + run_watch_loop(source.clone(), output.clone(), event) + }) + .map_err(|err| CliError::CantCreateWatcher(err))?; watcher .watch(source_path, RecursiveMode::NonRecursive) @@ -149,9 +149,6 @@ pub(crate) fn run_build(source: String, output: Option) -> Result<(), Cl let output_file_path = output.unwrap_or(format!("{}.bin", source)); File::create(output_file_path) .map_err(|err| CliError::CantCrateOutputFile(err))? - .write_all( - &render_legacy::render_cst(resolved_cst) - .map_err(CliError::Rendering)?, - ) + .write_all(&render_legacy::render_cst(resolved_cst).map_err(CliError::Rendering)?) .map_err(|err| CliError::CantCrateOutputFile(err)) } diff --git a/src/compiler/ast/mod.rs b/src/compiler/ast/mod.rs index 1492dcf..7dbee1f 100644 --- a/src/compiler/ast/mod.rs +++ b/src/compiler/ast/mod.rs @@ -1,5 +1,5 @@ mod node; mod parser; -pub(crate) use node::{AstNodeType, AstNode}; -pub(crate) use parser::{AstParser, AstParserError}; \ No newline at end of file +pub(crate) use node::{AstNode, AstNodeType}; +pub(crate) use parser::{AstParser, AstParserError}; diff --git a/src/compiler/ast/node.rs b/src/compiler/ast/node.rs index fe737b6..abebdf0 100644 --- a/src/compiler/ast/node.rs +++ b/src/compiler/ast/node.rs @@ -25,7 +25,6 @@ pub(crate) enum AstNodeType { } impl AstNodeType { - pub(crate) fn must_capture_value(&self) -> bool { match self { AstNodeType::AtomUtf8 @@ -49,7 +48,11 @@ pub(crate) struct AstNode { } impl AstNode { - pub(crate) fn new(node_type: AstNodeType, content: Option, children: Vec) -> Self { + pub(crate) fn new( + node_type: AstNodeType, + content: Option, + children: Vec, + ) -> Self { AstNode { node_type, content, diff --git a/src/compiler/ast/parser.rs b/src/compiler/ast/parser.rs index 4056ab2..6449019 100644 --- a/src/compiler/ast/parser.rs +++ b/src/compiler/ast/parser.rs @@ -26,7 +26,8 @@ impl AstParser { let pairs = AstPestParser::parse(Rule::file, source.as_str()) .map_err(|error| AstParserError::PestError(error))?; - let children: Result, _> = pairs.map(parse_ast_pair) + let children: Result, _> = pairs + .map(parse_ast_pair) .filter_map(filter_ignored_token) .collect(); @@ -34,11 +35,13 @@ impl AstParser { } } -fn filter_ignored_token(result: Result, AstParserError>) -> Option> { +fn filter_ignored_token( + result: Result, AstParserError>, +) -> Option> { match result { Ok(None) => None, Ok(Some(value)) => Some(Ok(value)), - Err(error) => Some(Err(error)) + Err(error) => Some(Err(error)), } } @@ -66,23 +69,22 @@ fn parse_ast_pair(p: Pair) -> Result, AstParserError> { Rule::emit_statement => AstNodeType::StatementEmit, Rule::EOI => return Ok(None), - _ => return Err(AstParserError::UnknownRule { - rule_name: format!("{:?}", p.as_rule()), - }), + _ => { + return Err(AstParserError::UnknownRule { + rule_name: format!("{:?}", p.as_rule()), + }) + } }; let node_value = node_type .must_capture_value() .then(|| p.as_str().to_string()); - let children: Result, _> = p.into_inner() + let children: Result, _> = p + .into_inner() .map(parse_ast_pair) .filter_map(filter_ignored_token) .collect(); - Ok( - Some( - AstNode::new(node_type, node_value, children?) - ) - ) -} \ No newline at end of file + Ok(Some(AstNode::new(node_type, node_value, children?))) +} diff --git a/src/compiler/compilation_result.rs b/src/compiler/compilation_result.rs index 790597b..a5ab358 100644 --- a/src/compiler/compilation_result.rs +++ b/src/compiler/compilation_result.rs @@ -1,12 +1,9 @@ pub(crate) struct Compilation { - pub(crate) content: Vec + pub(crate) content: Vec, } impl Compilation { - pub(crate) fn from(content: Vec) -> Self { - Compilation { - content - } + Compilation { content } } -} \ No newline at end of file +} diff --git a/src/compiler/compiler.rs b/src/compiler/compiler.rs index 495dfda..71f32ff 100644 --- a/src/compiler/compiler.rs +++ b/src/compiler/compiler.rs @@ -1,7 +1,7 @@ use crate::compiler::ast::{AstNode, AstParser, AstParserError}; -use crate::compiler::{Compilation, CompilerSource, HexoCompilerContext}; use crate::compiler::cst::{CstFile, CstParser, CstParserError}; use crate::compiler::rst::{HexoFile, RstCompiler, RstCompilerError}; +use crate::compiler::{Compilation, CompilerSource, HexoCompilerContext}; #[derive(Debug)] pub(crate) enum CompilerError { @@ -16,48 +16,54 @@ pub(crate) struct HexoCompiler { } impl HexoCompiler { - pub(crate) fn new(context: HexoCompilerContext) -> Self { - HexoCompiler { - context: context, - } + HexoCompiler { context: context } } - pub(crate) fn compile_ast(&self, source: &TSource) -> Result { + pub(crate) fn compile_ast( + &self, + source: &TSource, + ) -> Result { let ast_parser = AstParser::new(); - let source_text = source.read() - .map_err(|e| CompilerError::IO(e))?; + let source_text = source.read().map_err(|e| CompilerError::IO(e))?; - return Ok( - 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 { + 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))? - ); + return Ok(cst_parser + .parse(source.path(), ast) + .map_err(|e| CompilerError::CST(e))?); } - pub(crate) fn compile_rst(&self, source: &TSource) -> Result { + 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))? - ); + return Ok(rst_compiler + .compile(&cst) + .map_err(|e| CompilerError::RST(e))?); } - pub(crate) fn compile(&self, source: &TSource) -> Result { + 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 rst = rst_compiler + .compile(&cst) .map_err(|e| CompilerError::RST(e))?; return Ok(Compilation::from(rst.emits.as_vec())); diff --git a/src/compiler/compiler_context.rs b/src/compiler/compiler_context.rs index 9109dac..5e12f81 100644 --- a/src/compiler/compiler_context.rs +++ b/src/compiler/compiler_context.rs @@ -1,14 +1,13 @@ use std::path::PathBuf; pub(crate) struct HexoCompilerContext { - pub(crate) root_dir : PathBuf + pub(crate) root_dir: PathBuf, } impl HexoCompilerContext { - pub(crate) fn new() -> Self { HexoCompilerContext { - root_dir: PathBuf::from(".") + root_dir: PathBuf::from("."), } } } diff --git a/src/compiler/compiler_source.rs b/src/compiler/compiler_source.rs index 2ddda4d..50c230e 100644 --- a/src/compiler/compiler_source.rs +++ b/src/compiler/compiler_source.rs @@ -1,7 +1,7 @@ +use clap::builder::Str; use std::fs::File; use std::io::Read; use std::path::{Path, PathBuf}; -use clap::builder::Str; pub(crate) trait CompilerSource { fn read(&self) -> Result; @@ -15,7 +15,6 @@ pub(crate) struct StringCompilerSource { } impl StringCompilerSource { - pub(crate) fn new(path: PathBuf, text: &str) -> StringCompilerSource { return StringCompilerSource { content: text.to_string(), @@ -26,7 +25,7 @@ impl StringCompilerSource { impl CompilerSource for StringCompilerSource { fn read(&self) -> Result { - return Ok(self.content.clone()) + return Ok(self.content.clone()); } fn path(&self) -> PathBuf { @@ -39,16 +38,12 @@ pub(crate) struct FileCompilerSource { } impl FileCompilerSource { - pub(crate) fn new(path: PathBuf) -> FileCompilerSource { - return FileCompilerSource { - path: path, - }; + return FileCompilerSource { path: path }; } } impl CompilerSource for FileCompilerSource { - fn read(&self) -> Result { let mut p = File::open(self.path.clone())?; let mut buff = String::new(); @@ -60,4 +55,4 @@ impl CompilerSource for FileCompilerSource { fn path(&self) -> PathBuf { return self.path.clone(); } -} \ No newline at end of file +} diff --git a/src/compiler/cst/mod.rs b/src/compiler/cst/mod.rs index 683e2d4..cad8429 100644 --- a/src/compiler/cst/mod.rs +++ b/src/compiler/cst/mod.rs @@ -2,4 +2,4 @@ mod node; mod parser; pub(crate) use node::*; -pub(crate) use parser::*; \ No newline at end of file +pub(crate) use parser::*; diff --git a/src/compiler/cst/node.rs b/src/compiler/cst/node.rs index 6b6f734..52dbb69 100644 --- a/src/compiler/cst/node.rs +++ b/src/compiler/cst/node.rs @@ -11,8 +11,13 @@ pub(crate) enum CstAtom { Hex(u8), String(String), Number(u32), - Constant { name: String }, - Function { name: String, params: Vec }, + Constant { + name: String, + }, + Function { + name: String, + params: Vec, + }, } #[derive(Debug)] diff --git a/src/compiler/cst/parser.rs b/src/compiler/cst/parser.rs index 5659611..97ef288 100644 --- a/src/compiler/cst/parser.rs +++ b/src/compiler/cst/parser.rs @@ -1,17 +1,29 @@ use std::path::PathBuf; use crate::compiler::ast::{AstNode, AstNodeType}; -use crate::compiler::cst::{CstActualParameter, CstAtom, CstConstantStatement, CstEmitStatement, CstFunctionStatement}; use crate::compiler::cst::node::CstFile; +use crate::compiler::cst::{ + CstActualParameter, CstAtom, CstConstantStatement, CstEmitStatement, CstFunctionStatement, +}; use crate::compiler::util::encoding::decode_bytes_from_string; use crate::encoding_legacy; #[derive(Debug)] pub(crate) enum CstParserError { - UnexpectedNode { expected: Vec, actual: AstNodeType }, - MalformedNodeValue { message: String }, - MissingContent { node_type: AstNodeType }, - UnexpectedChildren { node_type: AstNodeType, children: Vec }, + UnexpectedNode { + expected: Vec, + actual: AstNodeType, + }, + MalformedNodeValue { + message: String, + }, + MissingContent { + node_type: AstNodeType, + }, + UnexpectedChildren { + node_type: AstNodeType, + children: Vec, + }, DuplicateNode, } @@ -22,7 +34,11 @@ impl CstParser { CstParser {} } - pub(crate) fn parse(&self, path: PathBuf, ast_root: AstNode) -> Result { + pub(crate) fn parse( + &self, + path: PathBuf, + ast_root: AstNode, + ) -> Result { return parse_file(path, &ast_root); } } @@ -33,23 +49,22 @@ fn parse_file(path: PathBuf, node: &AstNode) -> Result guard_node_type(node, AstNodeType::File)?; let (emits, functions, constants) = parse_function_body(node)?; - return Ok( - CstFile { - path: path, - main: CstFunctionStatement { - name: MAIN_FUNCTION_NAME.to_string(), - params: Vec::new(), - emits, - functions, - constants, - }, - }); + return Ok(CstFile { + path: path, + main: CstFunctionStatement { + name: MAIN_FUNCTION_NAME.to_string(), + params: Vec::new(), + emits, + functions, + constants, + }, + }); } type BodyParsingResult = ( Vec, Vec, - Vec + Vec, ); fn parse_function_body(node: &AstNode) -> Result { @@ -62,14 +77,16 @@ fn parse_function_body(node: &AstNode) -> Result constants.push(parse_constant(child)?), AstNodeType::StatementEmit => emits.push(parse_emit_statement(child)?), AstNodeType::StatementFn => functions.push(parse_function(child)?), - _ => return Err(CstParserError::UnexpectedNode { - actual: child.node_type, - expected: vec![ - AstNodeType::StatementConst, - AstNodeType::StatementEmit, - AstNodeType::StatementFn, - ], - }), + _ => { + return Err(CstParserError::UnexpectedNode { + actual: child.node_type, + expected: vec![ + AstNodeType::StatementConst, + AstNodeType::StatementEmit, + AstNodeType::StatementFn, + ], + }) + } } } @@ -90,12 +107,14 @@ fn parse_constant(node: &AstNode) -> Result Result { @@ -112,26 +131,22 @@ fn parse_function(node: &AstNode) -> Result return Err( - CstParserError::UnexpectedNode { + _ => { + return Err(CstParserError::UnexpectedNode { actual: child.node_type, - expected: vec![ - AstNodeType::StatementFnName, - AstNodeType::StatementFnBody, - ], - }), + expected: vec![AstNodeType::StatementFnName, AstNodeType::StatementFnBody], + }) + } } } - return Ok( - CstFunctionStatement { - name: parse_value_of(&node.children[0])?, - params: Vec::new(), - emits: emits.unwrap_or(Vec::new()), - functions: functions.unwrap_or(Vec::new()), - constants: constants.unwrap_or(Vec::new()), - } - ); + return Ok(CstFunctionStatement { + name: parse_value_of(&node.children[0])?, + params: Vec::new(), + emits: emits.unwrap_or(Vec::new()), + functions: functions.unwrap_or(Vec::new()), + constants: constants.unwrap_or(Vec::new()), + }); } fn parse_emit_statement(node: &AstNode) -> Result { @@ -142,11 +157,7 @@ fn parse_emit_statement(node: &AstNode) -> Result) -> Result<(), CstParserError> { @@ -156,16 +167,18 @@ fn parse_atom_into(node: &AstNode, buff: &mut Vec) -> Result<(), CstPar AstNodeType::AtomBaseNumber => parse_atom_base_num_into(node, buff)?, AstNodeType::AtomConst => parse_atom_constant_into(node, buff)?, AstNodeType::AtomFn => parse_atom_function_into(node, buff)?, - _ => return Err(CstParserError::UnexpectedNode { - actual: node.node_type, - expected: vec![ - AstNodeType::AtomHex, - AstNodeType::AtomUtf8, - AstNodeType::AtomBaseNumber, - AstNodeType::AtomConst, - AstNodeType::AtomFn, - ], - }) + _ => { + return Err(CstParserError::UnexpectedNode { + actual: node.node_type, + expected: vec![ + AstNodeType::AtomHex, + AstNodeType::AtomUtf8, + AstNodeType::AtomBaseNumber, + AstNodeType::AtomConst, + AstNodeType::AtomFn, + ], + }) + } } Ok(()) @@ -192,27 +205,23 @@ fn parse_atom_function_into(node: &AstNode, buf: &mut Vec) -> Result<() guard_empty(name)?; name = Some(parse_value_of(child)?); } - AstNodeType::AtomFnParams => { - params = Some(parse_atom_fn_params(child)?) + AstNodeType::AtomFnParams => params = Some(parse_atom_fn_params(child)?), + _ => { + return Err(CstParserError::UnexpectedNode { + actual: child.node_type, + expected: vec![AstNodeType::AtomFnName, AstNodeType::AtomFnParams], + }) } - _ => return Err(CstParserError::UnexpectedNode { - actual: child.node_type, - expected: vec![ - AstNodeType::AtomFnName, - AstNodeType::AtomFnParams, - ], - }) } } + let name_value = name.ok_or(CstParserError::MissingContent { + node_type: AstNodeType::AtomFnName, + })?; - let name_value = name.ok_or( - CstParserError::MissingContent { node_type: AstNodeType::AtomFnName } - )?; - - let params_value = params.ok_or( - CstParserError::MissingContent { node_type: AstNodeType::AtomFnParams } - )?; + let params_value = params.ok_or(CstParserError::MissingContent { + node_type: AstNodeType::AtomFnParams, + })?; buf.push(CstAtom::Function { name: name_value, @@ -236,12 +245,10 @@ fn parse_atom_fn_params(node: &AstNode) -> Result, CstPa parse_atom_into(p_child, &mut value)?; } - buf.push( - CstActualParameter { - name: param_counter.to_string(), - value: value, - } - ); + buf.push(CstActualParameter { + name: param_counter.to_string(), + value: value, + }); param_counter += 1; } @@ -251,12 +258,13 @@ fn parse_atom_fn_params(node: &AstNode) -> Result, CstPa fn parse_atom_hex_into(node: &AstNode, buf: &mut Vec) -> Result<(), CstParserError> { guard_node_type(node, AstNodeType::AtomHex)?; - let content = node.clone().content - .ok_or(CstParserError::MissingContent { node_type: AstNodeType::AtomHex })?; + let content = node.clone().content.ok_or(CstParserError::MissingContent { + node_type: AstNodeType::AtomHex, + })?; - let bytes = decode_bytes_from_string(&content) - .map_err(|x| CstParserError::MalformedNodeValue { - message: format!("can't parse bytes {}", content) + let bytes = + decode_bytes_from_string(&content).map_err(|x| CstParserError::MalformedNodeValue { + message: format!("can't parse bytes {}", content), })?; for byte in bytes { @@ -268,8 +276,9 @@ fn parse_atom_hex_into(node: &AstNode, buf: &mut Vec) -> Result<(), Cst fn parse_atom_utf8_into(node: &AstNode, buf: &mut Vec) -> Result<(), CstParserError> { guard_node_type(node, AstNodeType::AtomUtf8)?; - let content = node.clone().content - .ok_or(CstParserError::MissingContent { node_type: AstNodeType::AtomUtf8 })?; + let content = node.clone().content.ok_or(CstParserError::MissingContent { + node_type: AstNodeType::AtomUtf8, + })?; buf.push(CstAtom::String(content)); @@ -291,32 +300,38 @@ fn parse_atom_base_num_into(node: &AstNode, buf: &mut Vec) -> Result<() guard_empty(value)?; value = Some(parse_value_of(child)?) } - _ => return Err(CstParserError::UnexpectedNode { - actual: child.node_type, - expected: vec![ - AstNodeType::AtomBaseNumberBase, - AstNodeType::AtomBaseNumberValue, - ], - }) + _ => { + return Err(CstParserError::UnexpectedNode { + actual: child.node_type, + expected: vec![ + AstNodeType::AtomBaseNumberBase, + AstNodeType::AtomBaseNumberValue, + ], + }) + } } } - let base_value_str = base.ok_or(CstParserError::MissingContent { node_type: AstNodeType::AtomBaseNumberBase })?; - let base_value = u32::from_str_radix(base_value_str.as_str(), 10) - .map_err(|_| CstParserError::MalformedNodeValue { - message: format!("can't parse base {}", base_value_str) - })?; + let base_value_str = base.ok_or(CstParserError::MissingContent { + node_type: AstNodeType::AtomBaseNumberBase, + })?; + let base_value = u32::from_str_radix(base_value_str.as_str(), 10).map_err(|_| { + CstParserError::MalformedNodeValue { + message: format!("can't parse base {}", base_value_str), + } + })?; - let value_str = value.ok_or(CstParserError::MissingContent { node_type: AstNodeType::AtomBaseNumberValue })?; + let value_str = value.ok_or(CstParserError::MissingContent { + node_type: AstNodeType::AtomBaseNumberValue, + })?; - buf.push( - CstAtom::Number( - u32::from_str_radix(value_str.as_str(), base_value) - .map_err(|_| CstParserError::MalformedNodeValue { - message: format!("can't parse number {}", value_str) - })? - ) - ); + buf.push(CstAtom::Number( + u32::from_str_radix(value_str.as_str(), base_value).map_err(|_| { + CstParserError::MalformedNodeValue { + message: format!("can't parse number {}", value_str), + } + })?, + )); return Ok(()); } @@ -329,18 +344,17 @@ fn parse_value_of(node: &AstNode) -> Result { }); } - return node.clone().content - .ok_or(CstParserError::MissingContent { node_type: node.node_type }); + return node.clone().content.ok_or(CstParserError::MissingContent { + node_type: node.node_type, + }); } fn guard_node_type(node: &AstNode, expected_type: AstNodeType) -> Result<(), CstParserError> { if node.node_type != expected_type { - return Err( - CstParserError::UnexpectedNode { - actual: node.node_type, - expected: vec![expected_type], - } - ); + return Err(CstParserError::UnexpectedNode { + actual: node.node_type, + expected: vec![expected_type], + }); } return Ok(()); @@ -352,4 +366,4 @@ fn guard_empty(option: Option) -> Result<(), CstParserError> { } return Ok(()); -} \ No newline at end of file +} diff --git a/src/compiler/mod.rs b/src/compiler/mod.rs index 9b71cef..e351d4a 100644 --- a/src/compiler/mod.rs +++ b/src/compiler/mod.rs @@ -1,19 +1,15 @@ -mod compiler_context; +pub mod ast; +mod compilation_result; mod compiler; +mod compiler_context; mod compiler_source; -mod compilation_result; -mod source_finder; mod cst; mod rst; +mod source_finder; mod util; -pub mod ast; -pub(crate) use compiler_context::HexoCompilerContext; -pub(crate) use compiler::{HexoCompiler, CompilerError}; -pub(crate) use compiler_source::{CompilerSource, StringCompilerSource, FileCompilerSource}; pub(crate) use compilation_result::Compilation; -pub(crate) use source_finder::{SourceFinder, FileSourceFinder}; - - - - +pub(crate) use compiler::{CompilerError, HexoCompiler}; +pub(crate) use compiler_context::HexoCompilerContext; +pub(crate) use compiler_source::{CompilerSource, FileCompilerSource, StringCompilerSource}; +pub(crate) use source_finder::{FileSourceFinder, SourceFinder}; diff --git a/src/compiler/rst/compilation_context.rs b/src/compiler/rst/compilation_context.rs index f23febb..d692144 100644 --- a/src/compiler/rst/compilation_context.rs +++ b/src/compiler/rst/compilation_context.rs @@ -1,12 +1,12 @@ +use crate::compiler::cst::CstFile; use std::collections::HashMap; use std::path::PathBuf; -use crate::compiler::cst::CstFile; use crate::compiler::util::ByteBuffer; pub(crate) struct ConstantBinding { pub(crate) name: String, - pub(crate) byte_buffer: ByteBuffer + pub(crate) byte_buffer: ByteBuffer, } pub(crate) struct CompilationContext { @@ -23,10 +23,7 @@ impl CompilationContext { } pub(crate) fn bind_constant(&mut self, constant: ConstantBinding) { - self.constant_table.insert( - constant.name.clone(), - constant, - ); + self.constant_table.insert(constant.name.clone(), constant); } pub(crate) fn has_constant(&self, name: &String) -> bool { @@ -36,4 +33,4 @@ impl CompilationContext { pub(crate) fn get_constant(&self, name: &String) -> Option<&ConstantBinding> { return self.constant_table.get(name); } -} \ No newline at end of file +} diff --git a/src/compiler/rst/compiler.rs b/src/compiler/rst/compiler.rs index ec6447f..f226d2c 100644 --- a/src/compiler/rst/compiler.rs +++ b/src/compiler/rst/compiler.rs @@ -1,10 +1,10 @@ use std::path::PathBuf; -use clap::builder::Str; + use crate::compiler::cst::{CstAtom, CstAtomVec, CstEmitStatement, CstFile, CstFunctionStatement}; -use crate::compiler::HexoCompiler; use crate::compiler::rst::compilation_context::{CompilationContext, ConstantBinding}; use crate::compiler::rst::node::HexoFile; use crate::compiler::util::ByteBuffer; +use crate::compiler::HexoCompiler; #[derive(Debug)] pub(crate) enum RstCompilerError { @@ -17,9 +17,7 @@ pub(crate) struct RstCompiler<'a> { impl RstCompiler<'_> { pub(crate) fn new(parent: &HexoCompiler) -> RstCompiler { - RstCompiler { - parent: parent - } + RstCompiler { parent: parent } } pub(crate) fn compile(&self, cst: &CstFile) -> Result { @@ -27,16 +25,17 @@ impl RstCompiler<'_> { let bb = Self::build_bytes(&context, &cst.main.emits)?; - return Ok( - HexoFile { - path: cst.path.clone(), - context: context, - emits: bb, - } - ); + return Ok(HexoFile { + path: cst.path.clone(), + context: context, + emits: bb, + }); } - fn build_bytes(context: &CompilationContext, emits: &Vec) -> Result { + fn build_bytes( + context: &CompilationContext, + emits: &Vec, + ) -> Result { let mut byte_buffer = ByteBuffer::new(); for emit in emits { @@ -46,17 +45,17 @@ impl RstCompiler<'_> { return Ok(byte_buffer); } - fn build_bytes_into(context: &CompilationContext, atoms: &CstAtomVec, buffer: &mut ByteBuffer) -> Result<(), RstCompilerError> { + fn build_bytes_into( + context: &CompilationContext, + atoms: &CstAtomVec, + buffer: &mut ByteBuffer, + ) -> Result<(), RstCompilerError> { for atom in atoms { match atom { - CstAtom::Hex(byte) => - buffer.push_byte(*byte), - CstAtom::String(string) => - buffer.push_string(string.clone()), - CstAtom::Number(number) => - buffer.push_u32_shrunk(*number), - CstAtom::Constant { name } => - { Self::build_constant_into(context, &name, buffer)? } + CstAtom::Hex(byte) => buffer.push_byte(*byte), + CstAtom::String(string) => buffer.push_string(string.clone()), + CstAtom::Number(number) => buffer.push_u32_shrunk(*number), + CstAtom::Constant { name } => Self::build_constant_into(context, &name, buffer)?, CstAtom::Function { .. } => {} } } @@ -64,8 +63,13 @@ impl RstCompiler<'_> { Ok(()) } - fn build_constant_into(context: &CompilationContext, name: &String, buffer: &mut ByteBuffer) -> Result<(), RstCompilerError> { - let constant_binding = context.get_constant(name) + fn build_constant_into( + context: &CompilationContext, + name: &String, + buffer: &mut ByteBuffer, + ) -> Result<(), RstCompilerError> { + let constant_binding = context + .get_constant(name) .ok_or(RstCompilerError::UnresolvedConstant { name: name.clone() })?; buffer.push_byte_buffer(&constant_binding.byte_buffer); @@ -73,7 +77,10 @@ impl RstCompiler<'_> { Ok(()) } - fn build_context(file_path: &PathBuf, cst: &CstFunctionStatement) -> Result { + fn build_context( + file_path: &PathBuf, + cst: &CstFunctionStatement, + ) -> Result { let mut root_context = CompilationContext::new(file_path); Self::build_context_constants(&cst, &mut root_context)?; @@ -81,19 +88,19 @@ impl RstCompiler<'_> { return Ok(root_context); } - fn build_context_constants(cst: &&CstFunctionStatement, root_context: &mut CompilationContext) -> Result<(), RstCompilerError> { + fn build_context_constants( + cst: &&CstFunctionStatement, + root_context: &mut CompilationContext, + ) -> Result<(), RstCompilerError> { for constant in &cst.constants { let mut buff = ByteBuffer::new(); Self::build_bytes_into(&root_context, &constant.atoms, &mut buff)?; - root_context.bind_constant( - ConstantBinding { - name: constant.name.clone(), - byte_buffer: buff, - } - ) + root_context.bind_constant(ConstantBinding { + name: constant.name.clone(), + byte_buffer: buff, + }) } Ok(()) } } - diff --git a/src/compiler/rst/mod.rs b/src/compiler/rst/mod.rs index 596baee..5f5158d 100644 --- a/src/compiler/rst/mod.rs +++ b/src/compiler/rst/mod.rs @@ -1,6 +1,6 @@ -mod node; -mod compiler; mod compilation_context; +mod compiler; +mod node; pub(crate) use compiler::{RstCompiler, RstCompilerError}; -pub(crate) use node::*; \ No newline at end of file +pub(crate) use node::*; diff --git a/src/compiler/rst/node.rs b/src/compiler/rst/node.rs index 3258418..185c983 100644 --- a/src/compiler/rst/node.rs +++ b/src/compiler/rst/node.rs @@ -1,6 +1,6 @@ -use std::path::PathBuf; use crate::compiler::rst::compilation_context::CompilationContext; use crate::compiler::util::ByteBuffer; +use std::path::PathBuf; pub(crate) struct HexoFile { pub(crate) path: PathBuf, diff --git a/src/compiler/source_finder.rs b/src/compiler/source_finder.rs index ae4eeba..a150244 100644 --- a/src/compiler/source_finder.rs +++ b/src/compiler/source_finder.rs @@ -1,32 +1,25 @@ -use std::path::PathBuf; use crate::compiler::{CompilerSource, FileCompilerSource, StringCompilerSource}; +use std::path::PathBuf; pub(crate) trait SourceFinder { - fn find(&self, path: PathBuf) -> Option; } pub(crate) struct FileSourceFinder { - root_dir: PathBuf + root_dir: PathBuf, } impl SourceFinder for FileSourceFinder { - fn find(&self, path: PathBuf) -> Option { let path = self.root_dir.join(path); let source = FileCompilerSource::new(path); - Some( - source - ) + Some(source) } } impl FileSourceFinder { - pub(crate) fn new(root_dir: PathBuf) -> FileSourceFinder { - return FileSourceFinder { - root_dir: root_dir - }; + return FileSourceFinder { root_dir: root_dir }; } -} \ No newline at end of file +} diff --git a/src/compiler/util/byte_buffer.rs b/src/compiler/util/byte_buffer.rs index 58b1652..8dbd35f 100644 --- a/src/compiler/util/byte_buffer.rs +++ b/src/compiler/util/byte_buffer.rs @@ -6,9 +6,7 @@ pub(crate) struct ByteBuffer { impl ByteBuffer { pub(crate) fn new() -> Self { - ByteBuffer { - inner: Vec::new() - } + ByteBuffer { inner: Vec::new() } } pub(crate) fn push_byte(&mut self, byte: u8) { @@ -70,9 +68,6 @@ mod test { assert_eq!(buffer.len(), 4); - assert_eq!( - buffer.as_vec(), - vec![0, 0, 0, 13] - ); + assert_eq!(buffer.as_vec(), vec![0, 0, 0, 13]); } } diff --git a/src/compiler/util/encoding.rs b/src/compiler/util/encoding.rs index e6103ce..8706f48 100644 --- a/src/compiler/util/encoding.rs +++ b/src/compiler/util/encoding.rs @@ -18,4 +18,4 @@ pub(crate) fn to_shrunk_bytes(value: u32) -> Vec { value >>= 8; } bytes -} \ No newline at end of file +} diff --git a/src/compiler/util/mod.rs b/src/compiler/util/mod.rs index bccaf8d..93fc421 100644 --- a/src/compiler/util/mod.rs +++ b/src/compiler/util/mod.rs @@ -1,4 +1,4 @@ mod byte_buffer; pub(crate) mod encoding; -pub(crate) use byte_buffer::ByteBuffer; \ No newline at end of file +pub(crate) use byte_buffer::ByteBuffer; diff --git a/src/cst_legacy.rs b/src/cst_legacy.rs index 0d776ec..5adbb13 100644 --- a/src/cst_legacy.rs +++ b/src/cst_legacy.rs @@ -1,8 +1,8 @@ -use std::path::PathBuf; use crate::compiler::ast::{AstNode, AstNodeType}; use crate::cst_legacy::CstParseError::NodeValueMissing; use crate::encoding_legacy; use crate::encoding_legacy::to_shrunk_bytes; +use std::path::PathBuf; #[derive(Debug, Clone)] pub(crate) struct CstAtomStrip(Vec); @@ -60,7 +60,7 @@ impl CstAtomStrip { } impl FromIterator for CstAtomStrip { - fn from_iter>(iter: T) -> Self { + fn from_iter>(iter: T) -> Self { CstAtomStrip(iter.into_iter().collect()) } } @@ -171,16 +171,14 @@ pub(crate) enum CstParseError { pub(crate) fn parse_cst(ast_node: AstNode) -> Result { assert_eq!(ast_node.node_type, AstNodeType::File); - return Ok( - CstFile { - file_name: PathBuf::from("unknown"), // TODO remove unknown - statements: ast_node - .children - .into_iter() - .map(parse_cst_statement) - .collect(), - } - ); + return Ok(CstFile { + file_name: PathBuf::from("unknown"), // TODO remove unknown + statements: ast_node + .children + .into_iter() + .map(parse_cst_statement) + .collect(), + }); } fn parse_cst_statement(ast_node: AstNode) -> CstStatement { @@ -231,8 +229,11 @@ fn parse_cst_statement(ast_node: AstNode) -> CstStatement { fn lookup_value(node_type: AstNodeType, in_node: &AstNode) -> Result { for child in &in_node.children { if child.node_type == node_type { - return as Clone>::clone(&child.content) - .ok_or(CstParseError::Unexpected { message: "Can't clone child value" }); + return as Clone>::clone(&child.content).ok_or( + CstParseError::Unexpected { + message: "Can't clone child value", + }, + ); } } return Err(CstParseError::CstValueNotFound); diff --git a/src/main.rs b/src/main.rs index 49fc320..a878fae 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,18 +1,20 @@ +use compiler::CompilerSource; use std::env::temp_dir; use std::fs::File; use std::io::Read; use std::path::PathBuf; -use compiler::CompilerSource; use crate::cli::{run_build, run_cli}; -use crate::compiler::{FileCompilerSource, HexoCompiler, HexoCompilerContext, StringCompilerSource}; +use crate::compiler::{ + FileCompilerSource, HexoCompiler, HexoCompilerContext, StringCompilerSource, +}; mod cli; +mod compiler; mod cst_legacy; mod encoding_legacy; mod render_legacy; mod resolver_legacy; -mod compiler; fn main() { run_cli() @@ -23,9 +25,7 @@ fn new_compiler() { let context = HexoCompilerContext::new(); let compiler = HexoCompiler::new(context); - let source = FileCompilerSource::new( - PathBuf::from("sample.hexo"), - ); + let source = FileCompilerSource::new(PathBuf::from("sample.hexo")); let compilation_result = compiler.compile(&source).unwrap(); @@ -59,11 +59,12 @@ fn run_test_cases() { run_build( input_file.to_str().unwrap().to_string(), Some(output_file.to_str().unwrap().to_string()), - ).unwrap(); + ) + .unwrap(); let output_buf = read_file(&output_file); let expected_output_buf = read_file(&expected_output); assert_eq!(output_buf, expected_output_buf); } -} \ No newline at end of file +}