~ruther/gtkwave-tcl-generator

ref: f2309875e50e66ab61c32522d613d5420c557dfa gtkwave-tcl-generator/src/comment_parser.rs -rw-r--r-- 2.3 KiB
f2309875 — František Boháček feat: map new parsers to tcl generator inputs 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
use crate::display_elements::{DisplayColor, DisplayFormat};

pub enum Operation {
    UpdateContext(ContextUpdate),
    AddEmpty,
    AddSignal(String),
}

pub enum ContextUpdate {
    UpdateColor(Option<DisplayColor>),
    UpdateFormat(DisplayFormat),
    SetOmit(bool),
    Reset,
}

pub struct CommentParser {}

impl CommentParser {
    pub fn parse_comment(comment: &str) -> Vec<Operation> {
        comment
            .split(['\n', ','].as_ref())
            .map(|token| Self::parse_token(token))
            .filter(|operation| operation.is_some())
            .into_iter()
            .map(|operation| operation.unwrap())
            .collect()
    }

    fn parse_token(token: &str) -> Option<Operation> {
        Some(match token {
            "omit" => Operation::UpdateContext(ContextUpdate::SetOmit(true)),
            "empty" => Operation::AddEmpty,
            "reset" => Operation::UpdateContext(ContextUpdate::Reset),
            _ if token.starts_with("add signal ") => {
                Operation::AddSignal(token["add signal ".len()..].to_owned())
            }
            _ if token.starts_with("format ") => Operation::UpdateContext(
                ContextUpdate::UpdateFormat(Self::parse_format(&token["format ".len()..])),
            ),
            _ if token.starts_with("color ") => Operation::UpdateContext(
                ContextUpdate::UpdateColor(Self::parse_color(&token["color ".len()..])),
            ),
            _ => return None,
        })
    }

    fn parse_format(format: &str) -> DisplayFormat {
        match format {
            "hex" => DisplayFormat::Hex,
            "decimal" => DisplayFormat::Decimal,
            "signed decimal" => DisplayFormat::SignedDecimal,
            "binary" => DisplayFormat::Binary,
            _ => DisplayFormat::Decimal,
        }
    }

    fn parse_color(color: &str) -> Option<DisplayColor> {
        Some(match color {
            "normal" => DisplayColor::Normal,
            "red" => DisplayColor::Red,
            "orange" => DisplayColor::Orange,
            "yellow" => DisplayColor::Yellow,
            "green" => DisplayColor::Green,
            "blue" => DisplayColor::Blue,
            "Indigo" => DisplayColor::Indigo,
            "Violet" => DisplayColor::Violet,
            "Cycle" => DisplayColor::Cycle,
            _ => return None,
        })
    }
}
Do not follow this link