chisel/
solidity_helper.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
//! SolidityHelper
//!
//! This module contains the `SolidityHelper`, a [rustyline::Helper] implementation for
//! usage in Chisel. It is ported from [soli](https://github.com/jpopesculian/soli/blob/master/src/main.rs).

use crate::{
    dispatcher::PROMPT_ARROW,
    prelude::{ChiselCommand, COMMAND_LEADER, PROMPT_ARROW_STR},
};
use rustyline::{
    completion::Completer,
    highlight::Highlighter,
    hint::Hinter,
    validate::{ValidationContext, ValidationResult, Validator},
    Helper,
};
use solar_parse::{
    interface::{Pos, Session, SessionGlobals},
    token::{Token, TokenKind},
    Lexer,
};
use std::{borrow::Cow, ops::Range, str::FromStr};
use yansi::{Color, Style};

/// The maximum length of an ANSI prefix + suffix characters using [SolidityHelper].
///
/// * 5 - prefix:
///   * 2 - start: `\x1B[`
///   * 2 - fg: `3<fg_code>`
///   * 1 - end: `m`
/// * 4 - suffix: `\x1B[0m`
const MAX_ANSI_LEN: usize = 9;

/// A rustyline helper for Solidity code
pub struct SolidityHelper {
    errored: bool,

    do_paint: bool,
    sess: Session,
    globals: SessionGlobals,
}

impl Default for SolidityHelper {
    fn default() -> Self {
        Self::new()
    }
}

impl SolidityHelper {
    /// Create a new SolidityHelper.
    pub fn new() -> Self {
        Self {
            errored: false,
            do_paint: yansi::is_enabled(),
            sess: Session::builder().with_silent_emitter(None).build(),
            globals: SessionGlobals::new(),
        }
    }

    /// Returns whether the helper is in an errored state.
    pub fn errored(&self) -> bool {
        self.errored
    }

    /// Set the errored field.
    pub fn set_errored(&mut self, errored: bool) -> &mut Self {
        self.errored = errored;
        self
    }

    /// Highlights a Solidity source string.
    pub fn highlight<'a>(&self, input: &'a str) -> Cow<'a, str> {
        if !self.do_paint() {
            return Cow::Borrowed(input)
        }

        // Highlight commands separately
        if input.starts_with(COMMAND_LEADER) {
            let (cmd, rest) = match input.split_once(' ') {
                Some((cmd, rest)) => (cmd, Some(rest)),
                None => (input, None),
            };
            let cmd = cmd.strip_prefix(COMMAND_LEADER).unwrap_or(cmd);

            let mut out = String::with_capacity(input.len() + MAX_ANSI_LEN);

            // cmd
            out.push(COMMAND_LEADER);
            let cmd_res = ChiselCommand::from_str(cmd);
            let style = (if cmd_res.is_ok() { Color::Green } else { Color::Red }).foreground();
            Self::paint_unchecked(cmd, style, &mut out);

            // rest
            match rest {
                Some(rest) if !rest.is_empty() => {
                    out.push(' ');
                    out.push_str(rest);
                }
                _ => {}
            }

            Cow::Owned(out)
        } else {
            let mut out = String::with_capacity(input.len() * 2);
            self.with_contiguous_styles(input, |style, range| {
                Self::paint_unchecked(&input[range], style, &mut out);
            });
            Cow::Owned(out)
        }
    }

    /// Returns a list of styles and the ranges they should be applied to.
    ///
    /// Covers the entire source string, including any whitespace.
    fn with_contiguous_styles(&self, input: &str, mut f: impl FnMut(Style, Range<usize>)) {
        self.enter(|sess| {
            let len = input.len();
            let mut index = 0;
            for token in Lexer::new(sess, input) {
                let range = token.span.lo().to_usize()..token.span.hi().to_usize();
                let style = token_style(&token);
                if index < range.start {
                    f(Style::default(), index..range.start);
                }
                index = range.end;
                f(style, range);
            }
            if index < len {
                f(Style::default(), index..len);
            }
        });
    }

    /// Validate that a source snippet is closed (i.e., all braces and parenthesis are matched).
    fn validate_closed(&self, input: &str) -> ValidationResult {
        let mut depth = [0usize; 3];
        self.enter(|sess| {
            for token in Lexer::new(sess, input) {
                match token.kind {
                    TokenKind::OpenDelim(delim) => {
                        depth[delim as usize] += 1;
                    }
                    TokenKind::CloseDelim(delim) => {
                        depth[delim as usize] = depth[delim as usize].saturating_sub(1);
                    }
                    _ => {}
                }
            }
        });
        if depth == [0; 3] {
            ValidationResult::Valid(None)
        } else {
            ValidationResult::Incomplete
        }
    }

    /// Formats `input` with `style` into `out`, without checking `style.wrapping` or
    /// `self.do_paint`.
    fn paint_unchecked(string: &str, style: Style, out: &mut String) {
        if style == Style::default() {
            out.push_str(string);
        } else {
            let _ = style.fmt_prefix(out);
            out.push_str(string);
            let _ = style.fmt_suffix(out);
        }
    }

    fn paint_unchecked_owned(string: &str, style: Style) -> String {
        let mut out = String::with_capacity(MAX_ANSI_LEN + string.len());
        Self::paint_unchecked(string, style, &mut out);
        out
    }

    /// Returns whether to color the output.
    fn do_paint(&self) -> bool {
        self.do_paint
    }

    /// Enters the session.
    fn enter(&self, f: impl FnOnce(&Session)) {
        self.globals.set(|| self.sess.enter(|| f(&self.sess)));
    }
}

