Spanned
This commit is contained in:
@@ -39,24 +39,16 @@ impl<'a, T: TryFrom<pest::iterators::Pair<'a, Rule>, Error = AstError<'a, T>>>
|
|||||||
Ok(Self {
|
Ok(Self {
|
||||||
line: span.0,
|
line: span.0,
|
||||||
column: span.1,
|
column: span.1,
|
||||||
item: pair.try_into()?,
|
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 {
|
||||||
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 {
|
|
||||||
line: span.0,
|
line: span.0,
|
||||||
column: span.1,
|
column: span.1,
|
||||||
item: v,
|
item: v,
|
||||||
}),
|
}),
|
||||||
}
|
})?,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+22
-7
@@ -1,8 +1,8 @@
|
|||||||
use mist_parser::ast::{
|
use mist_parser::ast::{
|
||||||
Attribute, BinaryOp, Block, ClassItem, EnumItem, ExprPath, ExprPathSegment, Expression,
|
Attribute, BinaryOp, Block, ClassItem, EnumItem, ExprPath, ExprPathSegment, Expression,
|
||||||
FieldDecl, FunctionDecl, Generic, GenericDecl, Generics, GenericsDecl, Identifier, ImplDecl,
|
FieldDecl, FunctionDecl, Generic, GenericDecl, Generics, GenericsDecl, Identifier, ImplDecl,
|
||||||
Literal, Path, Pattern, Postfix, Prefix, Statement, StatementBranch, TopLevel, TopLevelKind,
|
Literal, Path, Pattern, Postfix, Prefix, Spanned, Statement, StatementBranch, TopLevel,
|
||||||
TypeExpr, TypeExprKind, TypePostfix, VarDecl, VarDeclStmt, Visibility,
|
TopLevelKind, TypeExpr, TypeExprKind, TypePostfix, VarDecl, VarDeclStmt, Visibility,
|
||||||
};
|
};
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
@@ -88,6 +88,17 @@ impl Default for RustCodegen {
|
|||||||
// GetRust — pure string production (expressions, types)
|
// 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 {
|
impl GetRust for TypeExpr {
|
||||||
fn get_rust(self) -> String {
|
fn get_rust(self) -> String {
|
||||||
get_type_postfixes(self.1) + &self.0.get_rust()
|
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)
|
// 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 {
|
impl ToRust for Block {
|
||||||
fn to_rust(self, cg: &mut RustCodegen) {
|
fn to_rust(self, cg: &mut RustCodegen) {
|
||||||
for stmt in self.0 {
|
for stmt in self.0 {
|
||||||
@@ -325,18 +343,15 @@ impl ToRust for Block {
|
|||||||
|
|
||||||
impl ToRust for TopLevel {
|
impl ToRust for TopLevel {
|
||||||
fn to_rust(self, cg: &mut RustCodegen) {
|
fn to_rust(self, cg: &mut RustCodegen) {
|
||||||
match self.0 {
|
if let TopLevelKind::ModAttribute = self.0.item {
|
||||||
TopLevelKind::ModAttribute => {
|
|
||||||
for attr in self.1 {
|
for attr in self.1 {
|
||||||
cg.addln(&format!("#![{}]", attr.get_rust()));
|
cg.addln(&format!("#![{}]", attr.get_rust()));
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
_ => {
|
|
||||||
for attr in self.1 {
|
for attr in self.1 {
|
||||||
cg.addln(&format!("#[{}]", attr.get_rust()));
|
cg.addln(&format!("#[{}]", attr.get_rust()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
self.0.to_rust(cg);
|
self.0.to_rust(cg);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user