Skip to content

Commit

Permalink
Remove PResult (#211)
Browse files Browse the repository at this point in the history
take care of PResult and most Results in the parser and remove ~200 unwrap()s
  • Loading branch information
ulmer-a authored Aug 6, 2021
1 parent 5e97491 commit 2200c8c
Show file tree
Hide file tree
Showing 21 changed files with 651 additions and 547 deletions.
4 changes: 2 additions & 2 deletions src/codegen/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ mod typesystem_test;
macro_rules! codegen_wihout_unwrap {
($code:tt) => {{
let lexer = crate::lexer::lex($code);
let (mut ast, ..) = crate::parser::parse(lexer).unwrap();
let (mut ast, ..) = crate::parser::parse(lexer);

let context = inkwell::context::Context::create();
crate::ast::pre_process(&mut ast);
Expand All @@ -21,7 +21,7 @@ macro_rules! codegen_wihout_unwrap {
macro_rules! codegen {
($code:tt) => {{
let lexer = crate::lexer::lex($code);
let (mut ast, ..) = crate::parser::parse(lexer).unwrap();
let (mut ast, ..) = crate::parser::parse(lexer);

let context = inkwell::context::Context::create();
crate::ast::pre_process(&mut ast);
Expand Down
18 changes: 9 additions & 9 deletions src/index/tests/index_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{ast::*, index::VariableType, typesystem::DataTypeInformation};
macro_rules! index {
($code:tt) => {{
let lexer = crate::lexer::lex($code);
let (mut ast, ..) = crate::parser::parse(lexer).unwrap();
let (mut ast, ..) = crate::parser::parse(lexer);

crate::ast::pre_process(&mut ast);
crate::index::visitor::visit(&ast)
Expand Down Expand Up @@ -563,7 +563,7 @@ fn pre_processing_generates_inline_enums_global() {
inline_enum : (a,b,c);
END_VAR
"#);
let (mut ast, ..) = parser::parse(lexer).unwrap();
let (mut ast, ..) = parser::parse(lexer);

// WHEN the AST ist pre-processed
crate::ast::pre_process(&mut ast);
Expand Down Expand Up @@ -606,7 +606,7 @@ fn pre_processing_generates_inline_structs_global() {
inline_struct: STRUCT a: INT; END_STRUCT
END_VAR
"#);
let (mut ast, ..) = parser::parse(lexer).unwrap();
let (mut ast, ..) = parser::parse(lexer);

// WHEN the AST ist pre-processed
crate::ast::pre_process(&mut ast);
Expand Down Expand Up @@ -649,7 +649,7 @@ fn pre_processing_generates_inline_enums() {
END_VAR
END_PROGRAM
"#);
let (mut ast, ..) = parser::parse(lexer).unwrap();
let (mut ast, ..) = parser::parse(lexer);

// WHEN the AST ist pre-processed
crate::ast::pre_process(&mut ast);
Expand Down Expand Up @@ -685,7 +685,7 @@ fn pre_processing_generates_inline_structs() {
END_VAR
END_PROGRAM
"#);
let (mut ast, ..) = parser::parse(lexer).unwrap();
let (mut ast, ..) = parser::parse(lexer);

// WHEN the AST ist pre-processed
crate::ast::pre_process(&mut ast);
Expand Down Expand Up @@ -728,7 +728,7 @@ fn pre_processing_generates_inline_arrays() {
END_VAR
END_PROGRAM
"#);
let (mut ast, ..) = parser::parse(lexer).unwrap();
let (mut ast, ..) = parser::parse(lexer);

// WHEN the AST ist pre-processed
crate::ast::pre_process(&mut ast);
Expand Down Expand Up @@ -778,7 +778,7 @@ fn pre_processing_generates_inline_array_of_array() {
END_VAR
END_PROGRAM
"#);
let (mut ast, ..) = parser::parse(lexer).unwrap();
let (mut ast, ..) = parser::parse(lexer);

// WHEN the AST ist pre-processed
crate::ast::pre_process(&mut ast);
Expand Down Expand Up @@ -859,7 +859,7 @@ fn pre_processing_nested_array_in_struct() {
END_PROGRAM
"#);

let (mut ast, ..) = parser::parse(lexer).unwrap();
let (mut ast, ..) = parser::parse(lexer);

// WHEN the AST ist pre-processed
crate::ast::pre_process(&mut ast);
Expand Down Expand Up @@ -918,7 +918,7 @@ fn pre_processing_generates_inline_array_of_array_of_array() {
END_VAR
END_PROGRAM
"#);
let (mut ast, ..) = parser::parse(lexer).unwrap();
let (mut ast, ..) = parser::parse(lexer);

// WHEN the AST ist pre-processed
crate::ast::pre_process(&mut ast);
Expand Down
15 changes: 15 additions & 0 deletions src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,20 @@ pub struct ParseSession<'a> {
pub parse_progress: usize,
}

#[macro_export]
macro_rules! expect_token {
($lexer:expr, $token:expr, $return_value:expr) => {
if $lexer.token != $token {
$lexer.accept_diagnostic(Diagnostic::unexpected_token_found(
format!("{:?}", $token),
$lexer.slice().to_string(),
$lexer.location(),
));
return $return_value;
}
};
}

impl<'a> ParseSession<'a> {
pub fn new(l: Lexer<'a, Token>) -> ParseSession<'a> {
let mut lexer = ParseSession {
Expand All @@ -39,6 +53,7 @@ impl<'a> ParseSession<'a> {
lexer
}

/// this function will be removed soon:
pub fn expect(&self, token: Token) -> Result<(), Diagnostic> {
if self.token != token {
Err(Diagnostic::unexpected_token_found(
Expand Down
9 changes: 3 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ pub fn compile_module<'c, T: SourceContainer>(
.load_source(encoding)
.map_err(|err| CompileError::io_read_error(err, location.clone()))?;

let (mut parse_result, diagnostics) = parse(e.source.as_str())?;
let (mut parse_result, diagnostics) = parse(e.source.as_str());
ast::pre_process(&mut parse_result);
full_index.import(index::visitor::visit(&parse_result));
unit.import(parse_result);
Expand Down Expand Up @@ -365,12 +365,9 @@ pub fn compile_module<'c, T: SourceContainer>(
Ok(code_generator)
}

fn parse(source: &str) -> Result<ParsedAst, CompileError> {
//Start lexing
fn parse(source: &str) -> ParsedAst {
let lexer = lexer::lex(source);
//Parse
//TODO : Parser should also return compile errors with sane locations
parser::parse(lexer).map_err(|err| err.into())
parser::parse(lexer)
}

#[cfg(test)]
Expand Down
Loading

0 comments on commit 2200c8c

Please sign in to comment.