Skip to content

Commit

Permalink
uhhh i forgor
Browse files Browse the repository at this point in the history
  • Loading branch information
InioX committed Jan 3, 2025
1 parent e1690ee commit 933bafd
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 46 deletions.
11 changes: 9 additions & 2 deletions matugen-parser/example/template.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@

15

hello

world

{{ colors.colors | a }}

{{ colors.colors | b: c }}
{{ colors.colors | b: c, d, e, f, g }}
{{ colors1.colors2 | b: . }}
{{ colors3.colors4 | b: a b }}
{{ colors5.colors6 | b: 1, 2.0 }}
{{ colors7.colors8 | b: . }}
{{ colors9.colors10 | b: c, d, e, f, g }}

{{ colors.source_color.default.hex }}

Expand Down
9 changes: 6 additions & 3 deletions matugen-parser/src/errors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ use parse::ParseError;

pub mod parse;

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

pub fn handle_error_panic<T>(f: Result<T, ParseError<'_>>) {
Expand Down
20 changes: 16 additions & 4 deletions matugen-parser/src/errors/parse.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::fmt;

use crate::parser::Parser;
use crate::{lexer::Token, parser::Parser};

#[derive(Debug)]
pub struct ParseError<'a> {
Expand All @@ -10,6 +10,7 @@ pub struct ParseError<'a> {
pub source: &'a str,
pub filename: &'a str,
pub line_number: u64,
pub cur_token: Token,
}

impl ParseError<'_> {
Expand All @@ -20,6 +21,7 @@ impl ParseError<'_> {
source: &'a str,
filename: &'a str,
line_number: u64,
cur_token: Token,
) -> ParseError<'a> {
ParseError {
err_type,
Expand All @@ -28,16 +30,18 @@ impl ParseError<'_> {
source,
filename,
line_number,
cur_token,
}
}
pub fn new_from_parser<'a>(err_type: ParseErrorTypes, parser: &Parser<'a>) -> ParseError<'a> {
ParseError {
err_type,
start: parser.last_bracket_start,
start: parser.parser_state.last_bracket_start,
end: parser.lexer_state.prev_token_end,
source: parser.source,
filename: &parser.filename,
line_number: parser.lexer_state.lexer.cur_line,
cur_token: parser.lexer_state.cur_token.clone(),
}
}
}
Expand All @@ -51,6 +55,11 @@ impl<'a> fmt::Display for ParseError<'a> {
ParseErrorTypes::UnclosedBracket => "Unclosed bracket",
ParseErrorTypes::DoubleDot => "Double dot",
ParseErrorTypes::DoubleString => "Double string",
ParseErrorTypes::DoubleComma => "Double comma",
ParseErrorTypes::FilterArgumentNotSeparated => {
"Filter argument not separated by a comma"
}
ParseErrorTypes::NoFilterArgument => "No filter argument",
};
let mut str = "".to_string();

Expand All @@ -62,8 +71,8 @@ impl<'a> fmt::Display for ParseError<'a> {

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,
"\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{:?}\n",
err_msg, self.filename, self.start, self.end, str, self.cur_token
)

// write!(
Expand All @@ -79,5 +88,8 @@ pub enum ParseErrorTypes {
UnclosedBracket,
DoubleDot,
DoubleString,
DoubleComma,
FilterArgumentNotSeparated,
UnexpectedFilterArgumentToken,
NoFilterArgument,
}
25 changes: 23 additions & 2 deletions matugen-parser/src/lexer.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use colored::Colorize;
use std::str::Chars;
use string_cache::DefaultAtom as Atom;

Expand Down Expand Up @@ -51,14 +52,16 @@ pub struct Lexer<'a> {
source: &'a str,
chars: Chars<'a>,
pub cur_line: u64,
pub opened: bool,
}

