taskforge/query/
ast.rs

1// Copyright 2018 Mathew Robinson <chasinglogic@gmail.com>. All rights reserved. Use of this source code is
2// governed by the Apache-2.0 license that can be found in the LICENSE file.
3
4
5//! Documentation about the Taskforge Query Language Abstract Syntax
6//! Tree (AST) and Expressions
7
8use super::token::{Operator, Token};
9use chrono::prelude::*;
10use std::fmt;
11
12/// The AST is the primary way a list will interact with Taskforge Queries.
13///
14/// Documentation about AST's is beyond the scope of this
15/// document. For information and an example of writing a Taskforge
16/// Query Language compiler see:
17///
18/// [Implementing a Taskforge List](/docs/development/building_a_list.html#Writing-A-Compiler)
19#[derive(Debug, Clone, PartialEq)]
20pub struct AST {
21    pub expression: Expression,
22}
23
24impl AST {
25    pub fn empty() -> AST {
26        AST {
27            expression: Expression::Invalid("".to_string()),
28        }
29    }
30}
31
32impl fmt::Display for AST {
33    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
34        self.expression.fmt(f)
35    }
36}
37
38/// Expression is an enum representing a value parsed by the TFQL
39/// parser.
40#[derive(Debug, Clone, PartialEq)]
41pub enum Expression {
42    String(String),
43    Number(f64),
44    Date(DateTime<Local>),
45    Bool(bool),
46
47    Invalid(String),
48
49    Infix(Box<Expression>, Operator, Box<Expression>),
50}
51
52impl fmt::Display for Expression {
53    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
54        match self {
55            Expression::String(s) => write!(f, "'{}'", s),
56            // debug printing properly prints decimal places
57            Expression::Number(n) => write!(f, "{:?}", n),
58            Expression::Date(d) => write!(f, "{}", d),
59            Expression::Bool(b) => write!(f, "{}", b),
60            Expression::Invalid(s) => write!(f, "INVALID: {}", s),
61            Expression::Infix(left, op, right) => write!(
62                f,
63                "({} {} {})",
64                match left.as_ref() {
65                    Expression::String(s) => s.to_string(),
66                    _ => format!("{}", left),
67                },
68                format!("{}", op),
69                format!("{}", right)
70            ),
71        }
72    }
73}
74
75impl<'a> From<&'a str> for Expression {
76    fn from(s: &'a str) -> Expression {
77        Expression::from(Token::from(s))
78    }
79}
80
81impl From<f64> for Expression {
82    fn from(num: f64) -> Expression {
83        Expression::from(Token::from(num))
84    }
85}
86
87impl From<bool> for Expression {
88    fn from(b: bool) -> Expression {
89        Expression::from(Token::from(b))
90    }
91}
92
93impl From<String> for Expression {
94    fn from(s: String) -> Expression {
95        Expression::from(Token::from(s))
96    }
97}
98
99/// From<Token> turns a Token which represents a literal value to a
100/// literal Expression
101impl From<Token> for Expression {
102    fn from(t: Token) -> Expression {
103        match t {
104            Token::Bool(b) => Expression::Bool(b),
105            Token::Str(s) => Expression::String(s),
106            Token::Float(f) => Expression::Number(f),
107            Token::Date(dte) => Expression::Date(dte),
108            _ => Expression::Invalid("not a literal".to_string()),
109        }
110    }
111}