]> git.saurik.com Git - cycript.git/blame - Highlight.cpp
Finish new build environment, relocatable objects.
[cycript.git] / Highlight.cpp
CommitLineData
82a02ede 1/* Cycript - Optimizing JavaScript Compiler/Runtime
c15969fd 2 * Copyright (C) 2009-2013 Jay Freeman (saurik)
82a02ede
JF
3*/
4
c15969fd 5/* GNU General Public License, Version 3 {{{ */
82a02ede 6/*
c15969fd
JF
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.
82a02ede 11 *
c15969fd
JF
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.
82a02ede 16 *
c15969fd 17 * You should have received a copy of the GNU General Public License
82a02ede
JF
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
27static void Skip(const char *data, size_t size, std::ostream &output, size_t &offset, cy::position &current, 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
42struct 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 56void CYLexerHighlight(const char *data, size_t size, std::ostream &output, bool ignore) {
82a02ede
JF
57 CYStream stream(data, data + size);
58 CYDriver driver(stream);
abc79d6e 59 driver.commented_ = true;
82a02ede
JF
60
61 size_t offset(0);
62 cy::position current;
63
64 CYLocalPool pool;
65
66 YYSTYPE value;
67 cy::location location;
68
69 while (cylex(&value, &location, driver.scanner_) != 0) {
70 CYColor color;
71
72 switch (value.highlight_) {
73 case hi::Comment: color = CYColor(true, 30); break;
74 case hi::Constant: color = CYColor(false, 31); break;
75 case hi::Control: color = CYColor(false, 33); break;
76 case hi::Escape: color = CYColor(true, 31); break;
77 case hi::Identifier: color = CYColor(false, 0); break;
78 case hi::Meta: color = CYColor(false, 32); break;
79 case hi::Nothing: color = CYColor(false, 0); break;
80 case hi::Operator: color = CYColor(false, 36); break;
81 case hi::Structure: color = CYColor(true, 34); break;
82 case hi::Type: color = CYColor(true, 34); break;
d4ab6b62
JF
83
84 // XXX: maybe I should use nodefault here?
85 default: color = CYColor(true, 0); break;
82a02ede
JF
86 }
87
88 Skip(data, size, output, offset, current, location.begin);
74296173
JF
89
90 if (color.code_ != 0) {
91 if (ignore)
92 output << CYIgnoreStart;
82a02ede 93 output << "\e[" << (color.bold_ ? '1' : '0') << ";" << color.code_ << "m";
74296173
JF
94 if (ignore)
95 output << CYIgnoreEnd;
96 }
97
82a02ede 98 Skip(data, size, output, offset, current, location.end);
74296173
JF
99
100 if (color.code_ != 0) {
101 if (ignore)
102 output << CYIgnoreStart;
82a02ede 103 output << "\e[0m";
74296173
JF
104 if (ignore)
105 output << CYIgnoreEnd;
106 }
82a02ede
JF
107 }
108
109 output.write(data + offset, size - offset);
110}