~ruther/gtkwave-tcl-generator

ref: 3fb1aa4ae032c5c5fc69d44b7b3c6566605fb7d3 gtkwave-tcl-generator/src/main.rs -rw-r--r-- 5.5 KiB
3fb1aa4a — František Boháček fix: make all matching colors lowercase 1 year, 7 months ago
                                                                                
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
pub mod comment_parser;
pub mod display_elements;
pub mod file_parser;
pub mod tcl_generator;

use std::{fs::File, io::Write, path::PathBuf};

use clap::{arg, Parser};
use comment_parser::{CommentParser, ContextUpdate, Operation};
use display_elements::DisplayFormat;
use file_parser::{FileParser, ParsedArchitecturePart, ParsedEntity};
use glob::glob;
use tcl_generator::TclGenerator;
use vhdl_lang::{syntax::Symbols, Source};

use crate::display_elements::DisplayColor;

#[derive(Parser)]
#[command(author, version, about, long_about = None)]
struct Cli {
    #[arg(short = 'f', long = "folder")]
    folder: PathBuf,
    #[arg(short = 't', long = "testbench")]
    testbench: String,
    #[arg(short = 'o', long = "output")]
    output: PathBuf,
}

fn main() {
    let cli = Cli::parse();
    let find_wildcard = cli.folder.to_str().unwrap().to_owned() + "/**/*.vhd";
    println!("Going to look into {find_wildcard}");

    let matching_files = glob(&find_wildcard).unwrap();

    let symbols = Symbols::default();

    let mut found = false;
    for file_result in matching_files {
        let file = file_result.unwrap();

        let source = Source::from_latin1_file(file.as_path()).unwrap();
        let contents = source.contents();
        let mut entities = FileParser::parse_file(&source, &contents, &symbols).unwrap();

        for entity in entities {
            if entity.name() != cli.testbench {
                continue;
            }
            found = true;
            println!("Found the testbench.");

            let tcl = generate_tcl(entity);

            let mut file = File::create(cli.output.clone()).unwrap();
            file.write_all(tcl.as_bytes()).unwrap();

            println!("Generated {}.", cli.output.display());

            break;
        }

        if found {
            break;
        }
    }

    if !found {
        println!("Could not find the entity.")
    }
}

#[derive(Eq, PartialEq, Clone)]
struct Context {
    color: Option<DisplayColor>,
    format: Option<DisplayFormat>,
    omit: bool,
}

impl Context {
    pub fn update<'a>(&mut self, updates: impl Iterator<Item = &'a ContextUpdate>) {
        for update in updates {
            match update {
                ContextUpdate::Reset => {
                    self.color = None;
                    self.format = None;
                    self.omit = false;
                }
                ContextUpdate::SetOmit(omit) => {
                    self.omit = omit.clone();
                }
                ContextUpdate::UpdateColor(color) => {
                    self.color = color.clone();
                }
                ContextUpdate::UpdateFormat(format) => {
                    self.format = Some(format.clone());
                }
            }
        }
    }

    pub fn fork<'a>(&self, updates: impl Iterator<Item = &'a ContextUpdate>) -> Context {
        let mut clone = self.clone();
        clone.update(updates);

        clone
    }

    pub fn decompose(&self) -> (Option<DisplayColor>, Option<DisplayFormat>, bool) {
        (self.color, self.format, self.omit)
    }
}

fn generate_tcl(entity: ParsedEntity) -> String {
    let architecture = entity.architecture().unwrap();

    let mut generator = TclGenerator::new("top.".to_owned() + entity.name() + ".");

    let mut context = Context {
        color: None,
        format: None,
        omit: false,
    };

    for part in architecture.parts() {
        match part {
            ParsedArchitecturePart::Comment(comment) => {
                let operations = CommentParser::parse_comment(&comment[..]);
                if let Some(operation) = operations.first() {
                    if let Operation::AddSignal(signal) = operation {
                        let context_operations = operations.iter().skip(1);
                        add_signal(
                            &mut generator,
                            signal.clone(),
                            Some(context_operations),
                            &context,
                        );
                    }
                } else {
                    let updates = operations.iter().filter_map(|op| match op {
                        Operation::UpdateContext(update) => Some(update),
                        Operation::AddEmpty => {
                            generator.add_empty();
                            None
                        }
                        _ => panic!(),
                    });
                    context.update(updates);
                }
            }
            ParsedArchitecturePart::Signal(signal) => {
                let mut operations = None;

                if let Some(comment) = signal.comment() {
                    operations = Some(CommentParser::parse_comment(comment));
                }

                add_signal(
                    &mut generator,
                    signal.name().to_owned(),
                    operations.as_ref().map(|x| x.iter()),
                    &context,
                );
            }
        }
    }

    generator.generate()
}

fn add_signal<'a>(
    generator: &mut TclGenerator,
    signal: String,
    operations: Option<impl Iterator<Item = &'a Operation>>,
    context: &Context,
) {
    let (color, format, omit) = if let Some(operations) = operations {
        let updates = operations.into_iter().filter_map(|op| {
            if let Operation::UpdateContext(update) = op {
                Some(update)
            } else {
                None
            }
        });

        context.fork(updates).decompose()
    } else {
        context.decompose()
    };

    generator.add_signal(signal, color, format);
}
Do not follow this link