| 1 |
// Scintilla source code edit control |
| 2 |
/** @file LexSmalltalk.cxx |
| 3 |
** Lexer for Smalltalk language. |
| 4 |
** Written by Sergey Philippov, sphilippov-at-gmail-dot-com |
| 5 |
**/ |
| 6 |
// Copyright 1998-2002 by Neil Hodgson <[email protected]> |
| 7 |
// The License.txt file describes the conditions under which this software may be distributed. |
| 8 |
|
| 9 |
#include <stdlib.h> |
| 10 |
#include <string.h> |
| 11 |
#include <stdio.h> |
| 12 |
#include <stdarg.h> |
| 13 |
#include <assert.h> |
| 14 |
#include <ctype.h> |
| 15 |
|
| 16 |
#include <string> |
| 17 |
#include <string_view> |
| 18 |
|
| 19 |
#include "ILexer.h" |
| 20 |
#include "Scintilla.h" |
| 21 |
#include "SciLexer.h" |
| 22 |
|
| 23 |
#include "WordList.h" |
| 24 |
#include "LexAccessor.h" |
| 25 |
#include "Accessor.h" |
| 26 |
#include "StyleContext.h" |
| 27 |
#include "CharacterSet.h" |
| 28 |
#include "LexerModule.h" |
| 29 |
|
| 30 |
using namespace Lexilla; |
| 31 |
|
| 32 |
/* |
| 33 |
| lexTable classificationBlock charClasses | |
| 34 |
charClasses := #(#DecDigit #Letter #Special #Upper #BinSel). |
| 35 |
lexTable := ByteArray new: 128. |
| 36 |
classificationBlock := [ :charClass :chars | |
| 37 |
| flag | |
| 38 |
flag := 1 bitShift: (charClasses indexOf: charClass) - 1. |
| 39 |
chars do: [ :char | lexTable at: char codePoint + 1 put: ((lexTable at: char codePoint + 1) bitOr: flag)]]. |
| 40 |
|
| 41 |
classificationBlock |
| 42 |
value: #DecDigit value: '0123456789'; |
| 43 |
value: #Letter value: '_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
| 44 |
value: #Special value: '()[]{};.^:'; |
| 45 |
value: #BinSel value: '~@%&*-+=|\/,<>?!'; |
| 46 |
value: #Upper value: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'. |
| 47 |
|
| 48 |
((String new: 500) streamContents: [ :stream | |
| 49 |
stream crLf; nextPutAll: 'static int ClassificationTable[256] = {'. |
| 50 |
lexTable keysAndValuesDo: [ :index :value | |
| 51 |
((index - 1) rem: 16) == 0 ifTrue: [ |
| 52 |
stream crLf; tab] |
| 53 |
ifFalse: [ |
| 54 |
stream space]. |
| 55 |
stream print: value. |
| 56 |
index ~= 256 ifTrue: [ |
| 57 |
stream nextPut: $,]]. |
| 58 |
stream crLf; nextPutAll: '};'; crLf. |
| 59 |
|
| 60 |
charClasses keysAndValuesDo: [ :index :name | |
| 61 |
stream |
| 62 |
crLf; |
| 63 |
nextPutAll: ( |
| 64 |
('static inline bool is<1s>(int ch) {return (ch > 0) && (ch %< 0x80) && ((ClassificationTable[ch] & <2p>) != 0);}') |
| 65 |
expandMacrosWith: name with: (1 bitShift: (index - 1))) |
| 66 |
]]) edit |
| 67 |
*/ |
| 68 |
|
| 69 |
// autogenerated {{{{ |
| 70 |
|
| 71 |
static int ClassificationTable[256] = { |
| 72 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 73 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 74 |
0, 16, 0, 0, 0, 16, 16, 0, 4, 4, 16, 16, 16, 16, 4, 16, |
| 75 |
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 4, 4, 16, 16, 16, 16, |
| 76 |
16, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, |
| 77 |
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 4, 16, 4, 4, 2, |
| 78 |
0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 79 |
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 4, 16, 4, 16, 0, |
| 80 |
}; |
| 81 |
|
| 82 |
static inline bool isDecDigit(int ch) {return (ch > 0) && (ch < 0x80) && ((ClassificationTable[ch] & 1) != 0);} |
| 83 |
static inline bool isLetter(int ch) {return (ch > 0) && (ch < 0x80) && ((ClassificationTable[ch] & 2) != 0);} |
| 84 |
static inline bool isSpecial(int ch) {return (ch > 0) && (ch < 0x80) && ((ClassificationTable[ch] & 4) != 0);} |
| 85 |
static inline bool isUpper(int ch) {return (ch > 0) && (ch < 0x80) && ((ClassificationTable[ch] & 8) != 0);} |
| 86 |
static inline bool isBinSel(int ch) {return (ch > 0) && (ch < 0x80) && ((ClassificationTable[ch] & 16) != 0);} |
| 87 |
// autogenerated }}}} |
| 88 |
|
| 89 |
static inline bool isAlphaNumeric(int ch) { |
| 90 |
return isDecDigit(ch) || isLetter(ch); |
| 91 |
} |
| 92 |
|
| 93 |
static inline bool isDigitOfRadix(int ch, int radix) |
| 94 |
{ |
| 95 |
if (isDecDigit(ch)) |
| 96 |
return (ch - '0') < radix; |
| 97 |
else if (!isUpper(ch)) |
| 98 |
return false; |
| 99 |
else |
| 100 |
return (ch - 'A' + 10) < radix; |
| 101 |
} |
| 102 |
|
| 103 |
static inline void skipComment(StyleContext& sc) |
| 104 |
{ |
| 105 |
while (sc.More() && sc.ch != '\"') |
| 106 |
sc.Forward(); |
| 107 |
} |
| 108 |
|
| 109 |
static inline void skipString(StyleContext& sc) |
| 110 |
{ |
| 111 |
while (sc.More()) { |
| 112 |
if (sc.ch == '\'') { |
| 113 |
if (sc.chNext != '\'') |
| 114 |
return; |
| 115 |
sc.Forward(); |
| 116 |
} |
| 117 |
sc.Forward(); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
static void handleHash(StyleContext& sc) |
| 122 |
{ |
| 123 |
if (isSpecial(sc.chNext)) { |
| 124 |
sc.SetState(SCE_ST_SPECIAL); |
| 125 |
return; |
| 126 |
} |
| 127 |
|
| 128 |
sc.SetState(SCE_ST_SYMBOL); |
| 129 |
sc.Forward(); |
| 130 |
if (sc.ch == '\'') { |
| 131 |
sc.Forward(); |
| 132 |
skipString(sc); |
| 133 |
} |
| 134 |
else { |
| 135 |
if (isLetter(sc.ch)) { |
| 136 |
while (isAlphaNumeric(sc.chNext) || sc.chNext == ':') |
| 137 |
sc.Forward(); |
| 138 |
} |
| 139 |
else if (isBinSel(sc.ch)) { |
| 140 |
while (isBinSel(sc.chNext)) |
| 141 |
sc.Forward(); |
| 142 |
} |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
static inline void handleSpecial(StyleContext& sc) |
| 147 |
{ |
| 148 |
if (sc.ch == ':' && sc.chNext == '=') { |
| 149 |
sc.SetState(SCE_ST_ASSIGN); |
| 150 |
sc.Forward(); |
| 151 |
} |
| 152 |
else { |
| 153 |
if (sc.ch == '^') |
| 154 |
sc.SetState(SCE_ST_RETURN); |
| 155 |
else |
| 156 |
sc.SetState(SCE_ST_SPECIAL); |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
static inline void skipInt(StyleContext& sc, int radix) |
| 161 |
{ |
| 162 |
while (isDigitOfRadix(sc.chNext, radix)) |
| 163 |
sc.Forward(); |
| 164 |
} |
| 165 |
|
| 166 |
static void handleNumeric(StyleContext& sc) |
| 167 |
{ |
| 168 |
char num[256]; |
| 169 |
int nl; |
| 170 |
int radix; |
| 171 |
|
| 172 |
sc.SetState(SCE_ST_NUMBER); |
| 173 |
num[0] = static_cast<char>(sc.ch); |
| 174 |
nl = 1; |
| 175 |
while (isDecDigit(sc.chNext)) { |
| 176 |
num[nl++] = static_cast<char>(sc.chNext); |
| 177 |
sc.Forward(); |
| 178 |
if (nl+1 == sizeof(num)/sizeof(num[0])) // overrun check |
| 179 |
break; |
| 180 |
} |
| 181 |
if (sc.chNext == 'r') { |
| 182 |
num[nl] = 0; |
| 183 |
if (num[0] == '-') |
| 184 |
radix = atoi(num + 1); |
| 185 |
else |
| 186 |
radix = atoi(num); |
| 187 |
sc.Forward(); |
| 188 |
if (sc.chNext == '-') |
| 189 |
sc.Forward(); |
| 190 |
skipInt(sc, radix); |
| 191 |
} |
| 192 |
else |
| 193 |
radix = 10; |
| 194 |
if (sc.chNext == '.') { |
| 195 |
if (!isDigitOfRadix(sc.GetRelative(2), radix)) |
| 196 |
return; |
| 197 |
sc.Forward(); |
| 198 |
skipInt(sc, radix); |
| 199 |
} |
| 200 |
|
| 201 |
if (sc.chNext == 's') { |
| 202 |
// ScaledDecimal |
| 203 |
sc.Forward(); |
| 204 |
while (isDecDigit(sc.chNext)) |
| 205 |
sc.Forward(); |
| 206 |
return; |
| 207 |
} |
| 208 |
else if (sc.chNext != 'e' && sc.chNext != 'd' && sc.chNext != 'q') |
| 209 |
return; |
| 210 |
sc.Forward(); |
| 211 |
if (sc.chNext == '+' || sc.chNext == '-') |
| 212 |
sc.Forward(); |
| 213 |
skipInt(sc, radix); |
| 214 |
} |
| 215 |
|
| 216 |
static inline void handleBinSel(StyleContext& sc) |
| 217 |
{ |
| 218 |
sc.SetState(SCE_ST_BINARY); |
| 219 |
while (isBinSel(sc.chNext)) |
| 220 |
sc.Forward(); |
| 221 |
} |
| 222 |
|
| 223 |
static void handleLetter(StyleContext& sc, WordList* specialSelectorList) |
| 224 |
{ |
| 225 |
char ident[256]; |
| 226 |
int il; |
| 227 |
int state; |
| 228 |
bool doubleColonPresent; |
| 229 |
|
| 230 |
sc.SetState(SCE_ST_DEFAULT); |
| 231 |
|
| 232 |
ident[0] = static_cast<char>(sc.ch); |
| 233 |
il = 1; |
| 234 |
while (isAlphaNumeric(sc.chNext)) { |
| 235 |
ident[il++] = static_cast<char>(sc.chNext); |
| 236 |
sc.Forward(); |
| 237 |
if (il+2 == sizeof(ident)/sizeof(ident[0])) // overrun check |
| 238 |
break; |
| 239 |
} |
| 240 |
|
| 241 |
if (sc.chNext == ':') { |
| 242 |
doubleColonPresent = true; |
| 243 |
ident[il++] = ':'; |
| 244 |
sc.Forward(); |
| 245 |
} |
| 246 |
else |
| 247 |
doubleColonPresent = false; |
| 248 |
ident[il] = 0; |
| 249 |
|
| 250 |
if (specialSelectorList->InList(ident)) |
| 251 |
state = SCE_ST_SPEC_SEL; |
| 252 |
else if (doubleColonPresent) |
| 253 |
state = SCE_ST_KWSEND; |
| 254 |
else if (isUpper(ident[0])) |
| 255 |
state = SCE_ST_GLOBAL; |
| 256 |
else { |
| 257 |
if (!strcmp(ident, "self")) |
| 258 |
state = SCE_ST_SELF; |
| 259 |
else if (!strcmp(ident, "super")) |
| 260 |
state = SCE_ST_SUPER; |
| 261 |
else if (!strcmp(ident, "nil")) |
| 262 |
state = SCE_ST_NIL; |
| 263 |
else if (!strcmp(ident, "true") || !strcmp(ident, "false")) |
| 264 |
state = SCE_ST_BOOL; |
| 265 |
else |
| 266 |
state = SCE_ST_DEFAULT; |
| 267 |
} |
| 268 |
|
| 269 |
sc.ChangeState(state); |
| 270 |
} |
| 271 |
|
| 272 |
static void colorizeSmalltalkDoc(Sci_PositionU startPos, Sci_Position length, int initStyle, WordList *wordLists[], Accessor &styler) |
| 273 |
{ |
| 274 |
StyleContext sc(startPos, length, initStyle, styler); |
| 275 |
|
| 276 |
if (initStyle == SCE_ST_COMMENT) { |
| 277 |
skipComment(sc); |
| 278 |
if (sc.More()) |
| 279 |
sc.Forward(); |
| 280 |
} |
| 281 |
else if (initStyle == SCE_ST_STRING) { |
| 282 |
skipString(sc); |
| 283 |
if (sc.More()) |
| 284 |
sc.Forward(); |
| 285 |
} |
| 286 |
|
| 287 |
for (; sc.More(); sc.Forward()) { |
| 288 |
int ch; |
| 289 |
|
| 290 |
ch = sc.ch; |
| 291 |
if (ch == '\"') { |
| 292 |
sc.SetState(SCE_ST_COMMENT); |
| 293 |
sc.Forward(); |
| 294 |
skipComment(sc); |
| 295 |
} |
| 296 |
else if (ch == '\'') { |
| 297 |
sc.SetState(SCE_ST_STRING); |
| 298 |
sc.Forward(); |
| 299 |
skipString(sc); |
| 300 |
} |
| 301 |
else if (ch == '#') |
| 302 |
handleHash(sc); |
| 303 |
else if (ch == '$') { |
| 304 |
sc.SetState(SCE_ST_CHARACTER); |
| 305 |
sc.Forward(); |
| 306 |
} |
| 307 |
else if (isSpecial(ch)) |
| 308 |
handleSpecial(sc); |
| 309 |
else if (isDecDigit(ch)) |
| 310 |
handleNumeric(sc); |
| 311 |
else if (isLetter(ch)) |
| 312 |
handleLetter(sc, wordLists[0]); |
| 313 |
else if (isBinSel(ch)) { |
| 314 |
if (ch == '-' && isDecDigit(sc.chNext)) |
| 315 |
handleNumeric(sc); |
| 316 |
else |
| 317 |
handleBinSel(sc); |
| 318 |
} |
| 319 |
else |
| 320 |
sc.SetState(SCE_ST_DEFAULT); |
| 321 |
} |
| 322 |
sc.Complete(); |
| 323 |
} |
| 324 |
|
| 325 |
static const char* const smalltalkWordListDesc[] = { |
| 326 |
"Special selectors", |
| 327 |
0 |
| 328 |
}; |
| 329 |
|
| 330 |
extern const LexerModule lmSmalltalk(SCLEX_SMALLTALK, colorizeSmalltalkDoc, "smalltalk", NULL, smalltalkWordListDesc); |