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