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