1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
|
// Scintilla source code edit control
/**
* @file LexRegistry.cxx
* @date July 26 2014
* @brief Lexer for Windows registration files(.reg)
* @author nkmathew
*
* The License.txt file describes the conditions under which this software may be
* distributed.
*
*/
#include <cstdlib>
#include <cassert>
#include <cctype>
#include <cstdio>
#include <string>
#include <string_view>
#include <vector>
#include <map>
#include <functional>
#include "ILexer.h"
#include "Scintilla.h"
#include "SciLexer.h"
#include "WordList.h"
#include "LexAccessor.h"
#include "StyleContext.h"
#include "CharacterSet.h"
#include "LexerModule.h"
#include "OptionSet.h"
#include "DefaultLexer.h"
using namespace Scintilla;
using namespace Lexilla;
static const char *const RegistryWordListDesc[] = {
0
};
struct OptionsRegistry {
bool foldCompact = false;
bool fold = false;
};
struct OptionSetRegistry : public OptionSet<OptionsRegistry> {
OptionSetRegistry() {
DefineProperty("fold.compact", &OptionsRegistry::foldCompact);
DefineProperty("fold", &OptionsRegistry::fold);
DefineWordListSets(RegistryWordListDesc);
}
};
class LexerRegistry : public DefaultLexer {
OptionsRegistry options;
OptionSetRegistry optSetRegistry;
static bool IsStringState(int state) {
return (state == SCE_REG_VALUENAME || state == SCE_REG_STRING);
}
static bool IsKeyPathState(int state) {
return (state == SCE_REG_ADDEDKEY || state == SCE_REG_DELETEDKEY);
}
static bool AtValueType(LexAccessor &styler, Sci_Position start) {
Sci_Position i = 0;
while (i < 10) {
i++;
char curr = styler.SafeGetCharAt(start+i, '\0');
if (curr == ':') {
return true;
} else if (!curr) {
return false;
}
}
return false;
}
static bool IsNextNonWhitespace(LexAccessor &styler, Sci_Position start, char ch) {
Sci_Position i = 0;
while (i < 100) {
i++;
char curr = styler.SafeGetCharAt(start+i, '\0');
char next = styler.SafeGetCharAt(start+i+1, '\0');
bool atEOL = (curr == '\r' && next != '\n') || (curr == '\n');
if (curr == ch) {
return true;
} else if (!isspacechar(curr) || atEOL) {
return false;
}
}
return false;
}
// Looks for the equal sign at the end of the string
static bool AtValueName(LexAccessor &styler, Sci_Position start) {
bool atEOL = false;
Sci_Position i = 0;
bool escaped = false;
while (!atEOL) {
i++;
char curr = styler.SafeGetCharAt(start+i, '\0');
char next = styler.SafeGetCharAt(start+i+1, '\0');
atEOL = (curr == '\r' && next != '\n') || (curr == '\n');
if (escaped) {
escaped = false;
continue;
}
escaped = curr == '\\';
if (curr == '"') {
return IsNextNonWhitespace(styler, start+i, '=');
} else if (!curr) {
return false;
}
}
return false;
}
static bool AtKeyPathEnd(LexAccessor &styler, Sci_Position start) {
bool atEOL = false;
Sci_Position i = 0;
while (!atEOL) {
i++;
char curr = styler.SafeGetCharAt(start+i, '\0');
char next = styler.SafeGetCharAt(start+i+1, '\0');
atEOL = (curr == '\r' && next != '\n') || (curr == '\n');
if (curr == ']' || !curr) {
// There's still at least one or more square brackets ahead
return false;
}
}
return true;
}
static bool AtGUID(LexAccessor &styler, Sci_Position start) {
int count = 8;
int portion = 0;
int offset = 1;
char digit = '\0';
while (portion < 5) {
int i = 0;
while (i < count) {
digit = styler.SafeGetCharAt(start+offset);
if (!(isxdigit(digit) || digit == '-')) {
return false;
}
offset++;
i++;
}
portion++;
count = (portion == 4) ? 13 : 5;
}
digit = styler.SafeGetCharAt(start+offset);
if (digit == '}') {
return true;
} else {
return false;
}
}
public:
LexerRegistry() : DefaultLexer("registry", SCLEX_REGISTRY) {}
virtual ~LexerRegistry() {}
int SCI_METHOD Version() const override {
return lvRelease5;
}
void SCI_METHOD Release() override {
delete this;
}
const char *SCI_METHOD PropertyNames() override {
return optSetRegistry.PropertyNames();
}
int SCI_METHOD PropertyType(const char *name) override {
return optSetRegistry.PropertyType(name);
}
const char *SCI_METHOD DescribeProperty(const char *name) override {
return optSetRegistry.DescribeProperty(name);
}
Sci_Position SCI_METHOD PropertySet(const char *key, const char *val) override {
if (optSetRegistry.PropertySet(&options, key, val)) {
return 0;
}
return -1;
}
const char * SCI_METHOD PropertyGet(const char *key) override {
return optSetRegistry.PropertyGet(key);
}
Sci_Position SCI_METHOD WordListSet(int, const char *) override {
return -1;
}
void *SCI_METHOD PrivateCall(int, void *) override {
return 0;
}
static ILexer5 *LexerFactoryRegistry() {
return new LexerRegistry;
}
const char *SCI_METHOD DescribeWordListSets() override {
return optSetRegistry.DescribeWordListSets();
}
void SCI_METHOD Lex(Sci_PositionU startPos,
Sci_Position length,
int initStyle,
IDocument *pAccess) override;
void SCI_METHOD Fold(Sci_PositionU startPos,
Sci_Position length,
int initStyle,
IDocument *pAccess) override;
};
void SCI_METHOD LexerRegistry::Lex(Sci_PositionU startPos,
Sci_Position length,
int initStyle,
IDocument *pAccess) {
int beforeGUID = SCE_REG_DEFAULT;
int beforeEscape = SCE_REG_DEFAULT;
CharacterSet setOperators = CharacterSet(CharacterSet::setNone, "-,.=:\\@()");
LexAccessor styler(pAccess);
StyleContext context(startPos, length, initStyle, styler);
bool highlight = true;
bool afterEqualSign = false;
while (context.More()) {
if (context.atLineStart) {
Sci_Position currPos = static_cast<Sci_Position>(context.currentPos);
bool continued = styler[currPos-3] == '\\';
highlight = continued ? true : false;
}
switch (context.state) {
case SCE_REG_COMMENT:
if (context.atLineEnd) {
context.SetState(SCE_REG_DEFAULT);
}
break;
case SCE_REG_VALUENAME:
case SCE_REG_STRING: {
Sci_Position currPos = static_cast<Sci_Position>(context.currentPos);
if (context.ch == '"') {
context.ForwardSetState(SCE_REG_DEFAULT);
} else if (context.ch == '\\') {
beforeEscape = context.state;
context.SetState(SCE_REG_ESCAPED);
context.Forward();
} else if (context.ch == '{') {
if (AtGUID(styler, currPos)) {
beforeGUID = context.state;
context.SetState(SCE_REG_STRING_GUID);
}
}
if (context.state == SCE_REG_STRING &&
context.ch == '%' &&
(isdigit(context.chNext) || context.chNext == '*')) {
context.SetState(SCE_REG_PARAMETER);
}
}
break;
case SCE_REG_PARAMETER:
context.ForwardSetState(SCE_REG_STRING);
if (context.ch == '"') {
context.ForwardSetState(SCE_REG_DEFAULT);
}
break;
case SCE_REG_VALUETYPE:
if (context.ch == ':') {
context.SetState(SCE_REG_DEFAULT);
afterEqualSign = false;
}
break;
case SCE_REG_HEXDIGIT:
case SCE_REG_OPERATOR:
context.SetState(SCE_REG_DEFAULT);
break;
case SCE_REG_DELETEDKEY:
case SCE_REG_ADDEDKEY: {
Sci_Position currPos = static_cast<Sci_Position>(context.currentPos);
if (context.ch == ']' && AtKeyPathEnd(styler, currPos)) {
context.ForwardSetState(SCE_REG_DEFAULT);
} else if (context.ch == '{') {
if (AtGUID(styler, currPos)) {
beforeGUID = context.state;
context.SetState(SCE_REG_KEYPATH_GUID);
}
}
}
break;
case SCE_REG_ESCAPED:
if (context.ch == '"') {
context.SetState(beforeEscape);
context.ForwardSetState(SCE_REG_DEFAULT);
} else if (context.ch == '\\') {
context.Forward();
} else {
context.SetState(beforeEscape);
beforeEscape = SCE_REG_DEFAULT;
}
break;
case SCE_REG_STRING_GUID:
case SCE_REG_KEYPATH_GUID: {
if (context.ch == '}') {
context.ForwardSetState(beforeGUID);
beforeGUID = SCE_REG_DEFAULT;
}
Sci_Position currPos = static_cast<Sci_Position>(context.currentPos);
if (context.ch == '"' && IsStringState(context.state)) {
context.ForwardSetState(SCE_REG_DEFAULT);
} else if (context.ch == ']' &&
AtKeyPathEnd(styler, currPos) &&
IsKeyPathState(context.state)) {
context.ForwardSetState(SCE_REG_DEFAULT);
} else if (context.ch == '\\' && IsStringState(context.state)) {
beforeEscape = context.state;
context.SetState(SCE_REG_ESCAPED);
context.Forward();
}
}
break;
}
// Determine if a new state should be entered.
if (context.state == SCE_REG_DEFAULT) {
Sci_Position currPos = static_cast<Sci_Position>(context.currentPos);
if (context.ch == ';') {
context.SetState(SCE_REG_COMMENT);
} else if (context.ch == '"') {
if (AtValueName(styler, currPos)) {
context.SetState(SCE_REG_VALUENAME);
} else {
context.SetState(SCE_REG_STRING);
}
} else if (context.ch == '[') {
if (IsNextNonWhitespace(styler, currPos, '-')) {
context.SetState(SCE_REG_DELETEDKEY);
} else {
context.SetState(SCE_REG_ADDEDKEY);
}
} else if (context.ch == '=') {
afterEqualSign = true;
highlight = true;
} else if (afterEqualSign) {
bool wordStart = isalpha(context.ch) && !isalpha(context.chPrev);
if (wordStart && AtValueType(styler, currPos)) {
context.SetState(SCE_REG_VALUETYPE);
}
} else if (isxdigit(context.ch) && highlight) {
context.SetState(SCE_REG_HEXDIGIT);
}
highlight = (context.ch == '@') ? true : highlight;
if (setOperators.Contains(context.ch) && highlight) {
context.SetState(SCE_REG_OPERATOR);
}
}
context.Forward();
}
context.Complete();
}
// Folding similar to that of FoldPropsDoc in LexOthers
void SCI_METHOD LexerRegistry::Fold(Sci_PositionU startPos,
Sci_Position length,
int,
IDocument *pAccess) {
if (!options.fold) {
return;
}
LexAccessor styler(pAccess);
Sci_Position currLine = styler.GetLine(startPos);
int visibleChars = 0;
Sci_PositionU endPos = startPos + length;
bool atKeyPath = false;
for (Sci_PositionU i = startPos; i < endPos; i++) {
atKeyPath = IsKeyPathState(styler.StyleAt(i)) ? true : atKeyPath;
char curr = styler.SafeGetCharAt(i);
char next = styler.SafeGetCharAt(i+1);
bool atEOL = (curr == '\r' && next != '\n') || (curr == '\n');
if (atEOL || i == (endPos-1)) {
int level = SC_FOLDLEVELBASE;
if (currLine > 0) {
int prevLevel = styler.LevelAt(currLine-1);
if (prevLevel & SC_FOLDLEVELHEADERFLAG) {
level += 1;
} else {
level = prevLevel;
}
}
if (!visibleChars && options.foldCompact) {
level |= SC_FOLDLEVELWHITEFLAG;
} else if (atKeyPath) {
level = SC_FOLDLEVELBASE | SC_FOLDLEVELHEADERFLAG;
}
if (level != styler.LevelAt(currLine)) {
styler.SetLevel(currLine, level);
}
currLine++;
visibleChars = 0;
atKeyPath = false;
}
if (!isspacechar(curr)) {
visibleChars++;
}
}
// Make the folding reach the last line in the file
int level = SC_FOLDLEVELBASE;
if (currLine > 0) {
int prevLevel = styler.LevelAt(currLine-1);
if (prevLevel & SC_FOLDLEVELHEADERFLAG) {
level += 1;
} else {
level = prevLevel;
}
}
styler.SetLevel(currLine, level);
}
LexerModule lmRegistry(SCLEX_REGISTRY,
LexerRegistry::LexerFactoryRegistry,
"registry",
RegistryWordListDesc);
|