Actually just three programming languages in a trenchcoat
at main 82 lines 3.4 kB view raw
1use super::*; 2use crate::{Parser, Spanned}; 3use trilogy_scanner::TokenType; 4 5#[derive(Clone, Debug, Spanned)] 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 Using(Box<UsingStatement>), 16 Assert(Box<AssertStatement>), 17 Expression(Box<Expression>), 18 Block(Box<Block>), 19} 20 21impl Statement { 22 pub(crate) fn parse(parser: &mut Parser) -> SyntaxResult<Self> { 23 let token = parser.peek(); 24 use TokenType::*; 25 match token.token_type { 26 KwLet => Ok(Self::Let(Box::new(LetStatement::parse(parser)?))), 27 KwIf => { 28 let expr = IfElseExpression::parse_statement(parser)?; 29 Ok(Self::If(Box::new(expr))) 30 } 31 KwMatch => Ok(Self::Match(Box::new(MatchExpression::parse(parser)?))), 32 KwWhile => Ok(Self::While(Box::new(WhileStatement::parse(parser)?))), 33 KwFor => Ok(Self::For(Box::new(ForStatement::parse(parser)?))), 34 KwDefer => Ok(Self::Defer(Box::new(DeferStatement::parse(parser)?))), 35 KwAssert => Ok(Self::Assert(Box::new(AssertStatement::parse(parser)?))), 36 OBrace => Ok(Self::Block(Box::new(Block::parse(parser)?))), 37 KwUsing => Ok(Self::Using(Box::new(UsingStatement::parse(parser, None)?))), 38 KwDo => { 39 let head = DoHead::parse(parser)?; 40 if parser.check(KwUsing).is_ok() { 41 Ok(Self::Using(Box::new(UsingStatement::parse( 42 parser, 43 Some(head), 44 )?))) 45 } else { 46 let procedure = DoExpression::parse_with_head(parser, head)?; 47 let expression = Expression::Do(Box::new(procedure)); 48 Ok(Self::Expression(Box::new(expression))) 49 } 50 } 51 _ => { 52 let expression = Expression::parse(parser)?; 53 if parser.check(IdentifierEq).is_ok() { 54 if !expression.is_lvalue() { 55 parser.error(SyntaxError::new( 56 expression.span(), 57 "cannot assign to an expression that is not a valid assignment target", 58 )); 59 } 60 Ok(Self::FunctionAssignment(Box::new( 61 FunctionAssignment::parse(parser, expression)?, 62 ))) 63 } else if parser 64 .check(AssignmentStatement::ASSIGNMENT_OPERATOR) 65 .is_ok() 66 { 67 if !expression.is_lvalue() { 68 parser.error(SyntaxError::new( 69 expression.span(), 70 "cannot assign to an expression that is not a valid assignment target", 71 )); 72 } 73 Ok(Self::Assignment(Box::new(AssignmentStatement::parse( 74 parser, expression, 75 )?))) 76 } else { 77 Ok(Self::Expression(Box::new(expression))) 78 } 79 } 80 } 81 } 82}