Skip to content

Commit

Permalink
reformat
Browse files Browse the repository at this point in the history
  • Loading branch information
lexa-diky committed May 13, 2024
1 parent fab11ff commit 74aa701
Show file tree
Hide file tree
Showing 23 changed files with 307 additions and 297 deletions.
6 changes: 4 additions & 2 deletions sample.hexo
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
$ hello 'world'
$ hello 'hello'
$ world 'world'
$ hello_world $hello $world

> $hello
> $hello_world
21 changes: 9 additions & 12 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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),
};
Expand All @@ -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),
}
}

Expand All @@ -102,9 +101,10 @@ fn run_watch(source: String, output: Option<String>) -> 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<Event, _>| run_watch_loop(source.clone(), output.clone(), event)
).map_err(|err| CliError::CantCreateWatcher(err))?;
let mut watcher = notify::recommended_watcher(move |event: Result<Event, _>| {
run_watch_loop(source.clone(), output.clone(), event)
})
.map_err(|err| CliError::CantCreateWatcher(err))?;

watcher
.watch(source_path, RecursiveMode::NonRecursive)
Expand Down Expand Up @@ -149,9 +149,6 @@ pub(crate) fn run_build(source: String, output: Option<String>) -> 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))
}
4 changes: 2 additions & 2 deletions src/compiler/ast/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
mod node;
mod parser;

pub(crate) use node::{AstNodeType, AstNode};
pub(crate) use parser::{AstParser, AstParserError};
pub(crate) use node::{AstNode, AstNodeType};
pub(crate) use parser::{AstParser, AstParserError};
7 changes: 5 additions & 2 deletions src/compiler/ast/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ pub(crate) enum AstNodeType {
}

impl AstNodeType {

pub(crate) fn must_capture_value(&self) -> bool {
match self {
AstNodeType::AtomUtf8
Expand All @@ -49,7 +48,11 @@ pub(crate) struct AstNode {
}

impl AstNode {
pub(crate) fn new(node_type: AstNodeType, content: Option<String>, children: Vec<AstNode>) -> Self {
pub(crate) fn new(
node_type: AstNodeType,
content: Option<String>,
children: Vec<AstNode>,
) -> Self {
AstNode {
node_type,
content,
Expand Down
28 changes: 15 additions & 13 deletions src/compiler/ast/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,22 @@ impl AstParser {
let pairs = AstPestParser::parse(Rule::file, source.as_str())
.map_err(|error| AstParserError::PestError(error))?;

let children: Result<Vec<AstNode>, _> = pairs.map(parse_ast_pair)
let children: Result<Vec<AstNode>, _> = pairs
.map(parse_ast_pair)
.filter_map(filter_ignored_token)
.collect();

return Ok(AstNode::new(AstNodeType::File, None, children?));
}
}

fn filter_ignored_token(result: Result<Option<AstNode>, AstParserError>) -> Option<Result<AstNode, AstParserError>> {
fn filter_ignored_token(
result: Result<Option<AstNode>, AstParserError>,
) -> Option<Result<AstNode, AstParserError>> {
match result {
Ok(None) => None,
Ok(Some(value)) => Some(Ok(value)),
Err(error) => Some(Err(error))
Err(error) => Some(Err(error)),
}
}

Expand Down Expand Up @@ -66,23 +69,22 @@ fn parse_ast_pair(p: Pair<Rule>) -> Result<Option<AstNode>, 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<Vec<AstNode>, _> = p.into_inner()
let children: Result<Vec<AstNode>, _> = p
.into_inner()
.map(parse_ast_pair)
.filter_map(filter_ignored_token)
.collect();

Ok(
Some(
AstNode::new(node_type, node_value, children?)
)
)
}
Ok(Some(AstNode::new(node_type, node_value, children?)))
}
9 changes: 3 additions & 6 deletions src/compiler/compilation_result.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
pub(crate) struct Compilation {
pub(crate) content: Vec<u8>
pub(crate) content: Vec<u8>,
}

impl Compilation {

pub(crate) fn from(content: Vec<u8>) -> Self {
Compilation {
content
}
Compilation { content }
}
}
}
54 changes: 30 additions & 24 deletions src/compiler/compiler.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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<TSource: CompilerSource>(&self, source: &TSource) -> Result<AstNode, CompilerError> {
pub(crate) fn compile_ast<TSource: CompilerSource>(
&self,
source: &TSource,
) -> Result<AstNode, CompilerError> {
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<TSource: CompilerSource>(&self, source: &TSource) -> Result<CstFile, CompilerError> {
pub(crate) fn compile_cst<TSource: CompilerSource>(
&self,
source: &TSource,
) -> Result<CstFile, CompilerError> {
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<TSource: CompilerSource>(&self, source: &TSource) -> Result<HexoFile, CompilerError> {
pub(crate) fn compile_rst<TSource: CompilerSource>(
&self,
source: &TSource,
) -> Result<HexoFile, CompilerError> {
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<TSource: CompilerSource>(&self, source: &TSource) -> Result<Compilation, CompilerError> {
pub(crate) fn compile<TSource: CompilerSource>(
&self,
source: &TSource,
) -> Result<Compilation, CompilerError> {
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()));
Expand Down
5 changes: 2 additions & 3 deletions src/compiler/compiler_context.rs
Original file line number Diff line number Diff line change
@@ -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("."),
}
}
}
13 changes: 4 additions & 9 deletions src/compiler/compiler_source.rs
Original file line number Diff line number Diff line change
@@ -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<String, std::io::Error>;
Expand All @@ -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(),
Expand All @@ -26,7 +25,7 @@ impl StringCompilerSource {

impl CompilerSource for StringCompilerSource {
fn read(&self) -> Result<String, std::io::Error> {
return Ok(self.content.clone())
return Ok(self.content.clone());
}

fn path(&self) -> PathBuf {
Expand All @@ -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<String, std::io::Error> {
let mut p = File::open(self.path.clone())?;
let mut buff = String::new();
Expand All @@ -60,4 +55,4 @@ impl CompilerSource for FileCompilerSource {
fn path(&self) -> PathBuf {
return self.path.clone();
}
}
}
2 changes: 1 addition & 1 deletion src/compiler/cst/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ mod node;
mod parser;

pub(crate) use node::*;
pub(crate) use parser::*;
pub(crate) use parser::*;
9 changes: 7 additions & 2 deletions src/compiler/cst/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,13 @@ pub(crate) enum CstAtom {
Hex(u8),
String(String),
Number(u32),
Constant { name: String },
Function { name: String, params: Vec<CstActualParameter> },
Constant {
name: String,
},
Function {
name: String,
params: Vec<CstActualParameter>,
},
}

#[derive(Debug)]
Expand Down
Loading

0 comments on commit 74aa701

Please sign in to comment.