Skip to content

Commit

Permalink
feat: keyword filters + args working
Browse files Browse the repository at this point in the history
  • Loading branch information
InioX committed Dec 23, 2024
1 parent abc7f58 commit 739e57e
Show file tree
Hide file tree
Showing 11 changed files with 507 additions and 450 deletions.
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions matugen-parser/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ version = "0.1.0"
edition = "2021"

[dependencies]
colored = "2.2.0"
nom = "7.1.3"
serde = "1.0.209"
string_cache = "0.8.7"
4 changes: 3 additions & 1 deletion matugen-parser/example/template.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{{ colors colors }}
{{ colors.colors | a }}

{{ colors.colors | b: c }}

{{ colors.source_color.default.hex }}

Expand Down
15 changes: 15 additions & 0 deletions matugen-parser/src/errors/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use parse::ParseError;

pub mod parse;

pub fn handle_error<T>(f: Result<T, ParseError<'_>>) {
if let Err(ref e) = f {
std::eprintln!("{}", e);
};
}

pub fn handle_error_panic<T>(f: Result<T, ParseError<'_>>) {
if let Err(ref e) = f {
panic!("{}", e);
};
}
83 changes: 83 additions & 0 deletions matugen-parser/src/errors/parse.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
use std::fmt;

use crate::parser::Parser;

#[derive(Debug)]
pub struct ParseError<'a> {
pub err_type: ParseErrorTypes,
pub start: usize,
pub end: usize,
pub source: &'a str,
pub filename: &'a str,
pub line_number: u64,
}

impl ParseError<'_> {
pub fn new<'a>(
err_type: ParseErrorTypes,
start: usize,
end: usize,
source: &'a str,
filename: &'a str,
line_number: u64,
) -> ParseError<'a> {
ParseError {
err_type,
start,
end,
source,
filename,
line_number,
}
}
pub fn new_from_parser<'a>(err_type: ParseErrorTypes, parser: &Parser<'a>) -> ParseError<'a> {
ParseError {
err_type,
start: parser.last_bracket_start,
end: parser.prev_token_end,
source: parser.source,
filename: &parser.filename,
line_number: parser.lexer.cur_line,
}
}
}

impl<'a> fmt::Display for ParseError<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let err_msg = match self.err_type {
ParseErrorTypes::UnexpectedFilterArgumentToken => {
"Unexpected character in filter argument"
}
ParseErrorTypes::UnclosedBracket => "Unclosed bracket",
ParseErrorTypes::DoubleDot => "Double dot",
ParseErrorTypes::DoubleString => "Double string",
};
let mut str = "".to_string();

let span = self.source.get(self.start..self.end).unwrap_or("");

for line in span.lines() {
str.push_str(&format!("{} \x1b[94m|\x1b[0m {}\n", self.line_number, line))
}

write!(
f,
"\n\u{1b}[2;30;41m ERROR \u{1b}[0m\u{1b}[2;30;47m {} \u{1b}[0m\n\x1b[94m-->\x1b[0m {}:{}:{}\n{}\n",
err_msg, self.filename, self.start, self.end, str,
)

// write!(
// f,
// "\n\u{1b}[1;31m[ERROR] {} {}..{}: {}:\u{1b}[0m\n{}\n",
// self.filename, self.start, self.end, err_msg, span,
// )
}
}

#[derive(Debug)]
pub enum ParseErrorTypes {
UnclosedBracket,
DoubleDot,
DoubleString,
UnexpectedFilterArgumentToken,
}
4 changes: 4 additions & 0 deletions matugen-parser/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pub mod errors;
pub mod lexer;
pub mod node;
pub mod parser;
1 change: 1 addition & 0 deletions matugen-parser/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::path::PathBuf;
use lexer::Lexer;
use parser::Parser;

mod errors;
mod lexer;
mod node;
mod parser;
Expand Down
Loading

0 comments on commit 739e57e

Please sign in to comment.