Refactoring variable definition parsing

This commit is contained in:
2026-05-04 06:59:31 +02:00
parent 4dcb3fae7b
commit a4c1f787ff
4 changed files with 92 additions and 53 deletions
+65 -38
View File
@@ -1,5 +1,3 @@
use std::collections::HashMap;
use pest::Parser;
use pest_derive::Parser;
@@ -17,8 +15,6 @@ pub type ParseError = pest::error::Error<Rule>;
pub fn parse(source: &str) -> Result<Vec<TopLevel>, ParseError> {
let mut pairs = MistParser::parse(Rule::program, source)?;
println!("{pairs:#?}");
let mut statements = vec![];
for pair in pairs.next().unwrap().into_inner() {
@@ -49,25 +45,32 @@ impl TryFrom<pest::iterators::Pair<'_, Rule>> for TypeExpr {
}
}
impl From<(bool, pest::iterators::Pair<'_, Rule>)> for ParamList {
fn from((export_allowed, pair): (bool, pest::iterators::Pair<Rule>)) -> Self {
impl From<pest::iterators::Pair<'_, Rule>> for FieldList {
fn from(pair: pest::iterators::Pair<Rule>) -> Self {
let params = pair
.into_inner()
.map(|p| {
let mut param_inner = p.into_inner();
let export =
if export_allowed && param_inner.peek().unwrap().as_rule() == Rule::export {
param_inner.next().unwrap();
true
} else {
false
};
let export = if param_inner.peek().unwrap().as_rule() == Rule::export {
param_inner.next().unwrap();
true
} else {
false
};
let param_type = TypeExpr::try_from(param_inner.next().unwrap()).unwrap();
let param_name = param_inner.next().unwrap().as_str().to_string();
(param_name, (export, param_type))
})
.collect();
FieldList(params)
}
}
impl From<pest::iterators::Pair<'_, Rule>> for ParamList {
fn from(pair: pest::iterators::Pair<Rule>) -> Self {
let params = pair.into_inner().map(VarDecl::from).collect();
ParamList(params)
}
}
@@ -106,9 +109,9 @@ impl TryFrom<pest::iterators::Pair<'_, Rule>> for TopLevel {
let name = inner.next().unwrap().as_str().to_string();
let params = if inner.peek().unwrap().as_rule() == Rule::param_list {
ParamList::from((false, inner.next().unwrap()))
ParamList::from(inner.next().unwrap())
} else {
ParamList(HashMap::new())
ParamList(Vec::new())
};
let body = Block::from(inner.next().unwrap());
@@ -136,7 +139,7 @@ impl TryFrom<pest::iterators::Pair<'_, Rule>> for TopLevel {
};
let name = inner.next().unwrap().as_str().to_string();
let fields_pair = inner.next().unwrap();
let fields = ParamList::from((true, fields_pair));
let fields = FieldList::from(fields_pair);
Ok(TopLevel::StructDecl {
export,
@@ -182,28 +185,7 @@ impl From<pest::iterators::Pair<'_, Rule>> for Statement {
Rule::block => Statement::Block(Block::from(pair.into_inner().next().unwrap())),
Rule::var_decl_statement => {
let mut inner = pair.into_inner();
let mut var_decl = inner.next().unwrap().into_inner();
let type_ = var_decl.next().map(TypeExpr::try_from).unwrap().ok();
let mutable = if var_decl.peek().unwrap().as_rule() == Rule::mutable {
var_decl.next();
true
} else {
false
};
let name = var_decl.next().unwrap().as_str().to_string();
let init = inner.next().map(Expression::from);
Statement::VarDecl(VarDeclStmt {
mutable,
name: name.as_str().to_string(),
init,
type_,
})
}
Rule::var_decl_statement => Statement::VarDecl(VarDeclStmt::from(pair)),
Rule::return_stmt => {
let mut inner = pair.into_inner();
@@ -348,3 +330,48 @@ impl From<pest::iterators::Pair<'_, Rule>> for Postfix {
}
}
}
impl From<pest::iterators::Pair<'_, Rule>> for VarDeclStmt {
fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self {
match pair.as_rule() {
Rule::var_decl_statement => {
let mut inner = pair.into_inner();
let decl = VarDecl::from(inner.next().unwrap());
let init = inner.next().map(Expression::from);
VarDeclStmt { decl, init }
}
_ => unimplemented!(),
}
}
}
impl From<pest::iterators::Pair<'_, Rule>> for VarDecl {
fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self {
match pair.as_rule() {
Rule::var_decl => {
let mut inner = pair.into_inner();
let type_ = inner.next().map(TypeExpr::try_from).unwrap().ok();
let mutable = if inner.peek().unwrap().as_rule() == Rule::mutable {
inner.next();
true
} else {
false
};
let name = inner.next().unwrap().as_str().to_string();
VarDecl {
mutable,
name: name.as_str().to_string(),
type_,
}
}
_ => unimplemented!("{:?}", pair.as_rule()),
}
}
}