]>
git.saurik.com Git - cycript.git/blob - Highlight.cpp
1 /* Cycript - The Truly Universal Scripting Language
2 * Copyright (C) 2009-2016 Jay Freeman (saurik)
5 /* GNU Affero General Public License, Version 3 {{{ */
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.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
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/>.
24 #include "Highlight.hpp"
26 bool CYLexerHighlight(hi::Value
&highlight
, CYLocation
&location
, void *scanner
);
28 static void Skip(const char *data
, size_t size
, std::ostream
&output
, size_t &offset
, CYPosition
¤t
, CYPosition target
) {
29 while (current
.line
!= target
.line
|| current
.column
!= target
.column
) {
30 _assert(offset
!= size
);
31 char next(data
[offset
++]);
35 _assert(current
.line
< target
.line
|| current
.line
== target
.line
&& current
.column
< target
.column
);
50 CYColor(bool bold
, unsigned code
) :
57 _visible
void CYLexerHighlight(const char *data
, size_t size
, std::ostream
&output
, bool ignore
) {
60 CYStream
stream(data
, data
+ size
);
61 CYDriver
driver(pool
, stream
);
62 driver
.highlight_
= true;
70 while (CYLexerHighlight(highlight
, location
, driver
.scanner_
)) {
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::Special
: color
= CYColor(false, 35); break;
83 case hi::Structure
: color
= CYColor(true, 34); break;
84 case hi::Type
: color
= CYColor(true, 34); break;
86 // XXX: maybe I should use nodefault here?
87 default: color
= CYColor(true, 0); break;
90 Skip(data
, size
, output
, offset
, current
, location
.begin
);
92 if (color
.code_
!= 0) {
94 output
<< CYIgnoreStart
;
95 output
<< "\e[" << (color
.bold_
? '1' : '0') << ";" << color
.code_
<< "m";
97 output
<< CYIgnoreEnd
;
100 Skip(data
, size
, output
, offset
, current
, location
.end
);
102 if (color
.code_
!= 0) {
104 output
<< CYIgnoreStart
;
107 output
<< CYIgnoreEnd
;
111 output
.write(data
+ offset
, size
- offset
);