| 1 |
// Scintilla source code edit control |
| 2 |
/** @file LexCPP.cxx |
| 3 |
** Lexer for C++, C, Java, and JavaScript. |
| 4 |
** Further folding features and configuration properties added by "Udo Lechner" <dlchnr(at)gmx(dot)net> |
| 5 |
**/ |
| 6 |
// Copyright 1998-2005 by Neil Hodgson <[email protected]> |
| 7 |
// The License.txt file describes the conditions under which this software may be distributed. |
| 8 |
|
| 9 |
#include <cstdlib> |
| 10 |
#include <cassert> |
| 11 |
#include <cstring> |
| 12 |
|
| 13 |
#include <utility> |
| 14 |
#include <string> |
| 15 |
#include <string_view> |
| 16 |
#include <vector> |
| 17 |
#include <map> |
| 18 |
#include <algorithm> |
| 19 |
#include <iterator> |
| 20 |
#include <functional> |
| 21 |
|
| 22 |
#include "ILexer.h" |
| 23 |
#include "Scintilla.h" |
| 24 |
#include "SciLexer.h" |
| 25 |
|
| 26 |
#include "StringCopy.h" |
| 27 |
#include "WordList.h" |
| 28 |
#include "LexAccessor.h" |
| 29 |
#include "Accessor.h" |
| 30 |
#include "StyleContext.h" |
| 31 |
#include "CharacterSet.h" |
| 32 |
#include "LexerModule.h" |
| 33 |
#include "OptionSet.h" |
| 34 |
#include "SparseState.h" |
| 35 |
#include "SubStyles.h" |
| 36 |
|
| 37 |
using namespace Scintilla; |
| 38 |
using namespace Lexilla; |
| 39 |
|
| 40 |
namespace { |
| 41 |
// Use an unnamed namespace to protect the functions and classes from name conflicts |
| 42 |
|
| 43 |
constexpr bool IsSpaceEquiv(int state) noexcept { |
| 44 |
return (state <= SCE_C_COMMENTDOC) || |
| 45 |
// including SCE_C_DEFAULT, SCE_C_COMMENT, SCE_C_COMMENTLINE |
| 46 |
(state == SCE_C_COMMENTLINEDOC) || (state == SCE_C_COMMENTDOCKEYWORD) || |
| 47 |
(state == SCE_C_COMMENTDOCKEYWORDERROR); |
| 48 |
} |
| 49 |
|
| 50 |
// Preconditions: sc.currentPos points to a character after '+' or '-'. |
| 51 |
// The test for pos reaching 0 should be redundant, |
| 52 |
// and is in only for safety measures. |
| 53 |
// Limitation: this code will give the incorrect answer for code like |
| 54 |
// a = b+++/ptn/... |
| 55 |
// Putting a space between the '++' post-inc operator and the '+' binary op |
| 56 |
// fixes this, and is highly recommended for readability anyway. |
| 57 |
bool FollowsPostfixOperator(const StyleContext &sc, LexAccessor &styler) { |
| 58 |
Sci_Position pos = sc.currentPos; |
| 59 |
while (--pos > 0) { |
| 60 |
const char ch = styler[pos]; |
| 61 |
if (ch == '+' || ch == '-') { |
| 62 |
return styler[pos - 1] == ch; |
| 63 |
} |
| 64 |
} |
| 65 |
return false; |
| 66 |
} |
| 67 |
|
| 68 |
bool followsReturnKeyword(const StyleContext &sc, LexAccessor &styler) { |
| 69 |
// Don't look at styles, so no need to flush. |
| 70 |
Sci_Position pos = sc.currentPos; |
| 71 |
const Sci_Position currentLine = styler.GetLine(pos); |
| 72 |
const Sci_Position lineStartPos = styler.LineStart(currentLine); |
| 73 |
while (--pos > lineStartPos) { |
| 74 |
const char ch = styler.SafeGetCharAt(pos); |
| 75 |
if (ch != ' ' && ch != '\t') { |
| 76 |
break; |
| 77 |
} |
| 78 |
} |
| 79 |
const char *retBack = "nruter"; |
| 80 |
const char *s = retBack; |
| 81 |
while (*s |
| 82 |
&& pos >= lineStartPos |
| 83 |
&& styler.SafeGetCharAt(pos) == *s) { |
| 84 |
s++; |
| 85 |
pos--; |
| 86 |
} |
| 87 |
return !*s; |
| 88 |
} |
| 89 |
|
| 90 |
constexpr bool IsOperatorOrSpace(int ch) noexcept { |
| 91 |
return isoperator(ch) || IsASpace(ch); |
| 92 |
} |
| 93 |
|
| 94 |
bool OnlySpaceOrTab(std::string_view s) noexcept { |
| 95 |
for (const char ch : s) { |
| 96 |
if (!IsASpaceOrTab(ch)) |
| 97 |
return false; |
| 98 |
} |
| 99 |
return true; |
| 100 |
} |
| 101 |
|
| 102 |
using Tokens = std::vector<std::string>; |
| 103 |
|
| 104 |
Tokens StringSplit(const std::string &text, int separator) { |
| 105 |
Tokens vs(text.empty() ? 0 : 1); |
| 106 |
for (const char ch : text) { |
| 107 |
if (ch == separator) { |
| 108 |
vs.emplace_back(); |
| 109 |
} else { |
| 110 |
vs.back() += ch; |
| 111 |
} |
| 112 |
} |
| 113 |
return vs; |
| 114 |
} |
| 115 |
|
| 116 |
struct BracketPair { |
| 117 |
Tokens::iterator itBracket; |
| 118 |
Tokens::iterator itEndBracket; |
| 119 |
}; |
| 120 |
|
| 121 |
BracketPair FindBracketPair(Tokens &tokens) { |
| 122 |
const Tokens::iterator itBracket = std::find(tokens.begin(), tokens.end(), "("); |
| 123 |
if (itBracket != tokens.end()) { |
| 124 |
size_t nest = 0; |
| 125 |
for (Tokens::iterator itTok = itBracket; itTok != tokens.end(); ++itTok) { |
| 126 |
if (*itTok == "(") { |
| 127 |
nest++; |
| 128 |
} else if (*itTok == ")") { |
| 129 |
nest--; |
| 130 |
if (nest == 0) { |
| 131 |
return { itBracket, itTok }; |
| 132 |
} |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
return { tokens.end(), tokens.end() }; |
| 137 |
} |
| 138 |
|
| 139 |
void highlightTaskMarker(StyleContext &sc, LexAccessor &styler, |
| 140 |
int activity, const WordList &markerList, bool caseSensitive) { |
| 141 |
if (IsOperatorOrSpace(sc.chPrev) && !IsOperatorOrSpace(sc.ch) && markerList.Length()) { |
| 142 |
std::string marker; |
| 143 |
for (Sci_PositionU currPos = sc.currentPos; true; currPos++) { |
| 144 |
const char ch = styler.SafeGetCharAt(currPos); |
| 145 |
if (IsOperatorOrSpace(ch)) { |
| 146 |
break; |
| 147 |
} |
| 148 |
if (caseSensitive) |
| 149 |
marker.push_back(ch); |
| 150 |
else |
| 151 |
marker.push_back(MakeLowerCase(ch)); |
| 152 |
} |
| 153 |
if (markerList.InList(marker)) { |
| 154 |
sc.SetState(SCE_C_TASKMARKER|activity); |
| 155 |
} |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
const CharacterSet setHexDigits(CharacterSet::setDigits, "ABCDEFabcdef"); |
| 160 |
const CharacterSet setOctDigits("01234567"); |
| 161 |
const CharacterSet setNoneNumeric; |
| 162 |
|
| 163 |
class EscapeSequence { |
| 164 |
const CharacterSet *escapeSetValid = nullptr; |
| 165 |
int digitsLeft = 0; |
| 166 |
public: |
| 167 |
int outerState = SCE_C_DEFAULT; |
| 168 |
EscapeSequence() = default; |
| 169 |
void resetEscapeState(int state, int nextChar) noexcept { |
| 170 |
digitsLeft = 0; |
| 171 |
outerState = state; |
| 172 |
escapeSetValid = &setNoneNumeric; |
| 173 |
if (nextChar == 'U') { |
| 174 |
digitsLeft = 9; |
| 175 |
escapeSetValid = &setHexDigits; |
| 176 |
} else if (nextChar == 'u') { |
| 177 |
digitsLeft = 5; |
| 178 |
escapeSetValid = &setHexDigits; |
| 179 |
} else if (nextChar == 'x') { |
| 180 |
digitsLeft = 5; |
| 181 |
escapeSetValid = &setHexDigits; |
| 182 |
} else if (setOctDigits.Contains(nextChar)) { |
| 183 |
digitsLeft = 3; |
| 184 |
escapeSetValid = &setOctDigits; |
| 185 |
} |
| 186 |
} |
| 187 |
[[nodiscard]] bool atEscapeEnd(int currChar) const noexcept { |
| 188 |
return (digitsLeft <= 0) || !escapeSetValid->Contains(currChar); |
| 189 |
} |
| 190 |
void consumeDigit() noexcept { |
| 191 |
digitsLeft--; |
| 192 |
} |
| 193 |
}; |
| 194 |
|
| 195 |
std::string GetRestOfLine(LexAccessor &styler, Sci_Position start, bool allowSpace) { |
| 196 |
std::string restOfLine; |
| 197 |
Sci_Position line = styler.GetLine(start); |
| 198 |
Sci_Position pos = start; |
| 199 |
Sci_Position endLine = styler.LineEnd(line); |
| 200 |
char ch = styler.SafeGetCharAt(start, '\n'); |
| 201 |
while (pos < endLine) { |
| 202 |
if (ch == '\\' && ((pos + 1) == endLine)) { |
| 203 |
// Continuation line |
| 204 |
line++; |
| 205 |
pos = styler.LineStart(line); |
| 206 |
endLine = styler.LineEnd(line); |
| 207 |
ch = styler.SafeGetCharAt(pos, '\n'); |
| 208 |
} else { |
| 209 |
const char chNext = styler.SafeGetCharAt(pos + 1, '\n'); |
| 210 |
if (ch == '/' && (chNext == '/' || chNext == '*')) |
| 211 |
break; |
| 212 |
if (allowSpace || (ch != ' ')) { |
| 213 |
restOfLine += ch; |
| 214 |
} |
| 215 |
pos++; |
| 216 |
ch = chNext; |
| 217 |
} |
| 218 |
} |
| 219 |
return restOfLine; |
| 220 |
} |
| 221 |
|
| 222 |
constexpr bool IsStreamCommentStyle(int style) noexcept { |
| 223 |
return style == SCE_C_COMMENT || |
| 224 |
style == SCE_C_COMMENTDOC || |
| 225 |
style == SCE_C_COMMENTDOCKEYWORD || |
| 226 |
style == SCE_C_COMMENTDOCKEYWORDERROR; |
| 227 |
} |
| 228 |
|
| 229 |
struct PPDefinition { |
| 230 |
Sci_Position line; |
| 231 |
std::string key; |
| 232 |
std::string value; |
| 233 |
bool isUndef; |
| 234 |
std::string arguments; |
| 235 |
PPDefinition(Sci_Position line_, std::string_view key_, std::string_view value_, bool isUndef_, std::string_view arguments_) : |
| 236 |
line(line_), key(key_), value(value_), isUndef(isUndef_), arguments(arguments_) { |
| 237 |
} |
| 238 |
}; |
| 239 |
|
| 240 |
constexpr int inactiveFlag = 0x40; |
| 241 |
|
| 242 |
class LinePPState { |
| 243 |
// Track the state of preprocessor conditionals to allow showing active and inactive |
| 244 |
// code in different styles. |
| 245 |
// Only works up to 31 levels of conditional nesting. |
| 246 |
|
| 247 |
// state is a bit mask with 1 bit per level |
| 248 |
// bit is 1 for level if section inactive, so any bits set = inactive style |
| 249 |
int state = 0; |
| 250 |
// ifTaken is a bit mask with 1 bit per level |
| 251 |
// bit is 1 for level if some branch at this level has been taken |
| 252 |
int ifTaken = 0; |
| 253 |
// level is the nesting level of #if constructs |
| 254 |
int level = -1; |
| 255 |
static const int maximumNestingLevel = 31; |
| 256 |
[[nodiscard]] int maskLevel() const noexcept { |
| 257 |
if (level >= 0) { |
| 258 |
return 1 << level; |
| 259 |
} |
| 260 |
return 1; |
| 261 |
} |
| 262 |
public: |
| 263 |
LinePPState() noexcept = default; |
| 264 |
[[nodiscard]] bool ValidLevel() const noexcept { |
| 265 |
return level >= 0 && level < maximumNestingLevel; |
| 266 |
} |
| 267 |
[[nodiscard]] bool IsActive() const noexcept { |
| 268 |
return state == 0; |
| 269 |
} |
| 270 |
[[nodiscard]] bool IsInactive() const noexcept { |
| 271 |
return state != 0; |
| 272 |
} |
| 273 |
[[nodiscard]] int ActiveState() const noexcept { |
| 274 |
return state ? inactiveFlag : 0; |
| 275 |
} |
| 276 |
[[nodiscard]] bool CurrentIfTaken() const noexcept { |
| 277 |
return (ifTaken & maskLevel()) != 0; |
| 278 |
} |
| 279 |
void StartSection(bool on) noexcept { |
| 280 |
level++; |
| 281 |
if (ValidLevel()) { |
| 282 |
if (on) { |
| 283 |
state &= ~maskLevel(); |
| 284 |
ifTaken |= maskLevel(); |
| 285 |
} else { |
| 286 |
state |= maskLevel(); |
| 287 |
ifTaken &= ~maskLevel(); |
| 288 |
} |
| 289 |
} |
| 290 |
} |
| 291 |
void EndSection() noexcept { |
| 292 |
if (ValidLevel()) { |
| 293 |
state &= ~maskLevel(); |
| 294 |
ifTaken &= ~maskLevel(); |
| 295 |
} |
| 296 |
level--; |
| 297 |
} |
| 298 |
void InvertCurrentLevel() noexcept { |
| 299 |
if (ValidLevel()) { |
| 300 |
state ^= maskLevel(); |
| 301 |
ifTaken |= maskLevel(); |
| 302 |
} |
| 303 |
} |
| 304 |
}; |
| 305 |
|
| 306 |
// Hold the preprocessor state for each line seen. |
| 307 |
// Currently one entry per line but could become sparse with just one entry per preprocessor line. |
| 308 |
class PPStates { |
| 309 |
std::vector<LinePPState> vlls; |
| 310 |
public: |
| 311 |
[[nodiscard]] LinePPState ForLine(Sci_Position line) const noexcept { |
| 312 |
if ((line > 0) && (vlls.size() > static_cast<size_t>(line))) { |
| 313 |
return vlls[line]; |
| 314 |
} |
| 315 |
return {}; |
| 316 |
} |
| 317 |
void Add(Sci_Position line, LinePPState lls) { |
| 318 |
vlls.resize(line+1); |
| 319 |
vlls[line] = lls; |
| 320 |
} |
| 321 |
}; |
| 322 |
|
| 323 |
enum class BackQuotedString : int { |
| 324 |
None, |
| 325 |
RawString, |
| 326 |
TemplateLiteral, |
| 327 |
}; |
| 328 |
|
| 329 |
// string interpolating state |
| 330 |
struct InterpolatingState { |
| 331 |
int state; |
| 332 |
int braceCount; |
| 333 |
}; |
| 334 |
|
| 335 |
struct Definition { |
| 336 |
std::string_view name; |
| 337 |
std::string_view value; |
| 338 |
std::string_view arguments; |
| 339 |
}; |
| 340 |
|
| 341 |
constexpr std::string_view TrimSpaceTab(std::string_view sv) noexcept { |
| 342 |
while (!sv.empty() && IsASpaceOrTab(sv.front())) { |
| 343 |
sv.remove_prefix(1); |
| 344 |
} |
| 345 |
return sv; |
| 346 |
} |
| 347 |
|
| 348 |
// Parse a macro definition, either from a #define line in a file or from keywords. |
| 349 |
// Either an object macro <NAME> <VALUE> or a function macro <NAME>(<ARGUMENTS>) <VALUE>. |
| 350 |
// VALUE is optional and is treated as "1" if missing. |
| 351 |
// #define ALLOW_PRINT |
| 352 |
// #define VERSION 37 |
| 353 |
// #define VER(a,b) a*10+b |
| 354 |
// Whitespace separates macro and value in files but keywords use '=' separator. |
| 355 |
// 'endName' contains a set of characters that terminate the name of the macro. |
| 356 |
|
| 357 |
constexpr Definition ParseDefine(std::string_view definition, std::string_view endName) { |
| 358 |
Definition ret; |
| 359 |
definition = TrimSpaceTab(definition); |
| 360 |
const size_t afterName = definition.find_first_of(endName); |
| 361 |
if (afterName != std::string_view::npos) { |
| 362 |
ret.name = definition.substr(0, afterName); |
| 363 |
if (definition.at(afterName) == '(') { |
| 364 |
// Macro |
| 365 |
definition.remove_prefix(afterName+1); |
| 366 |
const size_t closeBracket = definition.find(')'); |
| 367 |
if (closeBracket != std::string_view::npos) { |
| 368 |
ret.arguments = definition.substr(0, closeBracket); |
| 369 |
definition.remove_prefix(closeBracket+1); |
| 370 |
if (!definition.empty() && (endName.find(definition.front()) != std::string_view::npos)) { |
| 371 |
definition.remove_prefix(1); |
| 372 |
} |
| 373 |
ret.value = definition; |
| 374 |
} // else malformed as requires closing bracket |
| 375 |
} else { |
| 376 |
ret.value = definition.substr(afterName+1); |
| 377 |
} |
| 378 |
} else { |
| 379 |
ret.name = definition; |
| 380 |
ret.value = "1"; |
| 381 |
} |
| 382 |
return ret; |
| 383 |
} |
| 384 |
|
| 385 |
// An individual named option for use in an OptionSet |
| 386 |
|
| 387 |
// Options used for LexerCPP |
| 388 |
struct OptionsCPP { |
| 389 |
bool stylingWithinPreprocessor = false; |
| 390 |
bool identifiersAllowDollars = true; |
| 391 |
bool trackPreprocessor = true; |
| 392 |
bool updatePreprocessor = true; |
| 393 |
bool verbatimStringsAllowEscapes = false; |
| 394 |
bool triplequotedStrings = false; |
| 395 |
bool hashquotedStrings = false; |
| 396 |
BackQuotedString backQuotedStrings = BackQuotedString::None; |
| 397 |
bool escapeSequence = false; |
| 398 |
bool fold = false; |
| 399 |
bool foldSyntaxBased = true; |
| 400 |
bool foldComment = false; |
| 401 |
bool foldCommentMultiline = true; |
| 402 |
bool foldCommentExplicit = true; |
| 403 |
std::string foldExplicitStart; |
| 404 |
std::string foldExplicitEnd; |
| 405 |
bool foldExplicitAnywhere = false; |
| 406 |
bool foldPreprocessor = false; |
| 407 |
bool foldPreprocessorAtElse = false; |
| 408 |
bool foldCompact = false; |
| 409 |
bool foldAtElse = false; |
| 410 |
}; |
| 411 |
|
| 412 |
const char *const cppWordLists[] = { |
| 413 |
"Primary keywords and identifiers", |
| 414 |
"Secondary keywords and identifiers", |
| 415 |
"Documentation comment keywords", |
| 416 |
"Global classes and typedefs", |
| 417 |
"Preprocessor definitions", |
| 418 |
"Task marker and error marker keywords", |
| 419 |
nullptr, |
| 420 |
}; |
| 421 |
|
| 422 |
struct OptionSetCPP : public OptionSet<OptionsCPP> { |
| 423 |
OptionSetCPP() { |
| 424 |
DefineProperty("styling.within.preprocessor", &OptionsCPP::stylingWithinPreprocessor, |
| 425 |
"For C++ code, determines whether all preprocessor code is styled in the " |
| 426 |
"preprocessor style (0, the default) or only from the initial # to the end " |
| 427 |
"of the command word(1)."); |
| 428 |
|
| 429 |
DefineProperty("lexer.cpp.allow.dollars", &OptionsCPP::identifiersAllowDollars, |
| 430 |
"Set to 0 to disallow the '$' character in identifiers with the cpp lexer."); |
| 431 |
|
| 432 |
DefineProperty("lexer.cpp.track.preprocessor", &OptionsCPP::trackPreprocessor, |
| 433 |
"Set to 1 to interpret #if/#else/#endif to grey out code that is not active."); |
| 434 |
|
| 435 |
DefineProperty("lexer.cpp.update.preprocessor", &OptionsCPP::updatePreprocessor, |
| 436 |
"Set to 1 to update preprocessor definitions when #define found."); |
| 437 |
|
| 438 |
DefineProperty("lexer.cpp.verbatim.strings.allow.escapes", &OptionsCPP::verbatimStringsAllowEscapes, |
| 439 |
"Set to 1 to allow verbatim strings to contain escape sequences."); |
| 440 |
|
| 441 |
DefineProperty("lexer.cpp.triplequoted.strings", &OptionsCPP::triplequotedStrings, |
| 442 |
"Set to 1 to enable highlighting of triple-quoted strings."); |
| 443 |
|
| 444 |
DefineProperty("lexer.cpp.hashquoted.strings", &OptionsCPP::hashquotedStrings, |
| 445 |
"Set to 1 to enable highlighting of hash-quoted strings."); |
| 446 |
|
| 447 |
DefineProperty("lexer.cpp.backquoted.strings", &OptionsCPP::backQuotedStrings, |
| 448 |
"Set how to highlighting back-quoted strings. " |
| 449 |
"0 (the default) no highlighting. " |
| 450 |
"1 highlighted as Go raw string. " |
| 451 |
"2 highlighted as JavaScript template literal."); |
| 452 |
|
| 453 |
DefineProperty("lexer.cpp.escape.sequence", &OptionsCPP::escapeSequence, |
| 454 |
"Set to 1 to enable highlighting of escape sequences in strings"); |
| 455 |
|
| 456 |
DefineProperty("fold", &OptionsCPP::fold); |
| 457 |
|
| 458 |
DefineProperty("fold.cpp.syntax.based", &OptionsCPP::foldSyntaxBased, |
| 459 |
"Set this property to 0 to disable syntax based folding."); |
| 460 |
|
| 461 |
DefineProperty("fold.comment", &OptionsCPP::foldComment, |
| 462 |
"This option enables folding multi-line comments and explicit fold points when using the C++ lexer. " |
| 463 |
"Explicit fold points allows adding extra folding by placing a //{ comment at the start and a //} " |
| 464 |
"at the end of a section that should fold."); |
| 465 |
|
| 466 |
DefineProperty("fold.cpp.comment.multiline", &OptionsCPP::foldCommentMultiline, |
| 467 |
"Set this property to 0 to disable folding multi-line comments when fold.comment=1."); |
| 468 |
|
| 469 |
DefineProperty("fold.cpp.comment.explicit", &OptionsCPP::foldCommentExplicit, |
| 470 |
"Set this property to 0 to disable folding explicit fold points when fold.comment=1."); |
| 471 |
|
| 472 |
DefineProperty("fold.cpp.explicit.start", &OptionsCPP::foldExplicitStart, |
| 473 |
"The string to use for explicit fold start points, replacing the standard //{."); |
| 474 |
|
| 475 |
DefineProperty("fold.cpp.explicit.end", &OptionsCPP::foldExplicitEnd, |
| 476 |
"The string to use for explicit fold end points, replacing the standard //}."); |
| 477 |
|
| 478 |
DefineProperty("fold.cpp.explicit.anywhere", &OptionsCPP::foldExplicitAnywhere, |
| 479 |
"Set this property to 1 to enable explicit fold points anywhere, not just in line comments."); |
| 480 |
|
| 481 |
DefineProperty("fold.cpp.preprocessor.at.else", &OptionsCPP::foldPreprocessorAtElse, |
| 482 |
"This option enables folding on a preprocessor #else or #endif line of an #if statement."); |
| 483 |
|
| 484 |
DefineProperty("fold.preprocessor", &OptionsCPP::foldPreprocessor, |
| 485 |
"This option enables folding preprocessor directives when using the C++ lexer. " |
| 486 |
"Includes C#'s explicit #region and #endregion folding directives."); |
| 487 |
|
| 488 |
DefineProperty("fold.compact", &OptionsCPP::foldCompact); |
| 489 |
|
| 490 |
DefineProperty("fold.at.else", &OptionsCPP::foldAtElse, |
| 491 |
"This option enables C++ folding on a \"} else {\" line of an if statement."); |
| 492 |
|
| 493 |
DefineWordListSets(cppWordLists); |
| 494 |
} |
| 495 |
}; |
| 496 |
|
| 497 |
const char styleSubable[] = {SCE_C_IDENTIFIER, SCE_C_COMMENTDOCKEYWORD, 0}; |
| 498 |
|
| 499 |
LexicalClass lexicalClasses[] = { |
| 500 |
// Lexer Cpp SCLEX_CPP SCE_C_: |
| 501 |
0, "SCE_C_DEFAULT", "default", "White space", |
| 502 |
1, "SCE_C_COMMENT", "comment", "Comment: /* */.", |
| 503 |
2, "SCE_C_COMMENTLINE", "comment line", "Line Comment: //.", |
| 504 |
3, "SCE_C_COMMENTDOC", "comment documentation", "Doc comment: block comments beginning with /** or /*!", |
| 505 |
4, "SCE_C_NUMBER", "literal numeric", "Number", |
| 506 |
5, "SCE_C_WORD", "keyword", "Keyword", |
| 507 |
6, "SCE_C_STRING", "literal string", "Double quoted string", |
| 508 |
7, "SCE_C_CHARACTER", "literal string character", "Single quoted string", |
| 509 |
8, "SCE_C_UUID", "literal uuid", "UUIDs (only in IDL)", |
| 510 |
9, "SCE_C_PREPROCESSOR", "preprocessor", "Preprocessor", |
| 511 |
10, "SCE_C_OPERATOR", "operator", "Operators", |
| 512 |
11, "SCE_C_IDENTIFIER", "identifier", "Identifiers", |
| 513 |
12, "SCE_C_STRINGEOL", "error literal string", "End of line where string is not closed", |
| 514 |
13, "SCE_C_VERBATIM", "literal string multiline raw", "Verbatim strings for C#", |
| 515 |
14, "SCE_C_REGEX", "literal regex", "Regular expressions for JavaScript", |
| 516 |
15, "SCE_C_COMMENTLINEDOC", "comment documentation line", "Doc Comment Line: line comments beginning with /// or //!.", |
| 517 |
16, "SCE_C_WORD2", "identifier", "Keywords2", |
| 518 |
17, "SCE_C_COMMENTDOCKEYWORD", "comment documentation keyword", "Comment keyword", |
| 519 |
18, "SCE_C_COMMENTDOCKEYWORDERROR", "error comment documentation keyword", "Comment keyword error", |
| 520 |
19, "SCE_C_GLOBALCLASS", "identifier", "Global class", |
| 521 |
20, "SCE_C_STRINGRAW", "literal string multiline raw", "Raw strings for C++0x", |
| 522 |
21, "SCE_C_TRIPLEVERBATIM", "literal string multiline raw", "Triple-quoted strings for Vala", |
| 523 |
22, "SCE_C_HASHQUOTEDSTRING", "literal string", "Hash-quoted strings for Pike", |
| 524 |
23, "SCE_C_PREPROCESSORCOMMENT", "comment preprocessor", "Preprocessor stream comment", |
| 525 |
24, "SCE_C_PREPROCESSORCOMMENTDOC", "comment preprocessor documentation", "Preprocessor stream doc comment", |
| 526 |
25, "SCE_C_USERLITERAL", "literal", "User defined literals", |
| 527 |
26, "SCE_C_TASKMARKER", "comment taskmarker", "Task Marker", |
| 528 |
27, "SCE_C_ESCAPESEQUENCE", "literal string escapesequence", "Escape sequence", |
| 529 |
}; |
| 530 |
|
| 531 |
const int sizeLexicalClasses = static_cast<int>(std::size(lexicalClasses)); |
| 532 |
|
| 533 |
} |
| 534 |
|
| 535 |
class LexerCPP : public ILexer5 { |
| 536 |
bool caseSensitive; |
| 537 |
CharacterSet setWord; |
| 538 |
CharacterSet setNegationOp; |
| 539 |
CharacterSet setAddOp; |
| 540 |
CharacterSet setMultOp; |
| 541 |
CharacterSet setRelOp; |
| 542 |
CharacterSet setLogicalOp; |
| 543 |
CharacterSet setWordStart; |
| 544 |
PPStates vlls; |
| 545 |
std::vector<PPDefinition> ppDefineHistory; |
| 546 |
std::map<Sci_Position, std::vector<InterpolatingState>> interpolatingAtEol; |
| 547 |
WordList keywords; |
| 548 |
WordList keywords2; |
| 549 |
WordList keywords3; |
| 550 |
WordList keywords4; |
| 551 |
WordList ppDefinitions; |
| 552 |
WordList markerList; |
| 553 |
struct SymbolValue { |
| 554 |
std::string value; |
| 555 |
std::string arguments; |
| 556 |
SymbolValue() noexcept = default; |
| 557 |
SymbolValue(std::string_view value_, std::string_view arguments_) : value(value_), arguments(arguments_) { |
| 558 |
} |
| 559 |
SymbolValue &operator = (const std::string &value_) { |
| 560 |
value = value_; |
| 561 |
arguments.clear(); |
| 562 |
return *this; |
| 563 |
} |
| 564 |
[[nodiscard]] bool IsMacro() const noexcept { |
| 565 |
return !arguments.empty(); |
| 566 |
} |
| 567 |
}; |
| 568 |
using SymbolTable = std::map<std::string, SymbolValue>; |
| 569 |
SymbolTable preprocessorDefinitionsStart; |
| 570 |
OptionsCPP options; |
| 571 |
OptionSetCPP osCPP; |
| 572 |
EscapeSequence escapeSeq; |
| 573 |
SparseState<std::string> rawStringTerminators; |
| 574 |
enum { ssIdentifier, ssDocKeyword }; |
| 575 |
SubStyles subStyles{ styleSubable, SubStylesFirst, SubStylesAvailable, inactiveFlag }; |
| 576 |
std::string returnBuffer; |
| 577 |
public: |
| 578 |
explicit LexerCPP(bool caseSensitive_) : |
| 579 |
caseSensitive(caseSensitive_), |
| 580 |
setWord(CharacterSet::setAlphaNum, "._", true), |
| 581 |
setNegationOp("!"), |
| 582 |
setAddOp("+-"), |
| 583 |
setMultOp("*/%"), |
| 584 |
setRelOp("=!<>"), |
| 585 |
setLogicalOp("|&") { |
| 586 |
} |
| 587 |
// Deleted so LexerCPP objects can not be copied. |
| 588 |
LexerCPP(const LexerCPP &) = delete; |
| 589 |
LexerCPP(LexerCPP &&) = delete; |
| 590 |
void operator=(const LexerCPP &) = delete; |
| 591 |
void operator=(LexerCPP &&) = delete; |
| 592 |
virtual ~LexerCPP() = default; |
| 593 |
void SCI_METHOD Release() noexcept override { |
| 594 |
delete this; |
| 595 |
} |
| 596 |
int SCI_METHOD Version() const noexcept override { |
| 597 |
return lvRelease5; |
| 598 |
} |
| 599 |
const char *SCI_METHOD PropertyNames() override { |
| 600 |
return osCPP.PropertyNames(); |
| 601 |
} |
| 602 |
int SCI_METHOD PropertyType(const char *name) override { |
| 603 |
return osCPP.PropertyType(name); |
| 604 |
} |
| 605 |
const char *SCI_METHOD DescribeProperty(const char *name) override { |
| 606 |
return osCPP.DescribeProperty(name); |
| 607 |
} |
| 608 |
Sci_Position SCI_METHOD PropertySet(const char *key, const char *val) override; |
| 609 |
const char *SCI_METHOD DescribeWordListSets() override { |
| 610 |
return osCPP.DescribeWordListSets(); |
| 611 |
} |
| 612 |
Sci_Position SCI_METHOD WordListSet(int n, const char *wl) override; |
| 613 |
void SCI_METHOD Lex(Sci_PositionU startPos, Sci_Position length, int initStyle, IDocument *pAccess) override; |
| 614 |
void SCI_METHOD Fold(Sci_PositionU startPos, Sci_Position length, int initStyle, IDocument *pAccess) override; |
| 615 |
|
| 616 |
void *SCI_METHOD PrivateCall(int, void *) noexcept override { |
| 617 |
return nullptr; |
| 618 |
} |
| 619 |
|
| 620 |
int SCI_METHOD LineEndTypesSupported() noexcept override { |
| 621 |
return SC_LINE_END_TYPE_UNICODE; |
| 622 |
} |
| 623 |
|
| 624 |
int SCI_METHOD AllocateSubStyles(int styleBase, int numberStyles) override { |
| 625 |
return subStyles.Allocate(styleBase, numberStyles); |
| 626 |
} |
| 627 |
int SCI_METHOD SubStylesStart(int styleBase) override { |
| 628 |
return subStyles.Start(styleBase); |
| 629 |
} |
| 630 |
int SCI_METHOD SubStylesLength(int styleBase) override { |
| 631 |
return subStyles.Length(styleBase); |
| 632 |
} |
| 633 |
int SCI_METHOD StyleFromSubStyle(int subStyle) override { |
| 634 |
const int styleBase = subStyles.BaseStyle(MaskActive(subStyle)); |
| 635 |
const int inactive = subStyle & inactiveFlag; |
| 636 |
return styleBase | inactive; |
| 637 |
} |
| 638 |
int SCI_METHOD PrimaryStyleFromStyle(int style) noexcept override { |
| 639 |
return MaskActive(style); |
| 640 |
} |
| 641 |
void SCI_METHOD FreeSubStyles() override { |
| 642 |
subStyles.Free(); |
| 643 |
} |
| 644 |
void SCI_METHOD SetIdentifiers(int style, const char *identifiers) override { |
| 645 |
subStyles.SetIdentifiers(style, identifiers); |
| 646 |
} |
| 647 |
int SCI_METHOD DistanceToSecondaryStyles() noexcept override { |
| 648 |
return inactiveFlag; |
| 649 |
} |
| 650 |
const char *SCI_METHOD GetSubStyleBases() noexcept override { |
| 651 |
return styleSubable; |
| 652 |
} |
| 653 |
int SCI_METHOD NamedStyles() override { |
| 654 |
return std::max(subStyles.LastAllocated() + 1, |
| 655 |
sizeLexicalClasses) + |
| 656 |
inactiveFlag; |
| 657 |
} |
| 658 |
const char *SCI_METHOD NameOfStyle(int style) override { |
| 659 |
if (style >= NamedStyles()) |
| 660 |
return ""; |
| 661 |
if (style < sizeLexicalClasses) |
| 662 |
return lexicalClasses[style].name; |
| 663 |
// TODO: inactive and substyles |
| 664 |
return ""; |
| 665 |
} |
| 666 |
const char *SCI_METHOD TagsOfStyle(int style) override { |
| 667 |
if (style >= NamedStyles()) |
| 668 |
return "Excess"; |
| 669 |
returnBuffer.clear(); |
| 670 |
const int firstSubStyle = subStyles.FirstAllocated(); |
| 671 |
if (firstSubStyle >= 0) { |
| 672 |
const int lastSubStyle = subStyles.LastAllocated(); |
| 673 |
if (((style >= firstSubStyle) && (style <= (lastSubStyle))) || |
| 674 |
((style >= firstSubStyle + inactiveFlag) && (style <= (lastSubStyle + inactiveFlag)))) { |
| 675 |
int styleActive = style; |
| 676 |
if (style > lastSubStyle) { |
| 677 |
returnBuffer = "inactive "; |
| 678 |
styleActive -= inactiveFlag; |
| 679 |
} |
| 680 |
const int styleMain = StyleFromSubStyle(styleActive); |
| 681 |
returnBuffer += lexicalClasses[styleMain].tags; |
| 682 |
return returnBuffer.c_str(); |
| 683 |
} |
| 684 |
} |
| 685 |
if (style < sizeLexicalClasses) |
| 686 |
return lexicalClasses[style].tags; |
| 687 |
if (style >= inactiveFlag) { |
| 688 |
returnBuffer = "inactive "; |
| 689 |
const int styleActive = style - inactiveFlag; |
| 690 |
if (styleActive < sizeLexicalClasses) |
| 691 |
returnBuffer += lexicalClasses[styleActive].tags; |
| 692 |
else |
| 693 |
returnBuffer.clear(); |
| 694 |
return returnBuffer.c_str(); |
| 695 |
} |
| 696 |
return ""; |
| 697 |
} |
| 698 |
const char *SCI_METHOD DescriptionOfStyle(int style) override { |
| 699 |
if (style >= NamedStyles()) |
| 700 |
return ""; |
| 701 |
if (style < sizeLexicalClasses) |
| 702 |
return lexicalClasses[style].description; |
| 703 |
// TODO: inactive and substyles |
| 704 |
return ""; |
| 705 |
} |
| 706 |
|
| 707 |
// ILexer5 methods |
| 708 |
const char *SCI_METHOD GetName() override { |
| 709 |
return caseSensitive ? "cpp" : "cppnocase"; |
| 710 |
} |
| 711 |
int SCI_METHOD GetIdentifier() override { |
| 712 |
return caseSensitive ? SCLEX_CPP : SCLEX_CPPNOCASE; |
| 713 |
} |
| 714 |
const char *SCI_METHOD PropertyGet(const char *key) override; |
| 715 |
|
| 716 |
static ILexer5 *LexerFactoryCPP() { |
| 717 |
return new LexerCPP(true); |
| 718 |
} |
| 719 |
static ILexer5 *LexerFactoryCPPInsensitive() { |
| 720 |
return new LexerCPP(false); |
| 721 |
} |
| 722 |
constexpr static int MaskActive(int style) noexcept { |
| 723 |
return style & ~inactiveFlag; |
| 724 |
} |
| 725 |
void EvaluateTokens(Tokens &tokens, const SymbolTable &preprocessorDefinitions); |
| 726 |
[[nodiscard]] Tokens Tokenize(const std::string &expr) const; |
| 727 |
bool EvaluateExpression(const std::string &expr, const SymbolTable &preprocessorDefinitions); |
| 728 |
}; |
| 729 |
|
| 730 |
Sci_Position SCI_METHOD LexerCPP::PropertySet(const char *key, const char *val) { |
| 731 |
if (osCPP.PropertySet(&options, key, val)) { |
| 732 |
if (strcmp(key, "lexer.cpp.allow.dollars") == 0) { |
| 733 |
setWord = CharacterSet(CharacterSet::setAlphaNum, "._", true); |
| 734 |
if (options.identifiersAllowDollars) { |
| 735 |
setWord.Add('$'); |
| 736 |
} |
| 737 |
} |
| 738 |
return 0; |
| 739 |
} |
| 740 |
return -1; |
| 741 |
} |
| 742 |
|
| 743 |
const char *SCI_METHOD LexerCPP::PropertyGet(const char *key) { |
| 744 |
return osCPP.PropertyGet(key); |
| 745 |
} |
| 746 |
|
| 747 |
Sci_Position SCI_METHOD LexerCPP::WordListSet(int n, const char *wl) { |
| 748 |
WordList *wordListN = nullptr; |
| 749 |
switch (n) { |
| 750 |
case 0: |
| 751 |
wordListN = &keywords; |
| 752 |
break; |
| 753 |
case 1: |
| 754 |
wordListN = &keywords2; |
| 755 |
break; |
| 756 |
case 2: |
| 757 |
wordListN = &keywords3; |
| 758 |
break; |
| 759 |
case 3: |
| 760 |
wordListN = &keywords4; |
| 761 |
break; |
| 762 |
case 4: |
| 763 |
wordListN = &ppDefinitions; |
| 764 |
break; |
| 765 |
case 5: |
| 766 |
wordListN = &markerList; |
| 767 |
break; |
| 768 |
default: |
| 769 |
break; |
| 770 |
} |
| 771 |
Sci_Position firstModification = -1; |
| 772 |
if (wordListN) { |
| 773 |
if (wordListN->Set(wl)) { |
| 774 |
firstModification = 0; |
| 775 |
if (n == 4) { |
| 776 |
// Rebuild preprocessorDefinitions |
| 777 |
preprocessorDefinitionsStart.clear(); |
| 778 |
for (int nDefinition = 0; nDefinition < ppDefinitions.Length(); nDefinition++) { |
| 779 |
const Definition def = ParseDefine(ppDefinitions.WordAt(nDefinition), "(="); |
| 780 |
preprocessorDefinitionsStart[std::string(def.name)] = SymbolValue(def.value, def.arguments); |
| 781 |
} |
| 782 |
} |
| 783 |
} |
| 784 |
} |
| 785 |
return firstModification; |
| 786 |
} |
| 787 |
|
| 788 |
void SCI_METHOD LexerCPP::Lex(Sci_PositionU startPos, Sci_Position length, int initStyle, IDocument *pAccess) { |
| 789 |
LexAccessor styler(pAccess); |
| 790 |
|
| 791 |
const StyleContext::Transform transform = caseSensitive ? |
| 792 |
StyleContext::Transform::none : StyleContext::Transform::lower; |
| 793 |
|
| 794 |
const CharacterSet setOKBeforeRE("([{=,:;!%^&*|?~+-> "); |
| 795 |
const CharacterSet setCouldBePostOp("+-"); |
| 796 |
|
| 797 |
const CharacterSet setDoxygen(CharacterSet::setAlpha, "$@\\&<>#{}[]"); |
| 798 |
|
| 799 |
setWordStart = CharacterSet(CharacterSet::setAlpha, "_", true); |
| 800 |
|
| 801 |
const CharacterSet setInvalidRawFirst(" )\\\t\v\f\n"); |
| 802 |
|
| 803 |
if (options.identifiersAllowDollars) { |
| 804 |
setWordStart.Add('$'); |
| 805 |
} |
| 806 |
|
| 807 |
int chPrevNonWhite = ' '; |
| 808 |
int visibleChars = 0; |
| 809 |
bool lastWordWasUUID = false; |
| 810 |
int styleBeforeDCKeyword = SCE_C_DEFAULT; |
| 811 |
int styleBeforeTaskMarker = SCE_C_DEFAULT; |
| 812 |
bool continuationLine = false; |
| 813 |
bool isIncludePreprocessor = false; |
| 814 |
bool isStringInPreprocessor = false; |
| 815 |
bool inRERange = false; |
| 816 |
bool seenDocKeyBrace = false; |
| 817 |
|
| 818 |
std::vector<InterpolatingState> interpolatingStack; |
| 819 |
|
| 820 |
Sci_Position lineCurrent = styler.GetLine(startPos); |
| 821 |
if (options.backQuotedStrings == BackQuotedString::TemplateLiteral) { |
| 822 |
// code copied from LexPython |
| 823 |
auto it = interpolatingAtEol.find(lineCurrent - 1); |
| 824 |
if (it != interpolatingAtEol.end()) { |
| 825 |
interpolatingStack = it->second; |
| 826 |
} |
| 827 |
it = interpolatingAtEol.lower_bound(lineCurrent); |
| 828 |
if (it != interpolatingAtEol.end()) { |
| 829 |
interpolatingAtEol.erase(it, interpolatingAtEol.end()); |
| 830 |
} |
| 831 |
} |
| 832 |
|
| 833 |
if ((MaskActive(initStyle) == SCE_C_PREPROCESSOR) || |
| 834 |
(MaskActive(initStyle) == SCE_C_COMMENTLINE) || |
| 835 |
(MaskActive(initStyle) == SCE_C_COMMENTLINEDOC)) { |
| 836 |
// Set continuationLine if last character of previous line is '\' |
| 837 |
if (lineCurrent > 0) { |
| 838 |
const Sci_Position endLinePrevious = styler.LineEnd(lineCurrent - 1); |
| 839 |
if (endLinePrevious > 0) { |
| 840 |
continuationLine = styler.SafeGetCharAt(endLinePrevious-1) == '\\'; |
| 841 |
} |
| 842 |
} |
| 843 |
} |
| 844 |
|
| 845 |
// look back to set chPrevNonWhite properly for better regex colouring |
| 846 |
if (startPos > 0) { |
| 847 |
Sci_Position back = startPos; |
| 848 |
while (--back && IsSpaceEquiv(MaskActive(styler.StyleAt(back)))) |
| 849 |
; |
| 850 |
if (MaskActive(styler.StyleAt(back)) == SCE_C_OPERATOR) { |
| 851 |
chPrevNonWhite = styler.SafeGetCharAt(back); |
| 852 |
} |
| 853 |
} |
| 854 |
|
| 855 |
StyleContext sc(startPos, length, initStyle, styler); |
| 856 |
LinePPState preproc = vlls.ForLine(lineCurrent); |
| 857 |
|
| 858 |
bool definitionsChanged = false; |
| 859 |
|
| 860 |
// Truncate ppDefineHistory before current line |
| 861 |
|
| 862 |
if (!options.updatePreprocessor) |
| 863 |
ppDefineHistory.clear(); |
| 864 |
|
| 865 |
const std::vector<PPDefinition>::iterator itInvalid = std::find_if( |
| 866 |
ppDefineHistory.begin(), ppDefineHistory.end(), |
| 867 |
[lineCurrent](const PPDefinition &p) noexcept { return p.line >= lineCurrent; }); |
| 868 |
if (itInvalid != ppDefineHistory.end()) { |
| 869 |
ppDefineHistory.erase(itInvalid, ppDefineHistory.end()); |
| 870 |
definitionsChanged = true; |
| 871 |
} |
| 872 |
|
| 873 |
SymbolTable preprocessorDefinitions = preprocessorDefinitionsStart; |
| 874 |
for (const PPDefinition &ppDef : ppDefineHistory) { |
| 875 |
if (ppDef.isUndef) |
| 876 |
preprocessorDefinitions.erase(ppDef.key); |
| 877 |
else |
| 878 |
preprocessorDefinitions[ppDef.key] = SymbolValue(ppDef.value, ppDef.arguments); |
| 879 |
} |
| 880 |
|
| 881 |
std::string rawStringTerminator = rawStringTerminators.ValueAt(lineCurrent-1); |
| 882 |
SparseState<std::string> rawSTNew(lineCurrent); |
| 883 |
|
| 884 |
std::string currentText; |
| 885 |
|
| 886 |
int activitySet = preproc.ActiveState(); |
| 887 |
|
| 888 |
const WordClassifier &classifierIdentifiers = subStyles.Classifier(SCE_C_IDENTIFIER); |
| 889 |
const WordClassifier &classifierDocKeyWords = subStyles.Classifier(SCE_C_COMMENTDOCKEYWORD); |
| 890 |
|
| 891 |
Sci_PositionU lineEndNext = styler.LineEnd(lineCurrent); |
| 892 |
|
| 893 |
if (sc.currentPos == 0 && sc.Match('#', '!')) { |
| 894 |
// Shell Shebang at beginning of file |
| 895 |
sc.SetState(SCE_C_COMMENTLINE); |
| 896 |
sc.Forward(); |
| 897 |
} |
| 898 |
|
| 899 |
for (; sc.More();) { |
| 900 |
|
| 901 |
if (sc.atLineStart) { |
| 902 |
// Using MaskActive() is not needed in the following statement. |
| 903 |
// Inside inactive preprocessor declaration, state will be reset anyway at the end of this block. |
| 904 |
if ((sc.state == SCE_C_STRING) || (sc.state == SCE_C_CHARACTER)) { |
| 905 |
// Prevent SCE_C_STRINGEOL from leaking back to previous line which |
| 906 |
// ends with a line continuation by locking in the state up to this position. |
| 907 |
sc.SetState(sc.state); |
| 908 |
} |
| 909 |
if ((MaskActive(sc.state) == SCE_C_PREPROCESSOR) && (!continuationLine)) { |
| 910 |
sc.SetState(SCE_C_DEFAULT|activitySet); |
| 911 |
} |
| 912 |
// Reset states to beginning of colourise so no surprises |
| 913 |
// if different sets of lines lexed. |
| 914 |
visibleChars = 0; |
| 915 |
lastWordWasUUID = false; |
| 916 |
isIncludePreprocessor = false; |
| 917 |
inRERange = false; |
| 918 |
if (preproc.IsInactive()) { |
| 919 |
activitySet = inactiveFlag; |
| 920 |
sc.SetState(sc.state | activitySet); |
| 921 |
} |
| 922 |
} |
| 923 |
|
| 924 |
if (sc.atLineEnd) { |
| 925 |
lineCurrent++; |
| 926 |
lineEndNext = styler.LineEnd(lineCurrent); |
| 927 |
vlls.Add(lineCurrent, preproc); |
| 928 |
if (!rawStringTerminator.empty()) { |
| 929 |
rawSTNew.Set(lineCurrent-1, rawStringTerminator); |
| 930 |
} |
| 931 |
if (!interpolatingStack.empty()) { |
| 932 |
interpolatingAtEol[sc.currentLine] = interpolatingStack; |
| 933 |
} |
| 934 |
} |
| 935 |
|
| 936 |
// Handle line continuation generically. |
| 937 |
if (sc.ch == '\\') { |
| 938 |
if ((sc.currentPos+1) >= lineEndNext) { |
| 939 |
lineCurrent++; |
| 940 |
lineEndNext = styler.LineEnd(lineCurrent); |
| 941 |
vlls.Add(lineCurrent, preproc); |
| 942 |
if (!rawStringTerminator.empty()) { |
| 943 |
rawSTNew.Set(lineCurrent-1, rawStringTerminator); |
| 944 |
} |
| 945 |
sc.Forward(); |
| 946 |
if (sc.ch == '\r' && sc.chNext == '\n') { |
| 947 |
// Even in UTF-8, \r and \n are separate |
| 948 |
sc.Forward(); |
| 949 |
} |
| 950 |
continuationLine = true; |
| 951 |
sc.Forward(); |
| 952 |
continue; |
| 953 |
} |
| 954 |
} |
| 955 |
|
| 956 |
const bool atLineEndBeforeSwitch = sc.atLineEnd; |
| 957 |
|
| 958 |
// Determine if the current state should terminate. |
| 959 |
switch (MaskActive(sc.state)) { |
| 960 |
case SCE_C_OPERATOR: |
| 961 |
sc.SetState(SCE_C_DEFAULT|activitySet); |
| 962 |
break; |
| 963 |
case SCE_C_NUMBER: |
| 964 |
// We accept almost anything because of hex. and number suffixes |
| 965 |
if (sc.ch == '_') { |
| 966 |
sc.ChangeState(SCE_C_USERLITERAL|activitySet); |
| 967 |
} else if (!(setWord.Contains(sc.ch) |
| 968 |
|| (sc.ch == '\'') |
| 969 |
|| (AnyOf(sc.chPrev, 'e', 'E', 'p', 'P') && AnyOf(sc.ch, '+', '-')))) { |
| 970 |
sc.SetState(SCE_C_DEFAULT|activitySet); |
| 971 |
} |
| 972 |
break; |
| 973 |
case SCE_C_USERLITERAL: |
| 974 |
if (!(setWord.Contains(sc.ch))) |
| 975 |
sc.SetState(SCE_C_DEFAULT|activitySet); |
| 976 |
break; |
| 977 |
case SCE_C_IDENTIFIER: |
| 978 |
if (sc.atLineStart || sc.atLineEnd || !setWord.Contains(sc.ch) || (sc.ch == '.')) { |
| 979 |
sc.GetCurrentString(currentText, transform); |
| 980 |
if (keywords.InList(currentText)) { |
| 981 |
lastWordWasUUID = currentText == "uuid"; |
| 982 |
sc.ChangeState(SCE_C_WORD|activitySet); |
| 983 |
} else if (keywords2.InList(currentText)) { |
| 984 |
sc.ChangeState(SCE_C_WORD2|activitySet); |
| 985 |
} else if (keywords4.InList(currentText)) { |
| 986 |
sc.ChangeState(SCE_C_GLOBALCLASS|activitySet); |
| 987 |
} else { |
| 988 |
const int subStyle = classifierIdentifiers.ValueFor(currentText); |
| 989 |
if (subStyle >= 0) { |
| 990 |
sc.ChangeState(subStyle|activitySet); |
| 991 |
} |
| 992 |
} |
| 993 |
const bool literalString = sc.ch == '\"'; |
| 994 |
if (literalString || sc.ch == '\'') { |
| 995 |
std::string_view s = currentText; |
| 996 |
size_t lenS = s.length(); |
| 997 |
const bool raw = literalString && sc.chPrev == 'R' && !setInvalidRawFirst.Contains(sc.chNext); |
| 998 |
if (raw) { |
| 999 |
s.remove_suffix(1); |
| 1000 |
lenS--; |
| 1001 |
} |
| 1002 |
const bool valid = |
| 1003 |
(lenS == 0) || |
| 1004 |
((lenS == 1) && ((s[0] == 'L') || (s[0] == 'u') || (s[0] == 'U'))) || |
| 1005 |
((lenS == 2) && literalString && (s[0] == 'u') && (s[1] == '8')); |
| 1006 |
if (valid) { |
| 1007 |
if (literalString) { |
| 1008 |
if (raw) { |
| 1009 |
// Set the style of the string prefix to SCE_C_STRINGRAW but then change to |
| 1010 |
// SCE_C_DEFAULT as that allows the raw string start code to run. |
| 1011 |
sc.ChangeState(SCE_C_STRINGRAW|activitySet); |
| 1012 |
sc.SetState(SCE_C_DEFAULT|activitySet); |
| 1013 |
} else { |
| 1014 |
sc.ChangeState(SCE_C_STRING|activitySet); |
| 1015 |
} |
| 1016 |
} else { |
| 1017 |
sc.ChangeState(SCE_C_CHARACTER|activitySet); |
| 1018 |
} |
| 1019 |
} else { |
| 1020 |
sc.SetState(SCE_C_DEFAULT | activitySet); |
| 1021 |
} |
| 1022 |
} else { |
| 1023 |
sc.SetState(SCE_C_DEFAULT|activitySet); |
| 1024 |
} |
| 1025 |
} |
| 1026 |
break; |
| 1027 |
case SCE_C_PREPROCESSOR: |
| 1028 |
if (options.stylingWithinPreprocessor) { |
| 1029 |
if (IsASpace(sc.ch) || (sc.ch == '(')) { |
| 1030 |
sc.SetState(SCE_C_DEFAULT|activitySet); |
| 1031 |
} |
| 1032 |
} else if (isStringInPreprocessor && (sc.Match('>') || sc.Match('\"') || sc.atLineEnd)) { |
| 1033 |
isStringInPreprocessor = false; |
| 1034 |
} else if (!isStringInPreprocessor) { |
| 1035 |
if ((isIncludePreprocessor && sc.Match('<')) || sc.Match('\"')) { |
| 1036 |
isStringInPreprocessor = true; |
| 1037 |
} else if (sc.Match('/', '*')) { |
| 1038 |
if (sc.Match("/**") || sc.Match("/*!")) { |
| 1039 |
sc.SetState(SCE_C_PREPROCESSORCOMMENTDOC|activitySet); |
| 1040 |
} else { |
| 1041 |
sc.SetState(SCE_C_PREPROCESSORCOMMENT|activitySet); |
| 1042 |
} |
| 1043 |
sc.Forward(); // Eat the * |
| 1044 |
} else if (sc.Match('/', '/')) { |
| 1045 |
sc.SetState(SCE_C_DEFAULT|activitySet); |
| 1046 |
} |
| 1047 |
} |
| 1048 |
break; |
| 1049 |
case SCE_C_PREPROCESSORCOMMENT: |
| 1050 |
case SCE_C_PREPROCESSORCOMMENTDOC: |
| 1051 |
if (sc.Match('*', '/')) { |
| 1052 |
sc.Forward(); |
| 1053 |
sc.ForwardSetState(SCE_C_PREPROCESSOR|activitySet); |
| 1054 |
continue; // Without advancing in case of '\'. |
| 1055 |
} |
| 1056 |
break; |
| 1057 |
case SCE_C_COMMENT: |
| 1058 |
if (sc.Match('*', '/')) { |
| 1059 |
sc.Forward(); |
| 1060 |
sc.ForwardSetState(SCE_C_DEFAULT|activitySet); |
| 1061 |
} else { |
| 1062 |
styleBeforeTaskMarker = SCE_C_COMMENT; |
| 1063 |
highlightTaskMarker(sc, styler, activitySet, markerList, caseSensitive); |
| 1064 |
} |
| 1065 |
break; |
| 1066 |
case SCE_C_COMMENTDOC: |
| 1067 |
if (sc.Match('*', '/')) { |
| 1068 |
sc.Forward(); |
| 1069 |
sc.ForwardSetState(SCE_C_DEFAULT|activitySet); |
| 1070 |
} else if (sc.ch == '@' || sc.ch == '\\') { // JavaDoc and Doxygen support |
| 1071 |
// Verify that we have the conditions to mark a comment-doc-keyword |
| 1072 |
if ((IsASpace(sc.chPrev) || sc.chPrev == '*') && (!IsASpace(sc.chNext))) { |
| 1073 |
styleBeforeDCKeyword = SCE_C_COMMENTDOC; |
| 1074 |
sc.SetState(SCE_C_COMMENTDOCKEYWORD|activitySet); |
| 1075 |
} |
| 1076 |
} else if ((sc.ch == '<' && !(IsASpace(sc.chNext) || sc.chNext == '/')) |
| 1077 |
|| (sc.ch == '/' && sc.chPrev == '<')) { // XML comment style |
| 1078 |
styleBeforeDCKeyword = SCE_C_COMMENTDOC; |
| 1079 |
sc.ForwardSetState(SCE_C_COMMENTDOCKEYWORD | activitySet); |
| 1080 |
} |
| 1081 |
break; |
| 1082 |
case SCE_C_COMMENTLINE: |
| 1083 |
if (sc.atLineStart && !continuationLine) { |
| 1084 |
sc.SetState(SCE_C_DEFAULT|activitySet); |
| 1085 |
} else { |
| 1086 |
styleBeforeTaskMarker = SCE_C_COMMENTLINE; |
| 1087 |
highlightTaskMarker(sc, styler, activitySet, markerList, caseSensitive); |
| 1088 |
} |
| 1089 |
break; |
| 1090 |
case SCE_C_COMMENTLINEDOC: |
| 1091 |
if (sc.atLineStart && !continuationLine) { |
| 1092 |
sc.SetState(SCE_C_DEFAULT|activitySet); |
| 1093 |
} else if (sc.ch == '@' || sc.ch == '\\') { // JavaDoc and Doxygen support |
| 1094 |
// Verify that we have the conditions to mark a comment-doc-keyword |
| 1095 |
if ((IsASpace(sc.chPrev) || sc.chPrev == '/' || sc.chPrev == '!') && (!IsASpace(sc.chNext))) { |
| 1096 |
styleBeforeDCKeyword = SCE_C_COMMENTLINEDOC; |
| 1097 |
sc.SetState(SCE_C_COMMENTDOCKEYWORD|activitySet); |
| 1098 |
} |
| 1099 |
} else if ((sc.ch == '<' && sc.chNext != '/') |
| 1100 |
|| (sc.ch == '/' && sc.chPrev == '<')) { // XML comment style |
| 1101 |
styleBeforeDCKeyword = SCE_C_COMMENTLINEDOC; |
| 1102 |
sc.ForwardSetState(SCE_C_COMMENTDOCKEYWORD | activitySet); |
| 1103 |
} |
| 1104 |
break; |
| 1105 |
case SCE_C_COMMENTDOCKEYWORD: |
| 1106 |
if ((styleBeforeDCKeyword == SCE_C_COMMENTDOC) && sc.Match('*', '/')) { |
| 1107 |
sc.ChangeState(SCE_C_COMMENTDOCKEYWORDERROR); |
| 1108 |
sc.Forward(); |
| 1109 |
sc.ForwardSetState(SCE_C_DEFAULT|activitySet); |
| 1110 |
seenDocKeyBrace = false; |
| 1111 |
} else if (sc.ch == '[' || sc.ch == '{') { |
| 1112 |
seenDocKeyBrace = true; |
| 1113 |
} else if (!setDoxygen.Contains(sc.ch) |
| 1114 |
&& !(seenDocKeyBrace && AnyOf(sc.ch, ',', '.'))) { |
| 1115 |
if (!(IsASpace(sc.ch) || (sc.ch == 0))) { |
| 1116 |
sc.ChangeState(SCE_C_COMMENTDOCKEYWORDERROR|activitySet); |
| 1117 |
} else { |
| 1118 |
sc.GetCurrentString(currentText, transform); |
| 1119 |
assert(!currentText.empty()); |
| 1120 |
const std::string currentSuffix = currentText.substr(1); |
| 1121 |
if (!keywords3.InList(currentSuffix) && !keywords3.InList(currentText)) { |
| 1122 |
const int subStyleCDKW = classifierDocKeyWords.ValueFor(currentSuffix); |
| 1123 |
if (subStyleCDKW >= 0) { |
| 1124 |
sc.ChangeState(subStyleCDKW | activitySet); |
| 1125 |
} else { |
| 1126 |
sc.ChangeState(SCE_C_COMMENTDOCKEYWORDERROR | activitySet); |
| 1127 |
} |
| 1128 |
} |
| 1129 |
} |
| 1130 |
sc.SetState(styleBeforeDCKeyword|activitySet); |
| 1131 |
seenDocKeyBrace = false; |
| 1132 |
} else if (sc.ch == '>') { |
| 1133 |
sc.GetCurrentString(currentText, transform); |
| 1134 |
if (!keywords3.InList(currentText)) { |
| 1135 |
const int subStyleCDKW = classifierDocKeyWords.ValueFor(currentText.substr(1)); |
| 1136 |
if (subStyleCDKW >= 0) { |
| 1137 |
sc.ChangeState(subStyleCDKW | activitySet); |
| 1138 |
} else { |
| 1139 |
sc.ChangeState(SCE_C_COMMENTDOCKEYWORDERROR | activitySet); |
| 1140 |
} |
| 1141 |
} |
| 1142 |
sc.SetState(styleBeforeDCKeyword | activitySet); |
| 1143 |
seenDocKeyBrace = false; |
| 1144 |
} |
| 1145 |
break; |
| 1146 |
case SCE_C_STRING: |
| 1147 |
if (sc.atLineEnd) { |
| 1148 |
sc.ChangeState(SCE_C_STRINGEOL|activitySet); |
| 1149 |
} else if (isIncludePreprocessor) { |
| 1150 |
if (sc.ch == '>') { |
| 1151 |
sc.ForwardSetState(SCE_C_DEFAULT|activitySet); |
| 1152 |
isIncludePreprocessor = false; |
| 1153 |
} |
| 1154 |
} else if (sc.ch == '\\') { |
| 1155 |
if (options.escapeSequence) { |
| 1156 |
escapeSeq.resetEscapeState(sc.state, sc.chNext); |
| 1157 |
sc.SetState(SCE_C_ESCAPESEQUENCE|activitySet); |
| 1158 |
} |
| 1159 |
sc.Forward(); // Skip all characters after the backslash |
| 1160 |
} else if (sc.ch == '\"') { |
| 1161 |
if (sc.chNext == '_') { |
| 1162 |
sc.ChangeState(SCE_C_USERLITERAL|activitySet); |
| 1163 |
} else { |
| 1164 |
sc.ForwardSetState(SCE_C_DEFAULT|activitySet); |
| 1165 |
} |
| 1166 |
} |
| 1167 |
break; |
| 1168 |
case SCE_C_ESCAPESEQUENCE: |
| 1169 |
escapeSeq.consumeDigit(); |
| 1170 |
if (escapeSeq.atEscapeEnd(sc.ch)) { |
| 1171 |
sc.SetState(escapeSeq.outerState); |
| 1172 |
continue; |
| 1173 |
} |
| 1174 |
break; |
| 1175 |
case SCE_C_HASHQUOTEDSTRING: |
| 1176 |
if (sc.ch == '\\') { |
| 1177 |
if (sc.chNext == '\"' || sc.chNext == '\'' || sc.chNext == '\\') { |
| 1178 |
sc.Forward(); |
| 1179 |
} |
| 1180 |
} else if (sc.ch == '\"') { |
| 1181 |
sc.ForwardSetState(SCE_C_DEFAULT|activitySet); |
| 1182 |
} |
| 1183 |
break; |
| 1184 |
case SCE_C_STRINGRAW: |
| 1185 |
if (sc.Match(rawStringTerminator.c_str())) { |
| 1186 |
for (size_t termPos=rawStringTerminator.size(); termPos; termPos--) |
| 1187 |
sc.Forward(); |
| 1188 |
sc.SetState(SCE_C_DEFAULT|activitySet); |
| 1189 |
if (interpolatingStack.empty()) { |
| 1190 |
rawStringTerminator.clear(); |
| 1191 |
} |
| 1192 |
} else if (options.backQuotedStrings == BackQuotedString::TemplateLiteral) { |
| 1193 |
if (sc.ch == '\\') { |
| 1194 |
if (options.escapeSequence) { |
| 1195 |
escapeSeq.resetEscapeState(sc.state, sc.chNext); |
| 1196 |
sc.SetState(SCE_C_ESCAPESEQUENCE|activitySet); |
| 1197 |
} |
| 1198 |
sc.Forward(); // Skip all characters after the backslash |
| 1199 |
} else if (sc.Match('$', '{')) { |
| 1200 |
interpolatingStack.push_back({sc.state, 1}); |
| 1201 |
sc.SetState(SCE_C_OPERATOR|activitySet); |
| 1202 |
sc.Forward(); |
| 1203 |
} |
| 1204 |
} |
| 1205 |
break; |
| 1206 |
case SCE_C_CHARACTER: |
| 1207 |
if (sc.atLineEnd) { |
| 1208 |
sc.ChangeState(SCE_C_STRINGEOL|activitySet); |
| 1209 |
} else if (sc.ch == '\\') { |
| 1210 |
if (sc.chNext == '\"' || sc.chNext == '\'' || sc.chNext == '\\') { |
| 1211 |
sc.Forward(); |
| 1212 |
} |
| 1213 |
} else if (sc.ch == '\'') { |
| 1214 |
if (sc.chNext == '_') { |
| 1215 |
sc.ChangeState(SCE_C_USERLITERAL|activitySet); |
| 1216 |
} else { |
| 1217 |
sc.ForwardSetState(SCE_C_DEFAULT|activitySet); |
| 1218 |
} |
| 1219 |
} |
| 1220 |
break; |
| 1221 |
case SCE_C_REGEX: |
| 1222 |
if (sc.atLineStart) { |
| 1223 |
sc.SetState(SCE_C_DEFAULT|activitySet); |
| 1224 |
} else if (!inRERange && sc.ch == '/') { |
| 1225 |
sc.Forward(); |
| 1226 |
while (IsLowerCase(sc.ch)) |
| 1227 |
sc.Forward(); // gobble regex flags |
| 1228 |
sc.SetState(SCE_C_DEFAULT|activitySet); |
| 1229 |
} else if (sc.ch == '\\' && ((sc.currentPos+1) < lineEndNext)) { |
| 1230 |
// Gobble up the escaped character |
| 1231 |
sc.Forward(); |
| 1232 |
} else if (sc.ch == '[') { |
| 1233 |
inRERange = true; |
| 1234 |
} else if (sc.ch == ']') { |
| 1235 |
inRERange = false; |
| 1236 |
} |
| 1237 |
break; |
| 1238 |
case SCE_C_STRINGEOL: |
| 1239 |
if (sc.atLineStart) { |
| 1240 |
sc.SetState(SCE_C_DEFAULT|activitySet); |
| 1241 |
} |
| 1242 |
break; |
| 1243 |
case SCE_C_VERBATIM: |
| 1244 |
if (options.verbatimStringsAllowEscapes && (sc.ch == '\\')) { |
| 1245 |
sc.Forward(); // Skip all characters after the backslash |
| 1246 |
} else if (sc.ch == '\"') { |
| 1247 |
if (sc.chNext == '\"') { |
| 1248 |
sc.Forward(); |
| 1249 |
} else { |
| 1250 |
sc.ForwardSetState(SCE_C_DEFAULT|activitySet); |
| 1251 |
} |
| 1252 |
} |
| 1253 |
break; |
| 1254 |
case SCE_C_TRIPLEVERBATIM: |
| 1255 |
if (sc.Match(R"(""")")) { |
| 1256 |
while (sc.Match('"')) { |
| 1257 |
sc.Forward(); |
| 1258 |
} |
| 1259 |
sc.SetState(SCE_C_DEFAULT|activitySet); |
| 1260 |
} |
| 1261 |
break; |
| 1262 |
case SCE_C_UUID: |
| 1263 |
if (sc.atLineEnd || sc.ch == ')') { |
| 1264 |
sc.SetState(SCE_C_DEFAULT|activitySet); |
| 1265 |
} |
| 1266 |
break; |
| 1267 |
case SCE_C_TASKMARKER: |
| 1268 |
if (IsOperatorOrSpace(sc.ch)) { |
| 1269 |
sc.SetState(styleBeforeTaskMarker|activitySet); |
| 1270 |
styleBeforeTaskMarker = SCE_C_DEFAULT; |
| 1271 |
} |
| 1272 |
} |
| 1273 |
|
| 1274 |
if (sc.atLineEnd && !atLineEndBeforeSwitch) { |
| 1275 |
// State exit processing consumed characters up to end of line. |
| 1276 |
lineCurrent++; |
| 1277 |
lineEndNext = styler.LineEnd(lineCurrent); |
| 1278 |
vlls.Add(lineCurrent, preproc); |
| 1279 |
} |
| 1280 |
|
| 1281 |
const bool atLineEndBeforeStateEntry = sc.atLineEnd; |
| 1282 |
|
| 1283 |
// Determine if a new state should be entered. |
| 1284 |
if (MaskActive(sc.state) == SCE_C_DEFAULT) { |
| 1285 |
if (sc.Match('@', '\"')) { |
| 1286 |
sc.SetState(SCE_C_VERBATIM|activitySet); |
| 1287 |
sc.Forward(); |
| 1288 |
} else if (options.triplequotedStrings && sc.Match(R"(""")")) { |
| 1289 |
sc.SetState(SCE_C_TRIPLEVERBATIM|activitySet); |
| 1290 |
sc.Forward(2); |
| 1291 |
} else if (options.hashquotedStrings && sc.Match('#', '\"')) { |
| 1292 |
sc.SetState(SCE_C_HASHQUOTEDSTRING|activitySet); |
| 1293 |
sc.Forward(); |
| 1294 |
} else if ((options.backQuotedStrings != BackQuotedString::None) && sc.Match('`')) { |
| 1295 |
sc.SetState(SCE_C_STRINGRAW|activitySet); |
| 1296 |
rawStringTerminator = "`"; |
| 1297 |
} else if (IsADigit(sc.ch) || (sc.ch == '.' && IsADigit(sc.chNext))) { |
| 1298 |
if (lastWordWasUUID) { |
| 1299 |
sc.SetState(SCE_C_UUID|activitySet); |
| 1300 |
lastWordWasUUID = false; |
| 1301 |
} else { |
| 1302 |
sc.SetState(SCE_C_NUMBER|activitySet); |
| 1303 |
} |
| 1304 |
} else if (!sc.atLineEnd && (setWordStart.Contains(sc.ch) || (sc.ch == '@'))) { |
| 1305 |
if (lastWordWasUUID) { |
| 1306 |
sc.SetState(SCE_C_UUID|activitySet); |
| 1307 |
lastWordWasUUID = false; |
| 1308 |
} else { |
| 1309 |
sc.SetState(SCE_C_IDENTIFIER|activitySet); |
| 1310 |
} |
| 1311 |
} else if (sc.Match('/', '*')) { |
| 1312 |
if (sc.Match("/**") || sc.Match("/*!")) { // Support of Qt/Doxygen doc. style |
| 1313 |
sc.SetState(SCE_C_COMMENTDOC|activitySet); |
| 1314 |
} else { |
| 1315 |
sc.SetState(SCE_C_COMMENT|activitySet); |
| 1316 |
} |
| 1317 |
sc.Forward(); // Eat the * so it isn't used for the end of the comment |
| 1318 |
} else if (sc.Match('/', '/')) { |
| 1319 |
if ((sc.Match("///") && !sc.Match("////")) || sc.Match("//!")) |
| 1320 |
// Support of Qt/Doxygen doc. style |
| 1321 |
sc.SetState(SCE_C_COMMENTLINEDOC|activitySet); |
| 1322 |
else |
| 1323 |
sc.SetState(SCE_C_COMMENTLINE|activitySet); |
| 1324 |
} else if (sc.ch == '/' |
| 1325 |
&& (setOKBeforeRE.Contains(chPrevNonWhite) |
| 1326 |
|| followsReturnKeyword(sc, styler)) |
| 1327 |
&& (!setCouldBePostOp.Contains(chPrevNonWhite) |
| 1328 |
|| !FollowsPostfixOperator(sc, styler))) { |
| 1329 |
sc.SetState(SCE_C_REGEX|activitySet); // JavaScript's RegEx |
| 1330 |
inRERange = false; |
| 1331 |
} else if (sc.ch == '\"') { |
| 1332 |
if (sc.chPrev == 'R') { |
| 1333 |
styler.Flush(); |
| 1334 |
if (MaskActive(styler.StyleAt(sc.currentPos - 1)) == SCE_C_STRINGRAW) { |
| 1335 |
sc.SetState(SCE_C_STRINGRAW|activitySet); |
| 1336 |
rawStringTerminator = ")"; |
| 1337 |
for (Sci_Position termPos = sc.currentPos + 1;; termPos++) { |
| 1338 |
const char chTerminator = styler.SafeGetCharAt(termPos, '('); |
| 1339 |
if (chTerminator == '(') |
| 1340 |
break; |
| 1341 |
rawStringTerminator += chTerminator; |
| 1342 |
} |
| 1343 |
rawStringTerminator += '\"'; |
| 1344 |
} else { |
| 1345 |
sc.SetState(SCE_C_STRING|activitySet); |
| 1346 |
} |
| 1347 |
} else { |
| 1348 |
sc.SetState(SCE_C_STRING|activitySet); |
| 1349 |
} |
| 1350 |
isIncludePreprocessor = false; // ensure that '>' won't end the string |
| 1351 |
} else if (isIncludePreprocessor && sc.ch == '<') { |
| 1352 |
sc.SetState(SCE_C_STRING|activitySet); |
| 1353 |
} else if (sc.ch == '\'') { |
| 1354 |
sc.SetState(SCE_C_CHARACTER|activitySet); |
| 1355 |
} else if (sc.ch == '#' && visibleChars == 0) { |
| 1356 |
// Preprocessor commands are alone on their line |
| 1357 |
sc.SetState(SCE_C_PREPROCESSOR|activitySet); |
| 1358 |
// Skip whitespace between # and preprocessor word |
| 1359 |
do { |
| 1360 |
sc.Forward(); |
| 1361 |
} while ((sc.ch == ' ' || sc.ch == '\t') && sc.More()); |
| 1362 |
if (sc.Match("include")) { |
| 1363 |
isIncludePreprocessor = true; |
| 1364 |
} else { |
| 1365 |
if (options.trackPreprocessor && IsAlphaNumeric(sc.ch)) { |
| 1366 |
// If #if is nested too deeply (>31 levels) the active/inactive appearance |
| 1367 |
// will stop reflecting the code. |
| 1368 |
if (sc.Match("ifdef") || sc.Match("ifndef")) { |
| 1369 |
const bool isIfDef = sc.Match("ifdef"); |
| 1370 |
const int startRest = isIfDef ? 5 : 6; |
| 1371 |
const std::string restOfLine = GetRestOfLine(styler, sc.currentPos + startRest + 1, false); |
| 1372 |
const bool foundDef = preprocessorDefinitions.find(restOfLine) != preprocessorDefinitions.end(); |
| 1373 |
preproc.StartSection(isIfDef == foundDef); |
| 1374 |
} else if (sc.Match("if")) { |
| 1375 |
const std::string restOfLine = GetRestOfLine(styler, sc.currentPos + 2, true); |
| 1376 |
const bool ifGood = EvaluateExpression(restOfLine, preprocessorDefinitions); |
| 1377 |
preproc.StartSection(ifGood); |
| 1378 |
} else if (sc.Match("else")) { |
| 1379 |
// #else is shown as active if either preceding or following section is active |
| 1380 |
// as that means that it contributed to the result. |
| 1381 |
if (preproc.ValidLevel()) { |
| 1382 |
// If #else has no corresponding #if then take no action as invalid |
| 1383 |
if (!preproc.CurrentIfTaken()) { |
| 1384 |
// Inactive, may become active if parent scope active |
| 1385 |
assert(sc.state == (SCE_C_PREPROCESSOR | inactiveFlag)); |
| 1386 |
preproc.InvertCurrentLevel(); |
| 1387 |
activitySet = preproc.ActiveState(); |
| 1388 |
// If following is active then show "else" as active |
| 1389 |
if (!activitySet) |
| 1390 |
sc.ChangeState(SCE_C_PREPROCESSOR); |
| 1391 |
} else if (preproc.IsActive()) { |
| 1392 |
// Active -> inactive |
| 1393 |
assert(sc.state == SCE_C_PREPROCESSOR); |
| 1394 |
preproc.InvertCurrentLevel(); |
| 1395 |
activitySet = preproc.ActiveState(); |
| 1396 |
// Continue to show "else" as active as it ends active section. |
| 1397 |
} |
| 1398 |
} |
| 1399 |
} else if (sc.Match("elif")) { |
| 1400 |
// Ensure only one chosen out of #if .. #elif .. #elif .. #else .. #endif |
| 1401 |
// #elif is shown as active if either preceding or following section is active |
| 1402 |
// as that means that it contributed to the result. |
| 1403 |
if (preproc.ValidLevel()) { |
| 1404 |
if (!preproc.CurrentIfTaken()) { |
| 1405 |
// Inactive, if expression true then may become active if parent scope active |
| 1406 |
assert(sc.state == (SCE_C_PREPROCESSOR | inactiveFlag)); |
| 1407 |
// Similar to #if |
| 1408 |
const std::string restOfLine = GetRestOfLine(styler, sc.currentPos + 4, true); |
| 1409 |
const bool ifGood = EvaluateExpression(restOfLine, preprocessorDefinitions); |
| 1410 |
if (ifGood) { |
| 1411 |
preproc.InvertCurrentLevel(); |
| 1412 |
activitySet = preproc.ActiveState(); |
| 1413 |
if (!activitySet) |
| 1414 |
sc.ChangeState(SCE_C_PREPROCESSOR); |
| 1415 |
} |
| 1416 |
} else if (preproc.IsActive()) { |
| 1417 |
// Active -> inactive |
| 1418 |
assert(sc.state == SCE_C_PREPROCESSOR); |
| 1419 |
preproc.InvertCurrentLevel(); |
| 1420 |
activitySet = preproc.ActiveState(); |
| 1421 |
// Continue to show "elif" as active as it ends active section. |
| 1422 |
} |
| 1423 |
} |
| 1424 |
} else if (sc.Match("endif")) { |
| 1425 |
preproc.EndSection(); |
| 1426 |
activitySet = preproc.ActiveState(); |
| 1427 |
sc.ChangeState(SCE_C_PREPROCESSOR|activitySet); |
| 1428 |
} else if (sc.Match("define")) { |
| 1429 |
if (options.updatePreprocessor && preproc.IsActive()) { |
| 1430 |
const std::string restOfLine = GetRestOfLine(styler, sc.currentPos + 6, true); |
| 1431 |
const Definition def = ParseDefine(restOfLine, "( \t"); |
| 1432 |
preprocessorDefinitions[std::string(def.name)] = SymbolValue(def.value, def.arguments); |
| 1433 |
ppDefineHistory.emplace_back(lineCurrent, def.name, def.value, false, def.arguments); |
| 1434 |
definitionsChanged = true; |
| 1435 |
} |
| 1436 |
} else if (sc.Match("undef")) { |
| 1437 |
if (options.updatePreprocessor && preproc.IsActive()) { |
| 1438 |
const std::string restOfLine = GetRestOfLine(styler, sc.currentPos + 5, false); |
| 1439 |
Tokens tokens = Tokenize(restOfLine); |
| 1440 |
if (!tokens.empty()) { |
| 1441 |
const std::string key = tokens[0]; |
| 1442 |
preprocessorDefinitions.erase(key); |
| 1443 |
ppDefineHistory.emplace_back(lineCurrent, key, "", true, ""); |
| 1444 |
definitionsChanged = true; |
| 1445 |
} |
| 1446 |
} |
| 1447 |
} |
| 1448 |
} |
| 1449 |
} |
| 1450 |
} else if (isoperator(sc.ch)) { |
| 1451 |
sc.SetState(SCE_C_OPERATOR|activitySet); |
| 1452 |
if (!interpolatingStack.empty() && AnyOf(sc.ch, '{', '}')) { |
| 1453 |
InterpolatingState ¤t = interpolatingStack.back(); |
| 1454 |
if (sc.ch == '{') { |
| 1455 |
current.braceCount += 1; |
| 1456 |
} else { |
| 1457 |
current.braceCount -= 1; |
| 1458 |
if (current.braceCount == 0) { |
| 1459 |
sc.ForwardSetState(current.state); |
| 1460 |
interpolatingStack.pop_back(); |
| 1461 |
continue; |
| 1462 |
} |
| 1463 |
} |
| 1464 |
} |
| 1465 |
} |
| 1466 |
} |
| 1467 |
|
| 1468 |
if (sc.atLineEnd && !atLineEndBeforeStateEntry) { |
| 1469 |
// State entry processing consumed characters up to end of line. |
| 1470 |
lineCurrent++; |
| 1471 |
lineEndNext = styler.LineEnd(lineCurrent); |
| 1472 |
vlls.Add(lineCurrent, preproc); |
| 1473 |
} |
| 1474 |
|
| 1475 |
if (!IsASpace(sc.ch) && !IsSpaceEquiv(MaskActive(sc.state))) { |
| 1476 |
chPrevNonWhite = sc.ch; |
| 1477 |
visibleChars++; |
| 1478 |
} |
| 1479 |
continuationLine = false; |
| 1480 |
sc.Forward(); |
| 1481 |
} |
| 1482 |
const bool rawStringsChanged = rawStringTerminators.Merge(rawSTNew, lineCurrent); |
| 1483 |
if (definitionsChanged || rawStringsChanged) |
| 1484 |
styler.ChangeLexerState(startPos, startPos + length); |
| 1485 |
sc.Complete(); |
| 1486 |
} |
| 1487 |
|
| 1488 |
// Store both the current line's fold level and the next lines in the |
| 1489 |
// level store to make it easy to pick up with each increment |
| 1490 |
// and to make it possible to fiddle the current level for "} else {". |
| 1491 |
|
| 1492 |
void SCI_METHOD LexerCPP::Fold(Sci_PositionU startPos, Sci_Position length, int initStyle, IDocument *pAccess) { |
| 1493 |
|
| 1494 |
if (!options.fold) |
| 1495 |
return; |
| 1496 |
|
| 1497 |
LexAccessor styler(pAccess); |
| 1498 |
|
| 1499 |
const Sci_PositionU endPos = startPos + length; |
| 1500 |
int visibleChars = 0; |
| 1501 |
bool inLineComment = false; |
| 1502 |
Sci_Position lineCurrent = styler.GetLine(startPos); |
| 1503 |
int levelCurrent = SC_FOLDLEVELBASE; |
| 1504 |
if (lineCurrent > 0) |
| 1505 |
levelCurrent = styler.LevelAt(lineCurrent-1) >> 16; |
| 1506 |
Sci_PositionU lineStartNext = styler.LineStart(lineCurrent+1); |
| 1507 |
int levelMinCurrent = levelCurrent; |
| 1508 |
int levelNext = levelCurrent; |
| 1509 |
char chNext = styler[startPos]; |
| 1510 |
int styleNext = MaskActive(styler.StyleAt(startPos)); |
| 1511 |
int style = MaskActive(initStyle); |
| 1512 |
const bool userDefinedFoldMarkers = !options.foldExplicitStart.empty() && !options.foldExplicitEnd.empty(); |
| 1513 |
for (Sci_PositionU i = startPos; i < endPos; i++) { |
| 1514 |
const char ch = chNext; |
| 1515 |
chNext = styler.SafeGetCharAt(i + 1); |
| 1516 |
const int stylePrev = style; |
| 1517 |
style = styleNext; |
| 1518 |
styleNext = MaskActive(styler.StyleAt(i + 1)); |
| 1519 |
const bool atEOL = i == (lineStartNext-1); |
| 1520 |
if ((style == SCE_C_COMMENTLINE) || (style == SCE_C_COMMENTLINEDOC)) |
| 1521 |
inLineComment = true; |
| 1522 |
if (options.foldComment && options.foldCommentMultiline && IsStreamCommentStyle(style) && !inLineComment) { |
| 1523 |
if (!IsStreamCommentStyle(stylePrev)) { |
| 1524 |
levelNext++; |
| 1525 |
} else if (!IsStreamCommentStyle(styleNext) && !atEOL) { |
| 1526 |
// Comments don't end at end of line and the next character may be unstyled. |
| 1527 |
levelNext--; |
| 1528 |
} |
| 1529 |
} |
| 1530 |
if (options.foldComment && options.foldCommentExplicit && ((style == SCE_C_COMMENTLINE) || options.foldExplicitAnywhere)) { |
| 1531 |
if (userDefinedFoldMarkers) { |
| 1532 |
if (styler.Match(i, options.foldExplicitStart.c_str())) { |
| 1533 |
levelNext++; |
| 1534 |
} else if (styler.Match(i, options.foldExplicitEnd.c_str())) { |
| 1535 |
levelNext--; |
| 1536 |
} |
| 1537 |
} else { |
| 1538 |
if ((ch == '/') && (chNext == '/')) { |
| 1539 |
const char chNext2 = styler.SafeGetCharAt(i + 2); |
| 1540 |
if (chNext2 == '{') { |
| 1541 |
levelNext++; |
| 1542 |
} else if (chNext2 == '}') { |
| 1543 |
levelNext--; |
| 1544 |
} |
| 1545 |
} |
| 1546 |
} |
| 1547 |
} |
| 1548 |
if (options.foldPreprocessor && (style == SCE_C_PREPROCESSOR)) { |
| 1549 |
if (ch == '#') { |
| 1550 |
Sci_PositionU j = i + 1; |
| 1551 |
while ((j < endPos) && IsASpaceOrTab(styler.SafeGetCharAt(j))) { |
| 1552 |
j++; |
| 1553 |
} |
| 1554 |
if (styler.Match(j, "region") || styler.Match(j, "if")) { |
| 1555 |
levelNext++; |
| 1556 |
} else if (styler.Match(j, "end")) { |
| 1557 |
levelNext--; |
| 1558 |
} |
| 1559 |
|
| 1560 |
if (options.foldPreprocessorAtElse && (styler.Match(j, "else") || styler.Match(j, "elif"))) { |
| 1561 |
levelMinCurrent--; |
| 1562 |
} |
| 1563 |
} |
| 1564 |
} |
| 1565 |
if (options.foldSyntaxBased && (style == SCE_C_OPERATOR)) { |
| 1566 |
if (ch == '{' || ch == '[' || ch == '(') { |
| 1567 |
// Measure the minimum before a '{' to allow |
| 1568 |
// folding on "} else {" |
| 1569 |
if (options.foldAtElse && levelMinCurrent > levelNext) { |
| 1570 |
levelMinCurrent = levelNext; |
| 1571 |
} |
| 1572 |
levelNext++; |
| 1573 |
} else if (ch == '}' || ch == ']' || ch == ')') { |
| 1574 |
levelNext--; |
| 1575 |
} |
| 1576 |
} |
| 1577 |
if (!IsASpace(ch)) |
| 1578 |
visibleChars++; |
| 1579 |
if (atEOL || (i == endPos-1)) { |
| 1580 |
int levelUse = levelCurrent; |
| 1581 |
if ((options.foldSyntaxBased && options.foldAtElse) || |
| 1582 |
(options.foldPreprocessor && options.foldPreprocessorAtElse) |
| 1583 |
) { |
| 1584 |
levelUse = levelMinCurrent; |
| 1585 |
} |
| 1586 |
int lev = levelUse | levelNext << 16; |
| 1587 |
if (visibleChars == 0 && options.foldCompact) |
| 1588 |
lev |= SC_FOLDLEVELWHITEFLAG; |
| 1589 |
if (levelUse < levelNext) |
| 1590 |
lev |= SC_FOLDLEVELHEADERFLAG; |
| 1591 |
if (lev != styler.LevelAt(lineCurrent)) { |
| 1592 |
styler.SetLevel(lineCurrent, lev); |
| 1593 |
} |
| 1594 |
lineCurrent++; |
| 1595 |
lineStartNext = styler.LineStart(lineCurrent+1); |
| 1596 |
levelCurrent = levelNext; |
| 1597 |
levelMinCurrent = levelCurrent; |
| 1598 |
if (atEOL && (i == static_cast<Sci_PositionU>(styler.Length()-1))) { |
| 1599 |
// There is an empty line at end of file so give it same level and empty |
| 1600 |
styler.SetLevel(lineCurrent, (levelCurrent | levelCurrent << 16) | SC_FOLDLEVELWHITEFLAG); |
| 1601 |
} |
| 1602 |
visibleChars = 0; |
| 1603 |
inLineComment = false; |
| 1604 |
} |
| 1605 |
} |
| 1606 |
} |
| 1607 |
|
| 1608 |
void LexerCPP::EvaluateTokens(Tokens &tokens, const SymbolTable &preprocessorDefinitions) { |
| 1609 |
|
| 1610 |
// Remove whitespace tokens |
| 1611 |
tokens.erase(std::remove_if(tokens.begin(), tokens.end(), OnlySpaceOrTab), tokens.end()); |
| 1612 |
|
| 1613 |
// Evaluate defined statements to either 0 or 1 |
| 1614 |
for (size_t i=0; (i+1)<tokens.size();) { |
| 1615 |
if (tokens[i] == "defined") { |
| 1616 |
const char *val = "0"; |
| 1617 |
if (tokens[i+1] == "(") { |
| 1618 |
if (((i + 2)<tokens.size()) && (tokens[i + 2] == ")")) { |
| 1619 |
// defined() |
| 1620 |
tokens.erase(tokens.begin() + i + 1, tokens.begin() + i + 3); |
| 1621 |
} else if (((i+3)<tokens.size()) && (tokens[i+3] == ")")) { |
| 1622 |
// defined(<identifier>) |
| 1623 |
const SymbolTable::const_iterator it = preprocessorDefinitions.find(tokens[i+2]); |
| 1624 |
if (it != preprocessorDefinitions.end()) { |
| 1625 |
val = "1"; |
| 1626 |
} |
| 1627 |
tokens.erase(tokens.begin() + i + 1, tokens.begin() + i + 4); |
| 1628 |
} else { |
| 1629 |
// Spurious '(' so erase as more likely to result in false |
| 1630 |
tokens.erase(tokens.begin() + i + 1, tokens.begin() + i + 2); |
| 1631 |
} |
| 1632 |
} else { |
| 1633 |
// defined <identifier> |
| 1634 |
const SymbolTable::const_iterator it = preprocessorDefinitions.find(tokens[i+1]); |
| 1635 |
if (it != preprocessorDefinitions.end()) { |
| 1636 |
val = "1"; |
| 1637 |
} |
| 1638 |
tokens.erase(tokens.begin() + i + 1, tokens.begin() + i + 2); |
| 1639 |
} |
| 1640 |
tokens[i] = val; |
| 1641 |
} else { |
| 1642 |
i++; |
| 1643 |
} |
| 1644 |
} |
| 1645 |
|
| 1646 |
// Evaluate identifiers |
| 1647 |
constexpr size_t maxIterations = 100; |
| 1648 |
size_t iterations = 0; // Limit number of iterations in case there is a recursive macro. |
| 1649 |
for (size_t i = 0; (i<tokens.size()) && (iterations < maxIterations);) { |
| 1650 |
iterations++; |
| 1651 |
if (setWordStart.Contains(tokens[i][0])) { |
| 1652 |
const SymbolTable::const_iterator it = preprocessorDefinitions.find(tokens[i]); |
| 1653 |
if (it != preprocessorDefinitions.end()) { |
| 1654 |
// Tokenize value |
| 1655 |
Tokens macroTokens = Tokenize(it->second.value); |
| 1656 |
if (it->second.IsMacro()) { |
| 1657 |
if ((i + 1 < tokens.size()) && (tokens.at(i + 1) == "(")) { |
| 1658 |
// Create map of argument name to value |
| 1659 |
const Tokens argumentNames = StringSplit(it->second.arguments, ','); |
| 1660 |
std::map<std::string, std::string> arguments; |
| 1661 |
size_t arg = 0; |
| 1662 |
size_t tok = i+2; |
| 1663 |
while ((tok < tokens.size()) && (arg < argumentNames.size()) && (tokens.at(tok) != ")")) { |
| 1664 |
if (tokens.at(tok) != ",") { |
| 1665 |
arguments[argumentNames.at(arg)] = tokens.at(tok); |
| 1666 |
arg++; |
| 1667 |
} |
| 1668 |
tok++; |
| 1669 |
} |
| 1670 |
|
| 1671 |
// Remove invocation |
| 1672 |
tokens.erase(tokens.begin() + i, tokens.begin() + tok + 1); |
| 1673 |
|
| 1674 |
// Substitute values into macro |
| 1675 |
macroTokens.erase(std::remove_if(macroTokens.begin(), macroTokens.end(), OnlySpaceOrTab), macroTokens.end()); |
| 1676 |
|
| 1677 |
for (size_t iMacro = 0; iMacro < macroTokens.size();) { |
| 1678 |
if (setWordStart.Contains(macroTokens[iMacro][0])) { |
| 1679 |
const std::map<std::string, std::string>::const_iterator itFind = |
| 1680 |
arguments.find(macroTokens[iMacro]); |
| 1681 |
if (itFind != arguments.end()) { |
| 1682 |
// TODO: Possible that value will be expression so should insert tokenized form |
| 1683 |
macroTokens[iMacro] = itFind->second; |
| 1684 |
} |
| 1685 |
} |
| 1686 |
iMacro++; |
| 1687 |
} |
| 1688 |
|
| 1689 |
// Insert results back into tokens |
| 1690 |
tokens.insert(tokens.begin() + i, macroTokens.begin(), macroTokens.end()); |
| 1691 |
|
| 1692 |
} else { |
| 1693 |
i++; |
| 1694 |
} |
| 1695 |
} else { |
| 1696 |
// Remove invocation |
| 1697 |
tokens.erase(tokens.begin() + i); |
| 1698 |
// Insert results back into tokens |
| 1699 |
tokens.insert(tokens.begin() + i, macroTokens.begin(), macroTokens.end()); |
| 1700 |
} |
| 1701 |
} else { |
| 1702 |
// Identifier not found and value defaults to zero |
| 1703 |
tokens[i] = "0"; |
| 1704 |
} |
| 1705 |
} else { |
| 1706 |
i++; |
| 1707 |
} |
| 1708 |
} |
| 1709 |
|
| 1710 |
// Find bracketed subexpressions and recurse on them |
| 1711 |
BracketPair bracketPair = FindBracketPair(tokens); |
| 1712 |
while (bracketPair.itBracket != tokens.end()) { |
| 1713 |
Tokens inBracket(bracketPair.itBracket + 1, bracketPair.itEndBracket); |
| 1714 |
EvaluateTokens(inBracket, preprocessorDefinitions); |
| 1715 |
|
| 1716 |
// The insertion is done before the removal because there were failures with the opposite approach |
| 1717 |
tokens.insert(bracketPair.itBracket, inBracket.begin(), inBracket.end()); |
| 1718 |
|
| 1719 |
// insert invalidated bracketPair. Use a new variable to avoid warning from Coverity. |
| 1720 |
const BracketPair pairToErase = FindBracketPair(tokens); |
| 1721 |
tokens.erase(pairToErase.itBracket, pairToErase.itEndBracket + 1); |
| 1722 |
|
| 1723 |
bracketPair = FindBracketPair(tokens); |
| 1724 |
} |
| 1725 |
|
| 1726 |
// Evaluate logical negations |
| 1727 |
for (size_t j=0; (j+1)<tokens.size();) { |
| 1728 |
if (setNegationOp.Contains(tokens[j][0])) { |
| 1729 |
int isTrue = atoi(tokens[j+1].c_str()); |
| 1730 |
if (tokens[j] == "!") |
| 1731 |
isTrue = !isTrue; |
| 1732 |
const Tokens::iterator itInsert = |
| 1733 |
tokens.erase(tokens.begin() + j, tokens.begin() + j + 2); |
| 1734 |
tokens.insert(itInsert, isTrue ? "1" : "0"); |
| 1735 |
} else { |
| 1736 |
j++; |
| 1737 |
} |
| 1738 |
} |
| 1739 |
|
| 1740 |
// Evaluate expressions in precedence order |
| 1741 |
enum precedence { precMult, precAdd, precRelative |
| 1742 |
, precLogical, /* end marker */ precLast }; |
| 1743 |
for (int prec = precMult; prec < precLast; prec++) { |
| 1744 |
// Looking at 3 tokens at a time so end at 2 before end |
| 1745 |
for (size_t k=0; (k+2)<tokens.size();) { |
| 1746 |
const char chOp = tokens[k+1][0]; |
| 1747 |
if ( |
| 1748 |
((prec==precMult) && setMultOp.Contains(chOp)) || |
| 1749 |
((prec==precAdd) && setAddOp.Contains(chOp)) || |
| 1750 |
((prec==precRelative) && setRelOp.Contains(chOp)) || |
| 1751 |
((prec==precLogical) && setLogicalOp.Contains(chOp)) |
| 1752 |
) { |
| 1753 |
const int valA = atoi(tokens[k].c_str()); |
| 1754 |
const int valB = atoi(tokens[k+2].c_str()); |
| 1755 |
int result = 0; |
| 1756 |
if (tokens[k+1] == "+") |
| 1757 |
result = valA + valB; |
| 1758 |
else if (tokens[k+1] == "-") |
| 1759 |
result = valA - valB; |
| 1760 |
else if (tokens[k+1] == "*") |
| 1761 |
result = valA * valB; |
| 1762 |
else if (tokens[k+1] == "/") |
| 1763 |
result = valA / (valB ? valB : 1); |
| 1764 |
else if (tokens[k+1] == "%") |
| 1765 |
result = valA % (valB ? valB : 1); |
| 1766 |
else if (tokens[k+1] == "<") |
| 1767 |
result = valA < valB; |
| 1768 |
else if (tokens[k+1] == "<=") |
| 1769 |
result = valA <= valB; |
| 1770 |
else if (tokens[k+1] == ">") |
| 1771 |
result = valA > valB; |
| 1772 |
else if (tokens[k+1] == ">=") |
| 1773 |
result = valA >= valB; |
| 1774 |
else if (tokens[k+1] == "==") |
| 1775 |
result = valA == valB; |
| 1776 |
else if (tokens[k+1] == "!=") |
| 1777 |
result = valA != valB; |
| 1778 |
else if (tokens[k+1] == "||") |
| 1779 |
result = valA || valB; |
| 1780 |
else if (tokens[k+1] == "&&") |
| 1781 |
result = valA && valB; |
| 1782 |
const Tokens::iterator itInsert = |
| 1783 |
tokens.erase(tokens.begin() + k, tokens.begin() + k + 3); |
| 1784 |
tokens.insert(itInsert, std::to_string(result)); |
| 1785 |
} else { |
| 1786 |
k++; |
| 1787 |
} |
| 1788 |
} |
| 1789 |
} |
| 1790 |
} |
| 1791 |
|
| 1792 |
Tokens LexerCPP::Tokenize(const std::string &expr) const { |
| 1793 |
// Break into tokens |
| 1794 |
Tokens tokens; |
| 1795 |
const char *cp = expr.c_str(); |
| 1796 |
while (*cp) { |
| 1797 |
std::string word; |
| 1798 |
if (setWord.Contains(*cp)) { |
| 1799 |
// Identifiers and numbers |
| 1800 |
while (setWord.Contains(*cp)) { |
| 1801 |
word += *cp; |
| 1802 |
cp++; |
| 1803 |
} |
| 1804 |
} else if (IsASpaceOrTab(*cp)) { |
| 1805 |
while (IsASpaceOrTab(*cp)) { |
| 1806 |
word += *cp; |
| 1807 |
cp++; |
| 1808 |
} |
| 1809 |
} else if (setRelOp.Contains(*cp)) { |
| 1810 |
word += *cp; |
| 1811 |
cp++; |
| 1812 |
if (setRelOp.Contains(*cp)) { |
| 1813 |
word += *cp; |
| 1814 |
cp++; |
| 1815 |
} |
| 1816 |
} else if (setLogicalOp.Contains(*cp)) { |
| 1817 |
word += *cp; |
| 1818 |
cp++; |
| 1819 |
if (setLogicalOp.Contains(*cp)) { |
| 1820 |
word += *cp; |
| 1821 |
cp++; |
| 1822 |
} |
| 1823 |
} else { |
| 1824 |
// Should handle strings, characters, and comments here |
| 1825 |
word += *cp; |
| 1826 |
cp++; |
| 1827 |
} |
| 1828 |
tokens.push_back(word); |
| 1829 |
} |
| 1830 |
return tokens; |
| 1831 |
} |
| 1832 |
|
| 1833 |
bool LexerCPP::EvaluateExpression(const std::string &expr, const SymbolTable &preprocessorDefinitions) { |
| 1834 |
Tokens tokens = Tokenize(expr); |
| 1835 |
|
| 1836 |
EvaluateTokens(tokens, preprocessorDefinitions); |
| 1837 |
|
| 1838 |
// "0" or "" -> false else true |
| 1839 |
const bool isFalse = tokens.empty() || |
| 1840 |
((tokens.size() == 1) && (tokens[0].empty() || tokens[0] == "0")); |
| 1841 |
return !isFalse; |
| 1842 |
} |
| 1843 |
|
| 1844 |
extern const LexerModule lmCPP(SCLEX_CPP, LexerCPP::LexerFactoryCPP, "cpp", cppWordLists); |
| 1845 |
extern const LexerModule lmCPPNoCase(SCLEX_CPPNOCASE, LexerCPP::LexerFactoryCPPInsensitive, "cppnocase", cppWordLists); |