Skip to content

Commit

Permalink
wip(parser): fix ignoring first character
Browse files Browse the repository at this point in the history
  • Loading branch information
InioX committed Sep 8, 2024
1 parent 6e867e0 commit eb7d532
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 18 deletions.
19 changes: 16 additions & 3 deletions matugen-parser/src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub enum Kind {
Colon,
NewLine,
Eof,
Sof,
Identifier,
}

Expand All @@ -35,20 +36,31 @@ pub enum TokenValue {
pub struct Lexer<'a> {
source: &'a str,
chars: Chars<'a>,
pub cur_line: u64,
}

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

fn offset(&self) -> usize {
self.source.len() - self.chars.as_str().len()
}

pub fn start(&self) -> Token {
Token {
kind: Kind::Sof,
start: 0,
end: 0,
value: TokenValue::None,
}
}

// pub fn tokenize(&mut self) -> Vec<Token> {
// let mut tokens = Vec::new();
// while let token = self.next_token() {
Expand All @@ -72,14 +84,14 @@ impl<'a> Lexer<'a> {

pub fn next_token(&mut self) -> Token {
let start = self.offset();
let res = self.read_next_kind();
let (kind, value) = self.read_next_kind();
let end = self.offset();

Token {
kind: res.0,
kind,
start,
end,
value: res.1,
value,
}
}

Expand Down Expand Up @@ -111,6 +123,7 @@ impl<'a> Lexer<'a> {
}
_ => {
if next_char.unwrap() == 0xA as char || next_char.unwrap() == '\n' {
self.cur_line += 1;
(Kind::NewLine, TokenValue::None)
} else if !next_char.unwrap().is_alphanumeric() || next_char.unwrap() == '_' {
(Kind::Identifier, TokenValue::String(String::from(next_char.unwrap()).into()))
Expand Down
4 changes: 2 additions & 2 deletions matugen-parser/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ fn main() {
// {{ colors.aaa.default.hex | thingy: aaaa }}
// "#;

let source = r#"
let source = r#"{{ colors colors }}
{{ colors.source_color.default.hex }}
{{ image }}"#;
Expand Down
39 changes: 26 additions & 13 deletions matugen-parser/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub struct ParseError<'a> {
pub end: usize,
pub source: &'a str,
pub filename: &'a str,
pub line_number: u64,
}

impl<'a> fmt::Display for ParseError<'a> {
Expand All @@ -17,13 +18,14 @@ impl<'a> fmt::Display for ParseError<'a> {
}
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", line))
str.push_str(&format!("{} \x1b[94m|\x1b[0m {}\n", self.line_number, line))
}

write!(
Expand All @@ -44,6 +46,7 @@ impl<'a> fmt::Display for ParseError<'a> {
pub enum ParseErrorTypes {
UnclosedBracket,
DoubleDot,
DoubleString,
UnexpectedFilterArgumentToken,
}

Expand All @@ -57,7 +60,6 @@ use crate::node::{FilterDefinition, KeywordDefinition, Node, Program, Statement}
pub struct Parser<'a> {
source: &'a str,
filename: &'a str,
line_number: i64,
lexer: Lexer<'a>,

/// Current Token consumed from the lexer
Expand All @@ -79,14 +81,13 @@ impl<'a> Parser<'a> {
Parser {
source,
filename,
cur_token: lexer.next_token(), // There should always be at least one token, Eof
cur_token: lexer.start(),
lexer,
prev_token_end: 0,
opened: false,
closed: false,
seen_dot: false,
last_bracket_start: 0,
line_number: 0,
}
}

Expand Down Expand Up @@ -196,9 +197,7 @@ impl<'a> Parser<'a> {
let token = self.lexer.next_token();
self.prev_token_end = self.cur_token.end;
self.cur_token = token;
if self.at(Kind::NewLine) {
self.line_number += 1
}

println!("self at : {:?}", self.cur_token());
}

Expand All @@ -219,6 +218,7 @@ impl<'a> Parser<'a> {
end: self.prev_token_end,
source: self.source,
filename: &self.filename,
line_number: self.lexer.cur_line,
})
}
}
Expand All @@ -227,9 +227,11 @@ impl<'a> Parser<'a> {
let mut vec: Vec<Statement> = vec![];

while !self.at(Kind::Eof) {
self.bump_until_not_at(Kind::Lbracket);
if !self.at(Kind::Lbracket) {
self.bump_until_not_at(Kind::Lbracket);
}
// println!("getting opening, {:?}", self.cur_kind());
self.last_bracket_start = self.get_opening().unwrap_or(0);
self.last_bracket_start = self.get_opening().unwrap();
let start = self.start_node();

let mut strings: Vec<TokenValue> = vec![];
Expand Down Expand Up @@ -331,6 +333,8 @@ impl<'a> Parser<'a> {
end: self.prev_token_end,
source: self.source,
filename: &self.filename,
line_number: self.lexer.cur_line,

})
}
}
Expand All @@ -354,8 +358,8 @@ impl<'a> Parser<'a> {

self.bump_any();

while !self.closed && !self.at(Kind::Eof) {
match self.cur_kind() {
while !&self.closed && !self.at(Kind::Eof) {
match &self.cur_kind() {
Kind::Dot => {
if self.seen_dot && self.eat(Kind::Dot) {
self.seen_dot = false;
Expand All @@ -365,6 +369,8 @@ impl<'a> Parser<'a> {
end: self.prev_token_end + 1,
source: self.source,
filename: &self.filename,
line_number: self.lexer.cur_line,

});
} else {
self.seen_dot = true;
Expand All @@ -377,8 +383,15 @@ impl<'a> Parser<'a> {
self.bump(Kind::String);
self.seen_dot = false;
} else {
println!("double string");
break;
self.bump_while_not(Kind::RBracket);
return Err(ParseError {
err_type: ParseErrorTypes::DoubleString,
start: self.last_bracket_start,
end: self.prev_token_end + 1,
source: self.source,
filename: &self.filename,
line_number: self.lexer.cur_line,
});
}
}
Kind::Bar => {
Expand Down

0 comments on commit eb7d532

Please sign in to comment.