This commit is contained in:
2026-05-18 21:40:01 +02:00
parent a88ff4c8dd
commit d1330500b2
2 changed files with 36 additions and 29 deletions
+7 -15
View File
@@ -39,24 +39,16 @@ impl<'a, T: TryFrom<pest::iterators::Pair<'a, Rule>, Error = AstError<'a, T>>>
Ok(Self {
line: span.0,
column: span.1,
item: pair.try_into()?,
})
}
}
impl<'a, T> From<AstError<'a, T>> for AstError<'a, Spanned<T>> {
fn from(value: AstError<'a, T>) -> Self {
let span = value.span.start_pos().line_col();
Self {
span: value.span,
error_code: value.error_code,
error_message: value.error_message,
recovered: value.recovered.map(|v| Spanned {
item: pair.try_into().map_err(|err: AstError<'_, T>| AstError {
span: err.span,
error_code: err.error_code,
error_message: err.error_message,
recovered: err.recovered.map(|v| Spanned {
line: span.0,
column: span.1,
item: v,
}),
}
})?,
})
}
}
+22 -7
View File
@@ -1,8 +1,8 @@
use mist_parser::ast::{
Attribute, BinaryOp, Block, ClassItem, EnumItem, ExprPath, ExprPathSegment, Expression,
FieldDecl, FunctionDecl, Generic, GenericDecl, Generics, GenericsDecl, Identifier, ImplDecl,
Literal, Path, Pattern, Postfix, Prefix, Statement, StatementBranch, TopLevel, TopLevelKind,
TypeExpr, TypeExprKind, TypePostfix, VarDecl, VarDeclStmt, Visibility,
Literal, Path, Pattern, Postfix, Prefix, Spanned, Statement, StatementBranch, TopLevel,
TopLevelKind, TypeExpr, TypeExprKind, TypePostfix, VarDecl, VarDeclStmt, Visibility,
};
// ---------------------------------------------------------------------------
@@ -88,6 +88,17 @@ impl Default for RustCodegen {
// GetRust — pure string production (expressions, types)
// ---------------------------------------------------------------------------
impl<T: GetRust> GetRust for Spanned<T> {
fn get_rust(self) -> String {
format!(
"/* {}:{} */ {}",
self.line,
self.column,
self.item.get_rust()
)
}
}
impl GetRust for TypeExpr {
fn get_rust(self) -> String {
get_type_postfixes(self.1) + &self.0.get_rust()
@@ -315,6 +326,13 @@ impl GetRust for Vec<Postfix> {
// ToRust — output-writing (top-level, statements, blocks)
// ---------------------------------------------------------------------------
impl<T: ToRust> ToRust for Spanned<T> {
fn to_rust(self, cg: &mut RustCodegen) {
cg.add_indentedln(&format!("/* {}:{} */", self.line, self.column));
self.item.to_rust(cg);
}
}
impl ToRust for Block {
fn to_rust(self, cg: &mut RustCodegen) {
for stmt in self.0 {
@@ -325,18 +343,15 @@ impl ToRust for Block {
impl ToRust for TopLevel {
fn to_rust(self, cg: &mut RustCodegen) {
match self.0 {
TopLevelKind::ModAttribute => {
if let TopLevelKind::ModAttribute = self.0.item {
for attr in self.1 {
cg.addln(&format!("#![{}]", attr.get_rust()));
}
}
_ => {
} else {
for attr in self.1 {
cg.addln(&format!("#[{}]", attr.get_rust()));
}
}
}
self.0.to_rust(cg);
}