Updated parser
This commit is contained in:
@@ -177,7 +177,7 @@ impl GenRust for TopLevelKind {
|
|||||||
Self::Import(vis, path) => {
|
Self::Import(vis, path) => {
|
||||||
cg.addln(&format!("{}use {};", vis.get_rust(), path.get_rust()))
|
cg.addln(&format!("{}use {};", vis.get_rust(), path.get_rust()))
|
||||||
}
|
}
|
||||||
Self::Mod(vis, id) => cg.addln(&format!("{}mod {};", vis.get_rust(), id.get_rust())),
|
Self::DeclareModule(_, _) => {}
|
||||||
Self::FunctionDecl(decl) => decl.gen_rust(ctx, cg),
|
Self::FunctionDecl(decl) => decl.gen_rust(ctx, cg),
|
||||||
Self::ImplDecl(impl_) => impl_.gen_rust(ctx, cg),
|
Self::ImplDecl(impl_) => impl_.gen_rust(ctx, cg),
|
||||||
Self::StructDecl {
|
Self::StructDecl {
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ pub struct TopLevel(pub Spanned<TopLevelKind>, pub Vec<Attribute>);
|
|||||||
pub enum TopLevelKind {
|
pub enum TopLevelKind {
|
||||||
ModAttribute,
|
ModAttribute,
|
||||||
Import(Visibility, Path),
|
Import(Visibility, Path),
|
||||||
Mod(Visibility, Identifier),
|
DeclareModule(Visibility, Identifier),
|
||||||
ImplDecl(ImplDecl),
|
ImplDecl(ImplDecl),
|
||||||
TraitDecl {
|
TraitDecl {
|
||||||
visibility: Visibility,
|
visibility: Visibility,
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ pub struct AstError<'a, T> {
|
|||||||
pub enum ErrorCode {
|
pub enum ErrorCode {
|
||||||
InvalidStatement,
|
InvalidStatement,
|
||||||
AstGenBug,
|
AstGenBug,
|
||||||
|
Module,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> From<pest::error::Error<Rule>> for ParseError<'_, T> {
|
impl<T> From<pest::error::Error<Rule>> for ParseError<'_, T> {
|
||||||
|
|||||||
@@ -220,8 +220,8 @@ import = {
|
|||||||
visibility? ~ "use" ~ static_path ~ semicolon
|
visibility? ~ "use" ~ static_path ~ semicolon
|
||||||
}
|
}
|
||||||
|
|
||||||
mod_package = {
|
declare_module = {
|
||||||
visibility? ~ "mod" ~ identifier ~ semicolon
|
visibility? ~ "module" ~ identifier ~ semicolon
|
||||||
}
|
}
|
||||||
|
|
||||||
// ======================================================
|
// ======================================================
|
||||||
@@ -334,7 +334,7 @@ function_decl = {
|
|||||||
// ======================================================
|
// ======================================================
|
||||||
|
|
||||||
top_level = {
|
top_level = {
|
||||||
(attributes ~ (import | impl_for_decl | impl_decl | trait_decl | struct_decl | enum_decl | class_decl | mod_package | function_decl))
|
(attributes ~ (import | impl_for_decl | impl_decl | trait_decl | struct_decl | enum_decl | class_decl | declare_module | function_decl))
|
||||||
| mod_attribute
|
| mod_attribute
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
use pest::Parser;
|
use pest::{Parser, Span};
|
||||||
use pest_derive::Parser;
|
use pest_derive::Parser;
|
||||||
|
|
||||||
pub mod ast;
|
pub mod ast;
|
||||||
@@ -14,7 +14,9 @@ use crate::error::{IntoErr, ParseError};
|
|||||||
#[grammar = "./src/grammar.pest"]
|
#[grammar = "./src/grammar.pest"]
|
||||||
pub struct MistParser;
|
pub struct MistParser;
|
||||||
|
|
||||||
pub fn parse<'a>(source: &'a str) -> Result<Vec<TopLevel>, ParseError<'a, Vec<TopLevel>>> {
|
pub fn parse<'a>(
|
||||||
|
source: &'a str,
|
||||||
|
) -> Result<((Visibility, Identifier), Vec<TopLevel>), ParseError<'a, Vec<TopLevel>>> {
|
||||||
let mut pairs = MistParser::parse(Rule::program, source)?;
|
let mut pairs = MistParser::parse(Rule::program, source)?;
|
||||||
|
|
||||||
let mut statements = vec![];
|
let mut statements = vec![];
|
||||||
@@ -27,8 +29,28 @@ pub fn parse<'a>(source: &'a str) -> Result<Vec<TopLevel>, ParseError<'a, Vec<To
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let m = if let Some(v) = statements.get(0) {
|
||||||
|
if let TopLevelKind::DeclareModule(vis, name) = &v.0.item {
|
||||||
|
(vis.clone(), name.clone())
|
||||||
|
} else {
|
||||||
|
return Err(ParseError::Ast(error::AstError {
|
||||||
|
span: Span::new("", 0, 0).unwrap(),
|
||||||
|
error_code: error::ErrorCode::Module,
|
||||||
|
error_message: "Module must be declared, use `module <>;`".to_string(),
|
||||||
|
recovered: None,
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return Err(ParseError::Ast(error::AstError {
|
||||||
|
span: Span::new("", 0, 0).unwrap(),
|
||||||
|
error_code: error::ErrorCode::Module,
|
||||||
|
error_message: "Module must be declared, use `module <>;`".to_string(),
|
||||||
|
recovered: None,
|
||||||
|
}));
|
||||||
|
};
|
||||||
|
|
||||||
match analyzer.build(statements) {
|
match analyzer.build(statements) {
|
||||||
Ok(v) => Ok(v),
|
Ok(v) => Ok((m, v)),
|
||||||
Err(e) => Err(ParseError::Ast(e)),
|
Err(e) => Err(ParseError::Ast(e)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -93,7 +93,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for TopLevelKind {
|
|||||||
fields: collect_recovered(inner),
|
fields: collect_recovered(inner),
|
||||||
}),
|
}),
|
||||||
|
|
||||||
Rule::mod_package => ast_expr!(TopLevelKind::Mod(
|
Rule::declare_module => ast_expr!(TopLevelKind::DeclareModule(
|
||||||
Visibility::try_from(&mut inner),
|
Visibility::try_from(&mut inner),
|
||||||
inner.next().unwrap().try_into(),
|
inner.next().unwrap().try_into(),
|
||||||
)),
|
)),
|
||||||
|
|||||||
Reference in New Issue
Block a user