1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
// Copyright 2018 Mathew Robinson <chasinglogic@gmail.com>. All rights reserved. Use of this source code is
// governed by the Apache-2.0 license that can be found in the LICENSE file.


//! Documentation about the Taskforge Query Language Abstract Syntax
//! Tree (AST) and Expressions

use super::token::{Operator, Token};
use chrono::prelude::*;
use std::fmt;

/// The AST is the primary way a list will interact with Taskforge Queries.
///
/// Documentation about AST's is beyond the scope of this
/// document. For information and an example of writing a Taskforge
/// Query Language compiler see:
///
/// [Implementing a Taskforge List](/docs/development/building_a_list.html#Writing-A-Compiler)
#[derive(Debug, Clone, PartialEq)]
pub struct AST {
    pub expression: Expression,
}

impl AST {
    pub fn empty() -> AST {
        AST {
            expression: Expression::Invalid("".to_string()),
        }
    }
}

impl fmt::Display for AST {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        self.expression.fmt(f)
    }
}

/// Expression is an enum representing a value parsed by the TFQL
/// parser.
#[derive(Debug, Clone, PartialEq)]
pub enum Expression {
    String(String),
    Number(f64),
    Date(DateTime<Local>),
    Bool(bool),

    Invalid(String),

    Infix(Box<Expression>, Operator, Box<Expression>),
}

impl fmt::Display for Expression {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Expression::String(s) => write!(f, "'{}'", s),
            // debug printing properly prints decimal places
            Expression::Number(n) => write!(f, "{:?}", n),
            Expression::Date(d) => write!(f, "{}", d),
            Expression::Bool(b) => write!(f, "{}", b),
            Expression::Invalid(s) => write!(f, "INVALID: {}", s),
            Expression::Infix(left, op, right) => write!(
                f,
                "({} {} {})",
                match left.as_ref() {
                    Expression::String(s) => s.to_string(),
                    _ => format!("{}", left),
                },
                format!("{}", op),
                format!("{}", right)
            ),
        }
    }
}

impl<'a> From<&'a str> for Expression {
    fn from(s: &'a str) -> Expression {
        Expression::from(Token::from(s))
    }
}

impl From<f64> for Expression {
    fn from(num: f64) -> Expression {
        Expression::from(Token::from(num))
    }
}

impl From<bool> for Expression {
    fn from(b: bool) -> Expression {
        Expression::from(Token::from(b))
    }
}

impl From<String> for Expression {
    fn from(s: String) -> Expression {
        Expression::from(Token::from(s))
    }
}

/// From<Token> turns a Token which represents a literal value to a
/// literal Expression
impl From<Token> for Expression {
    fn from(t: Token) -> Expression {
        match t {
            Token::Bool(b) => Expression::Bool(b),
            Token::Str(s) => Expression::String(s),
            Token::Float(f) => Expression::Number(f),
            Token::Date(dte) => Expression::Date(dte),
            _ => Expression::Invalid("not a literal".to_string()),
        }
    }
}