Skip to content

Commit

Permalink
parser: add float test
Browse files Browse the repository at this point in the history
Signed-off-by: FedericoBruzzone <[email protected]>
  • Loading branch information
FedericoBruzzone committed Aug 19, 2024
1 parent e6053fc commit 74f7628
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 35 deletions.
12 changes: 6 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pub mod parser;
pub mod source;
pub mod utils;

use lexer::{token::Token, Lexer};
use lexer::Lexer;
use logger::Logger;
use parser::Parser;
use source::Source;
Expand All @@ -31,14 +31,14 @@ fn main() {

let file_path: &str = &args[0];
let source = Source::new(file_path);
let mut lexer = Lexer::new(&source);
let _tokens = (&mut lexer).collect::<Vec<Token>>();
let lexer = Lexer::new(&source);
// let tokens = (&mut lexer).collect::<Vec<Token>>();
// if lexer.errors().is_empty() {
// println!("No errors found");
// } else {
// lexer.emit_errors();
// }
let mut parser = Parser::new(lexer);
let _ast = (&mut parser).collect::<Vec<parser::ast::Block>>();
println!("{:#?}", parser.parse());
let mut parser = Parser::new(source, lexer); // It can accepts lexer or tokens
let ast = parser.parse();
println!("{:#?}", ast);
}
41 changes: 12 additions & 29 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
pub mod ast;

use crate::{
lexer::{
token::{Keyword, Literal, Token, TokenKind},
Lexer,
},
lexer::token::{Keyword, Literal, Token, TokenKind},
source::Source,
};
use std::iter::Peekable;
Expand All @@ -16,10 +13,10 @@ pub struct Parser<I: IntoIterator> {
source: Source,
}

impl<I: IntoIterator<Item = Token, IntoIter = Lexer>> Parser<I> {
pub fn new(lexer: I) -> Parser<I> {
let mut lexer: Lexer = lexer.into_iter();
let source = lexer.cursor().source().clone();
impl<I: IntoIterator<Item = Token>> Parser<I> {
pub fn new(source: Source, lexer: I) -> Parser<I> {
let mut lexer = lexer.into_iter();
let source = source.clone();
info!("Created Parser");
let curr_token = lexer.next();
Parser {
Expand All @@ -31,17 +28,6 @@ impl<I: IntoIterator<Item = Token, IntoIter = Lexer>> Parser<I> {

fn consume(&mut self) {
self.curr_token = self.lexer.next();
self.consume_until(&[TokenKind::TokenSpace, TokenKind::TokenTab]);
}

fn consume_until(&mut self, kinds: &[TokenKind]) {
while let Some(token) = self.curr_token.clone() {
if kinds.contains(&token.kind) {
self.consume();
} else {
break;
}
}
}

pub fn parse(&mut self) -> ast::Ast {
Expand Down Expand Up @@ -206,14 +192,6 @@ impl<I: IntoIterator<Item = Token, IntoIter = Lexer>> Parser<I> {
}
}

impl Iterator for Parser<Lexer> {
type Item = ast::Block;

fn next(&mut self) -> Option<Self::Item> {
self.parse_block()
}
}

#[cfg(test)]
pub mod tests {
use crate::{
Expand All @@ -227,7 +205,9 @@ pub mod tests {
let fs_files = collect_fs_files("./testdata/native_types", true);
assert_eq!(fs_files.len(), 16);

let fs_files = fs_files.iter().filter(|p| p.ends_with("id_int_assign.fs"));
let fs_files = fs_files
.iter()
.filter(|p| p.ends_with("id_int_assign.fs") || p.ends_with("id_float_assign.fs"));

for path in fs_files {
info!("file -> {:?}", path);
Expand All @@ -237,7 +217,10 @@ pub mod tests {
let source = Source::from(content);

let fs_file = path.to_str().unwrap();
let output_ast = Parser::new(Lexer::new(&source)).parse();
#[cfg(target_os = "windows")]
let fs_file = fs_file.replace("\r", "");

let output_ast = Parser::new(source.clone(), Lexer::new(&source)).parse();
let ast_file = fs_file.to_string().replace(".fs", ".ast.json");
let ast = std::fs::File::open(ast_file).unwrap();
// println!("{}", serde_json::to_string(&output_ast).unwrap());
Expand Down
39 changes: 39 additions & 0 deletions testdata/native_types/id_float_assign.ast.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"source": {
"file_path": "",
"content": "_x_float: float = 0.0\n"
},
"root": {
"stmts": [
{
"Assign": {
"lhs": {
"Identifier": {
"name": "_x_float",
"location": {
"file_path": "",
"line": 0,
"column_start": 0,
"column_end": 8
}
}
},
"type_": "Float",
"rhs": {
"Literal": {
"literal": {
"Float": 0.0
},
"location": {
"file_path": "",
"line": 0,
"column_start": 18,
"column_end": 21
}
}
}
}
}
]
}
}

0 comments on commit 74f7628

Please sign in to comment.