Actually just three programming languages in a trenchcoat
at string-repr-callable 52 lines 1.5 kB view raw
1use super::*; 2use crate::{Parser, Spanned}; 3use source_span::Span; 4use trilogy_scanner::{Token, TokenType::*}; 5 6/// A boolean query. 7/// 8/// ```trilogy 9/// is expression 10/// ``` 11#[derive(Clone, Debug, PrettyPrintSExpr)] 12pub struct BooleanQuery { 13 pub is: Token, 14 pub expression: Expression, 15 span: Span, 16} 17 18impl BooleanQuery { 19 pub(crate) fn parse(parser: &mut Parser) -> SyntaxResult<Self> { 20 let is = parser.expect(KwIs).unwrap(); 21 let expression = Expression::parse_parameter_list(parser)?.map_err(|patt| { 22 let error = SyntaxError::new( 23 patt.span(), 24 "expected an expression after `is`, but found a pattern", 25 ); 26 parser.error(error.clone()); 27 error 28 })?; // this isn't a parameter list, but we don't allow commas 29 Ok(Self { 30 span: is.span.union(expression.span()), 31 is, 32 expression, 33 }) 34 } 35} 36 37impl Spanned for BooleanQuery { 38 fn span(&self) -> Span { 39 self.span 40 } 41} 42 43#[cfg(test)] 44mod test { 45 use super::*; 46 47 test_parse!(bool_query_simple: "is true" => BooleanQuery::parse => "(BooleanQuery _ _)"); 48 test_parse!(bool_query_expression: "is x < 5" => BooleanQuery::parse => "(BooleanQuery _ _)"); 49 test_parse!(bool_query_application: "is f x y" => BooleanQuery::parse => "(BooleanQuery _ _)"); 50 test_parse_error!(bool_query_commas: "is x, x" => BooleanQuery::parse); 51 test_parse_error!(bool_query_invalid_expr: "is { let x = 5 }" => BooleanQuery::parse); 52}