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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
//! This file contains the parse AST.

use proc_macro2::Ident;
use std::fmt;

pub(crate) use crate::ast::{ParamDecl, PredicateDecl, PredicateKind, RuleHead};

/// A positional argument `arg2`.
#[derive(Debug, Clone)]
pub(crate) enum PositionalArg {
    Ident(Ident),
    Wildcard,
}

impl fmt::Display for PositionalArg {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            PositionalArg::Ident(ident) => write!(f, "{}", ident),
            PositionalArg::Wildcard => write!(f, "_"),
        }
    }
}

/// A named argument `.param2=arg2`.
#[derive(Debug, Clone)]
pub(crate) struct NamedArg {
    pub param: Ident,
    pub arg: Ident,
}

/// The list of atom's arguments.
#[derive(Debug, Clone)]
pub(crate) enum ArgList {
    /// arg1, arg2
    Positional(Vec<PositionalArg>),
    /// .param1=arg1, .param2=arg2
    Named(Vec<NamedArg>),
}

impl fmt::Display for ArgList {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let mut first = true;
        match self {
            ArgList::Positional(args) => {
                for arg in args {
                    if first {
                        first = false;
                    } else {
                        write!(f, ", ")?;
                    }
                    write!(f, "{}", arg)?;
                }
            }
            ArgList::Named(args) => {
                for kwarg in args {
                    if first {
                        first = false;
                    } else {
                        write!(f, ", ")?;
                    }
                    write!(f, ".{}={}", kwarg.param, kwarg.arg)?;
                }
            }
        }
        Ok(())
    }
}

/// A richer type of atom, which can be negated, and used as
/// premises/hypotheses in rules.
#[derive(Debug, Clone)]
pub(crate) struct Literal {
    pub is_negated: bool,
    pub predicate: Ident,
    pub args: ArgList,
}

impl fmt::Display for Literal {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        if self.is_negated {
            write!(f, "!")?;
        }
        write!(f, "{}({})", self.predicate, self.args)
    }
}

/// A rule describing how to compute facts.
///
/// ```plain
/// Internal(x, y) :- Input(x, y).
/// ```
#[derive(Debug, Clone)]
pub(crate) struct Rule {
    pub head: RuleHead,
    pub body: Vec<Literal>,
}

impl fmt::Display for Rule {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{} :- ", self.head)?;
        let mut first = true;
        for literal in &self.body {
            if first {
                first = false;
            } else {
                write!(f, ", ")?;
            }
            write!(f, "{}", literal)?;
        }
        write!(f, ".")
    }
}

/// Items present in the program.
#[derive(Debug, Clone)]
pub(crate) enum ProgramItem {
    PredicateDecl(PredicateDecl),
    Rule(Rule),
}

impl fmt::Display for ProgramItem {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            ProgramItem::PredicateDecl(decl) => write!(f, "{}", decl),
            ProgramItem::Rule(rule) => write!(f, "{}", rule),
        }
    }
}

/// A Datalog program.
#[derive(Debug, Clone)]
pub(crate) struct Program {
    pub items: Vec<ProgramItem>,
}

impl fmt::Display for Program {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        for item in &self.items {
            writeln!(f, "{}", item)?;
        }
        Ok(())
    }
}