-
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
137 additions
and
44 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 |
---|---|---|
@@ -0,0 +1,80 @@ | ||
use crate::{ | ||
ast::Statement, | ||
diagnostic::{Diagnostic, Error}, | ||
parser::{grammar::NonTerminal, ParseNode}, | ||
}; | ||
|
||
use super::Ast; | ||
|
||
pub fn build_ast<'a>(parse_tree: &'a ParseNode<'a>) -> Result<Ast<'a>, Vec<Diagnostic<'a>>> { | ||
let mut diagnostics = Vec::new(); | ||
let mut ast = Ast::Statement(Statement::Empty); | ||
|
||
match parse_tree { | ||
ParseNode::NonTerminal(NonTerminal::Start, children) => { | ||
match children | ||
.iter() | ||
.map(|child| build_ast(child)) | ||
.collect::<Result<Vec<_>, _>>() | ||
{ | ||
Ok(_) => {} | ||
Err(err) => { | ||
diagnostics.extend(err); | ||
} | ||
} | ||
|
||
ast = Ast::Statement(Statement::Empty); | ||
} | ||
_ => { | ||
let range = parse_tree.range(); | ||
|
||
diagnostics.push( | ||
Diagnostic::error("Structure error").with_error(Error::primary( | ||
range.file_id, | ||
range.position, | ||
range.length, | ||
format!("Expected start, got {:?}", parse_tree), | ||
)), | ||
); | ||
} | ||
} | ||
|
||
if !diagnostics.is_empty() { | ||
Err(diagnostics) | ||
} else { | ||
Ok(ast) | ||
} | ||
} | ||
|
||
pub fn build_top_level_statement<'a>( | ||
parse_tree: &'a ParseNode<'a>, | ||
) -> Result<Ast<'a>, Vec<Diagnostic<'a>>> { | ||
match parse_tree { | ||
ParseNode::NonTerminal(NonTerminal::RootItems, children) => { | ||
let root_items = children | ||
.iter() | ||
.map(|child| build_ast(child)) | ||
.collect::<Result<Vec<_>, _>>()?; | ||
|
||
Ok(Ast::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() {} | ||
} |
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 +1,23 @@ | ||
use std::collections::HashSet; | ||
|
||
use crate::diagnostic::Range; | ||
|
||
pub mod builder; | ||
|
||
pub enum Ast<'a> { | ||
Statement(Statement<'a>), | ||
} | ||
pub enum Statement<'a> { | ||
Empty, | ||
EnumDeclaration(Spanned<'a, EnumDeclaration<'a>>), | ||
} | ||
|
||
pub struct EnumDeclaration<'a> { | ||
pub identifier: Spanned<'a, &'a str>, | ||
pub items: Vec<Spanned<'a, &'a str>>, | ||
} | ||
|
||
pub struct Spanned<'a, T> { | ||
pub value: T, | ||
pub range: Range<'a>, | ||
} |
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