Spanned impl
This commit is contained in:
@@ -22,7 +22,7 @@ pub enum Attribute {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize)]
|
#[derive(Debug, Clone, Serialize)]
|
||||||
pub struct TopLevel(pub TopLevelKind, pub Vec<Attribute>);
|
pub struct TopLevel(pub Spanned<TopLevelKind>, pub Vec<Attribute>);
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize)]
|
#[derive(Debug, Clone, Serialize)]
|
||||||
pub enum TopLevelKind {
|
pub enum TopLevelKind {
|
||||||
|
|||||||
@@ -21,10 +21,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for TopLevel {
|
|||||||
let attributes = collect_recovered(inner.next().unwrap().into_inner());
|
let attributes = collect_recovered(inner.next().unwrap().into_inner());
|
||||||
|
|
||||||
ast_expr!(TopLevel(
|
ast_expr!(TopLevel(
|
||||||
inner
|
inner.next().map(Spanned::try_from).unwrap(),
|
||||||
.next()
|
|
||||||
.map(TopLevelKind::try_from)
|
|
||||||
.unwrap_or(Ok(TopLevelKind::ModAttribute)),
|
|
||||||
attributes,
|
attributes,
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
pub mod common;
|
pub mod common;
|
||||||
pub mod items;
|
pub mod items;
|
||||||
|
|
||||||
use crate::{Rule, ast::Spanned};
|
use crate::{Rule, ast::Spanned, error::AstError};
|
||||||
|
|
||||||
pub fn listen_rule(pairs: &mut pest::iterators::Pairs<'_, Rule>, rule: Rule) -> bool {
|
pub fn listen_rule(pairs: &mut pest::iterators::Pairs<'_, Rule>, rule: Rule) -> bool {
|
||||||
let consumed = pairs
|
let consumed = pairs
|
||||||
@@ -28,10 +28,10 @@ pub fn consume_rule<'a>(
|
|||||||
if consumed { pairs.next() } else { None }
|
if consumed { pairs.next() } else { None }
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, T: TryFrom<pest::iterators::Pair<'a, Rule>>> TryFrom<pest::iterators::Pair<'a, Rule>>
|
impl<'a, T: TryFrom<pest::iterators::Pair<'a, Rule>, Error = AstError<'a, T>>>
|
||||||
for Spanned<T>
|
TryFrom<pest::iterators::Pair<'a, Rule>> for Spanned<T>
|
||||||
{
|
{
|
||||||
type Error = T::Error;
|
type Error = AstError<'a, Self>;
|
||||||
|
|
||||||
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
|
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
|
||||||
let span = pair.as_span().start_pos().line_col();
|
let span = pair.as_span().start_pos().line_col();
|
||||||
@@ -43,3 +43,20 @@ impl<'a, T: TryFrom<pest::iterators::Pair<'a, Rule>>> TryFrom<pest::iterators::P
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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,
|
||||||
|
column: span.1,
|
||||||
|
item: v,
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user