tuple type parsing

This commit is contained in:
2026-05-04 17:11:56 +02:00
parent 416830b095
commit 32a08d3e7f
3 changed files with 12 additions and 27 deletions
+2 -1
View File
@@ -73,7 +73,8 @@ while_stmt = {
} }
// ================= EXPRESSIONS ================= // ================= EXPRESSIONS =================
type_expr = { static_path } tuple_type = { "(" ~ (type_expr ~ ("," ~ type_expr)*)? ~ ")" }
type_expr = { (static_path ~ ("<" ~ type_expr ~ ">")?) | tuple_type }
primary = { primary = {
integer integer
+9 -25
View File
@@ -26,22 +26,14 @@ pub fn parse(source: &str) -> Result<Vec<TopLevel>, ParseError> {
Ok(statements) Ok(statements)
} }
impl TryFrom<pest::iterators::Pair<'_, Rule>> for TypeExpr { impl From<pest::iterators::Pair<'_, Rule>> for TypeExpr {
type Error = (); fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self {
match pair.as_rule() {
fn try_from(pair: pest::iterators::Pair<'_, Rule>) -> Result<Self, Self::Error> { Rule::type_expr => TypeExpr::from(pair.into_inner().next().unwrap()),
if pair.as_str() == "void" {
return Err(());
}
Ok(match pair.as_rule() {
Rule::type_expr => {
let inner = pair.into_inner().next().unwrap();
TypeExpr::try_from(inner)?
}
Rule::static_path => TypeExpr::Path(StaticPath::from(pair)), Rule::static_path => TypeExpr::Path(StaticPath::from(pair)),
Rule::tuple_type => TypeExpr::Tuple(pair.into_inner().map(TypeExpr::from).collect()),
_ => unimplemented!("{pair:#?}"), _ => unimplemented!("{pair:#?}"),
}) }
} }
} }
@@ -68,7 +60,7 @@ impl From<pest::iterators::Pair<'_, Rule>> for FieldList {
} else { } else {
false false
}; };
let param_type = TypeExpr::try_from(param_inner.next().unwrap()).unwrap(); let param_type = TypeExpr::from(param_inner.next().unwrap());
let param_name = param_inner.next().unwrap().as_str().to_string(); let param_name = param_inner.next().unwrap().as_str().to_string();
(param_name, (export, param_type)) (param_name, (export, param_type))
}) })
@@ -108,15 +100,7 @@ impl TryFrom<pest::iterators::Pair<'_, Rule>> for TopLevel {
false false
}; };
let return_type = if let Some(next) = inner.peek() { let return_type = Some(TypeExpr::from(inner.next().unwrap()));
if next.as_rule() == Rule::type_expr {
TypeExpr::try_from(inner.next().unwrap()).ok()
} else {
None
}
} else {
None
};
let name = inner.next().unwrap().as_str().to_string(); let name = inner.next().unwrap().as_str().to_string();
let params = if inner.peek().unwrap().as_rule() == Rule::param_list { let params = if inner.peek().unwrap().as_rule() == Rule::param_list {
@@ -366,7 +350,7 @@ impl From<pest::iterators::Pair<'_, Rule>> for VarDecl {
Rule::var_decl => { Rule::var_decl => {
let mut inner = pair.into_inner(); let mut inner = pair.into_inner();
let type_ = inner.next().map(TypeExpr::try_from).unwrap().ok(); let type_ = Some(inner.next().map(TypeExpr::from).unwrap());
let mutable = if inner.peek().unwrap().as_rule() == Rule::mutable { let mutable = if inner.peek().unwrap().as_rule() == Rule::mutable {
inner.next(); inner.next();
true true
+1 -1
View File
@@ -1,3 +1,3 @@
void main() { (i32, i32) main() {
} }