Actually just three programming languages in a trenchcoat
at string-repr-callable 48 lines 1.3 kB view raw
1use super::*; 2use crate::{Parser, Spanned}; 3use trilogy_scanner::{Token, TokenType::*}; 4 5#[derive(Clone, Debug, PrettyPrintSExpr)] 6pub struct StructPattern { 7 pub atom: AtomLiteral, 8 pub open_paren: Token, 9 pub pattern: Pattern, 10 pub close_paren: Token, 11} 12 13impl Spanned for StructPattern { 14 fn span(&self) -> source_span::Span { 15 self.atom.span().union(self.close_paren.span) 16 } 17} 18 19impl StructPattern { 20 pub(crate) fn parse(parser: &mut Parser, atom: AtomLiteral) -> SyntaxResult<Self> { 21 let open_paren = parser 22 .expect(OParen) 23 .map_err(|token| parser.expected(token, "expected `(`"))?; 24 let pattern = Pattern::parse(parser)?; 25 let close_paren = parser 26 .expect(CParen) 27 .map_err(|token| parser.expected(token, "expected `)`"))?; 28 Ok(Self { 29 atom, 30 open_paren, 31 pattern, 32 close_paren, 33 }) 34 } 35} 36 37impl TryFrom<StructLiteral> for StructPattern { 38 type Error = SyntaxError; 39 40 fn try_from(value: StructLiteral) -> Result<Self, Self::Error> { 41 Ok(Self { 42 atom: value.atom, 43 open_paren: value.open_paren, 44 pattern: value.value.try_into()?, 45 close_paren: value.close_paren, 46 }) 47 } 48}