Actually just three programming languages in a trenchcoat
at main 43 lines 1.2 kB view raw
1use super::*; 2use crate::{Parser, Spanned}; 3use source_span::Span; 4use trilogy_scanner::{Token, TokenType}; 5 6#[derive(Clone, Debug)] 7pub struct ForStatement { 8 pub r#for: Token, 9 pub query: Query, 10 pub body: Block, 11 pub span: Span, 12} 13 14impl Spanned for ForStatement { 15 fn span(&self) -> Span { 16 self.span 17 } 18} 19 20impl ForStatement { 21 pub(crate) fn parse(parser: &mut Parser) -> SyntaxResult<Self> { 22 let r#for = parser.expect(TokenType::KwFor).unwrap(); 23 let query = Query::parse(parser)?; 24 let body = Block::parse(parser)?; 25 Ok(Self { 26 span: r#for.span.union(body.span()), 27 r#for, 28 query, 29 body, 30 }) 31 } 32} 33 34#[cfg(test)] 35mod test { 36 use super::*; 37 38 test_parse!(for_in: "for x in xs {}" => ForStatement::parse => ForStatement { .. }); 39 test_parse!(for_lookup: "for check(a, b, 3) {}" => ForStatement::parse => ForStatement { .. }); 40 test_parse!(for_body: "for check(a, b, 3) { break unit }" => ForStatement::parse => ForStatement { .. }); 41 test_parse_error!(for_query_expr: "for a + b { break }" => ForStatement::parse); 42 test_parse_error!(for_body_expr: "for check(a, b) (a + b)" => ForStatement::parse); 43}