impl<'a> Lexer<'a> {
pub fn new(input: &'a str) -> Self {
Lexer {
source: &input,
chars: input.chars(),
cur_line: 0,
cur_line: 1,
opened: false,
}
}

Expand Down Expand Up @@ -141,6 +144,15 @@ impl<'a> Lexer<'a> {
',' => (Kind::Comma, TokenValue::None),
' ' => (Kind::Space, TokenValue::None),
'0'..='9' => {
if !self.opened {
println!(
"{}",
format!("SKIPPED BECAUSE NOT OPEN: {:?}", next_char)
.red()
.bold()
);
self.read_next_kind();
}
let mut str = String::from(next_char.unwrap());
let mut is_float = false;

Expand All @@ -155,7 +167,6 @@ impl<'a> Lexer<'a> {
str.push(next_char);
self.chars.next();
} else {
println!("{}", str);
break;
}
}
Expand Down Expand Up @@ -215,6 +226,16 @@ impl<'a> Lexer<'a> {
TokenValue::String(String::from(next_char.unwrap()).into()),
)
} else {
if !self.opened {
println!(
"{}",
format!("SKIPPED BECAUSE NOT OPEN: {:?}", next_char)
.red()
.bold()
);
self.read_next_kind();
}

let start = self.offset() - 1;
let mut string = String::from(next_char.unwrap());
while let Some(next_char) = self.peek() {
Expand Down
98 changes: 70 additions & 28 deletions matugen-parser/src/parser/keywords.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ impl Parser<'_> {

self.lexer_state.bump_any();

while !self.opened {
while !self.lexer_state.lexer.opened {
if self.lexer_state.eat(&self.syntax.keyword_opening[0]) {
self.opened = true;
self.closed = false;
self.lexer_state.lexer.opened = true;
self.parser_state.closed = false;
} else if self.lexer_state.eat(&Kind::Eof) {
return None;
}
Expand All @@ -35,8 +35,8 @@ impl Parser<'_> {
println!("STARTING TO CLOSE");
self.lexer_state.bump_any();
if self.lexer_state.eat(&self.syntax.keyword_closing[0]) {
self.closed = true;
self.opened = false;
self.parser_state.closed = true;
self.lexer_state.lexer.opened = false;
Ok(())
} else {
Err(ParseError::new_from_parser(
Expand Down Expand Up @@ -65,13 +65,13 @@ impl Parser<'_> {
return vec;
}

self.last_bracket_start = opening.unwrap() - 1;
self.parser_state.last_bracket_start = opening.unwrap() - 1;
let start = self.start_node();

let mut strings: Vec<TokenValue> = vec![];
let mut filters: Vec<FilterDefinition> = vec![];

handle_error_panic(self.collect_strings(&mut strings, &mut filters));
handle_error(self.collect_strings(&mut strings, &mut filters));

vec.push(Statement::KeywordDefinition(Box::new(KeywordDefinition {
node: self.finish_node(start),
Expand Down Expand Up @@ -123,6 +123,8 @@ impl Parser<'_> {
// THIS SHOULD BE THE FILTER NAME
self.lexer_state.eat(&Kind::String);

let mut first_arg = true;

if !self.lexer_state.eat_ignore_spaces(&Kind::Colon) {
println!(
"{}",
Expand All @@ -133,37 +135,77 @@ impl Parser<'_> {
self.lexer_state
.bump_while_not(&self.syntax.keyword_closing[0])
} else {
// while !self.lexer_state.at(Kind::RBracket) {
// match self.lexer_state.cur_kind() {
// Kind::String => arguments.push(&self.lexer_state.cur_token.value),
// Kind::Number => todo!(),
// _ => {}
// }
// }
loop {
match self.lexer_state.cur_kind() {
Kind::Space => {
self.lexer_state.bump_until_not_at(&Kind::Space);
}
Kind::String => {
arguments.push(self.lexer_state.cur_token.value.clone());
self.lexer_state.bump(&Kind::String)
if self.parser_state.seen_comma || first_arg {
arguments.push(self.lexer_state.cur_token.clone().value);
self.lexer_state.bump(&Kind::String);
self.parser_state.seen_comma = false;
first_arg = false;
} else {
self.lexer_state
.bump_while_not(&self.syntax.keyword_closing[0]);
return Err(ParseError::new_from_parser(
ParseErrorTypes::FilterArgumentNotSeparated,
&self,
));
}
}
Kind::Number => {
arguments.push(self.lexer_state.cur_token.value.clone());
self.lexer_state.bump(&Kind::Number)
if self.parser_state.seen_comma || first_arg {
arguments.push(self.lexer_state.cur_token.clone().value);
self.lexer_state.bump(&Kind::Number);
self.parser_state.seen_comma = false;
first_arg = false;
} else {
self.lexer_state
.bump_while_not(&self.syntax.keyword_closing[0]);
return Err(ParseError::new_from_parser(
ParseErrorTypes::FilterArgumentNotSeparated,
&self,
));
}
}
Kind::Float => {
if self.parser_state.seen_comma || first_arg {
arguments.push(self.lexer_state.cur_token.clone().value);
self.lexer_state.bump(&Kind::Float);
self.parser_state.seen_comma = false;
first_arg = false;
} else {
self.lexer_state
.bump_while_not(&self.syntax.keyword_closing[0]);
return Err(ParseError::new_from_parser(
ParseErrorTypes::FilterArgumentNotSeparated,
&self,
));
}
}
kind if *kind == self.syntax.keyword_closing[1] => {
break;
}
Kind::Comma => {
self.lexer_state.bump_until_not_at(&Kind::Comma);
if self.parser_state.seen_comma && self.lexer_state.eat(&Kind::Comma) {
self.parser_state.seen_comma = false;
return Err(ParseError::new_from_parser(
ParseErrorTypes::DoubleComma,
&self,
));
} else {
self.parser_state.seen_comma = true;
self.lexer_state.bump(&Kind::Comma);
}
}
_ => {
self.lexer_state.bump_any();
return Err(ParseError::new_from_parser(
ParseErrorTypes::UnexpectedFilterArgumentToken,
&self,
))
));
}
}
}
Expand All @@ -190,25 +232,25 @@ impl Parser<'_> {

self.lexer_state.bump_any();

while !&self.closed && !self.lexer_state.at(&Kind::Eof) {
while !&self.parser_state.closed && !self.lexer_state.at(&Kind::Eof) {
match &self.lexer_state.cur_kind() {
Kind::Dot => {
if self.seen_dot && self.lexer_state.eat(&Kind::Dot) {
self.seen_dot = false;
if self.parser_state.seen_dot && self.lexer_state.eat(&Kind::Dot) {
self.parser_state.seen_dot = false;
return Err(ParseError::new_from_parser(
ParseErrorTypes::DoubleDot,
&self,
));
} else {
self.seen_dot = true;
self.parser_state.seen_dot = true;
self.lexer_state.bump(&Kind::Dot);
}
}
Kind::String => {
if self.seen_dot {
if self.parser_state.seen_dot {
strings.push(self.lexer_state.cur_token.clone().value);
self.lexer_state.bump(&Kind::String);
self.seen_dot = false;
self.parser_state.seen_dot = false;
} else {
self.lexer_state
.bump_while_not(&self.syntax.keyword_closing[0]);
Expand All @@ -232,8 +274,8 @@ impl Parser<'_> {
kind if **kind == self.syntax.keyword_closing[0] => {
return self.get_closing();
// if self.lexer_state.eat(Kind::RBracket) {
// self.closed = true;
// self.opened = false;
// self.parser_state.closed = true;
// self.lexer.opened = false;
// println!("closed without filter")
// } else {
// println!("fucked the closing");
Expand Down
19 changes: 12 additions & 7 deletions matugen-parser/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@ pub struct Parser<'a> {
pub source: &'a str,
pub filename: &'a str,
pub lexer_state: LexerState<'a>,
pub parser_state: ParserState,
pub syntax: &'a SyntaxSettings,
}

pub opened: bool,
#[derive(Debug)]
pub struct ParserState {
pub closed: bool,
pub seen_dot: bool,

pub seen_comma: bool,
pub last_bracket_start: usize,
pub syntax: &'a SyntaxSettings,
}

#[derive(Debug)]
Expand All @@ -39,10 +42,12 @@ impl<'a> Parser<'a> {
prev_token_end: 0,
lexer,
},
opened: false,
closed: false,
seen_dot: false,
last_bracket_start: 0,
parser_state: ParserState {
closed: false,
seen_dot: false,
seen_comma: false,
last_bracket_start: 0,
},
syntax: &syntax,
}
}
Expand Down

0 comments on commit 933bafd

Please sign in to comment.