]>
Commit | Line | Data |
---|---|---|
82a02ede | 1 | /* Cycript - Optimizing JavaScript Compiler/Runtime |
f95d2598 | 2 | * Copyright (C) 2009-2014 Jay Freeman (saurik) |
82a02ede JF |
3 | */ |
4 | ||
f95d2598 | 5 | /* GNU Affero General Public License, Version 3 {{{ */ |
82a02ede | 6 | /* |
f95d2598 JF |
7 | * This program is free software: you can redistribute it and/or modify |
8 | * it under the terms of the GNU Affero General Public License as published by | |
9 | * the Free Software Foundation, either version 3 of the License, or | |
10 | * (at your option) any later version. | |
11 | ||
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
c15969fd | 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
f95d2598 JF |
15 | * GNU Affero General Public License for more details. |
16 | ||
17 | * You should have received a copy of the GNU Affero General Public License | |
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
82a02ede JF |
19 | **/ |
20 | /* }}} */ | |
21 | ||
22 | #include "Highlight.hpp" | |
b12a9965 | 23 | #include "Parser.hpp" |
82a02ede JF |
24 | |
25 | #include "Cycript.tab.hh" | |
b12a9965 | 26 | #include "Driver.hpp" |
5587a93f | 27 | #include "Code.hpp" |
82a02ede | 28 | |
58afc6aa | 29 | static void Skip(const char *data, size_t size, std::ostream &output, size_t &offset, CYPosition ¤t, CYPosition target) { |
82a02ede JF |
30 | while (current.line != target.line || current.column != target.column) { |
31 | _assert(offset != size); | |
32 | char next(data[offset++]); | |
33 | ||
34 | output.put(next); | |
35 | ||
36 | _assert(current.line < target.line || current.line == target.line && current.column < target.column); | |
37 | if (next == '\n') | |
38 | current.lines(); | |
39 | else | |
40 | current.columns(); | |
41 | } | |
42 | } | |
43 | ||
44 | struct CYColor { | |
45 | bool bold_; | |
46 | unsigned code_; | |
47 | ||
48 | CYColor() { | |
49 | } | |
50 | ||
51 | CYColor(bool bold, unsigned code) : | |
52 | bold_(bold), | |
53 | code_(code) | |
54 | { | |
55 | } | |
56 | }; | |
57 | ||
74296173 | 58 | void CYLexerHighlight(const char *data, size_t size, std::ostream &output, bool ignore) { |
82a02ede JF |
59 | CYStream stream(data, data + size); |
60 | CYDriver driver(stream); | |
abc79d6e | 61 | driver.commented_ = true; |
82a02ede JF |
62 | |
63 | size_t offset(0); | |
58afc6aa | 64 | CYPosition current; |
82a02ede JF |
65 | |
66 | CYLocalPool pool; | |
67 | ||
68 | YYSTYPE value; | |
58afc6aa | 69 | CYLocation location; |
82a02ede JF |
70 | |
71 | while (cylex(&value, &location, driver.scanner_) != 0) { | |
72 | CYColor color; | |
73 | ||
74 | switch (value.highlight_) { | |
75 | case hi::Comment: color = CYColor(true, 30); break; | |
76 | case hi::Constant: color = CYColor(false, 31); break; | |
77 | case hi::Control: color = CYColor(false, 33); break; | |
dc5d7cf4 | 78 | case hi::Error: color = CYColor(true, 31); break; |
82a02ede JF |
79 | case hi::Identifier: color = CYColor(false, 0); break; |
80 | case hi::Meta: color = CYColor(false, 32); break; | |
81 | case hi::Nothing: color = CYColor(false, 0); break; | |
82 | case hi::Operator: color = CYColor(false, 36); break; | |
73439a14 | 83 | case hi::Special: color = CYColor(false, 35); break; |
82a02ede JF |
84 | case hi::Structure: color = CYColor(true, 34); break; |
85 | case hi::Type: color = CYColor(true, 34); break; | |
d4ab6b62 JF |
86 | |
87 | // XXX: maybe I should use nodefault here? | |
88 | default: color = CYColor(true, 0); break; | |
82a02ede JF |
89 | } |
90 | ||
91 | Skip(data, size, output, offset, current, location.begin); | |
74296173 JF |
92 | |
93 | if (color.code_ != 0) { | |
94 | if (ignore) | |
95 | output << CYIgnoreStart; | |
82a02ede | 96 | output << "\e[" << (color.bold_ ? '1' : '0') << ";" << color.code_ << "m"; |
74296173 JF |
97 | if (ignore) |
98 | output << CYIgnoreEnd; | |
99 | } | |
100 | ||
82a02ede | 101 | Skip(data, size, output, offset, current, location.end); |
74296173 JF |
102 | |
103 | if (color.code_ != 0) { | |
104 | if (ignore) | |
105 | output << CYIgnoreStart; | |
82a02ede | 106 | output << "\e[0m"; |
74296173 JF |
107 | if (ignore) |
108 | output << CYIgnoreEnd; | |
109 | } | |
82a02ede JF |
110 | } |
111 | ||
112 | output.write(data + offset, size - offset); | |
113 | } |