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
use crate::*;

pub use ansi_term::{self, ANSIGenericString, Color, Style};
use atty::is;

/// A structure for syntax highlighting pieces of JavaScript source code
/// using ANSI.
///
/// The highlighter will auto detect if stderr or stdout are terminals, if
/// they are not then it will return the original uncolored source code.
/// All errors encountered while lexing are ignored.
///
/// The highlighter is iterator based, which allows for coloring a part of code
/// at a time.
/// The highlighter's position can be controlled through various methods which allows
/// for reuse of the highlighter without the need to rescan the source code
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Highlighter<'s> {
    pub source: &'s str,
    tokens: Vec<Token>,
    /// Current token position
    cur: usize,
    /// Current byte index in source
    cur_idx: usize,
}

macro_rules! rgb {
    ($r:expr, $g:expr, $b:expr) => {
        Color::RGB($r, $g, $b)
    };
}

impl<'s> Highlighter<'s> {
    /// Make a new highlighter, this will invoke the lexer to get tokens.
    pub fn new(source: &'s str) -> Highlighter<'s> {
        let tokens = Lexer::from_str(source, 0).map(|t| t.0).collect();

        Self {
            source,
            tokens,
            cur: 0,
            cur_idx: 0,
        }
    }

    fn check_terminal(&self) -> bool {
        is(atty::Stream::Stderr) && is(atty::Stream::Stdout)
    }

    /// Reset the highlighter to the start of the source code
    pub fn reset(&mut self) {}

    /// Consume the rest of the highlighter's tokens and turn them into an ANSI colored string.
    /// This returns an unaltered string if stdout and stderr are not terminals.
    pub fn color(&mut self) -> String {
        if !self.check_terminal() {
            let ret = self.source[self.cur_idx..self.source.len()].to_string();
            self.cur = self.tokens.len();
            self.cur_idx = self.source.len();
            return ret;
        }

        self.map(|x| x.to_string()).collect()
    }

    fn src(&self) -> &'s str {
        &self.source[self.cur_idx..self.cur_idx + self.tokens.get(self.cur).unwrap().len]
    }
}

const PURPLE_IDENT: [&str; 4] = ["let", "class", "await", "yield"];
const BUILTINS: [&str; 27] = [
    "Math",
    "Promise",
    "Number",
    "String",
    "Date",
    "Infinity",
    "NaN",
    "undefined",
    "globalThis",
    "Object",
    "Function",
    "Symbol",
    "Boolean",
    "Error",
    "EvalError",
    "InternalError",
    "RangeError",
    "ReferenceError",
    "SyntaxError",
    "TypeError",
    "Number",
    "BigInt",
    "RegExp",
    "Array",
    "Map",
    "Set",
    "JSON",
];

impl<'s> Iterator for Highlighter<'s> {
    /// An individual colored token, you can see the color used by checking the string's style foreground
    type Item = ANSIGenericString<'s, str>;

    fn next(&mut self) -> Option<Self::Item> {
        if self.tokens.get(self.cur) == None {
            return None;
        }

        let color = match self.tokens.get(self.cur)?.kind {
            T!['{'] | T!['}'] | T!['('] | T![')'] => rgb![255, 215, 0],
            T![import] => rgb![97, 175, 239],
            T![ident] if PURPLE_IDENT.contains(&self.src()) => rgb![198, 120, 221],
            T![ident] if self.src() == "from" => rgb![97, 175, 239],
            T![ident] if BUILTINS.contains(&self.src()) => rgb![229, 192, 123],
            T![ident] => rgb![224, 108, 117],
            T![instanceof] | T![new] | T![?] | T![delete] | T![:] | T![const] => {
                rgb![198, 120, 221]
            }
            t if t.is_punct() => rgb![86, 182, 194],
            t if t.is_keyword() => rgb![198, 120, 221],
            SyntaxKind::STRING | SyntaxKind::BACKTICK | SyntaxKind::TEMPLATE_CHUNK => {
                rgb![152, 195, 121]
            }
            SyntaxKind::NUMBER => rgb![209, 154, 102],
            SyntaxKind::DOLLARCURLY => rgb![198, 120, 221],
            SyntaxKind::ERROR_TOKEN => rgb![244, 71, 71],
            SyntaxKind::COMMENT => rgb![127, 132, 142],
            _ => Color::White,
        };

        let string = self.src();
        self.cur_idx += self.tokens.get(self.cur).unwrap().len;
        self.cur += 1;
        Some(color.paint(string))
    }
}

/// Colors a piece of source code using ANSI.  
/// The string returned will be unaltered if stdout and stderr are not terminals.
pub fn color(source: &str) -> String {
    Highlighter::new(source).color()
}