scnr/internal/
dot.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
//! The `dot` module contains the conversion from an finite automata to a graphviz dot format.
//! The functions in this module are used for testing and debugging purposes.

use std::io::Write;

use dot_writer::{Attributes, DotWriter, RankDirection, Scope};

use crate::internal::compiled_nfa::CompiledNfa;

use super::{nfa::Nfa, CharClassID, CharacterClassRegistry, MultiPatternNfa, StateID};

/// Render the NFA to a graphviz dot format.
#[allow(dead_code)]
pub(crate) fn nfa_render<W: Write>(nfa: &Nfa, label: &str, output: &mut W) {
    let mut writer = DotWriter::from(output);
    writer.set_pretty_print(true);
    let mut digraph = writer.digraph();
    digraph
        .set_label(format!("{}: {}", label, nfa.pattern()).as_str())
        .set_rank_direction(RankDirection::LeftRight);
    for state in nfa.states() {
        let source_id = {
            let mut source_node = digraph.node_auto();
            source_node.set_label(&state.id().as_usize().to_string());
            if state.id() == nfa.start_state() {
                source_node
                    .set_shape(dot_writer::Shape::Circle)
                    .set_color(dot_writer::Color::Blue)
                    .set_pen_width(3.0);
            }
            if state.id() == nfa.end_state() {
                source_node
                    .set_shape(dot_writer::Shape::Circle)
                    .set_color(dot_writer::Color::Red)
                    .set_pen_width(3.0);
            }
            source_node.id()
        };
        for transition in state.transitions() {
            let target_state = transition.target_state();
            digraph
                .edge(
                    source_id.clone(),
                    format!("node_{}", target_state.as_usize()),
                )
                .attributes()
                .set_label(
                    &format!("#{}:'{}'", transition.char_class(), transition.ast())
                        .escape_default()
                        .to_string(),
                );
        }
        for epsilon_transition in state.epsilon_transitions() {
            let target_state = epsilon_transition.target_state();
            digraph
                .edge(
                    source_id.clone(),
                    format!("node_{}", target_state.as_usize()),
                )
                .attributes()
                .set_label("ε");
        }
    }
}

fn render_compiled_nfa(
    compiled_nfa: &CompiledNfa,
    node_prefix: &str,
    character_class_registry: &CharacterClassRegistry,
    graph: &mut Scope,
) {
    // Render the states of the NFA
    for id in 0..compiled_nfa.states.len() {
        let node_name = format!("\"{}{}\"", node_prefix, id);
        let mut source_node = graph.node_named(&node_name);
        if id == 0 {
            // Start state of the compiled NFA
            source_node
                .set_shape(dot_writer::Shape::Circle)
                .set_color(dot_writer::Color::Blue)
                .set_pen_width(3.0);
            source_node.set_label(&id.to_string());
        } else if compiled_nfa.end_states[id].0 {
            source_node
                .set_shape(dot_writer::Shape::Circle)
                .set_color(dot_writer::Color::Red)
                .set_pen_width(3.0);
            source_node.set_label(&format!("{} T{}", id, compiled_nfa.end_states[id].1));
        } else {
            source_node.set_label(&id.to_string());
        }
    }
    // Render the transitions of the NFA
    for (id, state) in compiled_nfa.states.iter().enumerate() {
        for (cc, next) in state.transitions.iter() {
            // Label the edge with the character class used to transition to the target state.
            graph
                .edge(
                    format!("\"{}{}\"", node_prefix, id),
                    format!("\"{}{}\"", node_prefix, next.as_usize()),
                )
                .attributes()
                .set_label(&format!(
                    "{} (C#{})",
                    character_class_registry.get_character_class(*cc).map_or(
                        "-".to_string(),
                        |cc| cc.ast().to_string().escape_debug().to_string()
                    ),
                    cc.id()
                ));
        }
    }
}

