Mass rewrite

This commit is contained in:
2026-05-14 19:33:12 +02:00
parent fd48d6c9c6
commit 03f9794671
+95 -53
View File
@@ -41,28 +41,32 @@ impl From<pest::error::Error<Rule>> for ParseError<'_> {
} }
} }
impl From<pest::iterators::Pair<'_, Rule>> for TypeExpr { impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for TypeExpr {
fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self { type Error = ParseError<'a>;
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
let rule = pair.as_rule(); let rule = pair.as_rule();
let mut inner = pair.into_inner(); let mut inner = pair.into_inner();
match rule { match rule {
Rule::type_expr => TypeExpr( Rule::type_expr => Ok(TypeExpr(
TypeExprKind::from(inner.next().unwrap()), TypeExprKind::from(inner.next().unwrap()),
inner.map(TypePostfix::from).collect(), inner.map(TypePostfix::from).collect(),
), )),
Rule::type_expr_param => TypeExpr::from(inner.next().unwrap()), Rule::type_expr_param => Self::try_from(inner.next().unwrap()),
Rule::lifetime => TypeExpr( Rule::lifetime => Ok(TypeExpr(
TypeExprKind::Lifetime(Identifier::from(inner.next().unwrap())), TypeExprKind::Lifetime(Identifier::from(inner.next().unwrap())),
Vec::new(), Vec::new(),
), )),
_ => unimplemented!("{rule:#?}"), _ => unimplemented!("{rule:#?}"),
} }
} }
} }
impl From<pest::iterators::Pair<'_, Rule>> for TypePostfix { impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for TypePostfix {
fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self { type Error = ParseError<'a>;
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
let rule = pair.as_rule(); let rule = pair.as_rule();
let mut inner = pair.into_inner(); let mut inner = pair.into_inner();
@@ -91,8 +95,10 @@ impl From<pest::iterators::Pair<'_, Rule>> for TypePostfix {
} }
} }
impl From<pest::iterators::Pair<'_, Rule>> for TypeExprKind { impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for TypeExprKind {
fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self { type Error = ParseError<'a>;
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
let rule = pair.as_rule(); let rule = pair.as_rule();
let mut inner = pair.into_inner(); let mut inner = pair.into_inner();
@@ -113,8 +119,10 @@ impl From<pest::iterators::Pair<'_, Rule>> for TypeExprKind {
} }
} }
impl From<pest::iterators::Pair<'_, Rule>> for Path { impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Path {
fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self { type Error = ParseError<'a>;
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
match pair.as_rule() { match pair.as_rule() {
Rule::static_path => Path(pair.into_inner().map(Identifier::from).collect()), Rule::static_path => Path(pair.into_inner().map(Identifier::from).collect()),
_ => unimplemented!("{pair:#?}"), _ => unimplemented!("{pair:#?}"),
@@ -122,14 +130,16 @@ impl From<pest::iterators::Pair<'_, Rule>> for Path {
} }
} }
impl From<pest::iterators::Pair<'_, Rule>> for ParamList { impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for ParamList {
fn from(pair: pest::iterators::Pair<Rule>) -> Self { fn from(pair: pest::iterators::Pair<Rule>) -> Self {
ParamList(pair.into_inner().map(VarDecl::from).collect()) ParamList(pair.into_inner().map(VarDecl::from).collect())
} }
} }
impl From<pest::iterators::Pair<'_, Rule>> for Attribute { impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Attribute {
fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self { type Error = ParseError<'a>;
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
match pair.as_rule() { match pair.as_rule() {
Rule::attribute => { Rule::attribute => {
// unwrap #[ ... ] // unwrap #[ ... ]
@@ -185,8 +195,10 @@ impl From<pest::iterators::Pair<'_, Rule>> for Attribute {
} }
} }
impl From<pest::iterators::Pair<'_, Rule>> for TopLevel { impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for TopLevel {
fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self { type Error = ParseError<'a>;
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
let mut inner = pair.into_inner(); let mut inner = pair.into_inner();
let attributes = inner let attributes = inner
@@ -206,8 +218,10 @@ impl From<pest::iterators::Pair<'_, Rule>> for TopLevel {
} }
} }
impl From<pest::iterators::Pair<'_, Rule>> for StatementBranch { impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for StatementBranch {
fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self { type Error = ParseError<'a>;
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
let mut inner = pair.into_inner(); let mut inner = pair.into_inner();
let condition = Expression::from(inner.next().unwrap()); let condition = Expression::from(inner.next().unwrap());
@@ -220,8 +234,10 @@ impl From<pest::iterators::Pair<'_, Rule>> for StatementBranch {
} }
} }
impl From<pest::iterators::Pair<'_, Rule>> for ClassConstructor { impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for ClassConstructor {
fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self { type Error = ParseError<'a>;
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
let mut inner = pair.into_inner(); let mut inner = pair.into_inner();
let visibility = Visibility::from(&mut inner); let visibility = Visibility::from(&mut inner);
@@ -243,8 +259,10 @@ impl From<pest::iterators::Pair<'_, Rule>> for ClassConstructor {
} }
} }
impl From<pest::iterators::Pair<'_, Rule>> for Generics { impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Generics {
fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self { type Error = ParseError<'a>;
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
let rule = pair.as_rule(); let rule = pair.as_rule();
let inner = pair.clone().into_inner(); let inner = pair.clone().into_inner();
@@ -269,8 +287,10 @@ impl From<pest::iterators::Pair<'_, Rule>> for Generics {
} }
} }
impl From<pest::iterators::Pair<'_, Rule>> for TopLevelKind { impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for TopLevelKind {
fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self { type Error = ParseError<'a>;
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
let rule = pair.as_rule(); let rule = pair.as_rule();
let mut inner = pair.clone().into_inner(); let mut inner = pair.clone().into_inner();
@@ -343,8 +363,10 @@ impl From<pest::iterators::Pair<'_, Rule>> for TopLevelKind {
} }
} }
impl From<pest::iterators::Pair<'_, Rule>> for ClassItem { impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for ClassItem {
fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self { type Error = ParseError<'a>;
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
let rule = pair.as_rule(); let rule = pair.as_rule();
match rule { match rule {
@@ -357,8 +379,10 @@ impl From<pest::iterators::Pair<'_, Rule>> for ClassItem {
} }
} }
impl From<pest::iterators::Pair<'_, Rule>> for ImplDecl { impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for ImplDecl {
fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self { type Error = ParseError<'a>;
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
let rule = pair.as_rule(); let rule = pair.as_rule();
let mut inner = pair.clone().into_inner(); let mut inner = pair.clone().into_inner();
@@ -386,8 +410,10 @@ impl From<pest::iterators::Pair<'_, Rule>> for ImplDecl {
} }
} }
impl From<pest::iterators::Pair<'_, Rule>> for EnumItem { impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for EnumItem {
fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self { type Error = ParseError<'a>;
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
let rule = pair.as_rule(); let rule = pair.as_rule();
let mut inner = pair.clone().into_inner(); let mut inner = pair.clone().into_inner();
@@ -417,7 +443,7 @@ impl From<pest::iterators::Pair<'_, Rule>> for EnumItem {
} }
} }
impl From<pest::iterators::Pair<'_, Rule>> for Block { impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Block {
fn from(pair: pest::iterators::Pair<Rule>) -> Self { fn from(pair: pest::iterators::Pair<Rule>) -> Self {
let statements = pair let statements = pair
.into_inner() .into_inner()
@@ -433,7 +459,7 @@ impl From<pest::iterators::Pair<'_, Rule>> for Block {
} }
} }
impl From<pest::iterators::Pair<'_, Rule>> for Statement { impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Statement {
fn from(pair: pest::iterators::Pair<Rule>) -> Self { fn from(pair: pest::iterators::Pair<Rule>) -> Self {
let rule = pair.as_rule(); let rule = pair.as_rule();
let mut inner = pair.clone().into_inner(); let mut inner = pair.clone().into_inner();
@@ -517,8 +543,10 @@ impl From<pest::iterators::Pair<'_, Rule>> for Statement {
} }
} }
impl From<pest::iterators::Pair<'_, Rule>> for Literal { impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Literal {
fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self { type Error = ParseError<'a>;
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
let rule = pair.as_rule(); let rule = pair.as_rule();
let mut inner = pair.clone().into_inner(); let mut inner = pair.clone().into_inner();
@@ -535,7 +563,7 @@ impl From<pest::iterators::Pair<'_, Rule>> for Literal {
} }
} }
impl From<pest::iterators::Pair<'_, Rule>> for Expression { impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Expression {
fn from(pair: pest::iterators::Pair<Rule>) -> Self { fn from(pair: pest::iterators::Pair<Rule>) -> Self {
let rule = pair.as_rule(); let rule = pair.as_rule();
let mut inner = pair.clone().into_inner(); let mut inner = pair.clone().into_inner();
@@ -567,7 +595,7 @@ impl From<pest::iterators::Pair<'_, Rule>> for Expression {
} }
} }
impl From<pest::iterators::Pair<'_, Rule>> for Prefix { impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Prefix {
fn from(pair: pest::iterators::Pair<Rule>) -> Self { fn from(pair: pest::iterators::Pair<Rule>) -> Self {
match pair.as_rule() { match pair.as_rule() {
Rule::prefix => Self::from(pair.into_inner().next().unwrap()), Rule::prefix => Self::from(pair.into_inner().next().unwrap()),
@@ -581,7 +609,7 @@ impl From<pest::iterators::Pair<'_, Rule>> for Prefix {
} }
} }
impl From<pest::iterators::Pair<'_, Rule>> for Postfix { impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Postfix {
fn from(pair: pest::iterators::Pair<Rule>) -> Self { fn from(pair: pest::iterators::Pair<Rule>) -> Self {
let rule = pair.as_rule(); let rule = pair.as_rule();
let mut inner = pair.into_inner(); let mut inner = pair.into_inner();
@@ -638,8 +666,10 @@ impl From<pest::iterators::Pair<'_, Rule>> for Postfix {
} }
} }
impl From<pest::iterators::Pair<'_, Rule>> for VarDeclStmt { impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for VarDeclStmt {
fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self { type Error = ParseError<'a>;
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
match pair.as_rule() { match pair.as_rule() {
Rule::var_decl_statement => { Rule::var_decl_statement => {
let mut inner = pair.into_inner(); let mut inner = pair.into_inner();
@@ -656,8 +686,10 @@ impl From<pest::iterators::Pair<'_, Rule>> for VarDeclStmt {
} }
} }
impl From<pest::iterators::Pair<'_, Rule>> for FieldDeclStmt { impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for FieldDeclStmt {
fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self { type Error = ParseError<'a>;
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
match pair.as_rule() { match pair.as_rule() {
Rule::class_field => { Rule::class_field => {
let mut inner = pair.into_inner(); let mut inner = pair.into_inner();
@@ -674,8 +706,10 @@ impl From<pest::iterators::Pair<'_, Rule>> for FieldDeclStmt {
} }
} }
impl From<pest::iterators::Pair<'_, Rule>> for Pattern { impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Pattern {
fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self { type Error = ParseError<'a>;
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
let rule = pair.as_rule(); let rule = pair.as_rule();
let mut inner = pair.clone().into_inner(); let mut inner = pair.clone().into_inner();
@@ -703,8 +737,10 @@ impl From<pest::iterators::Pair<'_, Rule>> for Pattern {
} }
} }
impl From<pest::iterators::Pair<'_, Rule>> for VarDecl { impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for VarDecl {
fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self { type Error = ParseError<'a>;
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
match pair.as_rule() { match pair.as_rule() {
Rule::var_decl => { Rule::var_decl => {
let mut inner = pair.into_inner(); let mut inner = pair.into_inner();
@@ -732,8 +768,10 @@ impl From<pest::iterators::Pair<'_, Rule>> for VarDecl {
} }
} }
impl From<pest::iterators::Pair<'_, Rule>> for FieldDecl { impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for FieldDecl {
fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self { type Error = ParseError<'a>;
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
match pair.as_rule() { match pair.as_rule() {
Rule::field => { Rule::field => {
let mut inner = pair.into_inner(); let mut inner = pair.into_inner();
@@ -754,8 +792,10 @@ impl From<pest::iterators::Pair<'_, Rule>> for FieldDecl {
} }
} }
impl From<pest::iterators::Pair<'_, Rule>> for FunctionDecl { impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for FunctionDecl {
fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self { type Error = ParseError<'a>;
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
let mut inner = pair.into_inner(); let mut inner = pair.into_inner();
let visibility = Visibility::from(&mut inner); let visibility = Visibility::from(&mut inner);
let return_type = TypeExpr::from(inner.next().unwrap()); let return_type = TypeExpr::from(inner.next().unwrap());
@@ -855,8 +895,10 @@ pub fn consume_rule<'a>(
if consumed { pairs.next() } else { None } if consumed { pairs.next() } else { None }
} }
impl From<pest::iterators::Pair<'_, Rule>> for Identifier { impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Identifier {
fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self { type Error = ParseError<'a>;
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
Identifier(pair.as_str().to_string()) Identifier(pair.as_str().to_string())
} }
} }