| 1 |
// Scintilla source code edit control |
| 2 |
/** @file LexMake.cxx |
| 3 |
** Lexer for make files. |
| 4 |
**/ |
| 5 |
// Copyright 1998-2001 by Neil Hodgson <[email protected]> |
| 6 |
// The License.txt file describes the conditions under which this software may be distributed. |
| 7 |
|
| 8 |
#include <cstdlib> |
| 9 |
#include <cassert> |
| 10 |
#include <cstring> |
| 11 |
#include <cctype> |
| 12 |
#include <cstdio> |
| 13 |
#include <cstdarg> |
| 14 |
|
| 15 |
#include <string> |
| 16 |
#include <string_view> |
| 17 |
|
| 18 |
#include "ILexer.h" |
| 19 |
#include "Scintilla.h" |
| 20 |
#include "SciLexer.h" |
| 21 |
|
| 22 |
#include "WordList.h" |
| 23 |
#include "LexAccessor.h" |
| 24 |
#include "Accessor.h" |
| 25 |
#include "StyleContext.h" |
| 26 |
#include "CharacterSet.h" |
| 27 |
#include "LexerModule.h" |
| 28 |
|
| 29 |
using namespace Lexilla; |
| 30 |
|
| 31 |
static inline bool AtEOL(Accessor &styler, Sci_PositionU i) { |
| 32 |
return (styler[i] == '\n') || |
| 33 |
((styler[i] == '\r') && (styler.SafeGetCharAt(i + 1) != '\n')); |
| 34 |
} |
| 35 |
|
| 36 |
static void ColouriseMakeLine( |
| 37 |
const std::string &lineBuffer, |
| 38 |
Sci_PositionU startLine, |
| 39 |
Sci_PositionU endPos, |
| 40 |
Accessor &styler) { |
| 41 |
|
| 42 |
const Sci_PositionU lengthLine = lineBuffer.length(); |
| 43 |
Sci_PositionU i = 0; |
| 44 |
Sci_Position lastNonSpace = -1; |
| 45 |
unsigned int state = SCE_MAKE_DEFAULT; |
| 46 |
bool bSpecial = false; |
| 47 |
|
| 48 |
// check for a tab character in column 0 indicating a command |
| 49 |
bool bCommand = false; |
| 50 |
if ((lengthLine > 0) && (lineBuffer[0] == '\t')) |
| 51 |
bCommand = true; |
| 52 |
|
| 53 |
// Skip initial spaces |
| 54 |
while ((i < lengthLine) && isspacechar(lineBuffer[i])) { |
| 55 |
i++; |
| 56 |
} |
| 57 |
if (i < lengthLine) { |
| 58 |
if (lineBuffer[i] == '#') { // Comment |
| 59 |
styler.ColourTo(endPos, SCE_MAKE_COMMENT); |
| 60 |
return; |
| 61 |
} |
| 62 |
if (lineBuffer[i] == '!') { // Special directive |
| 63 |
styler.ColourTo(endPos, SCE_MAKE_PREPROCESSOR); |
| 64 |
return; |
| 65 |
} |
| 66 |
} |
| 67 |
int varCount = 0; |
| 68 |
while (i < lengthLine) { |
| 69 |
if (((i + 1) < lengthLine) && (lineBuffer[i] == '$' && lineBuffer[i + 1] == '(')) { |
| 70 |
styler.ColourTo(startLine + i - 1, state); |
| 71 |
state = SCE_MAKE_IDENTIFIER; |
| 72 |
varCount++; |
| 73 |
} else if (state == SCE_MAKE_IDENTIFIER && lineBuffer[i] == ')') { |
| 74 |
if (--varCount == 0) { |
| 75 |
styler.ColourTo(startLine + i, state); |
| 76 |
state = SCE_MAKE_DEFAULT; |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
// skip identifier and target styling if this is a command line |
| 81 |
if (!bSpecial && !bCommand) { |
| 82 |
if (lineBuffer[i] == ':') { |
| 83 |
if (((i + 1) < lengthLine) && (lineBuffer[i + 1] == '=')) { |
| 84 |
// it's a ':=', so style as an identifier |
| 85 |
if (lastNonSpace >= 0) |
| 86 |
styler.ColourTo(startLine + lastNonSpace, SCE_MAKE_IDENTIFIER); |
| 87 |
styler.ColourTo(startLine + i - 1, SCE_MAKE_DEFAULT); |
| 88 |
styler.ColourTo(startLine + i + 1, SCE_MAKE_OPERATOR); |
| 89 |
} else { |
| 90 |
// We should check that no colouring was made since the beginning of the line, |
| 91 |
// to avoid colouring stuff like /OUT:file |
| 92 |
if (lastNonSpace >= 0) |
| 93 |
styler.ColourTo(startLine + lastNonSpace, SCE_MAKE_TARGET); |
| 94 |
styler.ColourTo(startLine + i - 1, SCE_MAKE_DEFAULT); |
| 95 |
styler.ColourTo(startLine + i, SCE_MAKE_OPERATOR); |
| 96 |
} |
| 97 |
bSpecial = true; // Only react to the first ':' of the line |
| 98 |
state = SCE_MAKE_DEFAULT; |
| 99 |
} else if (lineBuffer[i] == '=') { |
| 100 |
if (lastNonSpace >= 0) |
| 101 |
styler.ColourTo(startLine + lastNonSpace, SCE_MAKE_IDENTIFIER); |
| 102 |
styler.ColourTo(startLine + i - 1, SCE_MAKE_DEFAULT); |
| 103 |
styler.ColourTo(startLine + i, SCE_MAKE_OPERATOR); |
| 104 |
bSpecial = true; // Only react to the first '=' of the line |
| 105 |
state = SCE_MAKE_DEFAULT; |
| 106 |
} |
| 107 |
} |
| 108 |
if (!isspacechar(lineBuffer[i])) { |
| 109 |
lastNonSpace = i; |
| 110 |
} |
| 111 |
i++; |
| 112 |
} |
| 113 |
if (state == SCE_MAKE_IDENTIFIER) { |
| 114 |
styler.ColourTo(endPos, SCE_MAKE_IDEOL); // Error, variable reference not ended |
| 115 |
} else { |
| 116 |
styler.ColourTo(endPos, SCE_MAKE_DEFAULT); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
static void ColouriseMakeDoc(Sci_PositionU startPos, Sci_Position length, int, WordList *[], Accessor &styler) { |
| 121 |
std::string lineBuffer; |
| 122 |
styler.StartAt(startPos); |
| 123 |
styler.StartSegment(startPos); |
| 124 |
Sci_PositionU startLine = startPos; |
| 125 |
for (Sci_PositionU i = startPos; i < startPos + length; i++) { |
| 126 |
lineBuffer.push_back(styler[i]); |
| 127 |
if (AtEOL(styler, i)) { |
| 128 |
// End of line (or of line buffer) met, colourise it |
| 129 |
ColouriseMakeLine(lineBuffer, startLine, i, styler); |
| 130 |
lineBuffer.clear(); |
| 131 |
startLine = i + 1; |
| 132 |
} |
| 133 |
} |
| 134 |
if (!lineBuffer.empty()) { // Last line does not have ending characters |
| 135 |
ColouriseMakeLine(lineBuffer, startLine, startPos + length - 1, styler); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
static const char *const emptyWordListDesc[] = { |
| 140 |
nullptr |
| 141 |
}; |
| 142 |
|
| 143 |
extern const LexerModule lmMake(SCLEX_MAKEFILE, ColouriseMakeDoc, "makefile", nullptr, emptyWordListDesc); |