1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605
|
/** @file LexRaku.cxx
** Lexer for Raku
**
** Copyright (c) 2019 Mark Reay <mark@reay.net.au>
**/
// Copyright 1998-2005 by Neil Hodgson <neilh@scintilla.org>
// The License.txt file describes the conditions under which this software may be distributed.
/*
* Raku (Perl6) Lexer for Scintilla
* ---------------------------------
* ---------------------------------
* 06-Dec-2019: More Unicode support:
* - Added a full scope of allowed numbers and letters
* 29-Nov-2019: More highlighting / implemented basic folding:
* - Operators (blanket cover, no sequence checking)
* - Class / Grammar name highlighting
* - Folding:
* - Comments: line / multi-line
* - POD sections
* - Code blocks {}
* 26-Nov-2019: Basic syntax highlighting covering the following:
* - Comments, both line and embedded (multi-line)
* - POD, no inline highlighting as yet...
* - Heredoc block string, with variable highlighting (with qq)
* - Strings, with variable highlighting (with ")
* - Q Language, including adverbs (also basic q and qq)
* - Regex, including adverbs
* - Numbers
* - Bareword / identifiers
* - Types
* - Variables: mu, positional, associative, callable
* TODO:
* - POD inline
* - Better operator sequence coverage
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
#include <assert.h>
#include <ctype.h>
#include <string>
#include <vector>
#include <map>
#include "ILexer.h"
#include "Scintilla.h"
#include "SciLexer.h"
#include "WordList.h"
#include "LexAccessor.h"
#include "StyleContext.h"
#include "CharacterSet.h"
#include "CharacterCategory.h"
#include "LexerModule.h"
#include "OptionSet.h"
#include "DefaultLexer.h"
using namespace Scintilla;
namespace { // anonymous namespace to isolate any name clashes
/*----------------------------------------------------------------------------*
* --- DEFINITIONS: OPTIONS / CONSTANTS ---
*----------------------------------------------------------------------------*/
// Number types
#define RAKUNUM_BINARY 1 // order is significant: 1-3 cannot have a dot
#define RAKUNUM_OCTAL 2
#define RAKUNUM_FLOAT_EXP 3 // exponent part only
#define RAKUNUM_HEX 4 // may be a hex float
#define RAKUNUM_DECIMAL 5 // 1-5 are numbers; 6-7 are strings
#define RAKUNUM_VECTOR 6
#define RAKUNUM_V_VECTOR 7
#define RAKUNUM_VERSION 8 // can contain multiple '.'s
#define RAKUNUM_BAD 9
// Regex / Q string types
#define RAKUTYPE_REGEX_NORM 0 // 0 char ident
#define RAKUTYPE_REGEX_S 1 // order is significant:
#define RAKUTYPE_REGEX_M 2 // 1 char ident
#define RAKUTYPE_REGEX_Y 3 // 1 char ident
#define RAKUTYPE_REGEX 4 // > RAKUTYPE_REGEX == 2 char identifiers
#define RAKUTYPE_REGEX_RX 5 // 2 char ident
#define RAKUTYPE_REGEX_TR 6 // 2 char ident
#define RAKUTYPE_QLANG 7 // < RAKUTYPE_QLANG == RAKUTYPE_REGEX_?
#define RAKUTYPE_STR_WQ 8 // 0 char ident < word quote >
#define RAKUTYPE_STR_Q 9 // 1 char ident
#define RAKUTYPE_STR_QX 10 // 2 char ident
#define RAKUTYPE_STR_QW 11 // 2 char ident
#define RAKUTYPE_STR_QQ 12 // 2 char ident
#define RAKUTYPE_STR_QQX 13 // 3 char ident
#define RAKUTYPE_STR_QQW 14 // 3 char ident
#define RAKUTYPE_STR_QQWW 15 // 4 char ident
// Delimiter types
#define RAKUDELIM_BRACKET 0 // bracket: regex, Q language
#define RAKUDELIM_QUOTE 1 // quote: normal string
// rakuWordLists: keywords as defined in config
const char *const rakuWordLists[] = {
"Keywords and identifiers",
"Functions",
"Types basic",
"Types composite",
"Types domain-specific",
"Types exception",
"Adverbs",
nullptr,
};
// Options and defaults
struct OptionsRaku {
bool fold;
bool foldCompact;
bool foldComment;
bool foldCommentMultiline;
bool foldCommentPOD;
OptionsRaku() {
fold = true;
foldCompact = false;
foldComment = true;
foldCommentMultiline = true;
foldCommentPOD = true;
}
};
// init options and words
struct OptionSetRaku : public OptionSet<OptionsRaku> {
OptionSetRaku() {
DefineProperty("fold", &OptionsRaku::fold);
DefineProperty("fold.comment", &OptionsRaku::foldComment);
DefineProperty("fold.compact", &OptionsRaku::foldCompact);
DefineProperty("fold.raku.comment.multiline", &OptionsRaku::foldCommentMultiline,
"Set this property to 0 to disable folding multi-line comments when fold.comment=1.");
DefineProperty("fold.raku.comment.pod", &OptionsRaku::foldCommentPOD,
"Set this property to 0 to disable folding POD comments when fold.comment=1.");
// init word lists
DefineWordListSets(rakuWordLists);
}
};
// Delimiter pair
struct DelimPair {
int opener; // opener char
int closer[2]; // closer chars
bool interpol; // can variables be interpolated?
short count; // delimiter char count
DelimPair() {
opener = 0;
closer[0] = 0;
closer[1] = 0;
interpol = false;
count = 0;
}
bool isCloser(int ch) const {
return ch == closer[0] || ch == closer[1];
}
};
/*----------------------------------------------------------------------------*
* --- FUNCTIONS ---
*----------------------------------------------------------------------------*/
/*
* IsANewLine
* - returns true if this is a new line char
*/
constexpr bool IsANewLine(int ch) noexcept {
return ch == '\r' || ch == '\n';
}
/*
* IsAWhitespace
* - returns true if this is a whitespace (or newline) char
*/
bool IsAWhitespace(int ch) noexcept {
return IsASpaceOrTab(ch) || IsANewLine(ch);
}
/*
* IsAlphabet
* - returns true if this is an alphabetical char
*/
constexpr bool IsAlphabet(int ch) noexcept {
return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z');
}
/*
* IsCommentLine
* - returns true if this is a comment line
* - tests: SCE_RAKU_COMMENTLINE or SCE_RAKU_COMMENTEMBED
* modified from: LexPerl.cxx
*/
bool IsCommentLine(Sci_Position line, LexAccessor &styler, int type = SCE_RAKU_COMMENTLINE) {
Sci_Position pos = styler.LineStart(line);
Sci_Position eol_pos = styler.LineStart(line + 1) - 1;
for (Sci_Position i = pos; i < eol_pos; i++) {
char ch = styler[i];
int style = styler.StyleAt(i);
if (type == SCE_RAKU_COMMENTEMBED) {
if (i == (eol_pos - 1) && style == type)
return true;
} else { // make sure the line is NOT a SCE_RAKU_COMMENTEMBED
if (ch == '#' && style == type && styler[i+1] != '`' )
return true;
else if (!IsASpaceOrTab(ch))
return false;
}
}
return false;
}
/*
* GetBracketCloseChar
* - returns the end bracket char: opposite of start
* - see: http://www.unicode.org/Public/5.1.0/ucd/BidiMirroring.txt (first section)
* - Categories are general matches for valid BiDi types
* - Most closer chars are opener + 1
*/
int GetBracketCloseChar(const int ch) noexcept {
const CharacterCategory cc = CategoriseCharacter(ch);
switch (cc) {
case ccSm:
switch (ch) {
case 0x3C: return 0x3E; // LESS-THAN SIGN
case 0x2208: return 0x220B; // ELEMENT OF
case 0x2209: return 0x220C; // NOT AN ELEMENT OF
case 0x220A: return 0x220D; // SMALL ELEMENT OF
case 0x2215: return 0x29F5; // DIVISION SLASH
case 0x2243: return 0x22CD; // ASYMPTOTICALLY EQUAL TO
case 0x2298: return 0x29B8; // CIRCLED DIVISION SLASH
case 0x22A6: return 0x2ADE; // ASSERTION
case 0x22A8: return 0x2AE4; // TRUE
case 0x22A9: return 0x2AE3; // FORCES
case 0x22AB: return 0x2AE5; // DOUBLE VERTICAL BAR DOUBLE RIGHT TURNSTILE
case 0x22F2: return 0x22FA; // ELEMENT OF WITH LONG HORIZONTAL STROKE
case 0x22F3: return 0x22FB; // ELEMENT OF WITH VERTICAL BAR AT END OF HORIZONTAL STROKE
case 0x22F4: return 0x22FC; // SMALL ELEMENT OF WITH VERTICAL BAR AT END OF HORIZONTAL STROKE
case 0x22F6: return 0x22FD; // ELEMENT OF WITH OVERBAR
case 0x22F7: return 0x22FE; // SMALL ELEMENT OF WITH OVERBAR
case 0xFF1C: return 0xFF1E; // FULLWIDTH LESS-THAN SIGN
}
break;
case ccPs:
switch (ch) {
case 0x5B: return 0x5D; // LEFT SQUARE BRACKET
case 0x7B: return 0x7D; // LEFT CURLY BRACKET
case 0x298D: return 0x2990; // LEFT SQUARE BRACKET WITH TICK IN TOP CORNER
case 0x298F: return 0x298E; // LEFT SQUARE BRACKET WITH TICK IN BOTTOM CORNER
case 0xFF3B: return 0xFF3D; // FULLWIDTH LEFT SQUARE BRACKET
case 0xFF5B: return 0xFF5D; // FULLWIDTH LEFT CURLY BRACKET
}
break;
case ccPi:
break;
default: return 0;
}
return ch + 1;
}
/*
* IsValidQuoteOpener
* -
*/
bool IsValidQuoteOpener(const int ch, DelimPair &dp, int type = RAKUDELIM_BRACKET) noexcept {
dp.closer[0] = 0;
dp.closer[1] = 0;
dp.interpol = true;
if (type == RAKUDELIM_QUOTE) {
switch (ch) {
// Opener Closer Description
case '\'': dp.closer[0] = '\''; // APOSTROPHE
dp.interpol = false;
break;
case '"': dp.closer[0] = '"'; // QUOTATION MARK
break;
case 0x2018: dp.closer[0] = 0x2019; // LEFT SINGLE QUOTATION MARK
dp.interpol = false;
break;
case 0x201C: dp.closer[0] = 0x201D; // LEFT DOUBLE QUOTATION MARK
break;
case 0x201D: dp.closer[0] = 0x201C; // RIGHT DOUBLE QUOTATION MARK
break;
case 0x201E: dp.closer[0] = 0x201C; // DOUBLE LOW-9 QUOTATION MARK
dp.closer[1] = 0x201D;
break;
case 0xFF62: dp.closer[0] = 0xFF63; // HALFWIDTH LEFT CORNER BRACKET
dp.interpol = false;
break;
default: return false;
}
} else if (type == RAKUDELIM_BRACKET) {
dp.closer[0] = GetBracketCloseChar(ch);
}
dp.opener = ch;
dp.count = 1;
return dp.closer[0] > 0;
}
/*
* IsBracketOpenChar
* - true if this is a valid start bracket character
*/
bool IsBracketOpenChar(int ch) noexcept {
return GetBracketCloseChar(ch) > 0;
}
/*
* IsValidRegOrQAdjacent
* - returns true if ch is a valid character to put directly after Q / q
* * ref: Q Language: https://docs.raku.org/language/quoting
*/
bool IsValidRegOrQAdjacent(int ch) noexcept {
return !(IsAlphaNumeric(ch) || ch == '_' || ch == '(' || ch == ')' || ch == '\'' );
}
/*
* IsValidRegOrQPrecede
* - returns true if ch is a valid preceeding character to put directly before Q / q
* * ref: Q Language: https://docs.raku.org/language/quoting
*/
bool IsValidRegOrQPrecede(int ch) noexcept {
return !(IsAlphaNumeric(ch) || ch == '_');
}
/*
* MatchCharInRange
* - returns true if the mach character is found in range (of length)
* - ignoreDelim (default false)
*/
bool MatchCharInRange(StyleContext &sc, const Sci_Position length,
const int match, bool ignoreDelim = false) {
Sci_Position len = 0;
int chPrev = sc.chPrev;
while (++len < length) {
const int ch = sc.GetRelativeCharacter(len);
if (ch == match && (ignoreDelim || chPrev != '\\'))
return true;
}
return false;
}
/*
* PrevNonWhitespaceChar
* - returns the last non-whitespace char
*/
int PrevNonWhitespaceChar(StyleContext &sc) {
Sci_Position rel = 0;
Sci_Position max_back = 0 - sc.currentPos;
while (--rel > max_back) {
const int ch = sc.GetRelativeCharacter(rel);
if (!IsAWhitespace(ch))
return ch;
}
return 0; // no matching char
}
/*
* IsQLangStartAtScPos
* - returns true if this is a valid Q Language sc position
* - ref: https://docs.raku.org/language/quoting
* - Q :adverb :adverb //;
* - q,qx,qw,qq,qqx,qqw,qqww :adverb /:adverb /;
*/
bool IsQLangStartAtScPos(StyleContext &sc, int &type, const Sci_Position length) {
const bool valid_adj = IsValidRegOrQAdjacent(sc.chNext);
const int chFw2 = sc.GetRelativeCharacter(2);
const int chFw3 = sc.GetRelativeCharacter(3);
type = -1;
if (IsValidRegOrQPrecede(sc.chPrev)) {
if (sc.ch == 'Q' && valid_adj) {
type = RAKUTYPE_QLANG;
} else if (sc.ch == 'q') {
switch (sc.chNext) {
case 'x':
type = RAKUTYPE_STR_QX;
break;
case 'w':
type = RAKUTYPE_STR_QW;
break;
case 'q':
if (chFw2 == 'x') {
type = RAKUTYPE_STR_QQX;
} else if (chFw2 == 'w') {
if (chFw3 == 'w') {
type = RAKUTYPE_STR_QQWW;
} else {
type = RAKUTYPE_STR_QQW;
}
} else {
type = RAKUTYPE_STR_QQ;
}
break;
default:
type = RAKUTYPE_STR_Q;
}
} else if (sc.ch == '<' && MatchCharInRange(sc, length, '>')) {
type = RAKUTYPE_STR_WQ; // < word quote >
}
}
return type >= 0;
}
/*
* IsRegexStartAtScPos
* - returns true if this is a valid Regex sc position
* - ref: https://docs.raku.org/language/regexes
* - Regex: (rx/s/m/tr/y) :adverb /:adverb /;
* - regex R :adverb //;
* - /:adverb /;
*/
bool IsRegexStartAtScPos(StyleContext &sc, int &type, CharacterSet &set) {
const bool valid_adj = IsValidRegOrQAdjacent(sc.chNext);
type = -1;
if (IsValidRegOrQPrecede(sc.chPrev)) {
switch (sc.ch) {
case 'r':
if (sc.chNext == 'x')
type = RAKUTYPE_REGEX_RX;
break;
case 't':
case 'T':
if (sc.chNext == 'r' || sc.chNext == 'R')
type = RAKUTYPE_REGEX_TR;
break;
case 'm':
if (valid_adj)
type = RAKUTYPE_REGEX_M;
break;
case 's':
case 'S':
if (valid_adj)
type = RAKUTYPE_REGEX_S;
break;
case 'y':
if (valid_adj)
type = RAKUTYPE_REGEX_Y;
break;
case '/':
if (set.Contains(PrevNonWhitespaceChar(sc)))
type = RAKUTYPE_REGEX_NORM;
}
}
return type >= 0;
}
/*
* IsValidIdentPrecede
* - returns if ch is a valid preceeding char to put directly before an identifier
*/
bool IsValidIdentPrecede(int ch) noexcept {
return !(IsAlphaNumeric(ch) || ch == '_' || ch == '@' || ch == '$' || ch == '%');
}
/*
* IsValidDelimiter
* - returns if ch is a valid delimiter (most chars are valid)
* * ref: Q Language: https://docs.raku.org/language/quoting
*/
bool IsValidDelimiter(int ch) noexcept {
return !(IsAlphaNumeric(ch) || ch == ':');
}
/*
* GetDelimiterCloseChar
* - returns the corrisponding close char for a given delimiter (could be the same char)
*/
int GetDelimiterCloseChar(int ch) noexcept {
int ch_end = GetBracketCloseChar(ch);
if (ch_end == 0 && IsValidDelimiter(ch)) {
ch_end = ch;
}
return ch_end;
}
/*
* GetRepeatCharCount
* - returns the occurence count of match
*/
Sci_Position GetRepeatCharCount(StyleContext &sc, int chMatch, Sci_Position length) {
Sci_Position cnt = 0;
while (cnt < length) {
if (sc.GetRelativeCharacter(cnt) != chMatch) {
break;
}
cnt++;
}
return cnt;
}
/*
* LengthToDelimiter
* - returns the length until the end of a delimited string section
* - Ignores nested delimiters (if opener != closer)
* - no trailing char after last closer (default false)
*/
Sci_Position LengthToDelimiter(StyleContext &sc, const DelimPair &dp,
Sci_Position length, bool noTrailing = false) {
short cnt_open = 0; // count open bracket
short cnt_close = 0; // count close bracket
Sci_Position len = 0; // count characters
int chOpener = dp.opener; // look for nested opener / closer
if (dp.opener == dp.closer[0])
chOpener = 0; // no opening delimiter (no nesting possible)
while (len < length) {
const int chPrev = sc.GetRelativeCharacter(len - 1);
const int ch = sc.GetRelativeCharacter(len);
const int chNext = sc.GetRelativeCharacter(len+1);
if (cnt_open == 0 && cnt_close == dp.count) {
return len; // end condition has been met
} else {
if (chPrev != '\\' && ch == chOpener) { // ignore escape sequence
cnt_open++; // open nested bracket
} else if (chPrev != '\\' && dp.isCloser(ch)) { // ignore escape sequence
if ( cnt_open > 0 ) {
cnt_open--; // close nested bracket
} else if (dp.count > 1 && cnt_close < (dp.count - 1)) {
if (cnt_close > 1) {
if (dp.isCloser(chPrev)) {
cnt_close++;
} else { // reset if previous char was not close
cnt_close = 0;
}
} else {
cnt_close++;
}
} else if (!noTrailing || (IsAWhitespace(chNext))) {
cnt_close++; // found last close
if (cnt_close > 1 && !dp.isCloser(chPrev)) {
cnt_close = 0; // reset if previous char was not close
}
} else {
cnt_close = 0; // non handled close: reset
}
} else if (IsANewLine(ch)) {
cnt_open = 0; // reset after each line
cnt_close = 0;
}
}
len++;
}
return -1; // end condition has NOT been met
}
/*
* LengthToEndHeredoc
* - returns the length until the end of a heredoc section
* - delimiter string MUST begin on a new line
*/
Sci_Position LengthToEndHeredoc(const StyleContext &sc, LexAccessor &styler,
const Sci_Position length, const char *delim) {
bool on_new_ln = false;
int i = 0; // str index
for (int n = 0; n < length; n++) {
const char ch = styler.SafeGetCharAt(sc.currentPos + n, 0);
if (on_new_ln) {
if (delim[i] == '\0')
return n; // at end of str, match found!
if (ch != delim[i++])
i = 0; // no char match, reset 'i'ndex
}
if (i == 0) // detect new line
on_new_ln = IsANewLine(ch);
}
return -1; // no match found
}
/*
* LengthToNextChar
* - returns the length until the next character
*/
Sci_Position LengthToNextChar(StyleContext &sc, const Sci_Position length) {
Sci_Position len = 0;
while (++len < length) {
const int ch = sc.GetRelativeCharacter(len);
if (!IsASpaceOrTab(ch) && !IsANewLine(ch)) {
break;
}
}
return len;
}
/*
* GetRelativeString
* - gets a relitive string and sets it in &str
* - resets string before seting
*/
void GetRelativeString(StyleContext &sc, Sci_Position offset, Sci_Position length,
std::string &str) {
Sci_Position pos = offset;
str.clear();
while (pos < length) {
str += sc.GetRelativeCharacter(pos++);
}
}
} // end anonymous namespace
/*----------------------------------------------------------------------------*
* --- class: LexerRaku ---
*----------------------------------------------------------------------------*/
//class LexerRaku : public ILexerWithMetaData {
class LexerRaku : public DefaultLexer {
CharacterSet setWord;
CharacterSet setSigil;
CharacterSet setTwigil;
CharacterSet setOperator;
CharacterSet setSpecialVar;
WordList regexIdent; // identifiers that specify a regex
OptionsRaku options; // Options from config
OptionSetRaku osRaku;
WordList keywords; // Word Lists from config
WordList functions;
WordList typesBasic;
WordList typesComposite;
WordList typesDomainSpecific;
WordList typesExceptions;
WordList adverbs;
public:
// Defined as explicit, so that constructor can not be copied
explicit LexerRaku() :
DefaultLexer("raku", SCLEX_RAKU),
setWord(CharacterSet::setAlphaNum, "-_", 0x80),
setSigil(CharacterSet::setNone, "$&%@"),
setTwigil(CharacterSet::setNone, "!*.:<=?^~"),
setOperator(CharacterSet::setNone, "^&\\()-+=|{}[]:;<>,?!.~"),
setSpecialVar(CharacterSet::setNone, "_/!") {
regexIdent.Set("regex rule token");
}
// Deleted so LexerRaku objects can not be copied.
LexerRaku(const LexerRaku &) = delete;
LexerRaku(LexerRaku &&) = delete;
void operator=(const LexerRaku &) = delete;
void operator=(LexerRaku &&) = delete;
virtual ~LexerRaku() {
}
void SCI_METHOD Release() noexcept override {
delete this;
}
int SCI_METHOD Version() const noexcept override {
return lvIdentity;
}
const char *SCI_METHOD PropertyNames() override {
return osRaku.PropertyNames();
}
int SCI_METHOD PropertyType(const char *name) override {
return osRaku.PropertyType(name);
}
const char *SCI_METHOD DescribeProperty(const char *name) override {
return osRaku.DescribeProperty(name);
}
Sci_Position SCI_METHOD PropertySet(const char *key, const char *val) override;
const char *SCI_METHOD PropertyGet(const char *key) override {
return osRaku.PropertyGet(key);
}
const char *SCI_METHOD DescribeWordListSets() override {
return osRaku.DescribeWordListSets();
}
Sci_Position SCI_METHOD WordListSet(int n, const char *wl) override;
void SCI_METHOD Lex(Sci_PositionU startPos, Sci_Position length, int initStyle, IDocument *pAccess) override;
void SCI_METHOD Fold(Sci_PositionU startPos, Sci_Position length, int initStyle, IDocument *pAccess) override;
static ILexer *LexerFactoryRaku() {
return new LexerRaku();
}
protected:
bool IsOperatorChar(const int ch);
bool IsWordChar(const int ch, bool allowNumber = true);
bool IsWordStartChar(const int ch);
bool IsNumberChar(const int ch, int base = 10);
bool ProcessRegexTwinCapture(StyleContext &sc, const Sci_Position length,
int &type, const DelimPair &dp);
void ProcessStringVars(StyleContext &sc, const Sci_Position length, const int varState);
bool ProcessValidRegQlangStart(StyleContext &sc, Sci_Position length, const int type,
WordList &wordsAdverbs, DelimPair &dp);
Sci_Position LengthToNonWordChar(StyleContext &sc, Sci_Position length,
char *s, const int size, Sci_Position offset = 0);
};
/*----------------------------------------------------------------------------*
* --- METHODS: LexerRaku ---
*----------------------------------------------------------------------------*/
/*
* LexerRaku::IsOperatorChar
* - Test for both ASCII and Unicode operators
* see: https://docs.raku.org/language/unicode_entry
*/
bool LexerRaku::IsOperatorChar(const int ch) {
if (ch > 0x7F) {
switch (ch) {
// Unicode ASCII Equiv.
case 0x2208: // (elem)
case 0x2209: // !(elem)
case 0x220B: // (cont)
case 0x220C: // !(cont)
case 0x2216: // (-)
case 0x2229: // (&)
case 0x222A: // (|)
case 0x2282: // (<)
case 0x2283: // (>)
case 0x2284: // !(<)
case 0x2285: // !(>)
case 0x2286: // (<=)
case 0x2287: // (>=)
case 0x2288: // !(<=)
case 0x2289: // !(>=)
case 0x228D: // (.)
case 0x228E: // (+)
case 0x2296: // (^)
return true;
}
}
return setOperator.Contains(ch);
}
/*
* LexerRaku::IsWordChar
* - Test for both ASCII and Unicode identifier characters
* see: https://docs.raku.org/language/unicode_ascii
* also: ftp://ftp.unicode.org/Public/UCD/latest/ucd/UnicodeData.txt
* FIXME: *still* may not contain all valid characters
*/
bool LexerRaku::IsWordChar(const int ch, bool allowNumber) {
// Unicode numbers should not apear in word identifiers
if (ch > 0x7F) {
const CharacterCategory cc = CategoriseCharacter(ch);
switch (cc) {
// Letters
case ccLu:
case ccLl:
case ccLt:
case ccLm:
case ccLo:
return true;
default:
return false;
}
} else if (allowNumber && IsADigit(ch)) {
return true; // an ASCII number type
}
return setWord.Contains(ch);
}
/*
* LexerRaku::IsWordStartChar
* - Test for both ASCII and Unicode identifier "start / first" characters
*/
bool LexerRaku::IsWordStartChar(const int ch) {
return ch != '-' && IsWordChar(ch, false); // no numbers allowed
}
/*
* LexerRaku::IsNumberChar
* - Test for both ASCII and Unicode identifier number characters
* see: https://docs.raku.org/language/unicode_ascii
* also: ftp://ftp.unicode.org/Public/UCD/latest/ucd/UnicodeData.txt
* FILTERED by Unicode letters that are NUMBER
* and NOT PARENTHESIZED or CIRCLED
* FIXME: *still* may not contain all valid number characters
*/
bool LexerRaku::IsNumberChar(const int ch, int base) {
if (ch > 0x7F) {
const CharacterCategory cc = CategoriseCharacter(ch);
switch (cc) {
// Numbers
case ccNd:
case ccNl:
case ccNo:
return true;
default:
return false;
}
}
return IsADigit(ch, base);
}
/*
* LexerRaku::PropertySet
* -
*/
Sci_Position SCI_METHOD LexerRaku::PropertySet(const char *key, const char *val) {
if (osRaku.PropertySet(&options, key, val))
return 0;
return -1;
}
/*
* LexerRaku::WordListSet
* -
*/
Sci_Position SCI_METHOD LexerRaku::WordListSet(int n, const char *wl) {
WordList *wordListN = nullptr;
switch (n) {
case 0:
wordListN = &keywords;
break;
case 1:
wordListN = &functions;
break;
case 2:
wordListN = &typesBasic;
break;
case 3:
wordListN = &typesComposite;
break;
case 4:
wordListN = &typesDomainSpecific;
break;
case 5:
wordListN = &typesExceptions;
break;
case 6:
wordListN = &adverbs;
break;
}
Sci_Position firstModification = -1;
if (wordListN) {
WordList wlNew;
wlNew.Set(wl);
if (*wordListN != wlNew) {
wordListN->Set(wl);
firstModification = 0;
}
}
return firstModification;
}
/*
* LexerRaku::ProcessRegexTwinCapture
* - processes the transition between a regex pair (two sets of delimiters)
* - moves to first new delimiter, if a bracket
* - returns true when valid delimiter start found (if bracket)
*/
bool LexerRaku::ProcessRegexTwinCapture(StyleContext &sc, const Sci_Position length,
int &type, const DelimPair &dp) {
if (type == RAKUTYPE_REGEX_S || type == RAKUTYPE_REGEX_TR || type == RAKUTYPE_REGEX_Y) {
type = -1; // clear type
// move past chRegQClose if it was the previous char
if (dp.isCloser(sc.chPrev))
sc.Forward();
// no processing needed for non-bracket
if (dp.isCloser(dp.opener))
return true;
// move to next opening bracket
const Sci_Position len = LengthToNextChar(sc, length);
if (sc.GetRelativeCharacter(len) == dp.opener) {
sc.Forward(len);
return true;
}
}
return false;
}
/*
* LexerRaku::ProcessStringVars
* - processes a string and highlights any valid variables
*/
void LexerRaku::ProcessStringVars(StyleContext &sc, const Sci_Position length, const int varState) {
const int state = sc.state;
for (Sci_Position pos = 0; pos < length; pos++) {
if (sc.state == varState && !IsWordChar(sc.ch)) {
sc.SetState(state);
} else if (sc.chPrev != '\\'
&& (sc.ch == '$' || sc.ch == '@')
&& IsWordStartChar(sc.chNext)) {
sc.SetState(varState);
}
sc.Forward(); // Next character
}
}
/*
* LexerRaku::ProcessValidRegQlangStart
* - processes a section of the document range from after a Regex / Q delimiter
* - returns true on success
* - sets: adverbs, chOpen, chClose, chCount
* ref: https://docs.raku.org/language/regexes
*/
bool LexerRaku::ProcessValidRegQlangStart(StyleContext &sc, Sci_Position length, const int type,
WordList &wordsAdverbs, DelimPair &dp) {
Sci_Position startPos = sc.currentPos;
Sci_Position startLen = length;
const int target_state = sc.state;
int state = SCE_RAKU_DEFAULT;
std::string str;
// find our opening delimiter (and occurrences) / save any adverbs
dp.opener = 0; // adverbs can be after the first delimiter
bool got_all_adverbs = false; // in Regex statements
bool got_ident = false; // regex can have an identifier: 'regex R'
sc.SetState(state); // set state default to avoid pre-highlights
while ((dp.opener == 0 || !got_all_adverbs) && sc.More()) {
// move to the next non-space character
const bool was_space = IsAWhitespace(sc.ch);
if (!got_all_adverbs && was_space) {
sc.Forward(LengthToNextChar(sc, length));
}
length = startLen - (sc.currentPos - startPos); // update length remaining
// parse / eat an identifier (if type == RAKUTYPE_REGEX)
if (dp.opener == 0 && !got_ident && type == RAKUTYPE_REGEX && IsAlphabet(sc.ch)) {
// eat identifier / account for special adverb :sym<name>
bool got_sym = false;
while (sc.More()) {
sc.SetState(SCE_RAKU_IDENTIFIER);
while (sc.More() && (IsAlphaNumeric(sc.chNext)
|| sc.chNext == '_' || sc.chNext == '-')) {
sc.Forward();
}
sc.Forward();
if (got_sym && sc.ch == '>') {
sc.SetState(SCE_RAKU_OPERATOR); // '>'
sc.Forward();
break;
} else if (type == RAKUTYPE_REGEX && sc.Match(":sym<")) {
sc.SetState(SCE_RAKU_ADVERB); // ':sym'
sc.Forward(4);
sc.SetState(SCE_RAKU_OPERATOR); // '<'
sc.Forward();
got_sym = true;
} else {
break;
}
}
sc.SetState(state);
got_ident = true;
}
// parse / save an adverb: RAKUTYPE_REGEX only has adverbs after delim
// >= RAKUTYPE_QLANG only has adverbs before delim
else if (!got_all_adverbs && sc.ch == ':' && (!(dp.opener == 0 && got_ident)
&& !(dp.opener > 0 && type >= RAKUTYPE_QLANG))) {
sc.SetState(SCE_RAKU_ADVERB);
while (IsAlphaNumeric(sc.chNext) && sc.More()) {
sc.Forward();
str += sc.ch;
}
str += ' ';
sc.Forward();
sc.SetState(state);
}
// find starting delimiter
else if (dp.opener == 0 && (was_space || IsValidRegOrQAdjacent(sc.ch))
&& IsValidDelimiter(sc.ch)) { // make sure the delimiter is legal (most are)
sc.SetState((state = target_state));// start state here...
dp.opener = sc.ch; // this is our delimiter, get count
if (type < RAKUTYPE_QLANG) // type is Regex
dp.count = 1; // has only one delimiter
else
dp.count = GetRepeatCharCount(sc, dp.opener, length);
sc.Forward(dp.count);
}
// we must have all the adverbs by now...
else {
if (got_all_adverbs)
break; // prevent infinite loop: occurs on missing open char
got_all_adverbs = true;
}
}
// set word list / find a valid closing delimiter (or bomb!)
wordsAdverbs.Set(str.c_str());
dp.closer[0] = GetDelimiterCloseChar(dp.opener);
dp.closer[1] = 0; // no other closer char
return dp.closer[0] > 0;
}
/*
* LexerRaku::LengthToNonWordChar
* - returns the length until the next non "word" character: AlphaNum + '_'
* - also sets all the parsed chars in 's'
*/
Sci_Position LexerRaku::LengthToNonWordChar(StyleContext &sc, Sci_Position length,
char *s, const int size, Sci_Position offset) {
Sci_Position len = 0;
Sci_Position max_length = size < length ? size : length;
while (len <= max_length) {
const int ch = sc.GetRelativeCharacter(len + offset);
if (!IsWordChar(ch)) {
s[len] = '\0';
break;
}
s[len] = ch;
len++;
}
s[len + 1] = '\0';
return len;
}
/*
* LexerRaku::Lex
* - Main lexer method
*/
void SCI_METHOD LexerRaku::Lex(Sci_PositionU startPos, Sci_Position length, int initStyle, IDocument *pAccess) {
LexAccessor styler(pAccess);
DelimPair dpEmbeded; // delimiter pair: embeded comments
DelimPair dpString; // delimiter pair: string
DelimPair dpRegQ; // delimiter pair: Regex / Q Lang
std::string hereDelim; // heredoc delimiter (if in heredoc)
int hereState = 0; // heredoc state to use (Q / QQ)
int numState = 0; // number state / type
short cntDecimal = 0; // number decinal count
std::string wordLast; // last word seen
std::string identLast; // last identifier seen
std::string adverbLast; // last (single) adverb seen
WordList lastAdverbs; // last adverbs seen
Sci_Position len; // temp length value
char s[100]; // temp char string
int typeDetect; // temp type detected (for regex and Q lang)
Sci_Position lengthToEnd; // length until the end of range
// Backtrack to last SCE_RAKU_DEFAULT or 0
Sci_PositionU newStartPos = startPos;
if (initStyle != SCE_RAKU_DEFAULT) {
while (newStartPos > 0) {
newStartPos--;
if (styler.StyleAt(newStartPos) == SCE_RAKU_DEFAULT)
break;
}
}
// Backtrack to start of line before SCE_RAKU_HEREDOC_Q?
if (initStyle == SCE_RAKU_HEREDOC_Q || initStyle == SCE_RAKU_HEREDOC_QQ) {
while (newStartPos > 0) {
if (IsANewLine(styler.SafeGetCharAt(newStartPos - 1)))
break; // Stop if previous char is a new line
newStartPos--;
}
}
// Re-calculate (any) changed startPos, length and initStyle state
if (newStartPos < startPos) {
initStyle = SCE_RAKU_DEFAULT;
length += startPos - newStartPos;
startPos = newStartPos;
}
// init StyleContext
StyleContext sc(startPos, length, initStyle, styler);
// StyleContext Loop
for (; sc.More(); sc.Forward()) {
lengthToEnd = (length - (sc.currentPos - startPos)); // end of range
/* *** Determine if the current state should terminate ************** *
* Everything within the 'switch' statement processes characters up
* until the end of a syntax highlight section / state.
* ****************************************************************** */
switch (sc.state) {
case SCE_RAKU_OPERATOR:
sc.SetState(SCE_RAKU_DEFAULT);
break; // FIXME: better valid operator sequences needed?
case SCE_RAKU_COMMENTLINE:
if (IsANewLine(sc.ch)) {
sc.SetState(SCE_RAKU_DEFAULT);
}
break;
case SCE_RAKU_COMMENTEMBED:
if ((len = LengthToDelimiter(sc, dpEmbeded, lengthToEnd)) >= 0) {
sc.Forward(len); // Move to end delimiter
sc.SetState(SCE_RAKU_DEFAULT);
} else {
sc.Forward(lengthToEnd); // no end delimiter found
}
break;
case SCE_RAKU_POD:
if (sc.atLineStart && sc.Match("=end pod")) {
sc.Forward(8);
sc.SetState(SCE_RAKU_DEFAULT);
}
break;
case SCE_RAKU_STRING:
// Process the string for variables: move to end delimiter
if ((len = LengthToDelimiter(sc, dpString, lengthToEnd)) >= 0) {
if (dpString.interpol) {
ProcessStringVars(sc, len, SCE_RAKU_STRING_VAR);
} else {
sc.Forward(len);
}
sc.SetState(SCE_RAKU_DEFAULT);
} else {
sc.Forward(lengthToEnd); // no end delimiter found
}
break;
case SCE_RAKU_STRING_Q:
case SCE_RAKU_STRING_QQ:
case SCE_RAKU_STRING_Q_LANG:
// No string: previous char was the delimiter
if (dpRegQ.count == 1 && dpRegQ.isCloser(sc.chPrev)) {
sc.SetState(SCE_RAKU_DEFAULT);
}
// Process the string for variables: move to end delimiter
else if ((len = LengthToDelimiter(sc, dpRegQ, lengthToEnd)) >= 0) {
// set (any) heredoc delimiter string
if (lastAdverbs.InList("to")) {
GetRelativeString(sc, -1, len - dpRegQ.count, hereDelim);
hereState = SCE_RAKU_HEREDOC_Q; // default heredoc state
}
// select variable identifiers
if (sc.state == SCE_RAKU_STRING_QQ || lastAdverbs.InList("qq")) {
ProcessStringVars(sc, len, SCE_RAKU_STRING_VAR);
hereState = SCE_RAKU_HEREDOC_QQ; // potential heredoc state
} else {
sc.Forward(len);
}
sc.SetState(SCE_RAKU_DEFAULT);
} else {
sc.Forward(lengthToEnd); // no end delimiter found
}
break;
case SCE_RAKU_HEREDOC_Q:
case SCE_RAKU_HEREDOC_QQ:
if ((len = LengthToEndHeredoc(sc, styler, lengthToEnd, hereDelim.c_str())) >= 0) {
// select variable identifiers
if (sc.state == SCE_RAKU_HEREDOC_QQ) {
ProcessStringVars(sc, len, SCE_RAKU_STRING_VAR);
} else {
sc.Forward(len);
}
sc.SetState(SCE_RAKU_DEFAULT);
} else {
sc.Forward(lengthToEnd); // no end delimiter found
}
hereDelim.clear(); // clear heredoc delimiter
break;
case SCE_RAKU_REGEX:
// account for typeDetect = RAKUTYPE_REGEX_S/TR/Y
while (sc.state == SCE_RAKU_REGEX) {
// No string: previous char was the delimiter
if (dpRegQ.count == 1 && dpRegQ.isCloser(sc.chPrev)) {
if (ProcessRegexTwinCapture(sc, lengthToEnd, typeDetect, dpRegQ))
continue;
sc.SetState(SCE_RAKU_DEFAULT);
break;
}
// Process the string for variables: move to end delimiter
else if ((len = LengthToDelimiter(sc, dpRegQ, lengthToEnd)) >= 0) {
ProcessStringVars(sc, len, SCE_RAKU_REGEX_VAR);
if (ProcessRegexTwinCapture(sc, lengthToEnd, typeDetect, dpRegQ))
continue;
sc.SetState(SCE_RAKU_DEFAULT);
break;
} else {
sc.Forward(lengthToEnd); // no end delimiter found
break;
}
}
break;
case SCE_RAKU_NUMBER:
if (sc.ch == '.') {
if (sc.chNext == '.') { // '..' is an operator
sc.SetState(SCE_RAKU_OPERATOR);
sc.Forward();
if (sc.chNext == '.') // '...' is also an operator
sc.Forward();
break;
} else if (numState > RAKUNUM_FLOAT_EXP
&& (cntDecimal < 1 || numState == RAKUNUM_VERSION)) {
cntDecimal++;
sc.Forward();
} else {
sc.SetState(SCE_RAKU_DEFAULT);
break; // too many decinal places
}
}
switch (numState) {
case RAKUNUM_BINARY:
if (!IsNumberChar(sc.ch, 2))
sc.SetState(SCE_RAKU_DEFAULT);
break;
case RAKUNUM_OCTAL:
if (!IsNumberChar(sc.ch, 8))
sc.SetState(SCE_RAKU_DEFAULT);
break;
case RAKUNUM_HEX:
if (!IsNumberChar(sc.ch, 16))
sc.SetState(SCE_RAKU_DEFAULT);
break;
case RAKUNUM_DECIMAL:
case RAKUNUM_VERSION:
if (!IsNumberChar(sc.ch))
sc.SetState(SCE_RAKU_DEFAULT);
}
break;
case SCE_RAKU_WORD:
case SCE_RAKU_FUNCTION:
case SCE_RAKU_TYPEDEF:
case SCE_RAKU_ADVERB:
sc.SetState(SCE_RAKU_DEFAULT);
break;
case SCE_RAKU_MU:
case SCE_RAKU_POSITIONAL:
case SCE_RAKU_ASSOCIATIVE:
case SCE_RAKU_CALLABLE:
case SCE_RAKU_IDENTIFIER:
case SCE_RAKU_GRAMMAR:
case SCE_RAKU_CLASS:
sc.SetState(SCE_RAKU_DEFAULT);
break;
}
/* *** Determine if a new state should be entered ******************* *
* Everything below here identifies the beginning of a state, all or part
* of the characters within this state are processed here, the rest are
* completed above in the terminate state section.
* ****************************************************************** */
if (sc.state == SCE_RAKU_DEFAULT) {
// --- Single line comment
if (sc.ch == '#') {
sc.SetState(SCE_RAKU_COMMENTLINE);
}
// --- POD block
else if (sc.atLineStart && sc.Match("=begin pod")) {
sc.SetState(SCE_RAKU_POD);
sc.Forward(10);
}
// --- String (normal)
else if (sc.chPrev != '\\' && (IsValidQuoteOpener(sc.ch, dpString, RAKUDELIM_QUOTE))) {
sc.SetState(SCE_RAKU_STRING);
}
// --- String (Q Language) ----------------------------------------
// - https://docs.raku.org/language/quoting
// - Q :adverb :adverb //;
// - q,qx,qw,qq,qqx,qqw,qqww :adverb :adverb //;
else if (IsQLangStartAtScPos(sc, typeDetect, lengthToEnd)) {
int state = SCE_RAKU_STRING_Q_LANG;
Sci_Position forward = 1; // single char ident (default)
if (typeDetect > RAKUTYPE_QLANG) {
state = SCE_RAKU_STRING_Q;
if (typeDetect == RAKUTYPE_STR_WQ)
forward = 0; // no char ident
}
if (typeDetect > RAKUTYPE_STR_Q) {
if (typeDetect == RAKUTYPE_STR_QQ)
state = SCE_RAKU_STRING_QQ;
forward++; // two char ident
}
if (typeDetect > RAKUTYPE_STR_QQ)
forward++; // three char ident
if (typeDetect == RAKUTYPE_STR_QQWW)
forward++; // four char ident
// Proceed: check for a valid character after statement
if (IsValidRegOrQAdjacent(sc.GetRelative(forward)) || typeDetect == RAKUTYPE_QLANG) {
sc.SetState(state);
sc.Forward(forward);
lastAdverbs.Clear();
// Process: adverbs / opening delimiter / adverbs after delim
if (ProcessValidRegQlangStart(sc, lengthToEnd, typeDetect,
lastAdverbs, dpRegQ))
sc.SetState(state);
}
}
// --- Regex (rx/s/m/tr/y) ----------------------------------------
// - https://docs.raku.org/language/regexes
else if ((IsRegexStartAtScPos(sc, typeDetect, setOperator) || regexIdent.InList(wordLast.c_str()))) {
if (typeDetect == -1) { // must be a regex identifier word
wordLast.clear();
typeDetect = RAKUTYPE_REGEX;
}
Sci_Position forward = 0; // no ident (RAKUTYPE_REGEX, RAKUTYPE_REGEX_NORM)
if (typeDetect > 0 && typeDetect != RAKUTYPE_REGEX)
forward++; // single char ident
if (typeDetect > RAKUTYPE_REGEX)
forward++; // two char ident
// Proceed: check for a valid character after statement
if (IsValidRegOrQAdjacent(sc.GetRelative(forward)) || typeDetect == RAKUTYPE_REGEX_NORM) {
sc.SetState(SCE_RAKU_REGEX);
sc.Forward(forward);
lastAdverbs.Clear();
// Process: adverbs / opening delimiter / adverbs after delim
if (ProcessValidRegQlangStart(sc, lengthToEnd, typeDetect,
lastAdverbs, dpRegQ))
sc.SetState(SCE_RAKU_REGEX);
}
}
// --- Numbers ----------------------------------------------------
else if (IsValidIdentPrecede(sc.chPrev) && (IsNumberChar(sc.ch)
|| (sc.ch == 'v' && IsNumberChar(sc.chNext) && wordLast == "use"))) {
numState = RAKUNUM_DECIMAL; // default: decimal (base 10)
cntDecimal = 0;
sc.SetState(SCE_RAKU_NUMBER);
if (sc.ch == 'v') // forward past 'v'
sc.Forward();
if (wordLast == "use") { // package version number
numState = RAKUNUM_VERSION;
} else if (sc.ch == '0') { // other type of number
switch (sc.chNext) {
case 'b': // binary (base 2)
numState = RAKUNUM_BINARY;
break;
case 'o': // octal (base 8)
numState = RAKUNUM_OCTAL;
break;
case 'x': // hexadecimal (base 16)
numState = RAKUNUM_HEX;
}
if (numState != RAKUNUM_DECIMAL)
sc.Forward(); // forward to number type char
}
}
// --- Keywords / functions / types / barewords -------------------
else if ((sc.currentPos == 0 || sc.atLineStart || IsValidIdentPrecede(sc.chPrev))
&& IsWordStartChar(sc.ch)) {
len = LengthToNonWordChar(sc, lengthToEnd, s, sizeof(s));
if (keywords.InList(s)) {
sc.SetState(SCE_RAKU_WORD); // Keywords
} else if(functions.InList(s)) {
sc.SetState(SCE_RAKU_FUNCTION); // Functions
} else if(typesBasic.InList(s)) {
sc.SetState(SCE_RAKU_TYPEDEF); // Types (basic)
} else if(typesComposite.InList(s)) {
sc.SetState(SCE_RAKU_TYPEDEF); // Types (composite)
} else if(typesDomainSpecific.InList(s)) {
sc.SetState(SCE_RAKU_TYPEDEF); // Types (domain-specific)
} else if(typesExceptions.InList(s)) {
sc.SetState(SCE_RAKU_TYPEDEF); // Types (exceptions)
} else {
if (wordLast == "class")
sc.SetState(SCE_RAKU_CLASS); // a Class ident
else if (wordLast == "grammar")
sc.SetState(SCE_RAKU_GRAMMAR); // a Grammar ident
else
sc.SetState(SCE_RAKU_IDENTIFIER); // Bareword
identLast = s; // save identifier
}
if (adverbLast == "sym") { // special adverb ":sym"
sc.SetState(SCE_RAKU_IDENTIFIER); // treat as identifier
identLast = s; // save identifier
}
if (sc.state != SCE_RAKU_IDENTIFIER)
wordLast = s; // save word
sc.Forward(len - 1); // ...forward past word
}
// --- Adverbs ----------------------------------------------------
else if (sc.ch == ':' && IsWordStartChar(sc.chNext)) {
len = LengthToNonWordChar(sc, lengthToEnd, s, sizeof(s), 1);
if (adverbs.InList(s)) {
sc.SetState(SCE_RAKU_ADVERB); // Adverbs (begin with ':')
adverbLast = s; // save word
sc.Forward(len); // ...forward past word (less offset: 1)
}
}
// --- Identifiers: $mu / @positional / %associative / &callable --
// see: https://docs.raku.org/language/variables
else if (setSigil.Contains(sc.ch) && (setTwigil.Contains(sc.chNext)
|| setSpecialVar.Contains(sc.chNext)
|| IsWordStartChar(sc.chNext))) {
// State based on sigil
switch (sc.ch) {
case '$': sc.SetState(SCE_RAKU_MU);
break;
case '@': sc.SetState(SCE_RAKU_POSITIONAL);
break;
case '%': sc.SetState(SCE_RAKU_ASSOCIATIVE);
break;
case '&': sc.SetState(SCE_RAKU_CALLABLE);
}
const int state = sc.state;
sc.Forward();
char ch_delim = 0;
if (setSpecialVar.Contains(sc.ch)
&& !setWord.Contains(sc.chNext)) { // Process Special Var
ch_delim = -1;
} else if (setTwigil.Contains(sc.ch)) { // Process Twigil
sc.SetState(SCE_RAKU_OPERATOR);
if (sc.ch == '<' && setWord.Contains(sc.chNext))
ch_delim = '>';
sc.Forward();
sc.SetState(state);
}
// Process (any) identifier
if (ch_delim >= 0) {
sc.Forward(LengthToNonWordChar(sc, lengthToEnd, s, sizeof(s)) - 1);
if (ch_delim > 0 && sc.chNext == ch_delim) {
sc.Forward();
sc.SetState(SCE_RAKU_OPERATOR);
}
identLast = s; // save identifier
}
}
// --- Operators --------------------------------------------------
else if (IsOperatorChar(sc.ch)) {
// FIXME: better valid operator sequences needed?
sc.SetState(SCE_RAKU_OPERATOR);
}
// --- Heredoc: begin ---------------------------------------------
else if (!hereDelim.empty() && sc.atLineEnd) {
if (IsANewLine(sc.ch))
sc.Forward(); // skip a possible CRLF situation
sc.SetState(hereState);
}
// Reset words: on operator simi-colon OR '}' (end of statement)
if (sc.state == SCE_RAKU_OPERATOR && (sc.ch == ';' || sc.ch == '}')) {
wordLast.clear();
identLast.clear();
adverbLast.clear();
}
}
/* *** Determine if an "embedded comment" is to be entered ********** *
* This type of embedded comment section, or multi-line comment comes
* after a normal comment has begun... e.g: #`[ ... ]
* ****************************************************************** */
else if (sc.state == SCE_RAKU_COMMENTLINE && sc.chPrev == '#' && sc.ch == '`') {
if (IsBracketOpenChar(sc.chNext)) {
sc.Forward(); // Condition met for "embedded comment"
dpEmbeded.opener = sc.ch;
// Find the opposite (termination) closeing bracket (if any)
dpEmbeded.closer[0] = GetBracketCloseChar(dpEmbeded.opener);
if (dpEmbeded.closer[0] > 0) { // Enter "embedded comment"
// Find multiple opening character occurence
dpEmbeded.count = GetRepeatCharCount(sc, dpEmbeded.opener, lengthToEnd);
sc.SetState(SCE_RAKU_COMMENTEMBED);
sc.Forward(dpEmbeded.count - 1); // incremented in the next loop
}
}
}
}
// And we're done...
sc.Complete();
}
/*
* LexerRaku::Lex
* - Main fold method
* NOTE: although Raku uses and supports UNICODE characters, we're only looking
* at normal chars here, using 'SafeGetCharAt' - for folding purposes
* that is all we need.
*/
#define RAKU_HEADFOLD_SHIFT 4
#define RAKU_HEADFOLD_MASK 0xF0
void SCI_METHOD LexerRaku::Fold(Sci_PositionU startPos, Sci_Position length, int /* initStyle */, IDocument *pAccess) {
// init LexAccessor / return if fold option is off
if (!options.fold) return;
LexAccessor styler(pAccess);
// init char and line positions
const Sci_PositionU endPos = startPos + length;
Sci_Position lineCurrent = styler.GetLine(startPos);
// Backtrack to last SCE_RAKU_DEFAULT line
if (startPos > 0 && lineCurrent > 0) {
while (lineCurrent > 0 && styler.StyleAt(startPos) != SCE_RAKU_DEFAULT) {
lineCurrent--;
startPos = styler.LineStart(lineCurrent);
}
lineCurrent = styler.GetLine(startPos);
}
Sci_PositionU lineStart = startPos;
Sci_PositionU lineStartNext = styler.LineStart(lineCurrent + 1);
// init line folding level
int levelPrev = SC_FOLDLEVELBASE;
if (lineCurrent > 0)
levelPrev = styler.LevelAt(lineCurrent - 1) >> 16;
int levelCurrent = levelPrev;
// init char and style variables
char chNext = styler[startPos];
int stylePrev = styler.StyleAt(startPos - 1);
int styleNext = styler.StyleAt(startPos);
int styleNextStartLine = styler.StyleAt(lineStartNext);
int visibleChars = 0;
bool wasCommentMulti = false;
// main loop
for (Sci_PositionU i = startPos; i < endPos; i++) {
// next char, style and flags
const char ch = chNext;
chNext = styler.SafeGetCharAt(i + 1);
const int style = styleNext;
styleNext = styler.StyleAt(i + 1);
const bool atEOL = i == (lineStartNext - 1);
const bool atLineStart = i == lineStart;
// --- Comments / Multi-line / POD ------------------------------------
if (options.foldComment) {
// Multi-line
if (options.foldCommentMultiline) {
if (style == SCE_RAKU_COMMENTLINE && atLineStart && ch == '#' && chNext == '`'
&& styleNextStartLine == SCE_RAKU_COMMENTEMBED) {
levelCurrent++;
wasCommentMulti = true; // don't confuse line comments
} else if (style == SCE_RAKU_COMMENTEMBED && atLineStart
&& styleNextStartLine != SCE_RAKU_COMMENTEMBED) {
levelCurrent--;
}
}
// Line comments
if (!wasCommentMulti && atEOL && stylePrev == SCE_RAKU_COMMENTLINE
&& IsCommentLine(lineCurrent, styler)) {
if (!IsCommentLine(lineCurrent - 1, styler)
&& IsCommentLine(lineCurrent + 1, styler))
levelCurrent++;
else if (IsCommentLine(lineCurrent - 1, styler)
&& !IsCommentLine(lineCurrent + 1, styler))
levelCurrent--;
}
// POD
if (options.foldCommentPOD && atLineStart && style == SCE_RAKU_POD) {
if (styler.Match(i, "=begin"))
levelCurrent++;
else if (styler.Match(i, "=end"))
levelCurrent--;
}
}
// --- Code block -----------------------------------------------------
if (style == SCE_RAKU_OPERATOR) {
if (ch == '{') {
if (levelCurrent < levelPrev) levelPrev--;
levelCurrent++;
} else if (ch == '}') {
levelCurrent--;
}
}
// --- at end of line / range / apply fold ----------------------------
if (atEOL) {
int level = levelPrev;
// set level flags
level |= levelCurrent << 16;
if (visibleChars == 0 && options.foldCompact)
level |= SC_FOLDLEVELWHITEFLAG;
if ((levelCurrent > levelPrev) && (visibleChars > 0))
level |= SC_FOLDLEVELHEADERFLAG;
if (level != styler.LevelAt(lineCurrent)) {
styler.SetLevel(lineCurrent, level);
}
lineCurrent++;
lineStart = lineStartNext;
lineStartNext = styler.LineStart(lineCurrent + 1);
styleNextStartLine = styler.StyleAt(lineStartNext);
levelPrev = levelCurrent;
visibleChars = 0;
wasCommentMulti = false;
}
// increment visibleChars / set previous char
if (!isspacechar(ch))
visibleChars++;
stylePrev = style;
}
// Done: set real level of the next line
int flagsNext = styler.LevelAt(lineCurrent) & ~SC_FOLDLEVELNUMBERMASK;
styler.SetLevel(lineCurrent, levelPrev | flagsNext);
}
/*----------------------------------------------------------------------------*
* --- Scintilla: LexerModule ---
*----------------------------------------------------------------------------*/
LexerModule lmRaku(SCLEX_RAKU, LexerRaku::LexerFactoryRaku, "raku", rakuWordLists);
|