cairo_lang_parser/
colored_printer.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
use cairo_lang_syntax::node::db::SyntaxGroup;
use cairo_lang_syntax::node::green::GreenNodeDetails;
use cairo_lang_syntax::node::kind::SyntaxKind;
use cairo_lang_syntax::node::SyntaxNode;
use colored::{ColoredString, Colorize};
use smol_str::SmolStr;

struct ColoredPrinter<'a> {
    db: &'a dyn SyntaxGroup,
    /// Whether to also print empty and missing tokens/nodes
    verbose: bool,
    result: String,
}
impl<'a> ColoredPrinter<'a> {
    fn print(&mut self, syntax_node: &SyntaxNode) {
        let node = syntax_node.green_node(self.db);
        match &node.details {
            GreenNodeDetails::Token(text) => {
                if self.verbose && node.kind == SyntaxKind::TokenMissing {
                    self.result.push_str(format!("{}", "<m>".red()).as_str());
                } else {
                    self.result.push_str(set_color(text.clone(), node.kind).to_string().as_str());
                }
            }
            GreenNodeDetails::Node { .. } => {
                if self.verbose && is_missing_kind(node.kind) {
                    self.result.push_str(format!("{}", "<m>".red()).as_str());
                } else if self.verbose && is_empty_kind(node.kind) {
                    self.result.push_str(format!("{}", "<e>".red()).as_str());
                } else {
                    for child in self.db.get_children(syntax_node.clone()).iter() {
                        self.print(child);
                    }
                }
            }
        }
    }
}

// TODO(yuval): autogenerate both.
fn is_missing_kind(kind: SyntaxKind) -> bool {
    matches!(kind, SyntaxKind::ExprMissing | SyntaxKind::StatementMissing)
}

// TODO(yuval): Move to SyntaxKind.
pub fn is_empty_kind(kind: SyntaxKind) -> bool {
    matches!(
        kind,
        SyntaxKind::OptionStructArgExprEmpty
            | SyntaxKind::OptionTypeClauseEmpty
            | SyntaxKind::OptionReturnTypeClauseEmpty
            | SyntaxKind::OptionTerminalSemicolonEmpty
            | SyntaxKind::OptionTerminalColonColonEmpty
            | SyntaxKind::OptionWrappedGenericParamListEmpty
    )
}

fn set_color(text: SmolStr, kind: SyntaxKind) -> ColoredString {
    // TODO(yuval): use tags on SyntaxKind
    match kind {
        SyntaxKind::TokenIdentifier => text.truecolor(255, 255, 100), // Yellow
        SyntaxKind::TokenPlus
        | SyntaxKind::TokenMinus
        | SyntaxKind::TokenMul
        | SyntaxKind::TokenDiv
        | SyntaxKind::TokenMod
        | SyntaxKind::TokenDot => text.bright_magenta(),
        SyntaxKind::TokenLiteralNumber
        | SyntaxKind::TokenFalse
        | SyntaxKind::TokenTrue
        | SyntaxKind::TokenShortString
        | SyntaxKind::TokenString => text.bright_cyan(),
        SyntaxKind::TokenExtern
        | SyntaxKind::TokenType
        | SyntaxKind::TokenFunction
        | SyntaxKind::TokenModule
        | SyntaxKind::TokenEnum
        | SyntaxKind::TokenStruct
        | SyntaxKind::TokenTrait
        | SyntaxKind::TokenImpl => text.bright_blue(),
        SyntaxKind::TokenOf
        | SyntaxKind::TokenLet
        | SyntaxKind::TokenReturn
        | SyntaxKind::TokenMatch
        | SyntaxKind::TokenIf
        | SyntaxKind::TokenElse
        | SyntaxKind::TokenUse
        | SyntaxKind::TokenImplicits
        | SyntaxKind::TokenRef
        | SyntaxKind::TokenMut
        | SyntaxKind::TokenNoPanic => text.bright_blue(),
        SyntaxKind::TokenArrow
        | SyntaxKind::TokenMatchArrow
        | SyntaxKind::TokenColon
        | SyntaxKind::TokenColonColon
        | SyntaxKind::TokenDotDot
        | SyntaxKind::TokenSemicolon
        | SyntaxKind::TokenAnd
        | SyntaxKind::TokenAndAnd
        | SyntaxKind::TokenOr
        | SyntaxKind::TokenOrOr
        | SyntaxKind::TokenXor
        | SyntaxKind::TokenNot
        | SyntaxKind::TokenQuestionMark
        | SyntaxKind::TokenUnderscore
        | SyntaxKind::TokenHash => text.truecolor(255, 180, 255), // Pink
        SyntaxKind::TokenEq
        | SyntaxKind::TokenEqEq
        | SyntaxKind::TokenGE
        | SyntaxKind::TokenGT
        | SyntaxKind::TokenLE
        | SyntaxKind::TokenLT
        | SyntaxKind::TokenNeq => {
            text.truecolor(255, 165, 0) // Orange
        }
        SyntaxKind::TokenLBrace
        | SyntaxKind::TokenRBrace
        | SyntaxKind::TokenLBrack
        | SyntaxKind::TokenRBrack
        | SyntaxKind::TokenLParen
        | SyntaxKind::TokenRParen
        | SyntaxKind::TokenComma => text.clear(),
        SyntaxKind::TokenEndOfFile => text.clear(),
        SyntaxKind::TokenBadCharacters => text.red(),
        SyntaxKind::TokenMissing => text.clear(),
        SyntaxKind::TokenSkipped => text.on_red(), // red background
        SyntaxKind::TokenSingleLineComment
        | SyntaxKind::TokenWhitespace
        | SyntaxKind::TokenNewline
        | SyntaxKind::TokenEmpty => text.clear(),
        // TODO(yuval): Can this be made exhaustive?
        _ => panic!("Unexpected syntax kind: {kind:?}"),
    }
}

pub fn print_colored(db: &dyn SyntaxGroup, syntax_root: &SyntaxNode, verbose: bool) -> String {
    let mut printer = ColoredPrinter { db, verbose, result: "".to_string() };
    printer.print(syntax_root);
    printer.result
}