-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🚧 (parser): Work on earley parser
semver: chore
- Loading branch information
Showing
4 changed files
with
52 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,82 +1,32 @@ | ||
use crate::{ | ||
abstract_syntax_tree::Statement, | ||
concrete_syntax_tree::{grammar::NonTerminal, ConcreteSyntax}, | ||
diagnostic::{Diagnostic, Error}, | ||
diagnostic::{Diagnostic, Error, Severity}, | ||
}; | ||
|
||
use super::AstractSyntax; | ||
|
||
pub fn build_ast<'a>( | ||
syntax: &'a ConcreteSyntax<'a>, | ||
) -> Result<AstractSyntax<'a>, Vec<Diagnostic<'a>>> { | ||
let mut diagnostics = Vec::new(); | ||
let mut ast = AstractSyntax::Statement(Statement::Empty); | ||
|
||
match syntax { | ||
ConcreteSyntax::NonTerminal(NonTerminal::Start, children) => { | ||
match children | ||
.iter() | ||
.map(|child| build_ast(child)) | ||
.collect::<Result<Vec<_>, _>>() | ||
{ | ||
Ok(_) => {} | ||
Err(err) => { | ||
diagnostics.extend(err); | ||
} | ||
} | ||
|
||
ast = AstractSyntax::Statement(Statement::Empty); | ||
} | ||
_ => { | ||
let range = syntax.range(); | ||
|
||
diagnostics.push( | ||
Diagnostic::error("Structure error").with_error(Error::primary( | ||
range.file_id, | ||
range.position, | ||
range.length, | ||
format!("Expected start, got {:?}", syntax), | ||
)), | ||
); | ||
} | ||
} | ||
|
||
if !diagnostics.is_empty() { | ||
Err(diagnostics) | ||
} else { | ||
Ok(ast) | ||
} | ||
} | ||
|
||
pub fn build_top_level_statement<'a>( | ||
parse_tree: &'a ConcreteSyntax<'a>, | ||
) -> Result<AstractSyntax<'a>, Vec<Diagnostic<'a>>> { | ||
match parse_tree { | ||
ConcreteSyntax::NonTerminal(NonTerminal::RootItems, children) => { | ||
let root_items = children | ||
.iter() | ||
.map(|child| build_ast(child)) | ||
.collect::<Result<Vec<_>, _>>()?; | ||
|
||
Ok(AstractSyntax::Statement(Statement::Empty)) | ||
} | ||
_ => { | ||
let range = parse_tree.range(); | ||
|
||
return Err(vec![Diagnostic::error("Structure error").with_error( | ||
Error::primary( | ||
range.file_id, | ||
range.position, | ||
range.length, | ||
"Expected root items", | ||
), | ||
)]); | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
#[test] | ||
fn test() {} | ||
println!("Parsing {}", syntax); | ||
|
||
// match syntax { | ||
// ConcreteSyntax::NonTerminal(NonTerminal::EnumDeclaration, children) => { | ||
// let identifier = children[1].clone(); | ||
// let items = children[3].clone(); | ||
// } | ||
// _ => Err(vec![Diagnostic { | ||
// severity: Severity::Error, | ||
// title: "Failed to build AST".to_string(), | ||
// errors: vec![Error::primary( | ||
// syntax.range().file_id, | ||
// syntax.range().position, | ||
// syntax.range().length, | ||
// "Failed to build AST", | ||
// )], | ||
// }]), | ||
// } | ||
|
||
todo!() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters