diff --git a/parser/src/ast/mod.rs b/parser/src/ast/mod.rs index ad0b287..cea2418 100644 --- a/parser/src/ast/mod.rs +++ b/parser/src/ast/mod.rs @@ -17,6 +17,9 @@ pub struct Identifier(pub String); #[derive(Debug, Clone, Serialize, Default)] pub struct ParamList(pub Vec); +#[derive(Debug, Clone, Serialize)] +pub struct TypeExpr(pub TypeExprKind, pub Vec); + #[derive(Debug, Clone, Serialize)] pub enum TypePostfix { Ref, @@ -34,7 +37,11 @@ pub enum TypeExprKind { } #[derive(Debug, Clone, Serialize)] -pub struct TypeExpr(pub TypeExprKind, pub Vec); +pub struct Spanned { + pub line: usize, + pub column: usize, + pub item: T, +} impl TypeExpr { pub fn no_px(kind: TypeExprKind) -> Self { diff --git a/parser/src/parser/mod.rs b/parser/src/parser/mod.rs index d7dad09..08f20b8 100644 --- a/parser/src/parser/mod.rs +++ b/parser/src/parser/mod.rs @@ -1,7 +1,7 @@ pub mod common; pub mod items; -use crate::Rule; +use crate::{Rule, ast::Spanned}; pub fn listen_rule(pairs: &mut pest::iterators::Pairs<'_, Rule>, rule: Rule) -> bool { let consumed = pairs @@ -27,3 +27,19 @@ pub fn consume_rule<'a>( if consumed { pairs.next() } else { None } } + +impl<'a, T: TryFrom>> TryFrom> + for Spanned +{ + type Error = T::Error; + + fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result { + let span = pair.as_span().start_pos().line_col(); + + Ok(Self { + line: span.0, + column: span.1, + item: pair.try_into()?, + }) + } +}