Actually just three programming languages in a trenchcoat
1use super::*;
2use crate::{Parser, Spanned};
3use trilogy_scanner::TokenType;
4
5#[derive(Clone, Debug, Spanned, PrettyPrintSExpr)]
6pub enum Statement {
7 Let(Box<LetStatement>),
8 Assignment(Box<AssignmentStatement>),
9 FunctionAssignment(Box<FunctionAssignment>),
10 If(Box<IfElseExpression>),
11 Match(Box<MatchExpression>),
12 While(Box<WhileStatement>),
13 For(Box<ForStatement>),
14 Defer(Box<DeferStatement>),
15 Expression(Box<Expression>),
16 Assert(Box<AssertStatement>),
17 Block(Box<Block>),
18}
19
20impl Statement {
21 pub(crate) fn parse(parser: &mut Parser) -> SyntaxResult<Self> {
22 let token = parser.peek();
23 use TokenType::*;
24 match token.token_type {
25 KwLet => Ok(Self::Let(Box::new(LetStatement::parse(parser)?))),
26 KwIf => {
27 let expr = IfElseExpression::parse(parser)?;
28 if !expr.is_strict_statement() && !expr.is_strict_expression() {
29 parser.error(ErrorKind::IfStatementRestriction.at(expr.span()));
30 }
31 Ok(Self::If(Box::new(expr)))
32 }
33 KwMatch => Ok(Self::Match(Box::new(MatchExpression::parse(parser)?))),
34 KwWhile => Ok(Self::While(Box::new(WhileStatement::parse(parser)?))),
35 KwFor => Ok(Self::For(Box::new(ForStatement::parse(parser)?))),
36 KwDefer => Ok(Self::Defer(Box::new(DeferStatement::parse(parser)?))),
37 KwAssert => Ok(Self::Assert(Box::new(AssertStatement::parse(parser)?))),
38 OBrace => Ok(Self::Block(Box::new(Block::parse(parser)?))),
39 _ => {
40 let expression = Expression::parse_no_seq(parser)?;
41 if parser.check(IdentifierEq).is_ok() {
42 if !expression.is_lvalue() {
43 parser.error(SyntaxError::new(
44 expression.span(),
45 "cannot assign to an expression that is not a valid assignment target",
46 ));
47 }
48 Ok(Self::FunctionAssignment(Box::new(
49 FunctionAssignment::parse(parser, expression)?,
50 )))
51 } else if parser
52 .check(AssignmentStatement::ASSIGNMENT_OPERATOR)
53 .is_ok()
54 {
55 if !expression.is_lvalue() {
56 parser.error(SyntaxError::new(
57 expression.span(),
58 "cannot assign to an expression that is not a valid assignment target",
59 ));
60 }
61 Ok(Self::Assignment(Box::new(AssignmentStatement::parse(
62 parser, expression,
63 )?)))
64 } else {
65 Ok(Self::Expression(Box::new(expression)))
66 }
67 }
68 }
69 }
70}