// Render a compiled NFA
pub(crate) fn compiled_nfa_render<W: Write>(
    compiled_nfa: &CompiledNfa,
    label: &str,
    character_class_registry: &CharacterClassRegistry,
    output: &mut W,
) {
    let mut writer = DotWriter::from(output);
    writer.set_pretty_print(true);
    let mut digraph = writer.digraph();
    digraph
        .set_label(
            format!(
                "{}: {}...",
                label,
                compiled_nfa.pattern(0.into()).escape_default()
            )
            .as_str(),
        )
        .set_rank_direction(RankDirection::LeftRight);

    render_compiled_nfa(compiled_nfa, "", character_class_registry, &mut digraph);

    // Render the lookaheads of the NFA each into a separate cluster
    for (terminal_id, lookahead) in compiled_nfa.lookaheads.iter() {
        let mut cluster = digraph.cluster();
        cluster.set_label(&format!(
            "LA for T{}({})",
            terminal_id,
            if lookahead.is_positive { "Pos" } else { "Neg" }
        ));
        let node_prefix = format!("{}_", terminal_id);
        render_compiled_nfa(
            &lookahead.nfa,
            &node_prefix,
            character_class_registry,
            &mut cluster,
        );
    }
}

/// Render a MultiPatternNfa to a graphviz dot format.
#[allow(dead_code)]
pub(crate) fn multi_pattern_nfa_render<W: Write>(
    multi_pattern_nfa: &MultiPatternNfa,
    label: &str,
    character_class_registry: &CharacterClassRegistry,
    output: &mut W,
) {
    let mut writer = DotWriter::from(output);
    writer.set_pretty_print(true);
    let mut digraph = writer.digraph();
    digraph
        .set_label(label)
        .set_rank_direction(RankDirection::LeftRight);

    let node_ids = multi_pattern_nfa.nfas.iter().fold(vec![0], |mut acc, nfa| {
        nfa.states()
            .iter()
            .for_each(|state| acc.push(state.id().id()));
        acc
    });

    // Add the start transitions of the MultiPatternNfa to the epsilon transitions
    let mut epsilon_transitions = multi_pattern_nfa.start_transitions().iter().fold(
        Vec::<(usize, usize)>::new(),
        |mut acc, t| {
            acc.push((0, t.target_state().as_usize()));
            acc
        },
    );
    // Add the epsilon transitions of the NFAs to the epsilon transitions
    multi_pattern_nfa.nfas.iter().for_each(|nfa| {
        nfa.states().iter().for_each(|state| {
            state.epsilon_transitions().iter().for_each(|t| {
                epsilon_transitions.push((state.id().as_usize(), t.target_state().as_usize()));
            });
        });
    });

    // Add the transitions of the NFAs to the transitions
    let transitions = multi_pattern_nfa.nfas.iter().fold(
        Vec::<(StateID, StateID, CharClassID)>::new(),
        |mut acc, nfa| {
            nfa.states().iter().for_each(|state| {
                state.transitions().iter().for_each(|t| {
                    acc.push((state.id(), t.target_state(), t.char_class()));
                });
            });
            acc
        },
    );

    for node_id in node_ids {
        let mut source_node = digraph.node_auto();
        source_node.set_label(&node_id.to_string());
        if node_id == 0 {
            source_node
                .set_shape(dot_writer::Shape::Circle)
                .set_color(dot_writer::Color::Blue)
                .set_pen_width(3.0);
        }
        if multi_pattern_nfa.is_accepting_state(node_id.into()) {
            source_node
                .set_shape(dot_writer::Shape::Circle)
                .set_color(dot_writer::Color::Red)
                .set_pen_width(3.0);
        }
    }

    for (source_id, target_id) in epsilon_transitions {
        digraph
            .edge(format!("node_{}", source_id), format!("node_{}", target_id))
            .attributes()
            .set_label("ε");
    }

    for (source_id, target_id, char_class_id) in transitions {
        digraph
            .edge(
                format!("node_{}", source_id.as_usize()),
                format!("node_{}", target_id.as_usize()),
            )
            .attributes()
            .set_label(&format!(
                "{}:{}",
                character_class_registry
                    .get_character_class(char_class_id)
                    .map_or("-".to_string(), |cc| cc
                        .ast()
                        .to_string()
                        .escape_default()
                        .to_string()),
                char_class_id.id()
            ));
    }
}