forked from rismay/cycript
/
Highlight.cpp
111 lines (90 loc) · 3.36 KB
/
Highlight.cpp
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
/* Cycript - Optimizing JavaScript Compiler/Runtime
* Copyright (C) 2009-2013 Jay Freeman (saurik)
*/
/* GNU General Public License, Version 3 {{{ */
/*
* Cycript is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published
* by the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* Cycript is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Cycript. If not, see <http://www.gnu.org/licenses/>.
**/
/* }}} */
#include "Highlight.hpp"
#include "Parser.hpp"
#include "Cycript.tab.hh"
#include "Driver.hpp"
static void Skip(const char *data, size_t size, std::ostream &output, size_t &offset, cy::position ¤t, cy::position target) {
while (current.line != target.line || current.column != target.column) {
_assert(offset != size);
char next(data[offset++]);
output.put(next);
_assert(current.line < target.line || current.line == target.line && current.column < target.column);
if (next == '\n')
current.lines();
else
current.columns();
}
}
struct CYColor {
bool bold_;
unsigned code_;
CYColor() {
}
CYColor(bool bold, unsigned code) :
bold_(bold),
code_(code)
{
}
};
void CYLexerHighlight(const char *data, size_t size, std::ostream &output, bool ignore) {
CYStream stream(data, data + size);
CYDriver driver(stream);
driver.commented_ = true;
size_t offset(0);
cy::position current;
CYLocalPool pool;
YYSTYPE value;
cy::location location;
while (cylex(&value, &location, driver.scanner_) != 0) {
CYColor color;
switch (value.highlight_) {
case hi::Comment: color = CYColor(true, 30); break;
case hi::Constant: color = CYColor(false, 31); break;
case hi::Control: color = CYColor(false, 33); break;
case hi::Escape: color = CYColor(true, 31); break;
case hi::Identifier: color = CYColor(false, 0); break;
case hi::Meta: color = CYColor(false, 32); break;
case hi::Nothing: color = CYColor(false, 0); break;
case hi::Operator: color = CYColor(false, 36); break;
case hi::Structure: color = CYColor(true, 34); break;
case hi::Type: color = CYColor(true, 34); break;
// XXX: maybe I should use nodefault here?
default: color = CYColor(true, 0); break;
}
Skip(data, size, output, offset, current, location.begin);
if (color.code_ != 0) {
if (ignore)
output << CYIgnoreStart;
output << "\e[" << (color.bold_ ? '1' : '0') << ";" << color.code_ << "m";
if (ignore)
output << CYIgnoreEnd;
}
Skip(data, size, output, offset, current, location.end);
if (color.code_ != 0) {
if (ignore)
output << CYIgnoreStart;
output << "\e[0m";
if (ignore)
output << CYIgnoreEnd;
}
}
output.write(data + offset, size - offset);
}