paralegal_compiler/parsers/
clause.rs

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
use super::{
    relations::*,
    shared::*,
    variable_intro::{variable_def, variable_intro, variable_marked},
    Res,
};
use crate::common::ast::*;
use nom::{
    branch::alt,
    character::complete::space1,
    combinator::map,
    multi::many0,
    sequence::{delimited, pair, preceded, tuple},
};

use nom_supreme::tag::complete::tag;

fn l4_clause(s: &str) -> Res<&str, ASTNode> {
    let mut combinator = tuple((
        preceded(l4_bullet, alt((for_each, there_is, conditional))),
        l5_relations,
    ));
    let (remainder, (intro, body)) = combinator(s)?;
    Ok((remainder, ASTNode::Clause(Box::new(Clause { intro, body }))))
}

pub fn l4_clauses(s: &str) -> Res<&str, ASTNode> {
    map(
        pair(
            l4_clause,
            many0(pair(operator, alt((l4_clause, l4_relations)))),
        ),
        join_nodes,
    )(s)
}

fn l3_clause(s: &str) -> Res<&str, ASTNode> {
    let mut combinator = tuple((
        preceded(l3_bullet, alt((for_each, there_is, conditional))),
        alt((l4_relations, l4_clauses)),
    ));
    let (remainder, (intro, body)) = combinator(s)?;
    Ok((remainder, ASTNode::Clause(Box::new(Clause { intro, body }))))
}

pub fn l3_clauses(s: &str) -> Res<&str, ASTNode> {
    map(
        pair(
            l3_clause,
            many0(pair(operator, alt((l3_clause, l3_relations)))),
        ),
        join_nodes,
    )(s)
}

fn l2_clause(s: &str) -> Res<&str, ASTNode> {
    let mut combinator = tuple((
        preceded(l2_bullet, alt((for_each, there_is, conditional))),
        alt((l3_relations, l3_clauses)),
    ));
    let (remainder, (intro, body)) = combinator(s)?;
    Ok((remainder, ASTNode::Clause(Box::new(Clause { intro, body }))))
}

pub fn l2_clauses(s: &str) -> Res<&str, ASTNode> {
    map(
        pair(
            l2_clause,
            many0(pair(operator, alt((l2_clause, l2_relations)))),
        ),
        join_nodes,
    )(s)
}

fn l1_clause(s: &str) -> Res<&str, ASTNode> {
    let mut combinator = tuple((
        preceded(l1_bullet, alt((for_each, there_is))),
        alt((l2_relations, l2_clauses)),
    ));
    let (remainder, (intro, body)) = combinator(s)?;
    Ok((remainder, ASTNode::Clause(Box::new(Clause { intro, body }))))
}

pub fn l1_clauses(s: &str) -> Res<&str, ASTNode> {
    map(
        pair(
            alt((l1_clause, only_via)),
            many0(tuple((operator, alt((l1_clause, only_via))))),
        ),
        join_nodes,
    )(s)
}

fn only_via(s: &str) -> Res<&str, ASTNode> {
    let mut combinator = tuple((
        // these are only allowed to be present at the top level, hence the L1 bullet restriction
        delimited(
            tuple((l1_bullet, tag("Each"), space1)),
            variable_intro,
            tag("goes to a"),
        ),
        map(
            pair(
                alt((variable_marked, variable_def)),
                many0(tuple((operator, alt((variable_marked, variable_def))))),
            ),
            join_variable_intros,
        ),
        preceded(
            tag("only via a"),
            map(
                pair(
                    alt((variable_marked, variable_def)),
                    many0(tuple((operator, alt((variable_marked, variable_def))))),
                ),
                join_variable_intros,
            ),
        ),
    ));
    let (remainder, (src, sink, checkpoint)) = combinator(s)?;

    Ok((remainder, ASTNode::OnlyVia(src, sink, checkpoint)))
}

fn conditional(s: &str) -> Res<&str, ClauseIntro> {
    let mut combinator = delimited(tag("If"), relation, tuple((tag("then"), colon)));
    let (remainder, relation) = combinator(s)?;
    Ok((remainder, ClauseIntro::Conditional(relation)))
}

fn for_each(s: &str) -> Res<&str, ClauseIntro> {
    let mut combinator = delimited(tuple((tag("For each"), space1)), variable_intro, colon);
    let (remainder, var_intro) = combinator(s)?;
    Ok((remainder, ClauseIntro::ForEach(var_intro)))
}

fn there_is(s: &str) -> Res<&str, ClauseIntro> {
    let mut combinator = delimited(tag("There is a"), variable_intro, tag("where:"));
    let (remainder, var_intro) = combinator(s)?;
    Ok((remainder, ClauseIntro::ThereIs(var_intro)))
}