| 1 |
// Scintilla source code edit control |
| 2 |
/** @file LexLua.cxx |
| 3 |
** Lexer for Lua language. |
| 4 |
** |
| 5 |
** Written by Paul Winwood. |
| 6 |
** Folder by Alexey Yutkin. |
| 7 |
** Modified by Marcos E. Wurzius & Philippe Lhoste |
| 8 |
**/ |
| 9 |
|
| 10 |
#include <cstdlib> |
| 11 |
#include <cassert> |
| 12 |
#include <cstring> |
| 13 |
|
| 14 |
#include <string> |
| 15 |
#include <string_view> |
| 16 |
#include <vector> |
| 17 |
#include <map> |
| 18 |
|
| 19 |
#include "ILexer.h" |
| 20 |
#include "Scintilla.h" |
| 21 |
#include "SciLexer.h" |
| 22 |
|
| 23 |
#include "WordList.h" |
| 24 |
#include "LexAccessor.h" |
| 25 |
#include "Accessor.h" |
| 26 |
#include "StyleContext.h" |
| 27 |
#include "CharacterSet.h" |
| 28 |
#include "LexerModule.h" |
| 29 |
#include "OptionSet.h" |
| 30 |
#include "SubStyles.h" |
| 31 |
#include "DefaultLexer.h" |
| 32 |
|
| 33 |
using namespace Scintilla; |
| 34 |
using namespace Lexilla; |
| 35 |
|
| 36 |
namespace { |
| 37 |
|
| 38 |
// Test for [=[ ... ]=] delimiters, returns 0 if it's only a [ or ], |
| 39 |
// return 1 for [[ or ]], returns >=2 for [=[ or ]=] and so on. |
| 40 |
// The maximum number of '=' characters allowed is 254. |
| 41 |
int LongDelimCheck(StyleContext &sc) { |
| 42 |
constexpr int maximumEqualCharacters = 254; |
| 43 |
int sep = 1; |
| 44 |
while (sc.GetRelative(sep) == '=' && sep <= maximumEqualCharacters) |
| 45 |
sep++; |
| 46 |
if (sc.GetRelative(sep) == sc.ch) |
| 47 |
return sep; |
| 48 |
return 0; |
| 49 |
} |
| 50 |
|
| 51 |
const char *const luaWordListDesc[] = { |
| 52 |
"Keywords", |
| 53 |
"Basic functions", |
| 54 |
"String, (table) & math functions", |
| 55 |
"(coroutines), I/O & system facilities", |
| 56 |
"user1", |
| 57 |
"user2", |
| 58 |
"user3", |
| 59 |
"user4", |
| 60 |
nullptr |
| 61 |
}; |
| 62 |
|
| 63 |
const char styleSubable[] = { SCE_LUA_IDENTIFIER, 0 }; |
| 64 |
|
| 65 |
const LexicalClass lexicalClasses[] = { |
| 66 |
// Lexer Lua SCLEX_LUA SCE_LUA_: |
| 67 |
0, "SCE_LUA_DEFAULT", "default", "White space: Visible only in View Whitespace mode (or if it has a back colour)", |
| 68 |
1, "SCE_LUA_COMMENT", "comment", "Block comment (Lua 5.0)", |
| 69 |
2, "SCE_LUA_COMMENTLINE", "comment line", "Line comment", |
| 70 |
3, "SCE_LUA_COMMENTDOC", "comment documentation", "Doc comment", |
| 71 |
4, "SCE_LUA_NUMBER", "literal numeric", "Number", |
| 72 |
5, "SCE_LUA_WORD", "keyword", "Keyword", |
| 73 |
6, "SCE_LUA_STRING", "literal string", "(Double quoted) String", |
| 74 |
7, "SCE_LUA_CHARACTER", "literal string character", "Character (Single quoted string)", |
| 75 |
8, "SCE_LUA_LITERALSTRING", "literal string", "Literal string", |
| 76 |
9, "SCE_LUA_PREPROCESSOR", "preprocessor", "Preprocessor (obsolete in Lua 4.0 and up)", |
| 77 |
10, "SCE_LUA_OPERATOR", "operator", "Operators", |
| 78 |
11, "SCE_LUA_IDENTIFIER", "identifier", "Identifier (everything else...)", |
| 79 |
12, "SCE_LUA_STRINGEOL", "error literal string", "End of line where string is not closed", |
| 80 |
13, "SCE_LUA_WORD2", "identifier", "Other keywords", |
| 81 |
14, "SCE_LUA_WORD3", "identifier", "Other keywords", |
| 82 |
15, "SCE_LUA_WORD4", "identifier", "Other keywords", |
| 83 |
16, "SCE_LUA_WORD5", "identifier", "Other keywords", |
| 84 |
17, "SCE_LUA_WORD6", "identifier", "Other keywords", |
| 85 |
18, "SCE_LUA_WORD7", "identifier", "Other keywords", |
| 86 |
19, "SCE_LUA_WORD8", "identifier", "Other keywords", |
| 87 |
20, "SCE_LUA_LABEL", "label", "Labels", |
| 88 |
}; |
| 89 |
|
| 90 |
// Options used for LexerLua |
| 91 |
struct OptionsLua { |
| 92 |
bool foldCompact = true; |
| 93 |
}; |
| 94 |
|
| 95 |
struct OptionSetLua : public OptionSet<OptionsLua> { |
| 96 |
OptionSetLua() { |
| 97 |
DefineProperty("fold.compact", &OptionsLua::foldCompact); |
| 98 |
|
| 99 |
DefineWordListSets(luaWordListDesc); |
| 100 |
} |
| 101 |
}; |
| 102 |
|
| 103 |
class LexerLua : public DefaultLexer { |
| 104 |
WordList keywords; |
| 105 |
WordList keywords2; |
| 106 |
WordList keywords3; |
| 107 |
WordList keywords4; |
| 108 |
WordList keywords5; |
| 109 |
WordList keywords6; |
| 110 |
WordList keywords7; |
| 111 |
WordList keywords8; |
| 112 |
OptionsLua options; |
| 113 |
OptionSetLua osLua; |
| 114 |
SubStyles subStyles{styleSubable}; |
| 115 |
public: |
| 116 |
explicit LexerLua() : |
| 117 |
DefaultLexer("lua", SCLEX_LUA, lexicalClasses, std::size(lexicalClasses)) { |
| 118 |
} |
| 119 |
LexerLua(const LexerLua &) = delete; |
| 120 |
LexerLua(LexerLua &&) = delete; |
| 121 |
LexerLua &operator=(const LexerLua &) = delete; |
| 122 |
LexerLua &operator=(LexerLua &&) = delete; |
| 123 |
~LexerLua() override = default; |
| 124 |
void SCI_METHOD Release() noexcept override { |
| 125 |
delete this; |
| 126 |
} |
| 127 |
[[nodiscard]] int SCI_METHOD Version() const noexcept override { |
| 128 |
return lvRelease5; |
| 129 |
} |
| 130 |
const char *SCI_METHOD PropertyNames() noexcept override { |
| 131 |
return osLua.PropertyNames(); |
| 132 |
} |
| 133 |
int SCI_METHOD PropertyType(const char *name) override { |
| 134 |
return osLua.PropertyType(name); |
| 135 |
} |
| 136 |
const char *SCI_METHOD DescribeProperty(const char *name) override { |
| 137 |
return osLua.DescribeProperty(name); |
| 138 |
} |
| 139 |
Sci_Position SCI_METHOD PropertySet(const char *key, const char *val) override; |
| 140 |
const char *SCI_METHOD PropertyGet(const char *key) override { |
| 141 |
return osLua.PropertyGet(key); |
| 142 |
} |
| 143 |
const char *SCI_METHOD DescribeWordListSets() noexcept override { |
| 144 |
return osLua.DescribeWordListSets(); |
| 145 |
} |
| 146 |
Sci_Position SCI_METHOD WordListSet(int n, const char *wl) override; |
| 147 |
void SCI_METHOD Lex(Sci_PositionU startPos, Sci_Position length, int initStyle, IDocument *pAccess) override; |
| 148 |
void SCI_METHOD Fold(Sci_PositionU startPos, Sci_Position length, int initStyle, IDocument *pAccess) override; |
| 149 |
|
| 150 |
int SCI_METHOD AllocateSubStyles(int styleBase, int numberStyles) override { |
| 151 |
return subStyles.Allocate(styleBase, numberStyles); |
| 152 |
} |
| 153 |
int SCI_METHOD SubStylesStart(int styleBase) override { |
| 154 |
return subStyles.Start(styleBase); |
| 155 |
} |
| 156 |
int SCI_METHOD SubStylesLength(int styleBase) override { |
| 157 |
return subStyles.Length(styleBase); |
| 158 |
} |
| 159 |
int SCI_METHOD StyleFromSubStyle(int subStyle) override { |
| 160 |
const int styleBase = subStyles.BaseStyle(subStyle); |
| 161 |
return styleBase; |
| 162 |
} |
| 163 |
int SCI_METHOD PrimaryStyleFromStyle(int style) override { |
| 164 |
return style; |
| 165 |
} |
| 166 |
void SCI_METHOD FreeSubStyles() override { |
| 167 |
subStyles.Free(); |
| 168 |
} |
| 169 |
void SCI_METHOD SetIdentifiers(int style, const char *identifiers) override { |
| 170 |
subStyles.SetIdentifiers(style, identifiers); |
| 171 |
} |
| 172 |
int SCI_METHOD DistanceToSecondaryStyles() override { |
| 173 |
return 0; |
| 174 |
} |
| 175 |
const char *SCI_METHOD GetSubStyleBases() override { |
| 176 |
return styleSubable; |
| 177 |
} |
| 178 |
|
| 179 |
static ILexer5 *LexerFactoryLua() { |
| 180 |
return new LexerLua(); |
| 181 |
} |
| 182 |
}; |
| 183 |
|
| 184 |
Sci_Position SCI_METHOD LexerLua::PropertySet(const char *key, const char *val) { |
| 185 |
if (osLua.PropertySet(&options, key, val)) { |
| 186 |
return 0; |
| 187 |
} |
| 188 |
return -1; |
| 189 |
} |
| 190 |
|
| 191 |
Sci_Position SCI_METHOD LexerLua::WordListSet(int n, const char *wl) { |
| 192 |
WordList *wordListN = nullptr; |
| 193 |
switch (n) { |
| 194 |
case 0: |
| 195 |
wordListN = &keywords; |
| 196 |
break; |
| 197 |
case 1: |
| 198 |
wordListN = &keywords2; |
| 199 |
break; |
| 200 |
case 2: |
| 201 |
wordListN = &keywords3; |
| 202 |
break; |
| 203 |
case 3: |
| 204 |
wordListN = &keywords4; |
| 205 |
break; |
| 206 |
case 4: |
| 207 |
wordListN = &keywords5; |
| 208 |
break; |
| 209 |
case 5: |
| 210 |
wordListN = &keywords6; |
| 211 |
break; |
| 212 |
case 6: |
| 213 |
wordListN = &keywords7; |
| 214 |
break; |
| 215 |
case 7: |
| 216 |
wordListN = &keywords8; |
| 217 |
break; |
| 218 |
default: |
| 219 |
break; |
| 220 |
} |
| 221 |
Sci_Position firstModification = -1; |
| 222 |
if (wordListN) { |
| 223 |
if (wordListN->Set(wl)) { |
| 224 |
firstModification = 0; |
| 225 |
} |
| 226 |
} |
| 227 |
return firstModification; |
| 228 |
} |
| 229 |
|
| 230 |
constexpr int maskSeparator = 0xFF; |
| 231 |
constexpr int maskStringWs = 0x100; |
| 232 |
constexpr int maskDocComment = 0x200; |
| 233 |
|
| 234 |
void LexerLua::Lex(Sci_PositionU startPos, Sci_Position length, int initStyle, IDocument *pAccess) { |
| 235 |
LexAccessor styler(pAccess); |
| 236 |
|
| 237 |
// Accepts accented characters |
| 238 |
const CharacterSet setWordStart(CharacterSet::setAlpha, "_", true); |
| 239 |
const CharacterSet setWord(CharacterSet::setAlphaNum, "_", true); |
| 240 |
// Not exactly following number definition (several dots are seen as OK, etc.) |
| 241 |
// but probably enough in most cases. [pP] is for hex floats. |
| 242 |
const CharacterSet setNumber(CharacterSet::setDigits, ".-+abcdefpABCDEFP"); |
| 243 |
const CharacterSet setExponent("eEpP"); |
| 244 |
const CharacterSet setLuaOperator("*/-+()={}~[];<>,.^%:#&|"); |
| 245 |
const CharacterSet setEscapeSkip("\"'\\"); |
| 246 |
|
| 247 |
const WordClassifier &classifierIdentifiers = subStyles.Classifier(SCE_LUA_IDENTIFIER); |
| 248 |
|
| 249 |
Sci_Position currentLine = styler.GetLine(startPos); |
| 250 |
// Initialize long string [[ ... ]] or block comment --[[ ... ]], |
| 251 |
// if we are inside such a string. Block comment was introduced in Lua 5.0, |
| 252 |
// blocks with separators [=[ ... ]=] in Lua 5.1. |
| 253 |
// Continuation of a string (\z whitespace escaping) is controlled by stringWs. |
| 254 |
int sepCount = 0; |
| 255 |
int stringWs = 0; |
| 256 |
int lastLineDocComment = 0; |
| 257 |
if ((currentLine > 0) && |
| 258 |
AnyOf(initStyle, SCE_LUA_DEFAULT, SCE_LUA_LITERALSTRING, SCE_LUA_COMMENT, SCE_LUA_COMMENTDOC, SCE_LUA_STRING, SCE_LUA_CHARACTER)) { |
| 259 |
const int lineState = styler.GetLineState(currentLine - 1); |
| 260 |
sepCount = lineState & maskSeparator; |
| 261 |
stringWs = lineState & maskStringWs; |
| 262 |
lastLineDocComment = lineState & maskDocComment; |
| 263 |
} |
| 264 |
|
| 265 |
// results of identifier/keyword matching |
| 266 |
Sci_Position idenPos = 0; |
| 267 |
Sci_Position idenStartCharWidth = 0; |
| 268 |
Sci_Position idenWordPos = 0; |
| 269 |
int idenStyle = SCE_LUA_IDENTIFIER; |
| 270 |
bool foundGoto = false; |
| 271 |
|
| 272 |
// Do not leak onto next line |
| 273 |
if (AnyOf(initStyle, SCE_LUA_STRINGEOL, SCE_LUA_COMMENTLINE, SCE_LUA_COMMENTDOC, SCE_LUA_PREPROCESSOR)) { |
| 274 |
initStyle = SCE_LUA_DEFAULT; |
| 275 |
} |
| 276 |
|
| 277 |
StyleContext sc(startPos, length, initStyle, styler); |
| 278 |
if (startPos == 0 && sc.ch == '#' && sc.chNext == '!') { |
| 279 |
// shbang line: "#!" is a comment only if located at the start of the script |
| 280 |
sc.SetState(SCE_LUA_COMMENTLINE); |
| 281 |
} |
| 282 |
for (; sc.More(); sc.Forward()) { |
| 283 |
if (sc.atLineEnd) { |
| 284 |
// Update the line state, so it can be seen by next line |
| 285 |
currentLine = styler.GetLine(sc.currentPos); |
| 286 |
switch (sc.state) { |
| 287 |
case SCE_LUA_DEFAULT: |
| 288 |
case SCE_LUA_LITERALSTRING: |
| 289 |
case SCE_LUA_COMMENT: |
| 290 |
case SCE_LUA_COMMENTDOC: |
| 291 |
case SCE_LUA_STRING: |
| 292 |
case SCE_LUA_CHARACTER: |
| 293 |
// Inside a literal string, block comment or string, we set the line state |
| 294 |
styler.SetLineState(currentLine, lastLineDocComment | stringWs | sepCount); |
| 295 |
break; |
| 296 |
default: |
| 297 |
// Reset the line state |
| 298 |
styler.SetLineState(currentLine, 0); |
| 299 |
break; |
| 300 |
} |
| 301 |
} |
| 302 |
if (sc.atLineStart && (sc.state == SCE_LUA_STRING)) { |
| 303 |
// Prevent SCE_LUA_STRINGEOL from leaking back to previous line |
| 304 |
sc.SetState(SCE_LUA_STRING); |
| 305 |
} |
| 306 |
|
| 307 |
// Handle string line continuation |
| 308 |
if ((sc.state == SCE_LUA_STRING || sc.state == SCE_LUA_CHARACTER) && |
| 309 |
sc.ch == '\\') { |
| 310 |
if (sc.chNext == '\n' || sc.chNext == '\r') { |
| 311 |
sc.Forward(); |
| 312 |
if (sc.ch == '\r' && sc.chNext == '\n') { |
| 313 |
sc.Forward(); |
| 314 |
} |
| 315 |
continue; |
| 316 |
} |
| 317 |
} |
| 318 |
|
| 319 |
// Determine if the current state should terminate. |
| 320 |
if (sc.state == SCE_LUA_OPERATOR) { |
| 321 |
if (sc.ch == ':' && sc.chPrev == ':') { // :: <label> :: forward scan |
| 322 |
sc.Forward(); |
| 323 |
Sci_Position ln = 0; |
| 324 |
while (IsASpaceOrTab(sc.GetRelativeChar(ln))) // skip over spaces/tabs |
| 325 |
ln++; |
| 326 |
const Sci_Position ws1 = ln; |
| 327 |
if (setWordStart.Contains(sc.GetRelativeChar(ln))) { |
| 328 |
char cLabel = 0; |
| 329 |
std::string s; |
| 330 |
while (setWord.Contains(cLabel = sc.GetRelativeChar(ln))) { // get potential label |
| 331 |
s.push_back(cLabel); |
| 332 |
ln++; |
| 333 |
} |
| 334 |
const Sci_Position lbl = ln; |
| 335 |
if (!keywords.InList(s)) { |
| 336 |
while (IsASpaceOrTab(sc.GetRelativeChar(ln))) // skip over spaces/tabs |
| 337 |
ln++; |
| 338 |
const Sci_Position ws2 = ln - lbl; |
| 339 |
if (sc.GetRelativeChar(ln) == ':' && sc.GetRelativeChar(ln + 1) == ':') { |
| 340 |
// final :: found, complete valid label construct |
| 341 |
sc.ChangeState(SCE_LUA_LABEL); |
| 342 |
if (ws1) { |
| 343 |
sc.SetState(SCE_LUA_DEFAULT); |
| 344 |
sc.ForwardBytes(ws1); |
| 345 |
} |
| 346 |
sc.SetState(SCE_LUA_LABEL); |
| 347 |
sc.ForwardBytes(lbl - ws1); |
| 348 |
if (ws2) { |
| 349 |
sc.SetState(SCE_LUA_DEFAULT); |
| 350 |
sc.ForwardBytes(ws2); |
| 351 |
} |
| 352 |
sc.SetState(SCE_LUA_LABEL); |
| 353 |
sc.ForwardBytes(2); |
| 354 |
} |
| 355 |
} |
| 356 |
} |
| 357 |
} |
| 358 |
sc.SetState(SCE_LUA_DEFAULT); |
| 359 |
} else if (sc.state == SCE_LUA_NUMBER) { |
| 360 |
// We stop the number definition on non-numerical non-dot non-eEpP non-sign non-hexdigit char |
| 361 |
if (!setNumber.Contains(sc.ch)) { |
| 362 |
sc.SetState(SCE_LUA_DEFAULT); |
| 363 |
} else if (sc.ch == '-' || sc.ch == '+') { |
| 364 |
if (!setExponent.Contains(sc.chPrev)) |
| 365 |
sc.SetState(SCE_LUA_DEFAULT); |
| 366 |
} |
| 367 |
} else if (sc.state == SCE_LUA_IDENTIFIER) { |
| 368 |
idenPos -= idenStartCharWidth; // commit already-scanned identifier/word parts |
| 369 |
if (idenWordPos > 0) { |
| 370 |
idenWordPos--; |
| 371 |
sc.ChangeState(idenStyle); |
| 372 |
sc.ForwardBytes(idenWordPos); |
| 373 |
idenPos -= idenWordPos; |
| 374 |
if (idenPos > 0) { |
| 375 |
sc.SetState(SCE_LUA_IDENTIFIER); |
| 376 |
sc.ForwardBytes(idenPos); |
| 377 |
} |
| 378 |
} else { |
| 379 |
sc.ForwardBytes(idenPos); |
| 380 |
} |
| 381 |
sc.SetState(SCE_LUA_DEFAULT); |
| 382 |
if (foundGoto) { // goto <label> forward scan |
| 383 |
while (IsASpaceOrTab(sc.ch) && !sc.atLineEnd) |
| 384 |
sc.Forward(); |
| 385 |
if (setWordStart.Contains(sc.ch)) { |
| 386 |
sc.SetState(SCE_LUA_LABEL); |
| 387 |
sc.Forward(); |
| 388 |
while (setWord.Contains(sc.ch)) |
| 389 |
sc.Forward(); |
| 390 |
std::string s; |
| 391 |
sc.GetCurrentString(s, StyleContext::Transform::none); |
| 392 |
if (keywords.InList(s)) // labels cannot be keywords |
| 393 |
sc.ChangeState(SCE_LUA_WORD); |
| 394 |
} |
| 395 |
sc.SetState(SCE_LUA_DEFAULT); |
| 396 |
} |
| 397 |
} else if (AnyOf(sc.state, SCE_LUA_COMMENTLINE, SCE_LUA_COMMENTDOC, SCE_LUA_PREPROCESSOR)) { |
| 398 |
if (sc.atLineEnd) { |
| 399 |
sc.ForwardSetState(SCE_LUA_DEFAULT); |
| 400 |
} |
| 401 |
} else if (sc.state == SCE_LUA_STRING) { |
| 402 |
if (stringWs) { |
| 403 |
if (!IsASpace(sc.ch)) |
| 404 |
stringWs = 0; |
| 405 |
} |
| 406 |
if (sc.ch == '\\') { |
| 407 |
if (setEscapeSkip.Contains(sc.chNext)) { |
| 408 |
sc.Forward(); |
| 409 |
} else if (sc.chNext == 'z') { |
| 410 |
sc.Forward(); |
| 411 |
stringWs = maskStringWs; |
| 412 |
} |
| 413 |
} else if (sc.ch == '\"') { |
| 414 |
sc.ForwardSetState(SCE_LUA_DEFAULT); |
| 415 |
} else if (stringWs == 0 && sc.atLineEnd) { |
| 416 |
sc.ChangeState(SCE_LUA_STRINGEOL); |
| 417 |
sc.ForwardSetState(SCE_LUA_DEFAULT); |
| 418 |
} |
| 419 |
} else if (sc.state == SCE_LUA_CHARACTER) { |
| 420 |
if (stringWs) { |
| 421 |
if (!IsASpace(sc.ch)) |
| 422 |
stringWs = 0; |
| 423 |
} |
| 424 |
if (sc.ch == '\\') { |
| 425 |
if (setEscapeSkip.Contains(sc.chNext)) { |
| 426 |
sc.Forward(); |
| 427 |
} else if (sc.chNext == 'z') { |
| 428 |
sc.Forward(); |
| 429 |
stringWs = maskStringWs; |
| 430 |
} |
| 431 |
} else if (sc.ch == '\'') { |
| 432 |
sc.ForwardSetState(SCE_LUA_DEFAULT); |
| 433 |
} else if (stringWs == 0 && sc.atLineEnd) { |
| 434 |
sc.ChangeState(SCE_LUA_STRINGEOL); |
| 435 |
sc.ForwardSetState(SCE_LUA_DEFAULT); |
| 436 |
} |
| 437 |
} else if (sc.ch == ']' && (sc.state == SCE_LUA_LITERALSTRING || sc.state == SCE_LUA_COMMENT)) { |
| 438 |
const int sep = LongDelimCheck(sc); |
| 439 |
if (sep == sepCount) { // ]=]-style delim |
| 440 |
sc.Forward(sep); |
| 441 |
sc.ForwardSetState(SCE_LUA_DEFAULT); |
| 442 |
} |
| 443 |
} |
| 444 |
|
| 445 |
// Determine if a new state should be entered. |
| 446 |
if (sc.state == SCE_LUA_DEFAULT) { |
| 447 |
if (IsADigit(sc.ch) || (sc.ch == '.' && IsADigit(sc.chNext))) { |
| 448 |
sc.SetState(SCE_LUA_NUMBER); |
| 449 |
if (sc.ch == '0' && AnyOf(sc.chNext, 'x', 'X')) { |
| 450 |
sc.Forward(); |
| 451 |
} |
| 452 |
} else if (setWordStart.Contains(sc.ch)) { |
| 453 |
// For matching various identifiers with dots and colons, multiple |
| 454 |
// matches are done as identifier segments are added. Longest match is |
| 455 |
// set to a word style. The non-matched part is in identifier style. |
| 456 |
std::string ident; |
| 457 |
idenPos = 0; |
| 458 |
idenStartCharWidth = sc.width; |
| 459 |
idenWordPos = 0; |
| 460 |
idenStyle = SCE_LUA_IDENTIFIER; |
| 461 |
foundGoto = false; |
| 462 |
char cNext = 0; |
| 463 |
do { |
| 464 |
char cIdent = 0; |
| 465 |
const Sci_Position idenPosOld = idenPos; |
| 466 |
std::string identSeg; |
| 467 |
identSeg += sc.GetRelativeChar(idenPos++); |
| 468 |
while (setWord.Contains(cIdent = sc.GetRelativeChar(idenPos))) { |
| 469 |
identSeg += cIdent; |
| 470 |
idenPos++; |
| 471 |
} |
| 472 |
if (keywords.InList(identSeg) && (idenPosOld > 0)) { |
| 473 |
idenPos = idenPosOld - 1; // keywords cannot mix |
| 474 |
ident.pop_back(); |
| 475 |
break; |
| 476 |
} |
| 477 |
ident += identSeg; |
| 478 |
int newStyle = SCE_LUA_IDENTIFIER; |
| 479 |
if (keywords.InList(ident)) { |
| 480 |
newStyle = SCE_LUA_WORD; |
| 481 |
} else if (keywords2.InList(ident)) { |
| 482 |
newStyle = SCE_LUA_WORD2; |
| 483 |
} else if (keywords3.InList(ident)) { |
| 484 |
newStyle = SCE_LUA_WORD3; |
| 485 |
} else if (keywords4.InList(ident)) { |
| 486 |
newStyle = SCE_LUA_WORD4; |
| 487 |
} else if (keywords5.InList(ident)) { |
| 488 |
newStyle = SCE_LUA_WORD5; |
| 489 |
} else if (keywords6.InList(ident)) { |
| 490 |
newStyle = SCE_LUA_WORD6; |
| 491 |
} else if (keywords7.InList(ident)) { |
| 492 |
newStyle = SCE_LUA_WORD7; |
| 493 |
} else if (keywords8.InList(ident)) { |
| 494 |
newStyle = SCE_LUA_WORD8; |
| 495 |
} else { |
| 496 |
const int subStyle = classifierIdentifiers.ValueFor(ident); |
| 497 |
if (subStyle >= 0) { |
| 498 |
newStyle = subStyle; |
| 499 |
} |
| 500 |
} |
| 501 |
if (newStyle != SCE_LUA_IDENTIFIER) { |
| 502 |
idenStyle = newStyle; |
| 503 |
idenWordPos = idenPos; |
| 504 |
} |
| 505 |
if (idenStyle == SCE_LUA_WORD) // keywords cannot mix |
| 506 |
break; |
| 507 |
cNext = sc.GetRelativeChar(idenPos + 1); |
| 508 |
if ((cIdent == '.' || cIdent == ':') && setWordStart.Contains(cNext)) { |
| 509 |
ident += cIdent; |
| 510 |
idenPos++; |
| 511 |
} else { |
| 512 |
cNext = 0; |
| 513 |
} |
| 514 |
} while (cNext); |
| 515 |
if ((idenStyle == SCE_LUA_WORD) && (ident == "goto")) { |
| 516 |
foundGoto = true; |
| 517 |
} |
| 518 |
sc.SetState(SCE_LUA_IDENTIFIER); |
| 519 |
} else if (sc.ch == '\"') { |
| 520 |
sc.SetState(SCE_LUA_STRING); |
| 521 |
stringWs = 0; |
| 522 |
} else if (sc.ch == '\'') { |
| 523 |
sc.SetState(SCE_LUA_CHARACTER); |
| 524 |
stringWs = 0; |
| 525 |
} else if (sc.ch == '[') { |
| 526 |
sepCount = LongDelimCheck(sc); |
| 527 |
if (sepCount == 0) { |
| 528 |
sc.SetState(SCE_LUA_OPERATOR); |
| 529 |
} else { |
| 530 |
sc.SetState(SCE_LUA_LITERALSTRING); |
| 531 |
sc.Forward(sepCount); |
| 532 |
} |
| 533 |
} else if (sc.Match('-', '-')) { |
| 534 |
sc.SetState(lastLineDocComment ? SCE_LUA_COMMENTDOC : SCE_LUA_COMMENTLINE); |
| 535 |
if (sc.Match("--[")) { |
| 536 |
sc.Forward(2); |
| 537 |
sepCount = LongDelimCheck(sc); |
| 538 |
if (sepCount > 0) { |
| 539 |
sc.ChangeState(SCE_LUA_COMMENT); |
| 540 |
sc.Forward(sepCount); |
| 541 |
} |
| 542 |
} else if (sc.Match("---")) { |
| 543 |
sc.SetState(SCE_LUA_COMMENTDOC); |
| 544 |
lastLineDocComment = maskDocComment; |
| 545 |
} else { |
| 546 |
sc.Forward(); |
| 547 |
} |
| 548 |
} else if (sc.atLineStart && sc.Match('$')) { |
| 549 |
sc.SetState(SCE_LUA_PREPROCESSOR); // Obsolete since Lua 4.0, but still in old code |
| 550 |
} else if (setLuaOperator.Contains(sc.ch)) { |
| 551 |
sc.SetState(SCE_LUA_OPERATOR); |
| 552 |
} |
| 553 |
if (!AnyOf(sc.state, SCE_LUA_DEFAULT, SCE_LUA_COMMENTDOC)) { |
| 554 |
lastLineDocComment = 0; |
| 555 |
} |
| 556 |
} |
| 557 |
} |
| 558 |
|
| 559 |
sc.Complete(); |
| 560 |
} |
| 561 |
|
| 562 |
void LexerLua::Fold(Sci_PositionU startPos_, Sci_Position length, int initStyle, IDocument *pAccess) { |
| 563 |
LexAccessor styler(pAccess); |
| 564 |
const Sci_Position startPos = startPos_; |
| 565 |
const Sci_Position lengthDoc = startPos + length; |
| 566 |
int visibleChars = 0; |
| 567 |
Sci_Position lineCurrent = styler.GetLine(startPos); |
| 568 |
int levelPrev = styler.LevelAt(lineCurrent) & SC_FOLDLEVELNUMBERMASK; |
| 569 |
int levelCurrent = levelPrev; |
| 570 |
char chNext = styler[startPos]; |
| 571 |
const bool foldCompact = options.foldCompact; |
| 572 |
int style = initStyle; |
| 573 |
int styleNext = styler.StyleIndexAt(startPos); |
| 574 |
|
| 575 |
for (Sci_Position i = startPos; i < lengthDoc; i++) { |
| 576 |
const char ch = chNext; |
| 577 |
chNext = styler.SafeGetCharAt(i + 1); |
| 578 |
const int stylePrev = style; |
| 579 |
style = styleNext; |
| 580 |
if ((i + 1) < lengthDoc) { |
| 581 |
// Only read styles that have been set, otherwise treat style as continuing |
| 582 |
styleNext = styler.StyleIndexAt(i + 1); |
| 583 |
} |
| 584 |
const bool atEOL = (ch == '\r' && chNext != '\n') || (ch == '\n'); |
| 585 |
if (style == SCE_LUA_WORD) { |
| 586 |
// Fixed list of folding words: if, do, function, repeat, end, until |
| 587 |
// Must fix up next line with initial characters if any new words added. |
| 588 |
if ((style != stylePrev) && AnyOf(ch, 'i', 'd', 'f', 'e', 'r', 'u')) { |
| 589 |
constexpr Sci_Position maxFoldWord = 9; // "function"sv.length() + 1 |
| 590 |
std::string s; |
| 591 |
for (Sci_Position j = 0; j < maxFoldWord; j++) { |
| 592 |
if (!iswordchar(styler[i + j])) { |
| 593 |
break; |
| 594 |
} |
| 595 |
s.push_back(styler[i + j]); |
| 596 |
} |
| 597 |
|
| 598 |
if (s == "if" || s == "do" || s == "function" || s == "repeat") { |
| 599 |
levelCurrent++; |
| 600 |
} |
| 601 |
if (s == "end" || s == "until") { |
| 602 |
levelCurrent--; |
| 603 |
} |
| 604 |
} |
| 605 |
} else if (style == SCE_LUA_OPERATOR) { |
| 606 |
if (ch == '{' || ch == '(') { |
| 607 |
levelCurrent++; |
| 608 |
} else if (ch == '}' || ch == ')') { |
| 609 |
levelCurrent--; |
| 610 |
} |
| 611 |
} else if (style == SCE_LUA_LITERALSTRING || style == SCE_LUA_COMMENT) { |
| 612 |
if (stylePrev != style) { |
| 613 |
levelCurrent++; |
| 614 |
} else if (styleNext != style) { |
| 615 |
levelCurrent--; |
| 616 |
} |
| 617 |
} |
| 618 |
|
| 619 |
if (atEOL) { |
| 620 |
int lev = levelPrev; |
| 621 |
if (visibleChars == 0 && foldCompact) { |
| 622 |
lev |= SC_FOLDLEVELWHITEFLAG; |
| 623 |
} |
| 624 |
if ((levelCurrent > levelPrev) && (visibleChars > 0)) { |
| 625 |
lev |= SC_FOLDLEVELHEADERFLAG; |
| 626 |
} |
| 627 |
if (lev != styler.LevelAt(lineCurrent)) { |
| 628 |
styler.SetLevel(lineCurrent, lev); |
| 629 |
} |
| 630 |
lineCurrent++; |
| 631 |
levelPrev = levelCurrent; |
| 632 |
visibleChars = 0; |
| 633 |
} |
| 634 |
if (!isspacechar(ch)) { |
| 635 |
visibleChars++; |
| 636 |
} |
| 637 |
} |
| 638 |
// Fill in the real level of the next line, keeping the current flags as they will be filled in later |
| 639 |
|
| 640 |
const int flagsNext = styler.LevelAt(lineCurrent) & ~SC_FOLDLEVELNUMBERMASK; |
| 641 |
styler.SetLevel(lineCurrent, levelPrev | flagsNext); |
| 642 |
} |
| 643 |
|
| 644 |
} |
| 645 |
|
| 646 |
extern const LexerModule lmLua(SCLEX_LUA, LexerLua::LexerFactoryLua, "lua", luaWordListDesc); |