Fixed type/generics parsing
This commit is contained in:
@@ -31,8 +31,7 @@ pub enum TypePostfix {
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub enum TypeExprKind {
|
||||
Path(Path),
|
||||
PathParams(Path, Vec<TypeExpr>),
|
||||
Path(Path, Option<Generics>),
|
||||
Tuple(Vec<TypeExpr>),
|
||||
Lifetime(Identifier),
|
||||
}
|
||||
@@ -54,9 +53,10 @@ impl From<GenericDecl> for Generic {
|
||||
fn from(value: GenericDecl) -> Self {
|
||||
match value {
|
||||
GenericDecl::Lifetime(life) => Generic::Lifetime(life),
|
||||
GenericDecl::Type(ty, _) => {
|
||||
Generic::Type(TypeExpr(TypeExprKind::Path(Path(vec![ty])), Vec::new()))
|
||||
}
|
||||
GenericDecl::Type(ty, _) => Generic::Type(TypeExpr(
|
||||
TypeExprKind::Path(Path(vec![ty]), None),
|
||||
Vec::new(),
|
||||
)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -80,10 +80,10 @@ let_kw = { "let" }
|
||||
// ======================================================
|
||||
// GENERICS
|
||||
// ======================================================
|
||||
generic_decl = { lifetime | (identifier ~ (":" ~ type_expr_param ~ ("+" ~ type_expr_param)*)?) }
|
||||
generic_decl = { lifetime | (identifier ~ (":" ~ generic ~ ("+" ~ generic)*)?) }
|
||||
generics_decl = { "<" ~ (generic_decl ~ ("," ~ generic_decl)*)? ~ ">" }
|
||||
|
||||
generic = { lifetime | type_expr_param }
|
||||
generic = { lifetime | type_expr }
|
||||
generics = { "<" ~ (generic ~ ("," ~ generic)*)? ~ ">" }
|
||||
|
||||
// ======================================================
|
||||
@@ -95,7 +95,7 @@ tuple_type = {
|
||||
}
|
||||
|
||||
path_type = {
|
||||
static_path ~ ("<" ~ (type_expr_param ~ ("," ~ type_expr_param)*)? ~ ">")?
|
||||
static_path ~ generics?
|
||||
}
|
||||
|
||||
ref_type = {
|
||||
@@ -114,11 +114,6 @@ lifetime = {
|
||||
"'" ~ identifier
|
||||
}
|
||||
|
||||
type_expr_param = {
|
||||
type_expr
|
||||
| lifetime
|
||||
}
|
||||
|
||||
// ======================================================
|
||||
// LITERALS
|
||||
// ======================================================
|
||||
@@ -308,7 +303,7 @@ impl_decl = {
|
||||
// ======================================================
|
||||
// Traits
|
||||
// ======================================================
|
||||
trait_requirements = { ":" ~ type_expr_param ~ ("+" ~ type_expr_param)* }
|
||||
trait_requirements = { ":" ~ generic ~ ("+" ~ generic)* }
|
||||
|
||||
trait_item = _{
|
||||
function_decl
|
||||
|
||||
@@ -2,7 +2,7 @@ use crate::{
|
||||
Rule,
|
||||
ast::*,
|
||||
ast_ensure, ast_expr,
|
||||
error::{AstError, GetLength, IntoErr, collect_recovered},
|
||||
error::{AstError, IntoErr, collect_recovered},
|
||||
parser::{consume_rule, listen_rule},
|
||||
};
|
||||
|
||||
@@ -53,14 +53,10 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for TypeExprKind {
|
||||
match rule {
|
||||
Rule::tuple_type => ast_expr!(TypeExprKind::Tuple(collect_recovered(inner))),
|
||||
Rule::path_type => {
|
||||
let path = Path::try_from(inner.next().unwrap());
|
||||
let params = collect_recovered(inner);
|
||||
|
||||
if params.len() == 0 {
|
||||
ast_expr!(TypeExprKind::Path(path))
|
||||
} else {
|
||||
ast_expr!(TypeExprKind::PathParams(path, params))
|
||||
}
|
||||
ast_expr!(TypeExprKind::Path(
|
||||
Path::try_from(inner.next().unwrap()),
|
||||
inner.next().map(Generics::try_from).transpose()
|
||||
))
|
||||
}
|
||||
_ => AstError::bug_unimplemented(pair),
|
||||
}
|
||||
@@ -75,11 +71,11 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for TypeExpr {
|
||||
let mut inner = pair.clone().into_inner();
|
||||
|
||||
match rule {
|
||||
Rule::generic => Self::try_from(inner.next().unwrap()),
|
||||
Rule::type_expr => ast_expr!(TypeExpr(
|
||||
inner.next().unwrap().try_into(),
|
||||
collect_recovered(inner),
|
||||
)),
|
||||
Rule::type_expr_param => Self::try_from(inner.next().unwrap()),
|
||||
Rule::lifetime => ast_expr!(TypeExprKind::Lifetime(inner.next().unwrap().try_into()))
|
||||
.get_map(TypeExpr::no_px)
|
||||
.map(TypeExpr::no_px),
|
||||
|
||||
@@ -30,7 +30,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for FunctionDecl {
|
||||
VarDecl {
|
||||
name: name.clone(),
|
||||
type_: Some(TypeExpr(
|
||||
TypeExprKind::Path(Path(vec![Identifier("Self".to_string())])),
|
||||
TypeExprKind::Path(Path(vec![Identifier("Self".to_string())]), None),
|
||||
if is_ref {
|
||||
vec![if mutable {
|
||||
TypePostfix::RefMut
|
||||
|
||||
Reference in New Issue
Block a user