]> git.saurik.com Git - cycript.git/blame - Highlight.cpp
Verify lexer tokens are always default reductions.
[cycript.git] / Highlight.cpp
CommitLineData
82a02ede 1/* Cycript - Optimizing JavaScript Compiler/Runtime
c1d3e52e 2 * Copyright (C) 2009-2015 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
8a392978
JF
22#include "Code.hpp"
23#include "Driver.hpp"
82a02ede
JF
24#include "Highlight.hpp"
25
8a392978 26bool CYLexerHighlight(hi::Value &highlight, CYLocation &location, void *scanner);
82a02ede 27
58afc6aa 28static void Skip(const char *data, size_t size, std::ostream &output, size_t &offset, CYPosition &current, CYPosition target) {
82a02ede
JF
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')
51b2dc6b 37 current.Lines();
82a02ede 38 else
51b2dc6b 39 current.Columns();
82a02ede
JF
40 }
41}
42
43struct 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
d9c91152 57_visible void CYLexerHighlight(const char *data, size_t size, std::ostream &output, bool ignore) {
2c1d569a
JF
58 CYLocalPool pool;
59
82a02ede 60 CYStream stream(data, data + size);
2c1d569a 61 CYDriver driver(pool, stream);
b900e1a4 62 driver.highlight_ = true;
82a02ede
JF
63
64 size_t offset(0);
58afc6aa 65 CYPosition current;
82a02ede 66
8a392978 67 hi::Value highlight;
58afc6aa 68 CYLocation location;
82a02ede 69
8a392978 70 while (CYLexerHighlight(highlight, location, driver.scanner_)) {
82a02ede
JF
71 CYColor color;
72
8a392978 73 switch (highlight) {
82a02ede
JF
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;
dc5d7cf4 77 case hi::Error: color = CYColor(true, 31); break;
82a02ede
JF
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;
73439a14 82 case hi::Special: color = CYColor(false, 35); break;
82a02ede
JF
83 case hi::Structure: color = CYColor(true, 34); break;
84 case hi::Type: color = CYColor(true, 34); break;
d4ab6b62
JF
85
86 // XXX: maybe I should use nodefault here?
87 default: color = CYColor(true, 0); break;
82a02ede
JF
88 }
89
90 Skip(data, size, output, offset, current, location.begin);
74296173
JF
91
92 if (color.code_ != 0) {
93 if (ignore)
94 output << CYIgnoreStart;
82a02ede 95 output << "\e[" << (color.bold_ ? '1' : '0') << ";" << color.code_ << "m";
74296173
JF
96 if (ignore)
97 output << CYIgnoreEnd;
98 }
99
82a02ede 100 Skip(data, size, output, offset, current, location.end);
74296173
JF
101
102 if (color.code_ != 0) {
103 if (ignore)
104 output << CYIgnoreStart;
82a02ede 105 output << "\e[0m";
74296173
JF
106 if (ignore)
107 output << CYIgnoreEnd;
108 }
82a02ede
JF
109 }
110
111 output.write(data + offset, size - offset);
112}