Skip to content

Commit cc98968

Browse files
committed
Make the batch lexer an object lexer to provide more metadata and allow adding more features.
1 parent f31f449 commit cc98968

File tree

1 file changed

+67
-17
lines changed

1 file changed

+67
-17
lines changed

lexers/LexBatch.cxx

Lines changed: 67 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,51 @@
2727
#include "StyleContext.h"
2828
#include "CharacterSet.h"
2929
#include "LexerModule.h"
30+
#include "DefaultLexer.h"
3031

3132
using namespace Lexilla;
3233

3334
namespace {
3435

36+
const char *const batchWordListDesc[] = {
37+
"Internal Commands",
38+
"External Commands",
39+
nullptr
40+
};
41+
42+
const LexicalClass lexicalClasses[] = {
43+
// Lexer batch SCLEX_BATCH SCE_BAT_
44+
0, "SCE_BAT_DEFAULT", "default", "White space",
45+
1, "SCE_BAT_COMMENT", "comment", "Line comment",
46+
2, "SCE_BAT_WORD", "keyword", "Keyword",
47+
3, "SCE_BAT_LABEL", "label", "Label",
48+
4, "SCE_BAT_HIDE", "preprocessor", "Hide line @",
49+
5, "SCE_BAT_COMMAND", "identifier", "Command",
50+
6, "SCE_BAT_IDENTIFIER", "identifier", "Identifier",
51+
7, "SCE_BAT_OPERATOR", "operator", "Operator",
52+
8, "SCE_BAT_AFTER_LABEL", "comment","After label",
53+
};
54+
55+
class LexerBatch : public DefaultLexer {
56+
WordList keywords;
57+
WordList keywords2;
58+
public:
59+
explicit LexerBatch() :
60+
DefaultLexer("batch", SCLEX_BATCH, lexicalClasses, std::size(lexicalClasses)) {}
61+
LexerBatch(const LexerBatch &) = delete;
62+
LexerBatch(LexerBatch &&) = delete;
63+
LexerBatch &operator=(const LexerBatch &) = delete;
64+
LexerBatch &operator=(LexerBatch &&) = delete;
65+
~LexerBatch() override = default;
66+
67+
Sci_Position SCI_METHOD WordListSet(int n, const char *wl) override;
68+
void SCI_METHOD Lex(Sci_PositionU startPos, Sci_Position length, int initStyle, Scintilla::IDocument *pAccess) override;
69+
70+
static ILexer5 *LexerFactoryBatch() {
71+
return new LexerBatch();
72+
}
73+
};
74+
3575
constexpr bool Is0To9(char ch) noexcept {
3676
return (ch >= '0') && (ch <= '9');
3777
}
@@ -40,7 +80,7 @@ constexpr bool IsAlphabetic(int ch) noexcept {
4080
return IsUpperOrLowerCase(ch);
4181
}
4282

43-
inline bool AtEOL(Accessor &styler, Sci_PositionU i) {
83+
bool AtEOL(LexAccessor &styler, Sci_PositionU i) {
4484
return (styler[i] == '\n') ||
4585
((styler[i] == '\r') && (styler.SafeGetCharAt(i + 1) != '\n'));
4686
}
@@ -130,12 +170,30 @@ bool IsContinuation(const std::string &lineBuffer) noexcept {
130170
return true;
131171
}
132172

133-
void ColouriseBatchDoc(
134-
Sci_PositionU startPos,
135-
Sci_Position length,
136-
int /*initStyle*/,
137-
WordList *keywordlists[],
138-
Accessor &styler) {
173+
Sci_Position SCI_METHOD LexerBatch::WordListSet(int n, const char *wl) {
174+
WordList *wordListN = nullptr;
175+
switch (n) {
176+
case 0:
177+
wordListN = &keywords;
178+
break;
179+
case 1:
180+
wordListN = &keywords2;
181+
break;
182+
default:
183+
break;
184+
}
185+
Sci_Position firstModification = -1;
186+
if (wordListN) {
187+
if (wordListN->Set(wl)) {
188+
firstModification = 0;
189+
}
190+
}
191+
return firstModification;
192+
}
193+
194+
void LexerBatch::Lex(Sci_PositionU startPos, Sci_Position length, int, Scintilla::IDocument *pAccess) {
195+
LexAccessor styler(pAccess);
196+
139197
// Always backtracks to the start of a line that is not a continuation
140198
// of the previous line
141199
if (startPos > 0) {
@@ -154,9 +212,6 @@ void ColouriseBatchDoc(
154212
}
155213
}
156214

157-
const WordList &keywords = *keywordlists[0]; // Internal Commands
158-
const WordList &keywords2 = *keywordlists[1]; // External Commands (optional)
159-
160215
std::string lineBuffer;
161216
std::string word;
162217

@@ -567,14 +622,9 @@ void ColouriseBatchDoc(
567622
startLine = i + 1;
568623
}
569624
}
625+
styler.Flush();
570626
}
571627

572-
const char *const batchWordListDesc[] = {
573-
"Internal Commands",
574-
"External Commands",
575-
nullptr
576-
};
577-
578628
}
579629

580-
extern const LexerModule lmBatch(SCLEX_BATCH, ColouriseBatchDoc, "batch", nullptr, batchWordListDesc);
630+
extern const LexerModule lmBatch(SCLEX_LUA, LexerBatch::LexerFactoryBatch, "batch", batchWordListDesc);

0 commit comments

Comments
 (0)