2009-04-24 23:35:41 +00:00
|
|
|
// Scintilla source code edit control
|
|
|
|
/** @file LexLua.cxx
|
|
|
|
** Lexer for Lua language.
|
|
|
|
**
|
|
|
|
** Written by Paul Winwood.
|
|
|
|
** Folder by Alexey Yutkin.
|
|
|
|
** Modified by Marcos E. Wurzius & Philippe Lhoste
|
|
|
|
**/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
2010-08-21 23:59:56 +00:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <ctype.h>
|
2009-04-24 23:35:41 +00:00
|
|
|
|
2019-05-04 18:14:48 +00:00
|
|
|
#include <string>
|
|
|
|
|
2010-08-21 23:59:56 +00:00
|
|
|
#include "ILexer.h"
|
|
|
|
#include "Scintilla.h"
|
|
|
|
#include "SciLexer.h"
|
2009-04-24 23:35:41 +00:00
|
|
|
|
2019-05-04 18:14:48 +00:00
|
|
|
#include "StringCopy.h"
|
2010-08-21 23:59:56 +00:00
|
|
|
#include "WordList.h"
|
|
|
|
#include "LexAccessor.h"
|
2009-04-24 23:35:41 +00:00
|
|
|
#include "Accessor.h"
|
|
|
|
#include "StyleContext.h"
|
2009-04-25 23:38:15 +00:00
|
|
|
#include "CharacterSet.h"
|
2010-08-21 23:59:56 +00:00
|
|
|
#include "LexerModule.h"
|
2009-04-24 23:35:41 +00:00
|
|
|
|
|
|
|
using namespace Scintilla;
|
|
|
|
|
|
|
|
// Test for [=[ ... ]=] delimiters, returns 0 if it's only a [ or ],
|
|
|
|
// return 1 for [[ or ]], returns >=2 for [=[ or ]=] and so on.
|
|
|
|
// The maximum number of '=' characters allowed is 254.
|
|
|
|
static int LongDelimCheck(StyleContext &sc) {
|
|
|
|
int sep = 1;
|
|
|
|
while (sc.GetRelative(sep) == '=' && sep < 0xFF)
|
|
|
|
sep++;
|
|
|
|
if (sc.GetRelative(sep) == sc.ch)
|
|
|
|
return sep;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ColouriseLuaDoc(
|
2019-05-04 18:14:48 +00:00
|
|
|
Sci_PositionU startPos,
|
|
|
|
Sci_Position length,
|
2009-04-24 23:35:41 +00:00
|
|
|
int initStyle,
|
|
|
|
WordList *keywordlists[],
|
|
|
|
Accessor &styler) {
|
|
|
|
|
2019-05-04 18:14:48 +00:00
|
|
|
const WordList &keywords = *keywordlists[0];
|
|
|
|
const WordList &keywords2 = *keywordlists[1];
|
|
|
|
const WordList &keywords3 = *keywordlists[2];
|
|
|
|
const WordList &keywords4 = *keywordlists[3];
|
|
|
|
const WordList &keywords5 = *keywordlists[4];
|
|
|
|
const WordList &keywords6 = *keywordlists[5];
|
|
|
|
const WordList &keywords7 = *keywordlists[6];
|
|
|
|
const WordList &keywords8 = *keywordlists[7];
|
2009-04-24 23:35:41 +00:00
|
|
|
|
2009-04-25 23:38:15 +00:00
|
|
|
// Accepts accented characters
|
|
|
|
CharacterSet setWordStart(CharacterSet::setAlpha, "_", 0x80, true);
|
2013-08-28 00:44:27 +00:00
|
|
|
CharacterSet setWord(CharacterSet::setAlphaNum, "_", 0x80, true);
|
2009-04-25 23:38:15 +00:00
|
|
|
// Not exactly following number definition (several dots are seen as OK, etc.)
|
2011-07-17 22:30:49 +00:00
|
|
|
// but probably enough in most cases. [pP] is for hex floats.
|
|
|
|
CharacterSet setNumber(CharacterSet::setDigits, ".-+abcdefpABCDEFP");
|
|
|
|
CharacterSet setExponent(CharacterSet::setNone, "eEpP");
|
2019-05-04 18:14:48 +00:00
|
|
|
CharacterSet setLuaOperator(CharacterSet::setNone, "*/-+()={}~[];<>,.^%:#&|");
|
2009-04-25 23:38:15 +00:00
|
|
|
CharacterSet setEscapeSkip(CharacterSet::setNone, "\"'\\");
|
|
|
|
|
2019-05-04 18:14:48 +00:00
|
|
|
Sci_Position currentLine = styler.GetLine(startPos);
|
2009-04-24 23:35:41 +00:00
|
|
|
// Initialize long string [[ ... ]] or block comment --[[ ... ]] nesting level,
|
|
|
|
// if we are inside such a string. Block comment was introduced in Lua 5.0,
|
|
|
|
// blocks with separators [=[ ... ]=] in Lua 5.1.
|
2013-08-28 00:44:27 +00:00
|
|
|
// Continuation of a string (\z whitespace escaping) is controlled by stringWs.
|
2009-04-24 23:35:41 +00:00
|
|
|
int nestLevel = 0;
|
|
|
|
int sepCount = 0;
|
2011-07-17 22:30:49 +00:00
|
|
|
int stringWs = 0;
|
|
|
|
if (initStyle == SCE_LUA_LITERALSTRING || initStyle == SCE_LUA_COMMENT ||
|
|
|
|
initStyle == SCE_LUA_STRING || initStyle == SCE_LUA_CHARACTER) {
|
2019-05-04 18:14:48 +00:00
|
|
|
const int lineState = styler.GetLineState(currentLine - 1);
|
2011-07-17 22:30:49 +00:00
|
|
|
nestLevel = lineState >> 9;
|
2009-04-24 23:35:41 +00:00
|
|
|
sepCount = lineState & 0xFF;
|
2011-07-17 22:30:49 +00:00
|
|
|
stringWs = lineState & 0x100;
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
|
2019-05-04 18:14:48 +00:00
|
|
|
// results of identifier/keyword matching
|
|
|
|
Sci_Position idenPos = 0;
|
|
|
|
Sci_Position idenWordPos = 0;
|
|
|
|
int idenStyle = SCE_LUA_IDENTIFIER;
|
|
|
|
bool foundGoto = false;
|
|
|
|
|
2009-04-24 23:35:41 +00:00
|
|
|
// Do not leak onto next line
|
|
|
|
if (initStyle == SCE_LUA_STRINGEOL || initStyle == SCE_LUA_COMMENTLINE || initStyle == SCE_LUA_PREPROCESSOR) {
|
|
|
|
initStyle = SCE_LUA_DEFAULT;
|
|
|
|
}
|
|
|
|
|
|
|
|
StyleContext sc(startPos, length, initStyle, styler);
|
2019-05-04 18:14:48 +00:00
|
|
|
if (startPos == 0 && sc.ch == '#' && sc.chNext == '!') {
|
|
|
|
// shbang line: "#!" is a comment only if located at the start of the script
|
2009-04-24 23:35:41 +00:00
|
|
|
sc.SetState(SCE_LUA_COMMENTLINE);
|
|
|
|
}
|
|
|
|
for (; sc.More(); sc.Forward()) {
|
|
|
|
if (sc.atLineEnd) {
|
|
|
|
// Update the line state, so it can be seen by next line
|
|
|
|
currentLine = styler.GetLine(sc.currentPos);
|
|
|
|
switch (sc.state) {
|
|
|
|
case SCE_LUA_LITERALSTRING:
|
|
|
|
case SCE_LUA_COMMENT:
|
2011-07-17 22:30:49 +00:00
|
|
|
case SCE_LUA_STRING:
|
|
|
|
case SCE_LUA_CHARACTER:
|
|
|
|
// Inside a literal string, block comment or string, we set the line state
|
|
|
|
styler.SetLineState(currentLine, (nestLevel << 9) | stringWs | sepCount);
|
2009-04-24 23:35:41 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// Reset the line state
|
|
|
|
styler.SetLineState(currentLine, 0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (sc.atLineStart && (sc.state == SCE_LUA_STRING)) {
|
|
|
|
// Prevent SCE_LUA_STRINGEOL from leaking back to previous line
|
|
|
|
sc.SetState(SCE_LUA_STRING);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle string line continuation
|
|
|
|
if ((sc.state == SCE_LUA_STRING || sc.state == SCE_LUA_CHARACTER) &&
|
2009-04-25 23:38:15 +00:00
|
|
|
sc.ch == '\\') {
|
2009-04-24 23:35:41 +00:00
|
|
|
if (sc.chNext == '\n' || sc.chNext == '\r') {
|
|
|
|
sc.Forward();
|
|
|
|
if (sc.ch == '\r' && sc.chNext == '\n') {
|
|
|
|
sc.Forward();
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Determine if the current state should terminate.
|
|
|
|
if (sc.state == SCE_LUA_OPERATOR) {
|
2013-08-28 00:44:27 +00:00
|
|
|
if (sc.ch == ':' && sc.chPrev == ':') { // :: <label> :: forward scan
|
|
|
|
sc.Forward();
|
2019-05-04 18:14:48 +00:00
|
|
|
Sci_Position ln = 0;
|
2013-08-28 00:44:27 +00:00
|
|
|
while (IsASpaceOrTab(sc.GetRelative(ln))) // skip over spaces/tabs
|
|
|
|
ln++;
|
2019-05-04 18:14:48 +00:00
|
|
|
Sci_Position ws1 = ln;
|
2013-08-28 00:44:27 +00:00
|
|
|
if (setWordStart.Contains(sc.GetRelative(ln))) {
|
|
|
|
int c, i = 0;
|
|
|
|
char s[100];
|
|
|
|
while (setWord.Contains(c = sc.GetRelative(ln))) { // get potential label
|
|
|
|
if (i < 90)
|
2015-06-07 21:19:26 +00:00
|
|
|
s[i++] = static_cast<char>(c);
|
2013-08-28 00:44:27 +00:00
|
|
|
ln++;
|
|
|
|
}
|
2019-05-04 18:14:48 +00:00
|
|
|
s[i] = '\0'; Sci_Position lbl = ln;
|
2013-08-28 00:44:27 +00:00
|
|
|
if (!keywords.InList(s)) {
|
|
|
|
while (IsASpaceOrTab(sc.GetRelative(ln))) // skip over spaces/tabs
|
|
|
|
ln++;
|
2019-05-04 18:14:48 +00:00
|
|
|
Sci_Position ws2 = ln - lbl;
|
2013-08-28 00:44:27 +00:00
|
|
|
if (sc.GetRelative(ln) == ':' && sc.GetRelative(ln + 1) == ':') {
|
|
|
|
// final :: found, complete valid label construct
|
|
|
|
sc.ChangeState(SCE_LUA_LABEL);
|
|
|
|
if (ws1) {
|
|
|
|
sc.SetState(SCE_LUA_DEFAULT);
|
|
|
|
sc.ForwardBytes(ws1);
|
|
|
|
}
|
|
|
|
sc.SetState(SCE_LUA_LABEL);
|
|
|
|
sc.ForwardBytes(lbl - ws1);
|
|
|
|
if (ws2) {
|
|
|
|
sc.SetState(SCE_LUA_DEFAULT);
|
|
|
|
sc.ForwardBytes(ws2);
|
|
|
|
}
|
|
|
|
sc.SetState(SCE_LUA_LABEL);
|
|
|
|
sc.ForwardBytes(2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-04-24 23:35:41 +00:00
|
|
|
sc.SetState(SCE_LUA_DEFAULT);
|
|
|
|
} else if (sc.state == SCE_LUA_NUMBER) {
|
2011-07-17 22:30:49 +00:00
|
|
|
// We stop the number definition on non-numerical non-dot non-eEpP non-sign non-hexdigit char
|
2009-04-25 23:38:15 +00:00
|
|
|
if (!setNumber.Contains(sc.ch)) {
|
2009-04-24 23:35:41 +00:00
|
|
|
sc.SetState(SCE_LUA_DEFAULT);
|
|
|
|
} else if (sc.ch == '-' || sc.ch == '+') {
|
2011-07-17 22:30:49 +00:00
|
|
|
if (!setExponent.Contains(sc.chPrev))
|
2009-04-25 23:38:15 +00:00
|
|
|
sc.SetState(SCE_LUA_DEFAULT);
|
|
|
|
}
|
2009-04-24 23:35:41 +00:00
|
|
|
} else if (sc.state == SCE_LUA_IDENTIFIER) {
|
2019-05-04 18:14:48 +00:00
|
|
|
idenPos--; // commit already-scanned identitier/word parts
|
|
|
|
if (idenWordPos > 0) {
|
|
|
|
idenWordPos--;
|
|
|
|
sc.ChangeState(idenStyle);
|
|
|
|
sc.ForwardBytes(idenWordPos);
|
|
|
|
idenPos -= idenWordPos;
|
|
|
|
if (idenPos > 0) {
|
|
|
|
sc.SetState(SCE_LUA_IDENTIFIER);
|
|
|
|
sc.ForwardBytes(idenPos);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
sc.ForwardBytes(idenPos);
|
|
|
|
}
|
|
|
|
sc.SetState(SCE_LUA_DEFAULT);
|
|
|
|
if (foundGoto) { // goto <label> forward scan
|
|
|
|
while (IsASpaceOrTab(sc.ch) && !sc.atLineEnd)
|
|
|
|
sc.Forward();
|
|
|
|
if (setWordStart.Contains(sc.ch)) {
|
|
|
|
sc.SetState(SCE_LUA_LABEL);
|
|
|
|
sc.Forward();
|
|
|
|
while (setWord.Contains(sc.ch))
|
|
|
|
sc.Forward();
|
|
|
|
char s[100];
|
|
|
|
sc.GetCurrent(s, sizeof(s));
|
|
|
|
if (keywords.InList(s)) // labels cannot be keywords
|
|
|
|
sc.ChangeState(SCE_LUA_WORD);
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
sc.SetState(SCE_LUA_DEFAULT);
|
|
|
|
}
|
|
|
|
} else if (sc.state == SCE_LUA_COMMENTLINE || sc.state == SCE_LUA_PREPROCESSOR) {
|
|
|
|
if (sc.atLineEnd) {
|
|
|
|
sc.ForwardSetState(SCE_LUA_DEFAULT);
|
|
|
|
}
|
|
|
|
} else if (sc.state == SCE_LUA_STRING) {
|
2011-07-17 22:30:49 +00:00
|
|
|
if (stringWs) {
|
|
|
|
if (!IsASpace(sc.ch))
|
|
|
|
stringWs = 0;
|
|
|
|
}
|
2009-04-24 23:35:41 +00:00
|
|
|
if (sc.ch == '\\') {
|
2009-04-25 23:38:15 +00:00
|
|
|
if (setEscapeSkip.Contains(sc.chNext)) {
|
2009-04-24 23:35:41 +00:00
|
|
|
sc.Forward();
|
2013-08-28 00:44:27 +00:00
|
|
|
} else if (sc.chNext == 'z') {
|
2011-07-17 22:30:49 +00:00
|
|
|
sc.Forward();
|
|
|
|
stringWs = 0x100;
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
} else if (sc.ch == '\"') {
|
|
|
|
sc.ForwardSetState(SCE_LUA_DEFAULT);
|
2011-07-17 22:30:49 +00:00
|
|
|
} else if (stringWs == 0 && sc.atLineEnd) {
|
2009-04-24 23:35:41 +00:00
|
|
|
sc.ChangeState(SCE_LUA_STRINGEOL);
|
|
|
|
sc.ForwardSetState(SCE_LUA_DEFAULT);
|
|
|
|
}
|
|
|
|
} else if (sc.state == SCE_LUA_CHARACTER) {
|
2011-07-17 22:30:49 +00:00
|
|
|
if (stringWs) {
|
|
|
|
if (!IsASpace(sc.ch))
|
|
|
|
stringWs = 0;
|
|
|
|
}
|
2009-04-24 23:35:41 +00:00
|
|
|
if (sc.ch == '\\') {
|
2009-04-25 23:38:15 +00:00
|
|
|
if (setEscapeSkip.Contains(sc.chNext)) {
|
2009-04-24 23:35:41 +00:00
|
|
|
sc.Forward();
|
2013-08-28 00:44:27 +00:00
|
|
|
} else if (sc.chNext == 'z') {
|
2011-07-17 22:30:49 +00:00
|
|
|
sc.Forward();
|
|
|
|
stringWs = 0x100;
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
} else if (sc.ch == '\'') {
|
|
|
|
sc.ForwardSetState(SCE_LUA_DEFAULT);
|
2011-07-17 22:30:49 +00:00
|
|
|
} else if (stringWs == 0 && sc.atLineEnd) {
|
2009-04-24 23:35:41 +00:00
|
|
|
sc.ChangeState(SCE_LUA_STRINGEOL);
|
|
|
|
sc.ForwardSetState(SCE_LUA_DEFAULT);
|
|
|
|
}
|
|
|
|
} else if (sc.state == SCE_LUA_LITERALSTRING || sc.state == SCE_LUA_COMMENT) {
|
|
|
|
if (sc.ch == '[') {
|
2019-05-04 18:14:48 +00:00
|
|
|
const int sep = LongDelimCheck(sc);
|
2009-04-24 23:35:41 +00:00
|
|
|
if (sep == 1 && sepCount == 1) { // [[-only allowed to nest
|
|
|
|
nestLevel++;
|
|
|
|
sc.Forward();
|
|
|
|
}
|
|
|
|
} else if (sc.ch == ']') {
|
|
|
|
int sep = LongDelimCheck(sc);
|
|
|
|
if (sep == 1 && sepCount == 1) { // un-nest with ]]-only
|
|
|
|
nestLevel--;
|
|
|
|
sc.Forward();
|
|
|
|
if (nestLevel == 0) {
|
|
|
|
sc.ForwardSetState(SCE_LUA_DEFAULT);
|
|
|
|
}
|
|
|
|
} else if (sep > 1 && sep == sepCount) { // ]=]-style delim
|
|
|
|
sc.Forward(sep);
|
|
|
|
sc.ForwardSetState(SCE_LUA_DEFAULT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Determine if a new state should be entered.
|
|
|
|
if (sc.state == SCE_LUA_DEFAULT) {
|
|
|
|
if (IsADigit(sc.ch) || (sc.ch == '.' && IsADigit(sc.chNext))) {
|
|
|
|
sc.SetState(SCE_LUA_NUMBER);
|
|
|
|
if (sc.ch == '0' && toupper(sc.chNext) == 'X') {
|
2009-04-25 23:38:15 +00:00
|
|
|
sc.Forward();
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
2009-04-25 23:38:15 +00:00
|
|
|
} else if (setWordStart.Contains(sc.ch)) {
|
2019-05-04 18:14:48 +00:00
|
|
|
// For matching various identifiers with dots and colons, multiple
|
|
|
|
// matches are done as identifier segments are added. Longest match is
|
|
|
|
// set to a word style. The non-matched part is in identifier style.
|
|
|
|
std::string ident;
|
|
|
|
idenPos = 0;
|
|
|
|
idenWordPos = 0;
|
|
|
|
idenStyle = SCE_LUA_IDENTIFIER;
|
|
|
|
foundGoto = false;
|
|
|
|
int cNext;
|
|
|
|
do {
|
|
|
|
int c;
|
|
|
|
const Sci_Position idenPosOld = idenPos;
|
|
|
|
std::string identSeg;
|
|
|
|
identSeg += static_cast<char>(sc.GetRelative(idenPos++));
|
|
|
|
while (setWord.Contains(c = sc.GetRelative(idenPos))) {
|
|
|
|
identSeg += static_cast<char>(c);
|
|
|
|
idenPos++;
|
|
|
|
}
|
|
|
|
if (keywords.InList(identSeg.c_str()) && (idenPosOld > 0)) {
|
|
|
|
idenPos = idenPosOld - 1; // keywords cannot mix
|
|
|
|
ident.pop_back();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ident += identSeg;
|
|
|
|
const char* s = ident.c_str();
|
|
|
|
int newStyle = SCE_LUA_IDENTIFIER;
|
|
|
|
if (keywords.InList(s)) {
|
|
|
|
newStyle = SCE_LUA_WORD;
|
|
|
|
} else if (keywords2.InList(s)) {
|
|
|
|
newStyle = SCE_LUA_WORD2;
|
|
|
|
} else if (keywords3.InList(s)) {
|
|
|
|
newStyle = SCE_LUA_WORD3;
|
|
|
|
} else if (keywords4.InList(s)) {
|
|
|
|
newStyle = SCE_LUA_WORD4;
|
|
|
|
} else if (keywords5.InList(s)) {
|
|
|
|
newStyle = SCE_LUA_WORD5;
|
|
|
|
} else if (keywords6.InList(s)) {
|
|
|
|
newStyle = SCE_LUA_WORD6;
|
|
|
|
} else if (keywords7.InList(s)) {
|
|
|
|
newStyle = SCE_LUA_WORD7;
|
|
|
|
} else if (keywords8.InList(s)) {
|
|
|
|
newStyle = SCE_LUA_WORD8;
|
|
|
|
}
|
|
|
|
if (newStyle != SCE_LUA_IDENTIFIER) {
|
|
|
|
idenStyle = newStyle;
|
|
|
|
idenWordPos = idenPos;
|
|
|
|
}
|
|
|
|
if (idenStyle == SCE_LUA_WORD) // keywords cannot mix
|
|
|
|
break;
|
|
|
|
cNext = sc.GetRelative(idenPos + 1);
|
|
|
|
if ((c == '.' || c == ':') && setWordStart.Contains(cNext)) {
|
|
|
|
ident += static_cast<char>(c);
|
|
|
|
idenPos++;
|
|
|
|
} else {
|
|
|
|
cNext = 0;
|
|
|
|
}
|
|
|
|
} while (cNext);
|
|
|
|
if ((idenStyle == SCE_LUA_WORD) && (ident.compare("goto") == 0)) {
|
|
|
|
foundGoto = true;
|
|
|
|
}
|
2009-04-24 23:35:41 +00:00
|
|
|
sc.SetState(SCE_LUA_IDENTIFIER);
|
|
|
|
} else if (sc.ch == '\"') {
|
|
|
|
sc.SetState(SCE_LUA_STRING);
|
2011-07-17 22:30:49 +00:00
|
|
|
stringWs = 0;
|
2009-04-24 23:35:41 +00:00
|
|
|
} else if (sc.ch == '\'') {
|
|
|
|
sc.SetState(SCE_LUA_CHARACTER);
|
2011-07-17 22:30:49 +00:00
|
|
|
stringWs = 0;
|
2009-04-24 23:35:41 +00:00
|
|
|
} else if (sc.ch == '[') {
|
|
|
|
sepCount = LongDelimCheck(sc);
|
|
|
|
if (sepCount == 0) {
|
|
|
|
sc.SetState(SCE_LUA_OPERATOR);
|
|
|
|
} else {
|
|
|
|
nestLevel = 1;
|
|
|
|
sc.SetState(SCE_LUA_LITERALSTRING);
|
|
|
|
sc.Forward(sepCount);
|
|
|
|
}
|
|
|
|
} else if (sc.Match('-', '-')) {
|
|
|
|
sc.SetState(SCE_LUA_COMMENTLINE);
|
|
|
|
if (sc.Match("--[")) {
|
|
|
|
sc.Forward(2);
|
|
|
|
sepCount = LongDelimCheck(sc);
|
|
|
|
if (sepCount > 0) {
|
|
|
|
nestLevel = 1;
|
|
|
|
sc.ChangeState(SCE_LUA_COMMENT);
|
|
|
|
sc.Forward(sepCount);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
sc.Forward();
|
|
|
|
}
|
|
|
|
} else if (sc.atLineStart && sc.Match('$')) {
|
|
|
|
sc.SetState(SCE_LUA_PREPROCESSOR); // Obsolete since Lua 4.0, but still in old code
|
2009-04-25 23:38:15 +00:00
|
|
|
} else if (setLuaOperator.Contains(sc.ch)) {
|
2009-04-24 23:35:41 +00:00
|
|
|
sc.SetState(SCE_LUA_OPERATOR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-04-25 23:38:15 +00:00
|
|
|
|
2009-04-24 23:35:41 +00:00
|
|
|
sc.Complete();
|
|
|
|
}
|
|
|
|
|
2019-05-04 18:14:48 +00:00
|
|
|
static void FoldLuaDoc(Sci_PositionU startPos, Sci_Position length, int /* initStyle */, WordList *[],
|
2009-04-24 23:35:41 +00:00
|
|
|
Accessor &styler) {
|
2019-05-04 18:14:48 +00:00
|
|
|
const Sci_PositionU lengthDoc = startPos + length;
|
2009-04-24 23:35:41 +00:00
|
|
|
int visibleChars = 0;
|
2019-05-04 18:14:48 +00:00
|
|
|
Sci_Position lineCurrent = styler.GetLine(startPos);
|
2009-04-24 23:35:41 +00:00
|
|
|
int levelPrev = styler.LevelAt(lineCurrent) & SC_FOLDLEVELNUMBERMASK;
|
|
|
|
int levelCurrent = levelPrev;
|
|
|
|
char chNext = styler[startPos];
|
2019-05-04 18:14:48 +00:00
|
|
|
const bool foldCompact = styler.GetPropertyInt("fold.compact", 1) != 0;
|
2009-04-24 23:35:41 +00:00
|
|
|
int styleNext = styler.StyleAt(startPos);
|
|
|
|
|
2019-05-04 18:14:48 +00:00
|
|
|
for (Sci_PositionU i = startPos; i < lengthDoc; i++) {
|
|
|
|
const char ch = chNext;
|
2009-04-24 23:35:41 +00:00
|
|
|
chNext = styler.SafeGetCharAt(i + 1);
|
2019-05-04 18:14:48 +00:00
|
|
|
const int style = styleNext;
|
2009-04-24 23:35:41 +00:00
|
|
|
styleNext = styler.StyleAt(i + 1);
|
2019-05-04 18:14:48 +00:00
|
|
|
const bool atEOL = (ch == '\r' && chNext != '\n') || (ch == '\n');
|
2009-04-24 23:35:41 +00:00
|
|
|
if (style == SCE_LUA_WORD) {
|
|
|
|
if (ch == 'i' || ch == 'd' || ch == 'f' || ch == 'e' || ch == 'r' || ch == 'u') {
|
2013-08-28 00:44:27 +00:00
|
|
|
char s[10] = "";
|
2019-05-04 18:14:48 +00:00
|
|
|
for (Sci_PositionU j = 0; j < 8; j++) {
|
2009-04-24 23:35:41 +00:00
|
|
|
if (!iswordchar(styler[i + j])) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
s[j] = styler[i + j];
|
|
|
|
s[j + 1] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((strcmp(s, "if") == 0) || (strcmp(s, "do") == 0) || (strcmp(s, "function") == 0) || (strcmp(s, "repeat") == 0)) {
|
|
|
|
levelCurrent++;
|
|
|
|
}
|
|
|
|
if ((strcmp(s, "end") == 0) || (strcmp(s, "elseif") == 0) || (strcmp(s, "until") == 0)) {
|
|
|
|
levelCurrent--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (style == SCE_LUA_OPERATOR) {
|
|
|
|
if (ch == '{' || ch == '(') {
|
|
|
|
levelCurrent++;
|
|
|
|
} else if (ch == '}' || ch == ')') {
|
|
|
|
levelCurrent--;
|
|
|
|
}
|
|
|
|
} else if (style == SCE_LUA_LITERALSTRING || style == SCE_LUA_COMMENT) {
|
|
|
|
if (ch == '[') {
|
|
|
|
levelCurrent++;
|
|
|
|
} else if (ch == ']') {
|
|
|
|
levelCurrent--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (atEOL) {
|
|
|
|
int lev = levelPrev;
|
|
|
|
if (visibleChars == 0 && foldCompact) {
|
|
|
|
lev |= SC_FOLDLEVELWHITEFLAG;
|
|
|
|
}
|
|
|
|
if ((levelCurrent > levelPrev) && (visibleChars > 0)) {
|
|
|
|
lev |= SC_FOLDLEVELHEADERFLAG;
|
|
|
|
}
|
|
|
|
if (lev != styler.LevelAt(lineCurrent)) {
|
|
|
|
styler.SetLevel(lineCurrent, lev);
|
|
|
|
}
|
|
|
|
lineCurrent++;
|
|
|
|
levelPrev = levelCurrent;
|
|
|
|
visibleChars = 0;
|
|
|
|
}
|
|
|
|
if (!isspacechar(ch)) {
|
|
|
|
visibleChars++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Fill in the real level of the next line, keeping the current flags as they will be filled in later
|
|
|
|
|
|
|
|
int flagsNext = styler.LevelAt(lineCurrent) & ~SC_FOLDLEVELNUMBERMASK;
|
|
|
|
styler.SetLevel(lineCurrent, levelPrev | flagsNext);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char * const luaWordListDesc[] = {
|
|
|
|
"Keywords",
|
|
|
|
"Basic functions",
|
|
|
|
"String, (table) & math functions",
|
|
|
|
"(coroutines), I/O & system facilities",
|
|
|
|
"user1",
|
|
|
|
"user2",
|
|
|
|
"user3",
|
|
|
|
"user4",
|
|
|
|
0
|
|
|
|
};
|
|
|
|
|
2019-05-04 18:14:48 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
LexicalClass lexicalClasses[] = {
|
|
|
|
// Lexer Lua SCLEX_LUA SCE_LUA_:
|
|
|
|
0, "SCE_LUA_DEFAULT", "default", "White space: Visible only in View Whitespace mode (or if it has a back colour)",
|
|
|
|
1, "SCE_LUA_COMMENT", "comment", "Block comment (Lua 5.0)",
|
|
|
|
2, "SCE_LUA_COMMENTLINE", "comment line", "Line comment",
|
|
|
|
3, "SCE_LUA_COMMENTDOC", "comment documentation", "Doc comment -- Not used in Lua (yet?)",
|
|
|
|
4, "SCE_LUA_NUMBER", "literal numeric", "Number",
|
|
|
|
5, "SCE_LUA_WORD", "keyword", "Keyword",
|
|
|
|
6, "SCE_LUA_STRING", "literal string", "(Double quoted) String",
|
|
|
|
7, "SCE_LUA_CHARACTER", "literal string character", "Character (Single quoted string)",
|
|
|
|
8, "SCE_LUA_LITERALSTRING", "literal string", "Literal string",
|
|
|
|
9, "SCE_LUA_PREPROCESSOR", "preprocessor", "Preprocessor (obsolete in Lua 4.0 and up)",
|
|
|
|
10, "SCE_LUA_OPERATOR", "operator", "Operators",
|
|
|
|
11, "SCE_LUA_IDENTIFIER", "identifier", "Identifier (everything else...)",
|
|
|
|
12, "SCE_LUA_STRINGEOL", "error literal string", "End of line where string is not closed",
|
|
|
|
13, "SCE_LUA_WORD2", "identifier", "Other keywords",
|
|
|
|
14, "SCE_LUA_WORD3", "identifier", "Other keywords",
|
|
|
|
15, "SCE_LUA_WORD4", "identifier", "Other keywords",
|
|
|
|
16, "SCE_LUA_WORD5", "identifier", "Other keywords",
|
|
|
|
17, "SCE_LUA_WORD6", "identifier", "Other keywords",
|
|
|
|
18, "SCE_LUA_WORD7", "identifier", "Other keywords",
|
|
|
|
19, "SCE_LUA_WORD8", "identifier", "Other keywords",
|
|
|
|
20, "SCE_LUA_LABEL", "label", "Labels",
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
LexerModule lmLua(SCLEX_LUA, ColouriseLuaDoc, "lua", FoldLuaDoc, luaWordListDesc, lexicalClasses, ELEMENTS(lexicalClasses));
|