paralegal_compiler/parsers/
variable_intro.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
use nom::{
    branch::alt,
    character::complete::space0,
    combinator::peek,
    error::context,
    sequence::{delimited, separated_pair, terminated, tuple},
};
use nom_supreme::tag::complete::tag;

use super::{
    shared::{marker, variable},
    Res,
};
use crate::common::ast::*;

pub fn variable_def(s: &str) -> Res<&str, VariableIntro> {
    let (remainder, variable) = variable(s)?;
    Ok((
        remainder,
        VariableIntro {
            variable,
            intro: VariableIntroType::Variable,
        },
    ))
}

pub fn variable_marked(s: &str) -> Res<&str, VariableIntro> {
    let mut combinator = separated_pair(variable, tag("marked"), marker);
    let (remainder, (variable, marker)) = combinator(s)?;
    Ok((
        remainder,
        VariableIntro {
            variable,
            intro: VariableIntroType::VariableMarked {
                marker,
                on_type: false,
            },
        },
    ))
}

fn variable_type_marked(s: &str) -> Res<&str, VariableIntro> {
    let mut combinator = separated_pair(variable, tag("type marked"), marker);
    let (remainder, (variable, marker)) = combinator(s)?;
    Ok((
        remainder,
        VariableIntro {
            variable,
            intro: VariableIntroType::VariableMarked {
                marker,
                on_type: true,
            },
        },
    ))
}

fn variable_source_of(s: &str) -> Res<&str, VariableIntro> {
    let mut combinator = separated_pair(variable, tag("that produces"), variable);
    let (remainder, (source_of_var, var)) = combinator(s)?;
    Ok((
        remainder,
        VariableIntro {
            variable: source_of_var,
            intro: VariableIntroType::VariableSourceOf(var),
        },
    ))
}

fn roots(s: &str) -> Res<&str, VariableIntro> {
    let mut combinator = terminated(variable, tag("input"));
    let (remainder, var) = combinator(s)?;
    Ok((
        remainder,
        VariableIntro {
            variable: var,
            intro: VariableIntroType::Roots,
        },
    ))
}

fn nodes(s: &str) -> Res<&str, VariableIntro> {
    let mut combinator = terminated(variable, tag("item"));
    let (remainder, var) = combinator(s)?;
    Ok((
        remainder,
        VariableIntro {
            variable: var,
            intro: VariableIntroType::AllNodes,
        },
    ))
}

pub fn variable_intro(s: &str) -> Res<&str, VariableIntro> {
    let (remainder, _) = context(
        "a variable introduction, e.g. \"sensitive\"",
        tuple((space0, peek(variable))),
    )(s)?;
    delimited(
        space0,
        alt((
            roots,
            nodes,
            variable_type_marked,
            variable_source_of,
            variable_marked,
            // must try this last b/c it'll partially consume the above
            variable_def,
        )),
        space0,
    )(remainder)
}