aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/LexLua.cxx
diff options
context:
space:
mode:
authornyamatongwe <devnull@localhost>2005-12-02 00:52:07 +0000
committernyamatongwe <devnull@localhost>2005-12-02 00:52:07 +0000
commit41ababaa126118eb2f8ac892f1f53e172109c7c3 (patch)
tree611521b3956f8ac06ef07ce7c8e16f671bcc3b0f /src/LexLua.cxx
parentef2b3a6b37ab15b893ea809f041d78e12797e69f (diff)
downloadscintilla-mirror-41ababaa126118eb2f8ac892f1f53e172109c7c3.tar.gz
Patch from Kein-Hong Man support new Lua 5.1 long string and
block comment syntax [=[...]=] and --[=[...]=].
Diffstat (limited to 'src/LexLua.cxx')
-rw-r--r--src/LexLua.cxx113
1 files changed, 63 insertions, 50 deletions
diff --git a/src/LexLua.cxx b/src/LexLua.cxx
index 98670cc1a..7f6df070a 100644
--- a/src/LexLua.cxx
+++ b/src/LexLua.cxx
@@ -57,6 +57,18 @@ static inline bool IsLuaOperator(int ch) {
return false;
}
+// 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(
unsigned int startPos,
int length,
@@ -74,15 +86,15 @@ static void ColouriseLuaDoc(
WordList &keywords8 = *keywordlists[7];
int currentLine = styler.GetLine(startPos);
- // Initialize the literal string [[ ... ]] nesting level, if we are inside such a string.
- int literalStringLevel = 0;
- if (initStyle == SCE_LUA_LITERALSTRING) {
- literalStringLevel = styler.GetLineState(currentLine - 1);
- }
- // Initialize the block comment --[[ ... ]] nesting level, if we are inside such a comment
- int blockCommentLevel = 0;
- if (initStyle == SCE_LUA_COMMENT) {
- blockCommentLevel = styler.GetLineState(currentLine - 1);
+ // 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.
+ int nestLevel = 0;
+ int sepCount = 0;
+ if (initStyle == SCE_LUA_LITERALSTRING || initStyle == SCE_LUA_COMMENT) {
+ int lineState = styler.GetLineState(currentLine - 1);
+ nestLevel = lineState >> 8;
+ sepCount = lineState & 0xFF;
}
// Do not leak onto next line
@@ -101,12 +113,9 @@ static void ColouriseLuaDoc(
currentLine = styler.GetLine(sc.currentPos);
switch (sc.state) {
case SCE_LUA_LITERALSTRING:
- // Inside a literal string, we set the line state
- styler.SetLineState(currentLine, literalStringLevel);
- break;
- case SCE_LUA_COMMENT: // Block comment
- // Inside a block comment, we set the line state
- styler.SetLineState(currentLine, blockCommentLevel);
+ case SCE_LUA_COMMENT:
+ // Inside a literal string or block comment, we set the line state
+ styler.SetLineState(currentLine, (nestLevel << 8) | sepCount);
break;
default:
// Reset the line state
@@ -164,11 +173,7 @@ static void ColouriseLuaDoc(
}
sc.SetState(SCE_LUA_DEFAULT);
}
- } else if (sc.state == SCE_LUA_COMMENTLINE) {
- if (sc.atLineEnd) {
- sc.ForwardSetState(SCE_LUA_DEFAULT);
- }
- } else if (sc.state == SCE_LUA_PREPROCESSOR) {
+ } else if (sc.state == SCE_LUA_COMMENTLINE || sc.state == SCE_LUA_PREPROCESSOR) {
if (sc.atLineEnd) {
sc.ForwardSetState(SCE_LUA_DEFAULT);
}
@@ -194,26 +199,23 @@ static void ColouriseLuaDoc(
sc.ChangeState(SCE_LUA_STRINGEOL);
sc.ForwardSetState(SCE_LUA_DEFAULT);
}
- } else if (sc.state == SCE_LUA_LITERALSTRING) {
- if (sc.Match('[', '[')) {
- literalStringLevel++;
- sc.Forward();
- sc.SetState(SCE_LUA_LITERALSTRING);
- } else if (sc.Match(']', ']') && literalStringLevel > 0) {
- literalStringLevel--;
- sc.Forward();
- if (literalStringLevel == 0) {
- sc.ForwardSetState(SCE_LUA_DEFAULT);
+ } else if (sc.state == SCE_LUA_LITERALSTRING || sc.state == SCE_LUA_COMMENT) {
+ if (sc.ch == '[') {
+ int sep = LongDelimCheck(sc);
+ if (sep == 1 && sepCount == 1) { // [[-only allowed to nest
+ nestLevel++;
+ sc.Forward();
}
- }
- } else if (sc.state == SCE_LUA_COMMENT) { // Lua 5.0's block comment
- if (sc.Match('[', '[')) {
- blockCommentLevel++;
- sc.Forward();
- } else if (sc.Match(']', ']') && blockCommentLevel > 0) {
- blockCommentLevel--;
- sc.Forward();
- if (blockCommentLevel == 0) {
+ } 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);
}
}
@@ -225,21 +227,32 @@ static void ColouriseLuaDoc(
sc.SetState(SCE_LUA_NUMBER);
} else if (IsAWordStart(sc.ch)) {
sc.SetState(SCE_LUA_IDENTIFIER);
- } else if (sc.Match('\"')) {
+ } else if (sc.ch == '\"') {
sc.SetState(SCE_LUA_STRING);
- } else if (sc.Match('\'')) {
+ } else if (sc.ch == '\'') {
sc.SetState(SCE_LUA_CHARACTER);
- } else if (sc.Match('[', '[')) {
- literalStringLevel = 1;
- sc.SetState(SCE_LUA_LITERALSTRING);
- sc.Forward();
- } else if (sc.Match("--[[")) { // Lua 5.0's block comment
- blockCommentLevel = 1;
- sc.SetState(SCE_LUA_COMMENT);
- sc.Forward(3);
+ } 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);
- sc.Forward();
+ 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
} else if (IsLuaOperator(static_cast<char>(sc.ch))) {