impl Highlighter for SolidityHelper {
    fn highlight<'l>(&self, line: &'l str, _pos: usize) -> Cow<'l, str> {
        self.highlight(line)
    }

    fn highlight_char(&self, line: &str, pos: usize, _forced: bool) -> bool {
        pos == line.len()
    }

    fn highlight_prompt<'b, 's: 'b, 'p: 'b>(
        &'s self,
        prompt: &'p str,
        _default: bool,
    ) -> Cow<'b, str> {
        if !self.do_paint() {
            return Cow::Borrowed(prompt)
        }

        let mut out = prompt.to_string();

        // `^(\(ID: .*?\) )? ➜ `
        if prompt.starts_with("(ID: ") {
            let id_end = prompt.find(')').unwrap();
            let id_span = 5..id_end;
            let id = &prompt[id_span.clone()];
            out.replace_range(
                id_span,
                &Self::paint_unchecked_owned(id, Color::Yellow.foreground()),
            );
            out.replace_range(1..=2, &Self::paint_unchecked_owned("ID", Color::Cyan.foreground()));
        }

        if let Some(i) = out.find(PROMPT_ARROW) {
            let style =
                if self.errored { Color::Red.foreground() } else { Color::Green.foreground() };
            out.replace_range(i..=i + 2, &Self::paint_unchecked_owned(PROMPT_ARROW_STR, style));
        }

        Cow::Owned(out)
    }
}

impl Validator for SolidityHelper {
    fn validate(&self, ctx: &mut ValidationContext<'_>) -> rustyline::Result<ValidationResult> {
        Ok(self.validate_closed(ctx.input()))
    }
}

impl Completer for SolidityHelper {
    type Candidate = String;
}

impl Hinter for SolidityHelper {
    type Hint = String;
}

impl Helper for SolidityHelper {}

#[allow(non_upper_case_globals)]
#[deny(unreachable_patterns)]
fn token_style(token: &Token) -> Style {
    use solar_parse::{
        interface::kw::*,
        token::{TokenKind::*, TokenLitKind::*},
    };

    match token.kind {
        Literal(Str | HexStr | UnicodeStr, _) => Color::Green.foreground(),
        Literal(..) => Color::Yellow.foreground(),

        Ident(
            Memory | Storage | Calldata | Public | Private | Internal | External | Constant |
            Pure | View | Payable | Anonymous | Indexed | Abstract | Virtual | Override |
            Modifier | Immutable | Unchecked,
        ) => Color::Cyan.foreground(),

        Ident(s) if s.is_elementary_type() => Color::Blue.foreground(),
        Ident(Mapping) => Color::Blue.foreground(),

        Ident(s) if s.is_used_keyword() || s.is_yul_keyword() => Color::Magenta.foreground(),
        Arrow | FatArrow => Color::Magenta.foreground(),

        Comment(..) => Color::Primary.dim(),

        _ => Color::Primary.foreground(),
    }
}