Actually just three programming languages in a trenchcoat
1use super::{pattern::Precedence, *};
2use crate::{Parser, Spanned};
3use source_span::Span;
4use trilogy_scanner::{Token, TokenType::*};
5
6#[derive(Clone, Debug)]
7pub struct TuplePattern {
8 pub lhs: Pattern,
9 pub cons: Token,
10 pub rhs: Pattern,
11 pub span: Span,
12}
13
14impl Spanned for TuplePattern {
15 fn span(&self) -> Span {
16 self.span
17 }
18}
19
20impl TuplePattern {
21 pub(crate) fn new(lhs: Pattern, cons: Token, rhs: Pattern) -> Self {
22 Self {
23 span: lhs.span().union(rhs.span()),
24 lhs,
25 cons,
26 rhs,
27 }
28 }
29
30 pub(crate) fn parse(parser: &mut Parser, lhs: Pattern) -> SyntaxResult<Self> {
31 let cons = parser
32 .expect(OpColon)
33 .expect("Caller should have found this");
34 let rhs = Pattern::parse_precedence(parser, Precedence::Cons)?;
35 Ok(Self {
36 span: lhs.span().union(rhs.span()),
37 lhs,
38 cons,
39 rhs,
40 })
41 }
42}
43
44impl TryFrom<BinaryOperation> for TuplePattern {
45 type Error = SyntaxError;
46
47 fn try_from(value: BinaryOperation) -> Result<Self, Self::Error> {
48 let span = value.span();
49 match value.operator {
50 BinaryOperator::Cons(token) => Ok(Self {
51 span,
52 lhs: value.lhs.try_into()?,
53 cons: token,
54 rhs: value.rhs.try_into()?,
55 }),
56 _ => Err(SyntaxError::new(
57 value.span(),
58 "incorrect operator for tuple pattern",
59 )),
60 }
61 }
62}