| 1 |
// Scintilla source code edit control |
| 2 |
/** @file LexAsm.cxx |
| 3 |
** Lexer for Assembler, just for the MASM syntax |
| 4 |
** Written by The Black Horus |
| 5 |
** Enhancements and NASM stuff by Kein-Hong Man, 2003-10 |
| 6 |
** SCE_ASM_COMMENTBLOCK and SCE_ASM_CHARACTER are for future GNU as colouring |
| 7 |
** Converted to lexer object and added further folding features/properties by "Udo Lechner" <dlchnr(at)gmx(dot)net> |
| 8 |
**/ |
| 9 |
// Copyright 1998-2003 by Neil Hodgson <[email protected]> |
| 10 |
// The License.txt file describes the conditions under which this software may be distributed. |
| 11 |
|
| 12 |
#include <cstdlib> |
| 13 |
#include <cassert> |
| 14 |
#include <cstring> |
| 15 |
#include <cctype> |
| 16 |
#include <cstdio> |
| 17 |
#include <cstdarg> |
| 18 |
|
| 19 |
#include <string> |
| 20 |
#include <string_view> |
| 21 |
#include <map> |
| 22 |
#include <set> |
| 23 |
#include <functional> |
| 24 |
|
| 25 |
#include "ILexer.h" |
| 26 |
#include "Scintilla.h" |
| 27 |
#include "SciLexer.h" |
| 28 |
|
| 29 |
#include "WordList.h" |
| 30 |
#include "LexAccessor.h" |
| 31 |
#include "StyleContext.h" |
| 32 |
#include "CharacterSet.h" |
| 33 |
#include "LexerModule.h" |
| 34 |
#include "OptionSet.h" |
| 35 |
#include "DefaultLexer.h" |
| 36 |
|
| 37 |
using namespace Scintilla; |
| 38 |
using namespace Lexilla; |
| 39 |
|
| 40 |
namespace { |
| 41 |
|
| 42 |
bool IsAWordChar(const int ch) noexcept { |
| 43 |
return (ch < 0x80) && (isalnum(ch) || ch == '.' || |
| 44 |
ch == '_' || ch == '?'); |
| 45 |
} |
| 46 |
|
| 47 |
bool IsAWordStart(const int ch) noexcept { |
| 48 |
return (ch < 0x80) && (isalnum(ch) || ch == '_' || ch == '.' || |
| 49 |
ch == '%' || ch == '@' || ch == '$' || ch == '?'); |
| 50 |
} |
| 51 |
|
| 52 |
bool IsAsmOperator(const int ch) noexcept { |
| 53 |
if ((ch < 0x80) && (isalnum(ch))) |
| 54 |
return false; |
| 55 |
// '.' left out as it is used to make up numbers |
| 56 |
if (ch == '*' || ch == '/' || ch == '-' || ch == '+' || |
| 57 |
ch == '(' || ch == ')' || ch == '=' || ch == '^' || |
| 58 |
ch == '[' || ch == ']' || ch == '<' || ch == '&' || |
| 59 |
ch == '>' || ch == ',' || ch == '|' || ch == '~' || |
| 60 |
ch == '%' || ch == ':') |
| 61 |
return true; |
| 62 |
return false; |
| 63 |
} |
| 64 |
|
| 65 |
constexpr bool IsStreamCommentStyle(int style) noexcept { |
| 66 |
return style == SCE_ASM_COMMENTDIRECTIVE || style == SCE_ASM_COMMENTBLOCK; |
| 67 |
} |
| 68 |
|
| 69 |
// An individual named option for use in an OptionSet |
| 70 |
|
| 71 |
// Options used for LexerAsm |
| 72 |
struct OptionsAsm { |
| 73 |
std::string delimiter; |
| 74 |
bool fold; |
| 75 |
bool foldSyntaxBased; |
| 76 |
bool foldCommentMultiline; |
| 77 |
bool foldCommentExplicit; |
| 78 |
std::string foldExplicitStart; |
| 79 |
std::string foldExplicitEnd; |
| 80 |
bool foldExplicitAnywhere; |
| 81 |
bool foldCompact; |
| 82 |
std::string commentChar; |
| 83 |
OptionsAsm() { |
| 84 |
delimiter = ""; |
| 85 |
fold = false; |
| 86 |
foldSyntaxBased = true; |
| 87 |
foldCommentMultiline = false; |
| 88 |
foldCommentExplicit = false; |
| 89 |
foldExplicitStart = ""; |
| 90 |
foldExplicitEnd = ""; |
| 91 |
foldExplicitAnywhere = false; |
| 92 |
foldCompact = true; |
| 93 |
commentChar = ""; |
| 94 |
} |
| 95 |
}; |
| 96 |
|
| 97 |
const char *const asmWordListDesc[] = { |
| 98 |
"CPU instructions", |
| 99 |
"FPU instructions", |
| 100 |
"Registers", |
| 101 |
"Directives", |
| 102 |
"Directive operands", |
| 103 |
"Extended instructions", |
| 104 |
"Directives4Foldstart", |
| 105 |
"Directives4Foldend", |
| 106 |
nullptr |
| 107 |
}; |
| 108 |
|
| 109 |
struct OptionSetAsm : public OptionSet<OptionsAsm> { |
| 110 |
OptionSetAsm() { |
| 111 |
DefineProperty("lexer.asm.comment.delimiter", &OptionsAsm::delimiter, |
| 112 |
"Character used for COMMENT directive's delimiter, replacing the standard \"~\"."); |
| 113 |
|
| 114 |
DefineProperty("fold", &OptionsAsm::fold); |
| 115 |
|
| 116 |
DefineProperty("fold.asm.syntax.based", &OptionsAsm::foldSyntaxBased, |
| 117 |
"Set this property to 0 to disable syntax based folding."); |
| 118 |
|
| 119 |
DefineProperty("fold.asm.comment.multiline", &OptionsAsm::foldCommentMultiline, |
| 120 |
"Set this property to 1 to enable folding multi-line comments."); |
| 121 |
|
| 122 |
DefineProperty("fold.asm.comment.explicit", &OptionsAsm::foldCommentExplicit, |
| 123 |
"This option enables folding explicit fold points when using the Asm lexer. " |
| 124 |
"Explicit fold points allows adding extra folding by placing a ;{ comment at the start and a ;} " |
| 125 |
"at the end of a section that should fold."); |
| 126 |
|
| 127 |
DefineProperty("fold.asm.explicit.start", &OptionsAsm::foldExplicitStart, |
| 128 |
"The string to use for explicit fold start points, replacing the standard ;{."); |
| 129 |
|
| 130 |
DefineProperty("fold.asm.explicit.end", &OptionsAsm::foldExplicitEnd, |
| 131 |
"The string to use for explicit fold end points, replacing the standard ;}."); |
| 132 |
|
| 133 |
DefineProperty("fold.asm.explicit.anywhere", &OptionsAsm::foldExplicitAnywhere, |
| 134 |
"Set this property to 1 to enable explicit fold points anywhere, not just in line comments."); |
| 135 |
|
| 136 |
DefineProperty("fold.compact", &OptionsAsm::foldCompact); |
| 137 |
|
| 138 |
DefineProperty("lexer.as.comment.character", &OptionsAsm::commentChar, |
| 139 |
"Overrides the default comment character (which is ';' for asm and '#' for as)."); |
| 140 |
|
| 141 |
DefineWordListSets(asmWordListDesc); |
| 142 |
} |
| 143 |
}; |
| 144 |
|
| 145 |
class LexerAsm : public DefaultLexer { |
| 146 |
WordList cpuInstruction; |
| 147 |
WordList mathInstruction; |
| 148 |
WordList registers; |
| 149 |
WordList directive; |
| 150 |
WordList directiveOperand; |
| 151 |
WordList extInstruction; |
| 152 |
WordList directives4foldstart; |
| 153 |
WordList directives4foldend; |
| 154 |
OptionsAsm options; |
| 155 |
OptionSetAsm osAsm; |
| 156 |
int commentChar; |
| 157 |
public: |
| 158 |
LexerAsm(const char *languageName_, int language_, int commentChar_) : DefaultLexer(languageName_, language_) { |
| 159 |
commentChar = commentChar_; |
| 160 |
} |
| 161 |
virtual ~LexerAsm() { |
| 162 |
} |
| 163 |
void SCI_METHOD Release() override { |
| 164 |
delete this; |
| 165 |
} |
| 166 |
int SCI_METHOD Version() const override { |
| 167 |
return lvRelease5; |
| 168 |
} |
| 169 |
const char * SCI_METHOD PropertyNames() override { |
| 170 |
return osAsm.PropertyNames(); |
| 171 |
} |
| 172 |
int SCI_METHOD PropertyType(const char *name) override { |
| 173 |
return osAsm.PropertyType(name); |
| 174 |
} |
| 175 |
const char * SCI_METHOD DescribeProperty(const char *name) override { |
| 176 |
return osAsm.DescribeProperty(name); |
| 177 |
} |
| 178 |
Sci_Position SCI_METHOD PropertySet(const char *key, const char *val) override; |
| 179 |
const char * SCI_METHOD PropertyGet(const char *key) override { |
| 180 |
return osAsm.PropertyGet(key); |
| 181 |
} |
| 182 |
const char * SCI_METHOD DescribeWordListSets() override { |
| 183 |
return osAsm.DescribeWordListSets(); |
| 184 |
} |
| 185 |
Sci_Position SCI_METHOD WordListSet(int n, const char *wl) override; |
| 186 |
void SCI_METHOD Lex(Sci_PositionU startPos, Sci_Position length, int initStyle, IDocument *pAccess) override; |
| 187 |
void SCI_METHOD Fold(Sci_PositionU startPos, Sci_Position length, int initStyle, IDocument *pAccess) override; |
| 188 |
|
| 189 |
void * SCI_METHOD PrivateCall(int, void *) override { |
| 190 |
return nullptr; |
| 191 |
} |
| 192 |
|
| 193 |
static ILexer5 *LexerFactoryAsm() { |
| 194 |
return new LexerAsm("asm", SCLEX_ASM, ';'); |
| 195 |
} |
| 196 |
|
| 197 |
static ILexer5 *LexerFactoryAs() { |
| 198 |
return new LexerAsm("as", SCLEX_AS, '#'); |
| 199 |
} |
| 200 |
}; |
| 201 |
|
| 202 |
Sci_Position SCI_METHOD LexerAsm::PropertySet(const char *key, const char *val) { |
| 203 |
if (osAsm.PropertySet(&options, key, val)) { |
| 204 |
return 0; |
| 205 |
} |
| 206 |
return -1; |
| 207 |
} |
| 208 |
|
| 209 |
Sci_Position SCI_METHOD LexerAsm::WordListSet(int n, const char *wl) { |
| 210 |
WordList *wordListN = nullptr; |
| 211 |
switch (n) { |
| 212 |
case 0: |
| 213 |
wordListN = &cpuInstruction; |
| 214 |
break; |
| 215 |
case 1: |
| 216 |
wordListN = &mathInstruction; |
| 217 |
break; |
| 218 |
case 2: |
| 219 |
wordListN = ®isters; |
| 220 |
break; |
| 221 |
case 3: |
| 222 |
wordListN = &directive; |
| 223 |
break; |
| 224 |
case 4: |
| 225 |
wordListN = &directiveOperand; |
| 226 |
break; |
| 227 |
case 5: |
| 228 |
wordListN = &extInstruction; |
| 229 |
break; |
| 230 |
case 6: |
| 231 |
wordListN = &directives4foldstart; |
| 232 |
break; |
| 233 |
case 7: |
| 234 |
wordListN = &directives4foldend; |
| 235 |
break; |
| 236 |
} |
| 237 |
Sci_Position firstModification = -1; |
| 238 |
if (wordListN) { |
| 239 |
if (wordListN->Set(wl, true)) { |
| 240 |
firstModification = 0; |
| 241 |
} |
| 242 |
} |
| 243 |
return firstModification; |
| 244 |
} |
| 245 |
|
| 246 |
void SCI_METHOD LexerAsm::Lex(Sci_PositionU startPos, Sci_Position length, int initStyle, IDocument *pAccess) { |
| 247 |
LexAccessor styler(pAccess); |
| 248 |
|
| 249 |
const char commentCharacter = options.commentChar.empty() ? |
| 250 |
commentChar : options.commentChar.front(); |
| 251 |
|
| 252 |
// Do not leak onto next line |
| 253 |
if (initStyle == SCE_ASM_STRINGEOL) |
| 254 |
initStyle = SCE_ASM_DEFAULT; |
| 255 |
|
| 256 |
StyleContext sc(startPos, length, initStyle, styler); |
| 257 |
|
| 258 |
for (; sc.More(); sc.Forward()) |
| 259 |
{ |
| 260 |
|
| 261 |
if (sc.atLineStart) { |
| 262 |
switch (sc.state) { |
| 263 |
case SCE_ASM_STRING: |
| 264 |
case SCE_ASM_CHARACTER: |
| 265 |
// Prevent SCE_ASM_STRINGEOL from leaking back to previous line |
| 266 |
sc.SetState(sc.state); |
| 267 |
break; |
| 268 |
case SCE_ASM_COMMENT: |
| 269 |
sc.SetState(SCE_ASM_DEFAULT); |
| 270 |
break; |
| 271 |
default: |
| 272 |
break; |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
// Handle line continuation generically. |
| 277 |
if (sc.ch == '\\') { |
| 278 |
if (sc.chNext == '\n' || sc.chNext == '\r') { |
| 279 |
sc.Forward(); |
| 280 |
if (sc.ch == '\r' && sc.chNext == '\n') { |
| 281 |
sc.Forward(); |
| 282 |
} |
| 283 |
continue; |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
// Determine if the current state should terminate. |
| 288 |
if (sc.state == SCE_ASM_OPERATOR) { |
| 289 |
if (!IsAsmOperator(sc.ch)) { |
| 290 |
sc.SetState(SCE_ASM_DEFAULT); |
| 291 |
} |
| 292 |
} else if (sc.state == SCE_ASM_NUMBER) { |
| 293 |
if (!IsAWordChar(sc.ch)) { |
| 294 |
sc.SetState(SCE_ASM_DEFAULT); |
| 295 |
} |
| 296 |
} else if (sc.state == SCE_ASM_IDENTIFIER) { |
| 297 |
if (!IsAWordChar(sc.ch) ) { |
| 298 |
char s[100]; |
| 299 |
sc.GetCurrentLowered(s, sizeof(s)); |
| 300 |
bool IsDirective = false; |
| 301 |
|
| 302 |
if (cpuInstruction.InList(s)) { |
| 303 |
sc.ChangeState(SCE_ASM_CPUINSTRUCTION); |
| 304 |
} else if (mathInstruction.InList(s)) { |
| 305 |
sc.ChangeState(SCE_ASM_MATHINSTRUCTION); |
| 306 |
} else if (registers.InList(s)) { |
| 307 |
sc.ChangeState(SCE_ASM_REGISTER); |
| 308 |
} else if (directive.InList(s)) { |
| 309 |
sc.ChangeState(SCE_ASM_DIRECTIVE); |
| 310 |
IsDirective = true; |
| 311 |
} else if (directiveOperand.InList(s)) { |
| 312 |
sc.ChangeState(SCE_ASM_DIRECTIVEOPERAND); |
| 313 |
} else if (extInstruction.InList(s)) { |
| 314 |
sc.ChangeState(SCE_ASM_EXTINSTRUCTION); |
| 315 |
} |
| 316 |
sc.SetState(SCE_ASM_DEFAULT); |
| 317 |
if (IsDirective && !strcmp(s, "comment")) { |
| 318 |
const char delimiter = options.delimiter.empty() ? '~' : options.delimiter.c_str()[0]; |
| 319 |
while (IsASpaceOrTab(sc.ch) && !sc.atLineEnd) { |
| 320 |
sc.ForwardSetState(SCE_ASM_DEFAULT); |
| 321 |
} |
| 322 |
if (sc.ch == delimiter) { |
| 323 |
sc.SetState(SCE_ASM_COMMENTDIRECTIVE); |
| 324 |
} |
| 325 |
} |
| 326 |
} |
| 327 |
} else if (sc.state == SCE_ASM_COMMENTDIRECTIVE) { |
| 328 |
const char delimiter = options.delimiter.empty() ? '~' : options.delimiter.c_str()[0]; |
| 329 |
if (sc.ch == delimiter) { |
| 330 |
while (!sc.MatchLineEnd()) { |
| 331 |
sc.Forward(); |
| 332 |
} |
| 333 |
sc.SetState(SCE_ASM_DEFAULT); |
| 334 |
} |
| 335 |
} else if (sc.state == SCE_ASM_STRING) { |
| 336 |
if (sc.ch == '\\') { |
| 337 |
if (sc.chNext == '\"' || sc.chNext == '\'' || sc.chNext == '\\') { |
| 338 |
sc.Forward(); |
| 339 |
} |
| 340 |
} else if (sc.ch == '\"') { |
| 341 |
sc.ForwardSetState(SCE_ASM_DEFAULT); |
| 342 |
} else if (sc.atLineEnd) { |
| 343 |
sc.ChangeState(SCE_ASM_STRINGEOL); |
| 344 |
sc.ForwardSetState(SCE_ASM_DEFAULT); |
| 345 |
} |
| 346 |
} else if (sc.state == SCE_ASM_CHARACTER) { |
| 347 |
if (sc.ch == '\\') { |
| 348 |
if (sc.chNext == '\"' || sc.chNext == '\'' || sc.chNext == '\\') { |
| 349 |
sc.Forward(); |
| 350 |
} |
| 351 |
} else if (sc.ch == '\'') { |
| 352 |
sc.ForwardSetState(SCE_ASM_DEFAULT); |
| 353 |
} else if (sc.atLineEnd) { |
| 354 |
sc.ChangeState(SCE_ASM_STRINGEOL); |
| 355 |
sc.ForwardSetState(SCE_ASM_DEFAULT); |
| 356 |
} |
| 357 |
} |
| 358 |
|
| 359 |
// Determine if a new state should be entered. |
| 360 |
if (sc.state == SCE_ASM_DEFAULT) { |
| 361 |
if (sc.ch == commentCharacter) { |
| 362 |
sc.SetState(SCE_ASM_COMMENT); |
| 363 |
} else if (IsASCII(sc.ch) && (isdigit(sc.ch) || (sc.ch == '.' && IsASCII(sc.chNext) && isdigit(sc.chNext)))) { |
| 364 |
sc.SetState(SCE_ASM_NUMBER); |
| 365 |
} else if (IsAWordStart(sc.ch)) { |
| 366 |
sc.SetState(SCE_ASM_IDENTIFIER); |
| 367 |
} else if (sc.ch == '\"') { |
| 368 |
sc.SetState(SCE_ASM_STRING); |
| 369 |
} else if (sc.ch == '\'') { |
| 370 |
sc.SetState(SCE_ASM_CHARACTER); |
| 371 |
} else if (IsAsmOperator(sc.ch)) { |
| 372 |
sc.SetState(SCE_ASM_OPERATOR); |
| 373 |
} |
| 374 |
} |
| 375 |
|
| 376 |
} |
| 377 |
sc.Complete(); |
| 378 |
} |
| 379 |
|
| 380 |
// Store both the current line's fold level and the next lines in the |
| 381 |
// level store to make it easy to pick up with each increment |
| 382 |
// and to make it possible to fiddle the current level for "else". |
| 383 |
|
| 384 |
void SCI_METHOD LexerAsm::Fold(Sci_PositionU startPos, Sci_Position length, int initStyle, IDocument *pAccess) { |
| 385 |
|
| 386 |
if (!options.fold) |
| 387 |
return; |
| 388 |
|
| 389 |
LexAccessor styler(pAccess); |
| 390 |
|
| 391 |
const Sci_PositionU endPos = startPos + length; |
| 392 |
int visibleChars = 0; |
| 393 |
Sci_Position lineCurrent = styler.GetLine(startPos); |
| 394 |
int levelCurrent = SC_FOLDLEVELBASE; |
| 395 |
if (lineCurrent > 0) |
| 396 |
levelCurrent = styler.LevelAt(lineCurrent-1) >> 16; |
| 397 |
int levelNext = levelCurrent; |
| 398 |
char chNext = styler[startPos]; |
| 399 |
int styleNext = styler.StyleAt(startPos); |
| 400 |
int style = initStyle; |
| 401 |
char word[100]{}; |
| 402 |
int wordlen = 0; |
| 403 |
const bool userDefinedFoldMarkers = !options.foldExplicitStart.empty() && !options.foldExplicitEnd.empty(); |
| 404 |
for (Sci_PositionU i = startPos; i < endPos; i++) { |
| 405 |
const char ch = chNext; |
| 406 |
chNext = styler.SafeGetCharAt(i + 1); |
| 407 |
const int stylePrev = style; |
| 408 |
style = styleNext; |
| 409 |
styleNext = styler.StyleAt(i + 1); |
| 410 |
const bool atEOL = (ch == '\r' && chNext != '\n') || (ch == '\n'); |
| 411 |
if (options.foldCommentMultiline && IsStreamCommentStyle(style)) { |
| 412 |
if (!IsStreamCommentStyle(stylePrev)) { |
| 413 |
levelNext++; |
| 414 |
} else if (!IsStreamCommentStyle(styleNext) && !atEOL) { |
| 415 |
// Comments don't end at end of line and the next character may be unstyled. |
| 416 |
levelNext--; |
| 417 |
} |
| 418 |
} |
| 419 |
if (options.foldCommentExplicit && ((style == SCE_ASM_COMMENT) || options.foldExplicitAnywhere)) { |
| 420 |
if (userDefinedFoldMarkers) { |
| 421 |
if (styler.Match(i, options.foldExplicitStart.c_str())) { |
| 422 |
levelNext++; |
| 423 |
} else if (styler.Match(i, options.foldExplicitEnd.c_str())) { |
| 424 |
levelNext--; |
| 425 |
} |
| 426 |
} else { |
| 427 |
if (ch == ';') { |
| 428 |
if (chNext == '{') { |
| 429 |
levelNext++; |
| 430 |
} else if (chNext == '}') { |
| 431 |
levelNext--; |
| 432 |
} |
| 433 |
} |
| 434 |
} |
| 435 |
} |
| 436 |
if (options.foldSyntaxBased && (style == SCE_ASM_DIRECTIVE)) { |
| 437 |
word[wordlen++] = MakeLowerCase(ch); |
| 438 |
if (wordlen == 100) { // prevent overflow |
| 439 |
word[0] = '\0'; |
| 440 |
wordlen = 1; |
| 441 |
} |
| 442 |
if (styleNext != SCE_ASM_DIRECTIVE) { // reading directive ready |
| 443 |
word[wordlen] = '\0'; |
| 444 |
wordlen = 0; |
| 445 |
if (directives4foldstart.InList(word)) { |
| 446 |
levelNext++; |
| 447 |
} else if (directives4foldend.InList(word)){ |
| 448 |
levelNext--; |
| 449 |
} |
| 450 |
} |
| 451 |
} |
| 452 |
if (!IsASpace(ch)) |
| 453 |
visibleChars++; |
| 454 |
if (atEOL || (i == endPos-1)) { |
| 455 |
const int levelUse = levelCurrent; |
| 456 |
int lev = levelUse | levelNext << 16; |
| 457 |
if (visibleChars == 0 && options.foldCompact) |
| 458 |
lev |= SC_FOLDLEVELWHITEFLAG; |
| 459 |
if (levelUse < levelNext) |
| 460 |
lev |= SC_FOLDLEVELHEADERFLAG; |
| 461 |
if (lev != styler.LevelAt(lineCurrent)) { |
| 462 |
styler.SetLevel(lineCurrent, lev); |
| 463 |
} |
| 464 |
lineCurrent++; |
| 465 |
levelCurrent = levelNext; |
| 466 |
if (atEOL && (i == static_cast<Sci_PositionU>(styler.Length() - 1))) { |
| 467 |
// There is an empty line at end of file so give it same level and empty |
| 468 |
styler.SetLevel(lineCurrent, (levelCurrent | levelCurrent << 16) | SC_FOLDLEVELWHITEFLAG); |
| 469 |
} |
| 470 |
visibleChars = 0; |
| 471 |
} |
| 472 |
} |
| 473 |
} |
| 474 |
|
| 475 |
} |
| 476 |
|
| 477 |
extern const LexerModule lmAsm(SCLEX_ASM, LexerAsm::LexerFactoryAsm, "asm", asmWordListDesc); |
| 478 |
extern const LexerModule lmAs(SCLEX_AS, LexerAsm::LexerFactoryAs, "as", asmWordListDesc); |
| 479 |
|