| 1 |
// Scintilla source code edit control |
| 2 |
// Encoding: UTF-8 |
| 3 |
/** @file LexJulia.cxx |
| 4 |
** Lexer for Julia. |
| 5 |
** Reusing code from LexMatlab, LexPython and LexRust |
| 6 |
** |
| 7 |
** Written by Bertrand Lacoste |
| 8 |
** |
| 9 |
**/ |
| 10 |
// Copyright 1998-2001 by Neil Hodgson <[email protected]> |
| 11 |
// The License.txt file describes the conditions under which this software may be distributed. |
| 12 |
|
| 13 |
#include <cstdlib> |
| 14 |
#include <cassert> |
| 15 |
#include <cstring> |
| 16 |
|
| 17 |
#include <string> |
| 18 |
#include <string_view> |
| 19 |
#include <vector> |
| 20 |
#include <map> |
| 21 |
#include <algorithm> |
| 22 |
#include <functional> |
| 23 |
|
| 24 |
#include "ILexer.h" |
| 25 |
#include "Scintilla.h" |
| 26 |
#include "SciLexer.h" |
| 27 |
|
| 28 |
#include "StringCopy.h" |
| 29 |
#include "PropSetSimple.h" |
| 30 |
#include "WordList.h" |
| 31 |
#include "LexAccessor.h" |
| 32 |
#include "Accessor.h" |
| 33 |
#include "StyleContext.h" |
| 34 |
#include "CharacterSet.h" |
| 35 |
#include "CharacterCategory.h" |
| 36 |
#include "LexerModule.h" |
| 37 |
#include "OptionSet.h" |
| 38 |
#include "DefaultLexer.h" |
| 39 |
|
| 40 |
using namespace Scintilla; |
| 41 |
using namespace Lexilla; |
| 42 |
|
| 43 |
static const int MAX_JULIA_IDENT_CHARS = 1023; |
| 44 |
|
| 45 |
// Options used for LexerJulia |
| 46 |
struct OptionsJulia { |
| 47 |
bool fold; |
| 48 |
bool foldComment; |
| 49 |
bool foldCompact; |
| 50 |
bool foldDocstring; |
| 51 |
bool foldSyntaxBased; |
| 52 |
bool highlightTypeannotation; |
| 53 |
bool highlightLexerror; |
| 54 |
OptionsJulia() { |
| 55 |
fold = true; |
| 56 |
foldComment = true; |
| 57 |
foldCompact = false; |
| 58 |
foldDocstring = true; |
| 59 |
foldSyntaxBased = true; |
| 60 |
highlightTypeannotation = false; |
| 61 |
highlightLexerror = false; |
| 62 |
} |
| 63 |
}; |
| 64 |
|
| 65 |
const char * const juliaWordLists[] = { |
| 66 |
"Primary keywords and identifiers", |
| 67 |
"Built in types", |
| 68 |
"Other keywords", |
| 69 |
"Built in functions", |
| 70 |
0, |
| 71 |
}; |
| 72 |
|
| 73 |
struct OptionSetJulia : public OptionSet<OptionsJulia> { |
| 74 |
OptionSetJulia() { |
| 75 |
DefineProperty("fold", &OptionsJulia::fold); |
| 76 |
|
| 77 |
DefineProperty("fold.compact", &OptionsJulia::foldCompact); |
| 78 |
|
| 79 |
DefineProperty("fold.comment", &OptionsJulia::foldComment); |
| 80 |
|
| 81 |
DefineProperty("fold.julia.docstring", &OptionsJulia::foldDocstring, |
| 82 |
"Fold multiline triple-doublequote strings, usually used to document a function or type above the definition."); |
| 83 |
|
| 84 |
DefineProperty("fold.julia.syntax.based", &OptionsJulia::foldSyntaxBased, |
| 85 |
"Set this property to 0 to disable syntax based folding."); |
| 86 |
|
| 87 |
DefineProperty("lexer.julia.highlight.typeannotation", &OptionsJulia::highlightTypeannotation, |
| 88 |
"This option enables highlighting of the type identifier after `::`."); |
| 89 |
|
| 90 |
DefineProperty("lexer.julia.highlight.lexerror", &OptionsJulia::highlightLexerror, |
| 91 |
"This option enables highlighting of syntax error int character or number definition."); |
| 92 |
|
| 93 |
DefineWordListSets(juliaWordLists); |
| 94 |
} |
| 95 |
}; |
| 96 |
|
| 97 |
LexicalClass juliaLexicalClasses[] = { |
| 98 |
// Lexer Julia SCLEX_JULIA SCE_JULIA_: |
| 99 |
0, "SCE_JULIA_DEFAULT", "default", "White space", |
| 100 |
1, "SCE_JULIA_COMMENT", "comment", "Comment", |
| 101 |
2, "SCE_JULIA_NUMBER", "literal numeric", "Number", |
| 102 |
3, "SCE_JULIA_KEYWORD1", "keyword", "Reserved keywords", |
| 103 |
4, "SCE_JULIA_KEYWORD2", "identifier", "Builtin type names", |
| 104 |
5, "SCE_JULIA_KEYWORD3", "identifier", "Constants", |
| 105 |
6, "SCE_JULIA_CHAR", "literal string character", "Single quoted string", |
| 106 |
7, "SCE_JULIA_OPERATOR", "operator", "Operator", |
| 107 |
8, "SCE_JULIA_BRACKET", "bracket operator", "Bracket operator", |
| 108 |
9, "SCE_JULIA_IDENTIFIER", "identifier", "Identifier", |
| 109 |
10, "SCE_JULIA_STRING", "literal string", "Double quoted String", |
| 110 |
11, "SCE_JULIA_SYMBOL", "literal string symbol", "Symbol", |
| 111 |
12, "SCE_JULIA_MACRO", "macro preprocessor", "Macro", |
| 112 |
13, "SCE_JULIA_STRINGINTERP", "literal string interpolated", "String interpolation", |
| 113 |
14, "SCE_JULIA_DOCSTRING", "literal string documentation", "Docstring", |
| 114 |
15, "SCE_JULIA_STRINGLITERAL", "literal string", "String literal prefix", |
| 115 |
16, "SCE_JULIA_COMMAND", "literal string command", "Command", |
| 116 |
17, "SCE_JULIA_COMMANDLITERAL", "literal string command", "Command literal prefix", |
| 117 |
18, "SCE_JULIA_TYPEANNOT", "identifier type", "Type annotation identifier", |
| 118 |
19, "SCE_JULIA_LEXERROR", "lexer error", "Lexing error", |
| 119 |
20, "SCE_JULIA_KEYWORD4", "identifier", "Builtin function names", |
| 120 |
21, "SCE_JULIA_TYPEOPERATOR", "operator type", "Type annotation operator", |
| 121 |
}; |
| 122 |
|
| 123 |
class LexerJulia : public DefaultLexer { |
| 124 |
WordList keywords; |
| 125 |
WordList identifiers2; |
| 126 |
WordList identifiers3; |
| 127 |
WordList identifiers4; |
| 128 |
OptionsJulia options; |
| 129 |
OptionSetJulia osJulia; |
| 130 |
public: |
| 131 |
explicit LexerJulia() : |
| 132 |
DefaultLexer("julia", SCLEX_JULIA, juliaLexicalClasses, ELEMENTS(juliaLexicalClasses)) { |
| 133 |
} |
| 134 |
virtual ~LexerJulia() { |
| 135 |
} |
| 136 |
void SCI_METHOD Release() override { |
| 137 |
delete this; |
| 138 |
} |
| 139 |
int SCI_METHOD Version() const override { |
| 140 |
return lvRelease5; |
| 141 |
} |
| 142 |
const char * SCI_METHOD PropertyNames() override { |
| 143 |
return osJulia.PropertyNames(); |
| 144 |
} |
| 145 |
int SCI_METHOD PropertyType(const char *name) override { |
| 146 |
return osJulia.PropertyType(name); |
| 147 |
} |
| 148 |
const char * SCI_METHOD DescribeProperty(const char *name) override { |
| 149 |
return osJulia.DescribeProperty(name); |
| 150 |
} |
| 151 |
Sci_Position SCI_METHOD PropertySet(const char *key, const char *val) override; |
| 152 |
const char * SCI_METHOD PropertyGet(const char *key) override { |
| 153 |
return osJulia.PropertyGet(key); |
| 154 |
} |
| 155 |
const char * SCI_METHOD DescribeWordListSets() override { |
| 156 |
return osJulia.DescribeWordListSets(); |
| 157 |
} |
| 158 |
Sci_Position SCI_METHOD WordListSet(int n, const char *wl) override; |
| 159 |
void SCI_METHOD Lex(Sci_PositionU startPos, Sci_Position length, int initStyle, IDocument *pAccess) override; |
| 160 |
void SCI_METHOD Fold(Sci_PositionU startPos, Sci_Position length, int initStyle, IDocument *pAccess) override; |
| 161 |
void * SCI_METHOD PrivateCall(int, void *) override { |
| 162 |
return 0; |
| 163 |
} |
| 164 |
|
| 165 |
static ILexer5 *LexerFactoryJulia() { |
| 166 |
return new LexerJulia(); |
| 167 |
} |
| 168 |
}; |
| 169 |
|
| 170 |
Sci_Position SCI_METHOD LexerJulia::PropertySet(const char *key, const char *val) { |
| 171 |
if (osJulia.PropertySet(&options, key, val)) { |
| 172 |
return 0; |
| 173 |
} |
| 174 |
return -1; |
| 175 |
} |
| 176 |
|
| 177 |
Sci_Position SCI_METHOD LexerJulia::WordListSet(int n, const char *wl) { |
| 178 |
WordList *wordListN = nullptr; |
| 179 |
switch (n) { |
| 180 |
case 0: |
| 181 |
wordListN = &keywords; |
| 182 |
break; |
| 183 |
case 1: |
| 184 |
wordListN = &identifiers2; |
| 185 |
break; |
| 186 |
case 2: |
| 187 |
wordListN = &identifiers3; |
| 188 |
break; |
| 189 |
case 3: |
| 190 |
wordListN = &identifiers4; |
| 191 |
break; |
| 192 |
} |
| 193 |
Sci_Position firstModification = -1; |
| 194 |
if (wordListN) { |
| 195 |
if (wordListN->Set(wl)) { |
| 196 |
firstModification = 0; |
| 197 |
} |
| 198 |
} |
| 199 |
return firstModification; |
| 200 |
} |
| 201 |
|
| 202 |
static inline bool IsJuliaOperator(int ch) { |
| 203 |
if (ch == '%' || ch == '^' || ch == '&' || ch == '*' || |
| 204 |
ch == '-' || ch == '+' || ch == '=' || ch == '|' || |
| 205 |
ch == '<' || ch == '>' || ch == '/' || ch == '~' || |
| 206 |
ch == '\\' ) { |
| 207 |
return true; |
| 208 |
} |
| 209 |
return false; |
| 210 |
} |
| 211 |
|
| 212 |
// The list contains non-ascii unary operators |
| 213 |
static inline bool IsJuliaUnaryOperator (int ch) { |
| 214 |
if (ch == 0x00ac || ch == 0x221a || ch == 0x221b || |
| 215 |
ch == 0x221c || ch == 0x22c6 || ch == 0x00b1 || |
| 216 |
ch == 0x2213 ) { |
| 217 |
return true; |
| 218 |
} |
| 219 |
return false; |
| 220 |
} |
| 221 |
|
| 222 |
static inline bool IsJuliaParen (int ch) { |
| 223 |
if (ch == '(' || ch == ')' || ch == '{' || ch == '}' || |
| 224 |
ch == '[' || ch == ']' ) { |
| 225 |
return true; |
| 226 |
} |
| 227 |
return false; |
| 228 |
} |
| 229 |
|
| 230 |
// Unicode parsing from Julia source code: |
| 231 |
// https://github.com/JuliaLang/julia/blob/master/src/flisp/julia_extensions.c |
| 232 |
// keep the same function name to be easy to find again |
| 233 |
static int is_wc_cat_id_start(uint32_t wc) { |
| 234 |
const CharacterCategory cat = CategoriseCharacter((int) wc); |
| 235 |
|
| 236 |
return (cat == ccLu || cat == ccLl || |
| 237 |
cat == ccLt || cat == ccLm || |
| 238 |
cat == ccLo || cat == ccNl || |
| 239 |
cat == ccSc || // allow currency symbols |
| 240 |
// other symbols, but not arrows or replacement characters |
| 241 |
(cat == ccSo && !(wc >= 0x2190 && wc <= 0x21FF) && |
| 242 |
wc != 0xfffc && wc != 0xfffd && |
| 243 |
wc != 0x233f && // notslash |
| 244 |
wc != 0x00a6) || // broken bar |
| 245 |
|
| 246 |
// math symbol (category Sm) whitelist |
| 247 |
(wc >= 0x2140 && wc <= 0x2a1c && |
| 248 |
((wc >= 0x2140 && wc <= 0x2144) || // ⅀, ⅁, ⅂, ⅃, ⅄ |
| 249 |
wc == 0x223f || wc == 0x22be || wc == 0x22bf || // ∿, ⊾, ⊿ |
| 250 |
wc == 0x22a4 || wc == 0x22a5 || // ⊤ ⊥ |
| 251 |
|
| 252 |
(wc >= 0x2200 && wc <= 0x2233 && |
| 253 |
(wc == 0x2202 || wc == 0x2205 || wc == 0x2206 || // ∂, ∅, ∆ |
| 254 |
wc == 0x2207 || wc == 0x220e || wc == 0x220f || // ∇, ∎, ∏ |
| 255 |
wc == 0x2200 || wc == 0x2203 || wc == 0x2204 || // ∀, ∃, ∄ |
| 256 |
wc == 0x2210 || wc == 0x2211 || // ∐, ∑ |
| 257 |
wc == 0x221e || wc == 0x221f || // ∞, ∟ |
| 258 |
wc >= 0x222b)) || // ∫, ∬, ∭, ∮, ∯, ∰, ∱, ∲, ∳ |
| 259 |
|
| 260 |
(wc >= 0x22c0 && wc <= 0x22c3) || // N-ary big ops: ⋀, ⋁, ⋂, ⋃ |
| 261 |
(wc >= 0x25F8 && wc <= 0x25ff) || // ◸, ◹, ◺, ◻, ◼, ◽, ◾, ◿ |
| 262 |
|
| 263 |
(wc >= 0x266f && |
| 264 |
(wc == 0x266f || wc == 0x27d8 || wc == 0x27d9 || // ♯, ⟘, ⟙ |
| 265 |
(wc >= 0x27c0 && wc <= 0x27c1) || // ⟀, ⟁ |
| 266 |
(wc >= 0x29b0 && wc <= 0x29b4) || // ⦰, ⦱, ⦲, ⦳, ⦴ |
| 267 |
(wc >= 0x2a00 && wc <= 0x2a06) || // ⨀, ⨁, ⨂, ⨃, ⨄, ⨅, ⨆ |
| 268 |
(wc >= 0x2a09 && wc <= 0x2a16) || // ⨉, ⨊, ⨋, ⨌, ⨍, ⨎, ⨏, ⨐, ⨑, ⨒, ⨓, ⨔, ⨕, ⨖ |
| 269 |
wc == 0x2a1b || wc == 0x2a1c)))) || // ⨛, ⨜ |
| 270 |
|
| 271 |
(wc >= 0x1d6c1 && // variants of \nabla and \partial |
| 272 |
(wc == 0x1d6c1 || wc == 0x1d6db || |
| 273 |
wc == 0x1d6fb || wc == 0x1d715 || |
| 274 |
wc == 0x1d735 || wc == 0x1d74f || |
| 275 |
wc == 0x1d76f || wc == 0x1d789 || |
| 276 |
wc == 0x1d7a9 || wc == 0x1d7c3)) || |
| 277 |
|
| 278 |
// super- and subscript +-=() |
| 279 |
(wc >= 0x207a && wc <= 0x207e) || |
| 280 |
(wc >= 0x208a && wc <= 0x208e) || |
| 281 |
|
| 282 |
// angle symbols |
| 283 |
(wc >= 0x2220 && wc <= 0x2222) || // ∠, ∡, ∢ |
| 284 |
(wc >= 0x299b && wc <= 0x29af) || // ⦛, ⦜, ⦝, ⦞, ⦟, ⦠, ⦡, ⦢, ⦣, ⦤, ⦥, ⦦, ⦧, ⦨, ⦩, ⦪, ⦫, ⦬, ⦭, ⦮, ⦯ |
| 285 |
|
| 286 |
// Other_ID_Start |
| 287 |
wc == 0x2118 || wc == 0x212E || // ℘, ℮ |
| 288 |
(wc >= 0x309B && wc <= 0x309C) || // katakana-hiragana sound marks |
| 289 |
|
| 290 |
// bold-digits and double-struck digits |
| 291 |
(wc >= 0x1D7CE && wc <= 0x1D7E1)); // 𝟎 through 𝟗 (inclusive), 𝟘 through 𝟡 (inclusive) |
| 292 |
} |
| 293 |
|
| 294 |
static inline bool IsIdentifierFirstCharacter (int ch) { |
| 295 |
if (IsASCII(ch)) { |
| 296 |
return (bool) (isalpha(ch) || ch == '_'); |
| 297 |
} |
| 298 |
if (ch < 0xA1 || ch > 0x10ffff) { |
| 299 |
return false; |
| 300 |
} |
| 301 |
|
| 302 |
return is_wc_cat_id_start((uint32_t) ch); |
| 303 |
} |
| 304 |
|
| 305 |
static inline bool IsIdentifierCharacter (int ch) { |
| 306 |
if (IsASCII(ch)) { |
| 307 |
return (bool) (isalnum(ch) || ch == '_' || ch == '!'); |
| 308 |
} |
| 309 |
if (ch < 0xA1 || ch > 0x10ffff) { |
| 310 |
return false; |
| 311 |
} |
| 312 |
|
| 313 |
if (is_wc_cat_id_start((uint32_t) ch)) { |
| 314 |
return true; |
| 315 |
} |
| 316 |
|
| 317 |
const CharacterCategory cat = CategoriseCharacter(ch); |
| 318 |
|
| 319 |
if (cat == ccMn || cat == ccMc || |
| 320 |
cat == ccNd || cat == ccPc || |
| 321 |
cat == ccSk || cat == ccMe || |
| 322 |
cat == ccNo || |
| 323 |
// primes (single, double, triple, their reverses, and quadruple) |
| 324 |
(ch >= 0x2032 && ch <= 0x2037) || (ch == 0x2057)) { |
| 325 |
return true; |
| 326 |
} |
| 327 |
return false; |
| 328 |
} |
| 329 |
|
| 330 |
// keep the same function name to be easy to find again |
| 331 |
static const uint32_t opsuffs[] = { |
| 332 |
0x00b2, // ² |
| 333 |
0x00b3, // ³ |
| 334 |
0x00b9, // ¹ |
| 335 |
0x02b0, // ʰ |
| 336 |
0x02b2, // ʲ |
| 337 |
0x02b3, // ʳ |
| 338 |
0x02b7, // ʷ |
| 339 |
0x02b8, // ʸ |
| 340 |
0x02e1, // ˡ |
| 341 |
0x02e2, // ˢ |
| 342 |
0x02e3, // ˣ |
| 343 |
0x1d2c, // ᴬ |
| 344 |
0x1d2e, // ᴮ |
| 345 |
0x1d30, // ᴰ |
| 346 |
0x1d31, // ᴱ |
| 347 |
0x1d33, // ᴳ |
| 348 |
0x1d34, // ᴴ |
| 349 |
0x1d35, // ᴵ |
| 350 |
0x1d36, // ᴶ |
| 351 |
0x1d37, // ᴷ |
| 352 |
0x1d38, // ᴸ |
| 353 |
0x1d39, // ᴹ |
| 354 |
0x1d3a, // ᴺ |
| 355 |
0x1d3c, // ᴼ |
| 356 |
0x1d3e, // ᴾ |
| 357 |
0x1d3f, // ᴿ |
| 358 |
0x1d40, // ᵀ |
| 359 |
0x1d41, // ᵁ |
| 360 |
0x1d42, // ᵂ |
| 361 |
0x1d43, // ᵃ |
| 362 |
0x1d47, // ᵇ |
| 363 |
0x1d48, // ᵈ |
| 364 |
0x1d49, // ᵉ |
| 365 |
0x1d4d, // ᵍ |
| 366 |
0x1d4f, // ᵏ |
| 367 |
0x1d50, // ᵐ |
| 368 |
0x1d52, // ᵒ |
| 369 |
0x1d56, // ᵖ |
| 370 |
0x1d57, // ᵗ |
| 371 |
0x1d58, // ᵘ |
| 372 |
0x1d5b, // ᵛ |
| 373 |
0x1d5d, // ᵝ |
| 374 |
0x1d5e, // ᵞ |
| 375 |
0x1d5f, // ᵟ |
| 376 |
0x1d60, // ᵠ |
| 377 |
0x1d61, // ᵡ |
| 378 |
0x1d62, // ᵢ |
| 379 |
0x1d63, // ᵣ |
| 380 |
0x1d64, // ᵤ |
| 381 |
0x1d65, // ᵥ |
| 382 |
0x1d66, // ᵦ |
| 383 |
0x1d67, // ᵧ |
| 384 |
0x1d68, // ᵨ |
| 385 |
0x1d69, // ᵩ |
| 386 |
0x1d6a, // ᵪ |
| 387 |
0x1d9c, // ᶜ |
| 388 |
0x1da0, // ᶠ |
| 389 |
0x1da5, // ᶥ |
| 390 |
0x1da6, // ᶦ |
| 391 |
0x1dab, // ᶫ |
| 392 |
0x1db0, // ᶰ |
| 393 |
0x1db8, // ᶸ |
| 394 |
0x1dbb, // ᶻ |
| 395 |
0x1dbf, // ᶿ |
| 396 |
0x2032, // ′ |
| 397 |
0x2033, // ″ |
| 398 |
0x2034, // ‴ |
| 399 |
0x2035, // ‵ |
| 400 |
0x2036, // ‶ |
| 401 |
0x2037, // ‷ |
| 402 |
0x2057, // ⁗ |
| 403 |
0x2070, // ⁰ |
| 404 |
0x2071, // ⁱ |
| 405 |
0x2074, // ⁴ |
| 406 |
0x2075, // ⁵ |
| 407 |
0x2076, // ⁶ |
| 408 |
0x2077, // ⁷ |
| 409 |
0x2078, // ⁸ |
| 410 |
0x2079, // ⁹ |
| 411 |
0x207a, // ⁺ |
| 412 |
0x207b, // ⁻ |
| 413 |
0x207c, // ⁼ |
| 414 |
0x207d, // ⁽ |
| 415 |
0x207e, // ⁾ |
| 416 |
0x207f, // ⁿ |
| 417 |
0x2080, // ₀ |
| 418 |
0x2081, // ₁ |
| 419 |
0x2082, // ₂ |
| 420 |
0x2083, // ₃ |
| 421 |
0x2084, // ₄ |
| 422 |
0x2085, // ₅ |
| 423 |
0x2086, // ₆ |
| 424 |
0x2087, // ₇ |
| 425 |
0x2088, // ₈ |
| 426 |
0x2089, // ₉ |
| 427 |
0x208a, // ₊ |
| 428 |
0x208b, // ₋ |
| 429 |
0x208c, // ₌ |
| 430 |
0x208d, // ₍ |
| 431 |
0x208e, // ₎ |
| 432 |
0x2090, // ₐ |
| 433 |
0x2091, // ₑ |
| 434 |
0x2092, // ₒ |
| 435 |
0x2093, // ₓ |
| 436 |
0x2095, // ₕ |
| 437 |
0x2096, // ₖ |
| 438 |
0x2097, // ₗ |
| 439 |
0x2098, // ₘ |
| 440 |
0x2099, // ₙ |
| 441 |
0x209a, // ₚ |
| 442 |
0x209b, // ₛ |
| 443 |
0x209c, // ₜ |
| 444 |
0x2c7c, // ⱼ |
| 445 |
0x2c7d, // ⱽ |
| 446 |
0xa71b, // ꜛ |
| 447 |
0xa71c, // ꜜ |
| 448 |
0xa71d // ꜝ |
| 449 |
}; |
| 450 |
static const size_t opsuffs_len = sizeof(opsuffs) / (sizeof(uint32_t)); |
| 451 |
|
| 452 |
// keep the same function name to be easy to find again |
| 453 |
static bool jl_op_suffix_char(uint32_t wc) { |
| 454 |
if (wc < 0xA1 || wc > 0x10ffff) { |
| 455 |
return false; |
| 456 |
} |
| 457 |
const CharacterCategory cat = CategoriseCharacter((int) wc); |
| 458 |
if (cat == ccMn || cat == ccMc || |
| 459 |
cat == ccMe) { |
| 460 |
return true; |
| 461 |
} |
| 462 |
|
| 463 |
for (size_t i = 0; i < opsuffs_len; ++i) { |
| 464 |
if (wc == opsuffs[i]) { |
| 465 |
return true; |
| 466 |
} |
| 467 |
} |
| 468 |
return false; |
| 469 |
} |
| 470 |
|
| 471 |
// keep the same function name to be easy to find again |
| 472 |
static bool never_id_char(uint32_t wc) { |
| 473 |
const CharacterCategory cat = CategoriseCharacter((int) wc); |
| 474 |
return ( |
| 475 |
// spaces and control characters: |
| 476 |
(cat >= ccZs && cat <= ccCs) || |
| 477 |
|
| 478 |
// ASCII and Latin1 non-connector punctuation |
| 479 |
(wc < 0xff && |
| 480 |
cat >= ccPd && cat <= ccPo) || |
| 481 |
|
| 482 |
wc == '`' || |
| 483 |
|
| 484 |
// mathematical brackets |
| 485 |
(wc >= 0x27e6 && wc <= 0x27ef) || |
| 486 |
// angle, corner, and lenticular brackets |
| 487 |
(wc >= 0x3008 && wc <= 0x3011) || |
| 488 |
// tortoise shell, square, and more lenticular brackets |
| 489 |
(wc >= 0x3014 && wc <= 0x301b) || |
| 490 |
// fullwidth parens |
| 491 |
(wc == 0xff08 || wc == 0xff09) || |
| 492 |
// fullwidth square brackets |
| 493 |
(wc == 0xff3b || wc == 0xff3d)); |
| 494 |
} |
| 495 |
|
| 496 |
|
| 497 |
static bool IsOperatorFirstCharacter (int ch) { |
| 498 |
if (IsASCII(ch)) { |
| 499 |
if (IsJuliaOperator(ch) || |
| 500 |
ch == '!' || ch == '?' || |
| 501 |
ch == ':' || ch == ';' || |
| 502 |
ch == ',' || ch == '.' ) { |
| 503 |
return true; |
| 504 |
}else { |
| 505 |
return false; |
| 506 |
} |
| 507 |
} else if (is_wc_cat_id_start((uint32_t) ch)) { |
| 508 |
return false; |
| 509 |
} else if (IsJuliaUnaryOperator(ch) || |
| 510 |
! never_id_char((uint32_t) ch)) { |
| 511 |
return true; |
| 512 |
} |
| 513 |
return false; |
| 514 |
} |
| 515 |
|
| 516 |
static bool IsOperatorCharacter (int ch) { |
| 517 |
if (IsOperatorFirstCharacter(ch) || |
| 518 |
(!IsASCII(ch) && jl_op_suffix_char((uint32_t) ch)) ) { |
| 519 |
return true; |
| 520 |
} |
| 521 |
return false; |
| 522 |
} |
| 523 |
|
| 524 |
static bool CheckBoundsIndexing(char *str) { |
| 525 |
if (strcmp("begin", str) == 0 || strcmp("end", str) == 0 ) { |
| 526 |
return true; |
| 527 |
} |
| 528 |
return false; |
| 529 |
} |
| 530 |
|
| 531 |
static int CheckKeywordFoldPoint(char *str) { |
| 532 |
if (strcmp ("if", str) == 0 || |
| 533 |
strcmp ("for", str) == 0 || |
| 534 |
strcmp ("while", str) == 0 || |
| 535 |
strcmp ("try", str) == 0 || |
| 536 |
strcmp ("do", str) == 0 || |
| 537 |
strcmp ("begin", str) == 0 || |
| 538 |
strcmp ("let", str) == 0 || |
| 539 |
strcmp ("baremodule", str) == 0 || |
| 540 |
strcmp ("quote", str) == 0 || |
| 541 |
strcmp ("module", str) == 0 || |
| 542 |
strcmp ("struct", str) == 0 || |
| 543 |
strcmp ("type", str) == 0 || |
| 544 |
strcmp ("macro", str) == 0 || |
| 545 |
strcmp ("function", str) == 0) { |
| 546 |
return 1; |
| 547 |
} |
| 548 |
if (strcmp("end", str) == 0) { |
| 549 |
return -1; |
| 550 |
} |
| 551 |
return 0; |
| 552 |
} |
| 553 |
|
| 554 |
static bool IsNumberExpon(int ch, int base) { |
| 555 |
if ((base == 10 && (ch == 'e' || ch == 'E' || ch == 'f')) || |
| 556 |
(base == 16 && (ch == 'p' || ch == 'P'))) { |
| 557 |
return true; |
| 558 |
} |
| 559 |
return false; |
| 560 |
} |
| 561 |
|
| 562 |
/* Scans a sequence of digits, returning true if it found any. */ |
| 563 |
static bool ScanDigits(StyleContext& sc, int base, bool allow_sep) { |
| 564 |
bool found = false; |
| 565 |
for (;;) { |
| 566 |
if (IsADigit(sc.chNext, base) || (allow_sep && sc.chNext == '_')) { |
| 567 |
found = true; |
| 568 |
sc.Forward(); |
| 569 |
} else { |
| 570 |
break; |
| 571 |
} |
| 572 |
} |
| 573 |
return found; |
| 574 |
} |
| 575 |
|
| 576 |
static inline bool ScanNHexas(StyleContext &sc, int max) { |
| 577 |
int n = 0; |
| 578 |
bool error = false; |
| 579 |
|
| 580 |
sc.Forward(); |
| 581 |
if (!IsADigit(sc.ch, 16)) { |
| 582 |
error = true; |
| 583 |
} else { |
| 584 |
while (IsADigit(sc.ch, 16) && n < max) { |
| 585 |
sc.Forward(); |
| 586 |
n++; |
| 587 |
} |
| 588 |
} |
| 589 |
return error; |
| 590 |
} |
| 591 |
|
| 592 |
static void resumeCharacter(StyleContext &sc, bool lexerror) { |
| 593 |
bool error = false; |
| 594 |
|
| 595 |
// ''' case |
| 596 |
if (sc.chPrev == '\'' && sc.ch == '\'' && sc.chNext == '\'') { |
| 597 |
sc.Forward(); |
| 598 |
sc.ForwardSetState(SCE_JULIA_DEFAULT); |
| 599 |
return; |
| 600 |
} else if (lexerror && sc.chPrev == '\'' && sc.ch == '\'') { |
| 601 |
sc.ChangeState(SCE_JULIA_LEXERROR); |
| 602 |
sc.ForwardSetState(SCE_JULIA_DEFAULT); |
| 603 |
|
| 604 |
// Escape characters |
| 605 |
} else if (sc.ch == '\\') { |
| 606 |
sc.Forward(); |
| 607 |
if (sc.ch == '\'' || sc.ch == '\\' ) { |
| 608 |
sc.Forward(); |
| 609 |
} else if (sc.ch == 'n' || sc.ch == 't' || sc.ch == 'a' || |
| 610 |
sc.ch == 'b' || sc.ch == 'e' || sc.ch == 'f' || |
| 611 |
sc.ch == 'r' || sc.ch == 'v' ) { |
| 612 |
sc.Forward(); |
| 613 |
} else if (sc.ch == 'x') { |
| 614 |
error |= ScanNHexas(sc, 2); |
| 615 |
} else if (sc.ch == 'u') { |
| 616 |
error |= ScanNHexas(sc, 4); |
| 617 |
} else if (sc.ch == 'U') { |
| 618 |
error |= ScanNHexas(sc, 8); |
| 619 |
} else if (IsADigit(sc.ch, 8)) { |
| 620 |
int n = 1; |
| 621 |
int max = 3; |
| 622 |
sc.Forward(); |
| 623 |
while (IsADigit(sc.ch, 8) && n < max) { |
| 624 |
sc.Forward(); |
| 625 |
n++; |
| 626 |
} |
| 627 |
} |
| 628 |
|
| 629 |
if (lexerror) { |
| 630 |
if (sc.ch != '\'') { |
| 631 |
error = true; |
| 632 |
while (sc.ch != '\'' && |
| 633 |
sc.ch != '\r' && |
| 634 |
sc.ch != '\n') { |
| 635 |
sc.Forward(); |
| 636 |
} |
| 637 |
} |
| 638 |
|
| 639 |
if (error) { |
| 640 |
sc.ChangeState(SCE_JULIA_LEXERROR); |
| 641 |
sc.ForwardSetState(SCE_JULIA_DEFAULT); |
| 642 |
} |
| 643 |
} |
| 644 |
} else if (lexerror) { |
| 645 |
if (sc.ch < 0x20 || sc.ch > 0x10ffff) { |
| 646 |
error = true; |
| 647 |
} else { |
| 648 |
// single character |
| 649 |
sc.Forward(); |
| 650 |
|
| 651 |
if (sc.ch != '\'') { |
| 652 |
error = true; |
| 653 |
while (sc.ch != '\'' && |
| 654 |
sc.ch != '\r' && |
| 655 |
sc.ch != '\n') { |
| 656 |
sc.Forward(); |
| 657 |
} |
| 658 |
} |
| 659 |
} |
| 660 |
|
| 661 |
if (error) { |
| 662 |
sc.ChangeState(SCE_JULIA_LEXERROR); |
| 663 |
sc.ForwardSetState(SCE_JULIA_DEFAULT); |
| 664 |
} |
| 665 |
} |
| 666 |
|
| 667 |
// closing quote |
| 668 |
if (sc.ch == '\'') { |
| 669 |
if (sc.chNext == '\'') { |
| 670 |
sc.Forward(); |
| 671 |
} else { |
| 672 |
sc.ForwardSetState(SCE_JULIA_DEFAULT); |
| 673 |
} |
| 674 |
} |
| 675 |
} |
| 676 |
|
| 677 |
static inline bool IsACharacter(StyleContext &sc) { |
| 678 |
return (sc.chPrev == '\'' && sc.chNext == '\''); |
| 679 |
} |
| 680 |
|
| 681 |
static void ScanParenInterpolation(StyleContext &sc) { |
| 682 |
// TODO: no syntax highlighting inside a string interpolation |
| 683 |
|
| 684 |
// Level of nested parenthesis |
| 685 |
int interp_level = 0; |
| 686 |
|
| 687 |
// If true, it is inside a string and parenthesis are not counted. |
| 688 |
bool allow_paren_string = false; |
| 689 |
|
| 690 |
|
| 691 |
// check for end of states |
| 692 |
for (; sc.More(); sc.Forward()) { |
| 693 |
// TODO: check corner cases for nested string interpolation |
| 694 |
// TODO: check corner cases with Command inside interpolation |
| 695 |
|
| 696 |
if ( sc.ch == '\"' && sc.chPrev != '\\') { |
| 697 |
// Toggle the string environment (parenthesis are not counted inside a string) |
| 698 |
allow_paren_string = !allow_paren_string; |
| 699 |
} else if ( !allow_paren_string ) { |
| 700 |
if ( sc.ch == '(' && !IsACharacter(sc) ) { |
| 701 |
interp_level ++; |
| 702 |
} else if ( sc.ch == ')' && !IsACharacter(sc) && interp_level > 0 ) { |
| 703 |
interp_level --; |
| 704 |
if (interp_level == 0) { |
| 705 |
// Exit interpolation |
| 706 |
return; |
| 707 |
} |
| 708 |
} |
| 709 |
} |
| 710 |
} |
| 711 |
} |
| 712 |
/* |
| 713 |
* Start parsing a number, parse the base. |
| 714 |
*/ |
| 715 |
static void initNumber (StyleContext &sc, int &base, bool &with_dot) { |
| 716 |
base = 10; |
| 717 |
with_dot = false; |
| 718 |
sc.SetState(SCE_JULIA_NUMBER); |
| 719 |
if (sc.ch == '0') { |
| 720 |
if (sc.chNext == 'x') { |
| 721 |
sc.Forward(); |
| 722 |
base = 16; |
| 723 |
if (sc.chNext == '.') { |
| 724 |
sc.Forward(); |
| 725 |
with_dot = true; |
| 726 |
} |
| 727 |
} else if (sc.chNext == 'o') { |
| 728 |
sc.Forward(); |
| 729 |
base = 8; |
| 730 |
} else if (sc.chNext == 'b') { |
| 731 |
sc.Forward(); |
| 732 |
base = 2; |
| 733 |
} |
| 734 |
} else if (sc.ch == '.') { |
| 735 |
with_dot = true; |
| 736 |
} |
| 737 |
} |
| 738 |
|
| 739 |
/* |
| 740 |
* Resume parsing a String or Command, bounded by the `quote` character (\" or \`) |
| 741 |
* The `triple` argument specifies if it is a triple-quote String or Command. |
| 742 |
* Interpolation is detected (with `$`), and parsed if `allow_interp` is true. |
| 743 |
*/ |
| 744 |
static void resumeStringLike(StyleContext &sc, int quote, bool triple, bool allow_interp, bool full_highlight) { |
| 745 |
int stylePrev = sc.state; |
| 746 |
bool checkcurrent = false; |
| 747 |
|
| 748 |
// Escape characters |
| 749 |
if (sc.ch == '\\') { |
| 750 |
if (sc.chNext == quote || sc.chNext == '\\' || sc.chNext == '$') { |
| 751 |
sc.Forward(); |
| 752 |
} |
| 753 |
} else if (allow_interp && sc.ch == '$') { |
| 754 |
// If the interpolation is only of a variable, do not change state |
| 755 |
if (sc.chNext == '(') { |
| 756 |
if (full_highlight) { |
| 757 |
sc.SetState(SCE_JULIA_STRINGINTERP); |
| 758 |
} else { |
| 759 |
sc.ForwardSetState(SCE_JULIA_STRINGINTERP); |
| 760 |
} |
| 761 |
ScanParenInterpolation(sc); |
| 762 |
sc.ForwardSetState(stylePrev); |
| 763 |
|
| 764 |
checkcurrent = true; |
| 765 |
|
| 766 |
} else if (full_highlight && IsIdentifierFirstCharacter(sc.chNext)) { |
| 767 |
sc.SetState(SCE_JULIA_STRINGINTERP); |
| 768 |
sc.Forward(); |
| 769 |
sc.Forward(); |
| 770 |
for (; sc.More(); sc.Forward()) { |
| 771 |
if (! IsIdentifierCharacter(sc.ch)) { |
| 772 |
break; |
| 773 |
} |
| 774 |
} |
| 775 |
sc.SetState(stylePrev); |
| 776 |
|
| 777 |
checkcurrent = true; |
| 778 |
} |
| 779 |
|
| 780 |
if (checkcurrent) { |
| 781 |
// Check that the current character is not a special char, |
| 782 |
// otherwise it will be skipped |
| 783 |
resumeStringLike(sc, quote, triple, allow_interp, full_highlight); |
| 784 |
} |
| 785 |
|
| 786 |
} else if (sc.ch == quote) { |
| 787 |
if (triple) { |
| 788 |
if (sc.chNext == quote && sc.GetRelativeCharacter(2) == quote) { |
| 789 |
// Move to the end of the triple quotes |
| 790 |
Sci_PositionU nextIndex = sc.currentPos + 2; |
| 791 |
while (nextIndex > sc.currentPos && sc.More()) { |
| 792 |
sc.Forward(); |
| 793 |
} |
| 794 |
sc.ForwardSetState(SCE_JULIA_DEFAULT); |
| 795 |
} |
| 796 |
} else { |
| 797 |
sc.ForwardSetState(SCE_JULIA_DEFAULT); |
| 798 |
} |
| 799 |
} |
| 800 |
} |
| 801 |
|
| 802 |
static void resumeCommand(StyleContext &sc, bool triple, bool allow_interp) { |
| 803 |
return resumeStringLike(sc, '`', triple, allow_interp, true); |
| 804 |
} |
| 805 |
|
| 806 |
static void resumeString(StyleContext &sc, bool triple, bool allow_interp) { |
| 807 |
return resumeStringLike(sc, '"', triple, allow_interp, true); |
| 808 |
} |
| 809 |
|
| 810 |
static void resumeNumber (StyleContext &sc, int base, bool &with_dot, bool lexerror) { |
| 811 |
if (IsNumberExpon(sc.ch, base)) { |
| 812 |
if (IsADigit(sc.chNext) || sc.chNext == '+' || sc.chNext == '-') { |
| 813 |
sc.Forward(); |
| 814 |
// Capture all digits |
| 815 |
ScanDigits(sc, 10, false); |
| 816 |
sc.Forward(); |
| 817 |
} |
| 818 |
sc.SetState(SCE_JULIA_DEFAULT); |
| 819 |
} else if (sc.ch == '.' && sc.chNext == '.') { |
| 820 |
// Interval operator `..` |
| 821 |
sc.SetState(SCE_JULIA_OPERATOR); |
| 822 |
sc.Forward(); |
| 823 |
sc.ForwardSetState(SCE_JULIA_DEFAULT); |
| 824 |
} else if (sc.ch == '.' && !with_dot) { |
| 825 |
with_dot = true; |
| 826 |
ScanDigits(sc, base, true); |
| 827 |
} else if (IsADigit(sc.ch, base) || sc.ch == '_') { |
| 828 |
ScanDigits(sc, base, true); |
| 829 |
} else if (IsADigit(sc.ch) && !IsADigit(sc.ch, base)) { |
| 830 |
if (lexerror) { |
| 831 |
sc.ChangeState(SCE_JULIA_LEXERROR); |
| 832 |
} |
| 833 |
ScanDigits(sc, 10, false); |
| 834 |
sc.ForwardSetState(SCE_JULIA_DEFAULT); |
| 835 |
} else { |
| 836 |
sc.SetState(SCE_JULIA_DEFAULT); |
| 837 |
} |
| 838 |
} |
| 839 |
|
| 840 |
static void resumeOperator (StyleContext &sc) { |
| 841 |
if (sc.chNext == ':' && (sc.ch == ':' || sc.ch == '<' || |
| 842 |
(sc.ch == '>' && (sc.chPrev != '-' && sc.chPrev != '=')))) { |
| 843 |
// Case `:a=>:b` |
| 844 |
sc.Forward(); |
| 845 |
sc.ForwardSetState(SCE_JULIA_DEFAULT); |
| 846 |
} else if (sc.ch == ':') { |
| 847 |
// Case `foo(:baz,:baz)` or `:one+:two` |
| 848 |
// Let the default case switch decide if it is a symbol |
| 849 |
sc.SetState(SCE_JULIA_DEFAULT); |
| 850 |
} else if (sc.ch == '\'') { |
| 851 |
sc.SetState(SCE_JULIA_DEFAULT); |
| 852 |
} else if ((sc.ch == '.' && sc.chPrev != '.') || IsIdentifierFirstCharacter(sc.ch) || |
| 853 |
(! (sc.chPrev == '.' && IsOperatorFirstCharacter(sc.ch)) && |
| 854 |
! IsOperatorCharacter(sc.ch)) ) { |
| 855 |
sc.SetState(SCE_JULIA_DEFAULT); |
| 856 |
} |
| 857 |
} |
| 858 |
|
| 859 |
void SCI_METHOD LexerJulia::Lex(Sci_PositionU startPos, Sci_Position length, int initStyle, IDocument *pAccess) { |
| 860 |
PropSetSimple props; |
| 861 |
Accessor styler(pAccess, &props); |
| 862 |
|
| 863 |
Sci_Position pos = startPos; |
| 864 |
styler.StartAt(pos); |
| 865 |
styler.StartSegment(pos); |
| 866 |
|
| 867 |
// use the line state of each line to store block/multiline states |
| 868 |
Sci_Position curLine = styler.GetLine(startPos); |
| 869 |
// Default is false for everything and 0 counters. |
| 870 |
int lineState = (curLine > 0) ? styler.GetLineState(curLine-1) : 0; |
| 871 |
|
| 872 |
bool transpose = (lineState >> 0) & 0x01; // 1 bit to know if ' is allowed to mean transpose |
| 873 |
bool istripledocstring = (lineState >> 1) & 0x01; // 1 bit to know if we are in a triple doublequotes string |
| 874 |
bool triple_backtick = (lineState >> 2) & 0x01; // 1 bit to know if we are in a triple backtick command |
| 875 |
bool israwstring = (lineState >> 3) & 0x01; // 1 bit to know if we are in a raw string |
| 876 |
int indexing_level = (int)((lineState >> 4) & 0x0F); // 4 bits of bracket nesting counter |
| 877 |
int list_comprehension = (int)((lineState >> 8) & 0x0F); // 4 bits of parenthesis nesting counter |
| 878 |
int commentDepth = (int)((lineState >> 12) & 0x0F); // 4 bits of nested comment counter |
| 879 |
|
| 880 |
// base for parsing number |
| 881 |
int base = 10; |
| 882 |
// number has a float dot ? |
| 883 |
bool with_dot = false; |
| 884 |
|
| 885 |
StyleContext sc(startPos, length, initStyle, styler); |
| 886 |
|
| 887 |
for (; sc.More(); sc.Forward()) { |
| 888 |
|
| 889 |
//// check for end of states |
| 890 |
switch (sc.state) { |
| 891 |
case SCE_JULIA_BRACKET: |
| 892 |
sc.SetState(SCE_JULIA_DEFAULT); |
| 893 |
break; |
| 894 |
case SCE_JULIA_OPERATOR: |
| 895 |
resumeOperator(sc); |
| 896 |
break; |
| 897 |
case SCE_JULIA_TYPEOPERATOR: |
| 898 |
sc.SetState(SCE_JULIA_DEFAULT); |
| 899 |
break; |
| 900 |
case SCE_JULIA_TYPEANNOT: |
| 901 |
if (! IsIdentifierCharacter(sc.ch)) { |
| 902 |
sc.SetState(SCE_JULIA_DEFAULT); |
| 903 |
} |
| 904 |
break; |
| 905 |
case SCE_JULIA_IDENTIFIER: |
| 906 |
// String literal |
| 907 |
if (sc.ch == '\"') { |
| 908 |
// If the string literal has a prefix, interpolation is disabled |
| 909 |
israwstring = true; |
| 910 |
sc.ChangeState(SCE_JULIA_STRINGLITERAL); |
| 911 |
sc.SetState(SCE_JULIA_DEFAULT); |
| 912 |
|
| 913 |
} else if (sc.ch == '`') { |
| 914 |
// If the string literal has a prefix, interpolation is disabled |
| 915 |
israwstring = true; |
| 916 |
sc.ChangeState(SCE_JULIA_COMMANDLITERAL); |
| 917 |
sc.SetState(SCE_JULIA_DEFAULT); |
| 918 |
|
| 919 |
// Continue if the character is an identifier character |
| 920 |
} else if (! IsIdentifierCharacter(sc.ch)) { |
| 921 |
char s[MAX_JULIA_IDENT_CHARS + 1]; |
| 922 |
sc.GetCurrent(s, sizeof(s)); |
| 923 |
|
| 924 |
// Treat the keywords differently if we are indexing or not |
| 925 |
if ( indexing_level > 0 && CheckBoundsIndexing(s)) { |
| 926 |
// Inside [], (), `begin` and `end` are numbers not block keywords |
| 927 |
sc.ChangeState(SCE_JULIA_NUMBER); |
| 928 |
transpose = false; |
| 929 |
|
| 930 |
} else { |
| 931 |
if (keywords.InList(s)) { |
| 932 |
sc.ChangeState(SCE_JULIA_KEYWORD1); |
| 933 |
transpose = false; |
| 934 |
} else if (identifiers2.InList(s)) { |
| 935 |
sc.ChangeState(SCE_JULIA_KEYWORD2); |
| 936 |
transpose = false; |
| 937 |
} else if (identifiers3.InList(s)) { |
| 938 |
sc.ChangeState(SCE_JULIA_KEYWORD3); |
| 939 |
transpose = false; |
| 940 |
} else if (identifiers4.InList(s)) { |
| 941 |
sc.ChangeState(SCE_JULIA_KEYWORD4); |
| 942 |
// These identifiers can be used for variable names also, |
| 943 |
// so transpose is not forbidden. |
| 944 |
//transpose = false; |
| 945 |
} |
| 946 |
} |
| 947 |
sc.SetState(SCE_JULIA_DEFAULT); |
| 948 |
|
| 949 |
// TODO: recognize begin-end blocks inside list comprehension |
| 950 |
// b = [(begin n%2; n*2 end) for n in 1:10] |
| 951 |
// TODO: recognize better comprehension for-if to avoid problem with code-folding |
| 952 |
// c = [(if isempty(a); missing else first(b) end) for (a, b) in zip(l1, l2)] |
| 953 |
} |
| 954 |
break; |
| 955 |
case SCE_JULIA_NUMBER: |
| 956 |
resumeNumber(sc, base, with_dot, options.highlightLexerror); |
| 957 |
break; |
| 958 |
case SCE_JULIA_CHAR: |
| 959 |
resumeCharacter(sc, options.highlightLexerror); |
| 960 |
break; |
| 961 |
case SCE_JULIA_DOCSTRING: |
| 962 |
resumeString(sc, true, !israwstring); |
| 963 |
if (sc.state == SCE_JULIA_DEFAULT && israwstring) { |
| 964 |
israwstring = false; |
| 965 |
} |
| 966 |
break; |
| 967 |
case SCE_JULIA_STRING: |
| 968 |
resumeString(sc, false, !israwstring); |
| 969 |
if (sc.state == SCE_JULIA_DEFAULT && israwstring) { |
| 970 |
israwstring = false; |
| 971 |
} |
| 972 |
break; |
| 973 |
case SCE_JULIA_COMMAND: |
| 974 |
resumeCommand(sc, triple_backtick, !israwstring); |
| 975 |
break; |
| 976 |
case SCE_JULIA_MACRO: |
| 977 |
if (IsASpace(sc.ch) || ! IsIdentifierCharacter(sc.ch)) { |
| 978 |
sc.SetState(SCE_JULIA_DEFAULT); |
| 979 |
} |
| 980 |
break; |
| 981 |
case SCE_JULIA_SYMBOL: |
| 982 |
if (! IsIdentifierCharacter(sc.ch)) { |
| 983 |
sc.SetState(SCE_JULIA_DEFAULT); |
| 984 |
} |
| 985 |
break; |
| 986 |
case SCE_JULIA_COMMENT: |
| 987 |
if( commentDepth > 0 ) { |
| 988 |
// end or start of a nested a block comment |
| 989 |
if ( sc.ch == '=' && sc.chNext == '#') { |
| 990 |
commentDepth --; |
| 991 |
sc.Forward(); |
| 992 |
|
| 993 |
if (commentDepth == 0) { |
| 994 |
sc.ForwardSetState(SCE_JULIA_DEFAULT); |
| 995 |
} |
| 996 |
} else if( sc.ch == '#' && sc.chNext == '=') { |
| 997 |
commentDepth ++; |
| 998 |
sc.Forward(); |
| 999 |
} |
| 1000 |
} else { |
| 1001 |
// single line comment |
| 1002 |
if (sc.atLineEnd || sc.ch == '\r' || sc.ch == '\n') { |
| 1003 |
sc.SetState(SCE_JULIA_DEFAULT); |
| 1004 |
transpose = false; |
| 1005 |
} |
| 1006 |
} |
| 1007 |
break; |
| 1008 |
} |
| 1009 |
|
| 1010 |
// check start of a new state |
| 1011 |
if (sc.state == SCE_JULIA_DEFAULT) { |
| 1012 |
if (sc.ch == '#') { |
| 1013 |
sc.SetState(SCE_JULIA_COMMENT); |
| 1014 |
// increment depth if we are a block comment |
| 1015 |
if(sc.chNext == '=') { |
| 1016 |
commentDepth ++; |
| 1017 |
sc.Forward(); |
| 1018 |
} |
| 1019 |
} else if (sc.ch == '!') { |
| 1020 |
sc.SetState(SCE_JULIA_OPERATOR); |
| 1021 |
transpose = false; |
| 1022 |
} else if (sc.ch == '\'') { |
| 1023 |
if (transpose) { |
| 1024 |
sc.SetState(SCE_JULIA_OPERATOR); |
| 1025 |
} else { |
| 1026 |
sc.SetState(SCE_JULIA_CHAR); |
| 1027 |
} |
| 1028 |
} else if (sc.ch == '\"') { |
| 1029 |
istripledocstring = (sc.chNext == '\"' && sc.GetRelativeCharacter(2) == '\"'); |
| 1030 |
if (istripledocstring) { |
| 1031 |
sc.SetState(SCE_JULIA_DOCSTRING); |
| 1032 |
// Move to the end of the triple quotes |
| 1033 |
Sci_PositionU nextIndex = sc.currentPos + 2; |
| 1034 |
while (nextIndex > sc.currentPos && sc.More()) { |
| 1035 |
sc.Forward(); |
| 1036 |
} |
| 1037 |
} else { |
| 1038 |
sc.SetState(SCE_JULIA_STRING); |
| 1039 |
} |
| 1040 |
} else if (sc.ch == '`') { |
| 1041 |
triple_backtick = (sc.chNext == '`' && sc.GetRelativeCharacter(2) == '`'); |
| 1042 |
sc.SetState(SCE_JULIA_COMMAND); |
| 1043 |
if (triple_backtick) { |
| 1044 |
// Move to the end of the triple backticks |
| 1045 |
Sci_PositionU nextIndex = sc.currentPos + 2; |
| 1046 |
while (nextIndex > sc.currentPos && sc.More()) { |
| 1047 |
sc.Forward(); |
| 1048 |
} |
| 1049 |
} |
| 1050 |
} else if (IsADigit(sc.ch) || (sc.ch == '.' && IsADigit(sc.chNext))) { |
| 1051 |
initNumber(sc, base, with_dot); |
| 1052 |
} else if (IsIdentifierFirstCharacter(sc.ch)) { |
| 1053 |
sc.SetState(SCE_JULIA_IDENTIFIER); |
| 1054 |
transpose = true; |
| 1055 |
} else if (sc.ch == '@') { |
| 1056 |
sc.SetState(SCE_JULIA_MACRO); |
| 1057 |
transpose = false; |
| 1058 |
|
| 1059 |
// Several parsing of operators, should keep the order of `if` blocks |
| 1060 |
} else if ((sc.ch == ':' || sc.ch == '<' || sc.ch == '>') && sc.chNext == ':') { |
| 1061 |
sc.SetState(SCE_JULIA_TYPEOPERATOR); |
| 1062 |
sc.Forward(); |
| 1063 |
// Highlight the next identifier, if option is set |
| 1064 |
if (options.highlightTypeannotation && |
| 1065 |
IsIdentifierFirstCharacter(sc.chNext)) { |
| 1066 |
sc.ForwardSetState(SCE_JULIA_TYPEANNOT); |
| 1067 |
} |
| 1068 |
} else if (sc.ch == ':') { |
| 1069 |
// TODO: improve detection of range |
| 1070 |
// should be solved with begin-end parsing |
| 1071 |
// `push!(arr, s1 :s2)` and `a[begin :end] |
| 1072 |
if (IsIdentifierFirstCharacter(sc.chNext) && |
| 1073 |
! IsIdentifierCharacter(sc.chPrev) && |
| 1074 |
sc.chPrev != ')' && sc.chPrev != ']' ) { |
| 1075 |
sc.SetState(SCE_JULIA_SYMBOL); |
| 1076 |
} else { |
| 1077 |
sc.SetState(SCE_JULIA_OPERATOR); |
| 1078 |
} |
| 1079 |
} else if (IsJuliaParen(sc.ch)) { |
| 1080 |
if (sc.ch == '[') { |
| 1081 |
list_comprehension ++; |
| 1082 |
indexing_level ++; |
| 1083 |
} else if (sc.ch == ']' && (indexing_level > 0)) { |
| 1084 |
list_comprehension --; |
| 1085 |
indexing_level --; |
| 1086 |
} else if (sc.ch == '(') { |
| 1087 |
list_comprehension ++; |
| 1088 |
} else if (sc.ch == ')' && (list_comprehension > 0)) { |
| 1089 |
list_comprehension --; |
| 1090 |
} |
| 1091 |
|
| 1092 |
if (sc.ch == ')' || sc.ch == ']' || sc.ch == '}') { |
| 1093 |
transpose = true; |
| 1094 |
} else { |
| 1095 |
transpose = false; |
| 1096 |
} |
| 1097 |
sc.SetState(SCE_JULIA_BRACKET); |
| 1098 |
} else if (IsOperatorFirstCharacter(sc.ch)) { |
| 1099 |
transpose = false; |
| 1100 |
sc.SetState(SCE_JULIA_OPERATOR); |
| 1101 |
} else { |
| 1102 |
transpose = false; |
| 1103 |
} |
| 1104 |
} |
| 1105 |
|
| 1106 |
// update the line information (used for line-by-line lexing and folding) |
| 1107 |
if (sc.atLineEnd) { |
| 1108 |
// set the line state to the current state |
| 1109 |
curLine = styler.GetLine(sc.currentPos); |
| 1110 |
|
| 1111 |
lineState = ((transpose ? 1 : 0) << 0) | |
| 1112 |
((istripledocstring ? 1 : 0) << 1) | |
| 1113 |
((triple_backtick ? 1 : 0) << 2) | |
| 1114 |
((israwstring ? 1 : 0) << 3) | |
| 1115 |
((indexing_level & 0x0F) << 4) | |
| 1116 |
((list_comprehension & 0x0F) << 8) | |
| 1117 |
((commentDepth & 0x0F) << 12); |
| 1118 |
styler.SetLineState(curLine, lineState); |
| 1119 |
} |
| 1120 |
} |
| 1121 |
sc.Complete(); |
| 1122 |
} |
| 1123 |
|
| 1124 |
void SCI_METHOD LexerJulia::Fold(Sci_PositionU startPos, Sci_Position length, int initStyle, IDocument *pAccess) { |
| 1125 |
|
| 1126 |
if (!options.fold) |
| 1127 |
return; |
| 1128 |
|
| 1129 |
LexAccessor styler(pAccess); |
| 1130 |
|
| 1131 |
Sci_PositionU endPos = startPos + length; |
| 1132 |
int visibleChars = 0; |
| 1133 |
Sci_Position lineCurrent = styler.GetLine(startPos); |
| 1134 |
int levelCurrent = SC_FOLDLEVELBASE; |
| 1135 |
int lineState = 0; |
| 1136 |
if (lineCurrent > 0) { |
| 1137 |
levelCurrent = styler.LevelAt(lineCurrent-1) >> 16; |
| 1138 |
lineState = styler.GetLineState(lineCurrent-1); |
| 1139 |
} |
| 1140 |
|
| 1141 |
// level of nested brackets |
| 1142 |
int indexing_level = (int)((lineState >> 4) & 0x0F); // 4 bits of bracket nesting counter |
| 1143 |
// level of nested parenthesis or brackets |
| 1144 |
int list_comprehension = (int)((lineState >> 8) & 0x0F); // 4 bits of parenthesis nesting counter |
| 1145 |
//int commentDepth = (int)((lineState >> 12) & 0x0F); // 4 bits of nested comment counter |
| 1146 |
|
| 1147 |
Sci_PositionU lineStartNext = styler.LineStart(lineCurrent+1); |
| 1148 |
int levelNext = levelCurrent; |
| 1149 |
char chNext = styler[startPos]; |
| 1150 |
int stylePrev = styler.StyleAt(startPos - 1); |
| 1151 |
int styleNext = styler.StyleAt(startPos); |
| 1152 |
int style = initStyle; |
| 1153 |
char word[100]; |
| 1154 |
int wordlen = 0; |
| 1155 |
for (Sci_PositionU i = startPos; i < endPos; i++) { |
| 1156 |
char ch = chNext; |
| 1157 |
chNext = styler.SafeGetCharAt(i + 1); |
| 1158 |
style = styleNext; |
| 1159 |
styleNext = styler.StyleAt(i + 1); |
| 1160 |
bool atEOL = i == (lineStartNext-1); |
| 1161 |
|
| 1162 |
// a start/end of comment block |
| 1163 |
if (options.foldComment && style == SCE_JULIA_COMMENT) { |
| 1164 |
// start of block comment |
| 1165 |
if (ch == '#' && chNext == '=') { |
| 1166 |
levelNext ++; |
| 1167 |
} |
| 1168 |
// end of block comment |
| 1169 |
if (ch == '=' && chNext == '#' && levelNext > 0) { |
| 1170 |
levelNext --; |
| 1171 |
} |
| 1172 |
} |
| 1173 |
|
| 1174 |
// Syntax based folding, accounts for list comprehension |
| 1175 |
if (options.foldSyntaxBased) { |
| 1176 |
// list comprehension allow `for`, `if` and `begin` without `end` |
| 1177 |
if (style == SCE_JULIA_BRACKET) { |
| 1178 |
if (ch == '[') { |
| 1179 |
list_comprehension ++; |
| 1180 |
indexing_level ++; |
| 1181 |
levelNext ++; |
| 1182 |
} else if (ch == ']') { |
| 1183 |
list_comprehension --; |
| 1184 |
indexing_level --; |
| 1185 |
levelNext --; |
| 1186 |
} else if (ch == '(') { |
| 1187 |
list_comprehension ++; |
| 1188 |
levelNext ++; |
| 1189 |
} else if (ch == ')') { |
| 1190 |
list_comprehension --; |
| 1191 |
levelNext --; |
| 1192 |
} |
| 1193 |
// check non-negative |
| 1194 |
if (indexing_level < 0) { |
| 1195 |
indexing_level = 0; |
| 1196 |
} |
| 1197 |
if (list_comprehension < 0) { |
| 1198 |
list_comprehension = 0; |
| 1199 |
} |
| 1200 |
} |
| 1201 |
|
| 1202 |
// keyword |
| 1203 |
if (style == SCE_JULIA_KEYWORD1) { |
| 1204 |
word[wordlen++] = static_cast<char>(ch); |
| 1205 |
if (wordlen == 100) { // prevent overflow |
| 1206 |
word[0] = '\0'; |
| 1207 |
wordlen = 1; |
| 1208 |
} |
| 1209 |
if (styleNext != SCE_JULIA_KEYWORD1) { |
| 1210 |
word[wordlen] = '\0'; |
| 1211 |
wordlen = 0; |
| 1212 |
if (list_comprehension <= 0 && indexing_level <= 0) { |
| 1213 |
levelNext += CheckKeywordFoldPoint(word); |
| 1214 |
} |
| 1215 |
} |
| 1216 |
} |
| 1217 |
} |
| 1218 |
|
| 1219 |
// Docstring |
| 1220 |
if (options.foldDocstring) { |
| 1221 |
if (stylePrev != SCE_JULIA_DOCSTRING && style == SCE_JULIA_DOCSTRING) { |
| 1222 |
levelNext ++; |
| 1223 |
} else if (style == SCE_JULIA_DOCSTRING && styleNext != SCE_JULIA_DOCSTRING) { |
| 1224 |
levelNext --; |
| 1225 |
} |
| 1226 |
} |
| 1227 |
|
| 1228 |
// check non-negative level |
| 1229 |
if (levelNext < 0) { |
| 1230 |
levelNext = 0; |
| 1231 |
} |
| 1232 |
|
| 1233 |
if (!IsASpace(ch)) { |
| 1234 |
visibleChars++; |
| 1235 |
} |
| 1236 |
stylePrev = style; |
| 1237 |
|
| 1238 |
if (atEOL || (i == endPos-1)) { |
| 1239 |
int levelUse = levelCurrent; |
| 1240 |
int lev = levelUse | levelNext << 16; |
| 1241 |
if (visibleChars == 0 && options.foldCompact) { |
| 1242 |
lev |= SC_FOLDLEVELWHITEFLAG; |
| 1243 |
} |
| 1244 |
if (levelUse < levelNext) { |
| 1245 |
lev |= SC_FOLDLEVELHEADERFLAG; |
| 1246 |
} |
| 1247 |
if (lev != styler.LevelAt(lineCurrent)) { |
| 1248 |
styler.SetLevel(lineCurrent, lev); |
| 1249 |
} |
| 1250 |
lineCurrent++; |
| 1251 |
lineStartNext = styler.LineStart(lineCurrent+1); |
| 1252 |
levelCurrent = levelNext; |
| 1253 |
if (atEOL && (i == static_cast<Sci_PositionU>(styler.Length() - 1))) { |
| 1254 |
// There is an empty line at end of file so give it same level and empty |
| 1255 |
styler.SetLevel(lineCurrent, (levelCurrent | levelCurrent << 16) | SC_FOLDLEVELWHITEFLAG); |
| 1256 |
} |
| 1257 |
visibleChars = 0; |
| 1258 |
} |
| 1259 |
} |
| 1260 |
} |
| 1261 |
|
| 1262 |
extern const LexerModule lmJulia(SCLEX_JULIA, LexerJulia::LexerFactoryJulia, "julia", juliaWordLists); |