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
|
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013-2016 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include "py/nlr.h"
#include "py/lexer.h"
#include "py/parse.h"
#include "py/parsenum.h"
#include "py/smallint.h"
#if MICROPY_ENABLE_COMPILER && MICROPY_USE_SMALL_HEAP_COMPILER
#define RULE_ACT_ARG_MASK (0x0f)
#define RULE_ACT_KIND_MASK (0x30)
#define RULE_ACT_ALLOW_IDENT (0x40)
#define RULE_ACT_ADD_BLANK (0x80)
#define RULE_ACT_OR (0x10)
#define RULE_ACT_AND (0x20)
#define RULE_ACT_LIST (0x30)
#define RULE_ARG_KIND_MASK (0xf000)
#define RULE_ARG_ARG_MASK (0x0fff)
#define RULE_ARG_TOK (0x1000)
#define RULE_ARG_RULE (0x2000)
#define RULE_ARG_OPT_RULE (0x3000)
#define ADD_BLANK_NODE(rule) ((rule->act & RULE_ACT_ADD_BLANK) != 0)
// (un)comment to use rule names; for debugging
//#define USE_RULE_NAME (1)
typedef struct _rule_t {
byte rule_id;
byte act;
#ifdef USE_RULE_NAME
const char *rule_name;
#endif
uint16_t arg[];
} rule_t;
enum {
// define rules with a compile function
#define DEF_RULE(rule, comp, kind, ...) RULE_##rule,
#define DEF_RULE_NC(rule, kind, ...)
#include "py/grammar.h"
#undef DEF_RULE
#undef DEF_RULE_NC
RULE_const_object, // special node for a constant, generic Python object
// define rules without a compile function
#define DEF_RULE(rule, comp, kind, ...)
#define DEF_RULE_NC(rule, kind, ...) RULE_##rule,
#include "py/grammar.h"
#undef DEF_RULE
#undef DEF_RULE_NC
};
#define or(n) (RULE_ACT_OR | n)
#define and(n) (RULE_ACT_AND | n)
#define and_ident(n) (RULE_ACT_AND | n | RULE_ACT_ALLOW_IDENT)
#define and_blank(n) (RULE_ACT_AND | n | RULE_ACT_ADD_BLANK)
#define one_or_more (RULE_ACT_LIST | 2)
#define list (RULE_ACT_LIST | 1)
#define list_with_end (RULE_ACT_LIST | 3)
#define tok(t) (RULE_ARG_TOK | MP_TOKEN_##t)
#define rule(r) (RULE_ARG_RULE | RULE_##r)
#define opt_rule(r) (RULE_ARG_OPT_RULE | RULE_##r)
#ifdef USE_RULE_NAME
#define DEF_RULE(rule, comp, kind, ...) static const rule_t rule_##rule = { RULE_##rule, kind, #rule, { __VA_ARGS__ } };
#define DEF_RULE_NC(rule, kind, ...) static const rule_t rule_##rule = { RULE_##rule, kind, #rule, { __VA_ARGS__ } };
#else
#define DEF_RULE(rule, comp, kind, ...) static const rule_t rule_##rule = { RULE_##rule, kind, { __VA_ARGS__ } };
#define DEF_RULE_NC(rule, kind, ...) static const rule_t rule_##rule = { RULE_##rule, kind, { __VA_ARGS__ } };
#endif
#include "py/grammar.h"
#undef or
#undef and
#undef list
#undef list_with_end
#undef tok
#undef rule
#undef opt_rule
#undef one_or_more
#undef DEF_RULE
#undef DEF_RULE_NC
STATIC const rule_t *rules[] = {
// define rules with a compile function
#define DEF_RULE(rule, comp, kind, ...) &rule_##rule,
#define DEF_RULE_NC(rule, kind, ...)
#include "py/grammar.h"
#undef DEF_RULE
#undef DEF_RULE_NC
NULL, // RULE_const_object
// define rules without a compile function
#define DEF_RULE(rule, comp, kind, ...)
#define DEF_RULE_NC(rule, kind, ...) &rule_##rule,
#include "py/grammar.h"
#undef DEF_RULE
#undef DEF_RULE_NC
};
typedef struct _rule_stack_t {
size_t src_line : 8 * sizeof(size_t) - 8; // maximum bits storing source line number
size_t rule_id : 8; // this must be large enough to fit largest rule number
size_t arg_i : 16; // this dictates the maximum nodes in a "list" of things
size_t pt_off : 16;
} rule_stack_t;
typedef struct _mp_parse_chunk_t {
size_t alloc;
union {
size_t used;
struct _mp_parse_chunk_t *next;
} union_;
byte data[];
} mp_parse_chunk_t;
typedef enum {
PARSE_ERROR_NONE = 0,
PARSE_ERROR_MEMORY,
PARSE_ERROR_CONST,
} parse_error_t;
typedef struct _parser_t {
parse_error_t parse_error;
size_t rule_stack_alloc;
size_t rule_stack_top;
rule_stack_t *rule_stack;
mp_uint_t cur_scope_id;
size_t co_alloc;
size_t co_used;
mp_uint_t *co_data;
mp_lexer_t *lexer;
mp_parse_tree_t tree;
#if MICROPY_COMP_CONST
mp_map_t consts;
#endif
} parser_t;
STATIC void push_rule(parser_t *parser, size_t src_line, const rule_t *rule, size_t arg_i, size_t pt_off) {
if (parser->parse_error) {
return;
}
if (parser->rule_stack_top >= parser->rule_stack_alloc) {
rule_stack_t *rs = m_renew_maybe(rule_stack_t, parser->rule_stack, parser->rule_stack_alloc, parser->rule_stack_alloc + MICROPY_ALLOC_PARSE_RULE_INC, true);
if (rs == NULL) {
parser->parse_error = PARSE_ERROR_MEMORY;
return;
}
parser->rule_stack = rs;
parser->rule_stack_alloc += MICROPY_ALLOC_PARSE_RULE_INC;
}
rule_stack_t *rs = &parser->rule_stack[parser->rule_stack_top++];
rs->src_line = src_line;
rs->rule_id = rule->rule_id;
rs->arg_i = arg_i;
rs->pt_off = pt_off;
}
STATIC void push_rule_from_arg(parser_t *parser, size_t arg) {
assert((arg & RULE_ARG_KIND_MASK) == RULE_ARG_RULE || (arg & RULE_ARG_KIND_MASK) == RULE_ARG_OPT_RULE);
size_t rule_id = arg & RULE_ARG_ARG_MASK;
push_rule(parser, parser->lexer->tok_line, rules[rule_id], 0, 0);
}
STATIC void pop_rule(parser_t *parser, const rule_t **rule, size_t *arg_i, size_t *src_line, size_t *pt_off) {
assert(!parser->parse_error);
parser->rule_stack_top -= 1;
*rule = rules[parser->rule_stack[parser->rule_stack_top].rule_id];
*arg_i = parser->rule_stack[parser->rule_stack_top].arg_i;
*src_line = parser->rule_stack[parser->rule_stack_top].src_line;
*pt_off = parser->rule_stack[parser->rule_stack_top].pt_off;
}
typedef struct _pt_t {
vstr_t vv;
} pt_t;
STATIC pt_t *pt_new(void) {
pt_t *pt = m_new_obj(pt_t);
vstr_init(&pt->vv, 16);
return pt;
}
STATIC byte *pt_raw_add_blank(pt_t *pt, size_t nbytes) {
return (byte*)vstr_add_len(&pt->vv, nbytes);
}
STATIC byte *pt_raw_ins_blank(pt_t *pt, size_t pt_off, size_t nbytes) {
return (byte*)vstr_ins_blank_bytes(&pt->vv, pt_off, nbytes);
}
STATIC void pt_raw_truncate_at(pt_t *pt, size_t pt_off) {
pt->vv.len = pt_off;
}
STATIC int vuint_nbytes(size_t val) {
int n = 0;
do {
n += 1;
val >>= 7;
} while (val != 0);
return n;
}
STATIC void vuint_store(byte *p, int nbytes, size_t val) {
p += nbytes;
*--p = val & 0x7f;
for (--nbytes; nbytes > 0; --nbytes) {
val >>= 7;
*--p = 0x80 | (val & 0x7f);
}
}
STATIC size_t vuint_load(const byte **p_in) {
const byte *p = *p_in;
size_t val = 0;
do {
val = (val << 7) + (*p & 0x7f);
} while ((*p++ & 0x80) != 0);
*p_in = p;
return val;
}
STATIC byte *pt_advance(const byte *p, bool full_rule) {
switch (*p++) {
case MP_PT_NULL:
break;
case MP_PT_TOKEN:
p += 1;
break;
case MP_PT_SMALL_INT:
p += BYTES_PER_WORD;
break;
case MP_PT_STRING:
p += 2;
break;
case MP_PT_BYTES:
p += 2;
break;
case MP_PT_CONST_OBJECT:
vuint_load(&p);
break;
default:
if (p[-1] < MP_PT_RULE_BASE) {
// MP_PT_ID_BASE
p += 1;
} else {
// MP_PT_RULE_BASE
vuint_load(&p);
uint32_t n = vuint_load(&p);
if (full_rule) {
p += n;
}
}
break;
}
return (byte*)p;
}
bool mp_parse_node_get_int_maybe(const byte *p, mp_obj_t *o, mp_uint_t *co_data) {
if (pt_is_small_int(p)) {
*o = MP_OBJ_NEW_SMALL_INT(pt_small_int_value(p));
return true;
} else if (*p == MP_PT_CONST_OBJECT) {
size_t idx;
p = pt_extract_const_obj(p, &idx);
*o = (mp_obj_t)co_data[idx];
return true;
} else {
return false;
}
}
// TODO this could perhaps allow *p to be null and in that case return null?
const byte *mp_parse_node_extract_list(const byte **p, size_t pn_kind) {
if (pt_is_null(*p)) {
*p += 1;
return *p;
} else if (!pt_is_any_rule(*p)) {
return pt_advance(*p, true);
} else {
if (!pt_is_rule(*p, pn_kind)) {
return pt_advance(*p, true);
} else {
const byte *ptop;
*p = pt_rule_extract_top(*p, &ptop);
return ptop;
}
}
}
/*
const byte *pt_extract_id(const byte *p, qstr *qst) {
//assert(*p == MP_PT_ID_BASE);
*qst = p[1] | ((p[0] - MP_PT_ID_BASE) << 8);
return p + 2;
}
*/
const byte *pt_extract_const_obj(const byte *p, size_t *idx) {
assert(*p == MP_PT_CONST_OBJECT);
p += 1;
*idx = vuint_load(&p);
return p;
}
const byte *pt_get_small_int(const byte *p, mp_int_t *val) {
assert(*p == MP_PT_SMALL_INT);
*val = 0;
for (size_t i = 0; i < BYTES_PER_WORD; i++) {
*val |= (mp_int_t)*++p << (8 * i);
}
return p + 1;
}
mp_int_t pt_small_int_value(const byte *p) {
mp_int_t val;
pt_get_small_int(p, &val);
return val;
}
int pt_num_nodes(const byte *p, const byte *ptop) {
int n = 0;
while (p < ptop) {
n += 1;
p = pt_advance(p, true);
}
return n;
}
const byte *pt_next(const byte *p) {
return pt_advance(p, true);
}
const byte *pt_rule_first(const byte *p) {
return pt_advance(p, false);
}
#if 0
void pt_show(const byte *p, const byte *ptop) {
const byte *start = p;
while (p < ptop) {
printf("%04u ", (uint)(p - (byte*)start));
const byte *p2 = pt_advance(p, false);
for (const byte *p3 = p; p3 < p2; ++p3) {
printf("%02x ", *p3);
}
for (int i = 8 - (p2 - p); i > 0; --i) {
printf(" ");
}
switch (*p) {
case MP_PT_NULL:
printf("NULL\n");
break;
case MP_PT_TOKEN:
printf("TOKEN %u\n", p[1]);
break;
case MP_PT_SMALL_INT:
printf("SMALL_INT " INT_FMT "\n", pt_small_int_value(p));
break;
case MP_PT_STRING:
printf("STRING %s\n", qstr_str(p[1] | (p[2] << 8)));
break;
case MP_PT_BYTES:
printf("BYTES %s\n", qstr_str(p[1] | (p[2] << 8)));
break;
case MP_PT_CONST_OBJECT:
printf("CONST_OBJECT\n");
break;
default:
if (p[0] < MP_PT_RULE_BASE) {
// MP_PT_ID_BASE
printf("ID %s\n", qstr_str(p[1] | ((p[0] - MP_PT_ID_BASE) << 8)));
} else {
// MP_PT_RULE_BASE
byte rule_id = p[0] - MP_PT_RULE_BASE;
const byte *p4 = p + 1;
uint32_t src_line = vuint_load(&p4);
uint32_t n = vuint_load(&p4);
#if USE_RULE_NAME
printf("RULE %s line=%u bytes=%u\n", rules[rule_id]->rule_name, src_line, n);
#else
printf("RULE %d line=%u bytes=%u\n", rule_id, src_line, n);
#endif
}
break;
}
p = p2;
}
}
#endif
STATIC void pt_add_null(pt_t *pt) {
*pt_raw_add_blank(pt, 1) = MP_PT_NULL;
}
STATIC void pt_add_kind_byte(pt_t *pt, byte kind, byte b) {
byte *buf = pt_raw_add_blank(pt, 2);
buf[0] = kind;
buf[1] = b;
}
STATIC void pt_add_kind_qstr(pt_t *pt, byte kind, qstr qst) {
if (kind == MP_PT_ID_BASE) {
assert((qst >> 12) == 0);
byte *buf = pt_raw_add_blank(pt, 2);
buf[0] = MP_PT_ID_BASE + (qst >> 8);
buf[1] = qst;
} else {
assert((qst >> 16) == 0);
byte *buf = pt_raw_add_blank(pt, 3);
buf[0] = kind;
buf[1] = qst;
buf[2] = qst >> 8;
}
}
// valid for up to BYTES_PER_WORD=8
const byte pt_const_int0[] = {MP_PT_SMALL_INT, 0, 0, 0, 0, 0, 0, 0, 0};
STATIC void pt_add_kind_int(pt_t *pt, byte kind, mp_int_t val) {
byte *buf = pt_raw_add_blank(pt, 1 + BYTES_PER_WORD);
buf[0] = kind;
for (size_t i = 0; i < BYTES_PER_WORD; ++i) {
buf[i + 1] = val;
val >>= 8;
}
}
STATIC void pt_del_tail_bytes(pt_t *pt, size_t nbytes) {
vstr_cut_tail_bytes(&pt->vv, nbytes);
}
STATIC const byte *pt_del_byte(pt_t *pt, const byte *p) {
vstr_cut_out_bytes(&pt->vv, p - (byte*)pt->vv.buf, 1);
return p;
}
#if MICROPY_COMP_MODULE_CONST
#include "py/builtin.h"
STATIC const mp_rom_map_elem_t mp_constants_table[] = {
#if MICROPY_PY_UCTYPES
{ MP_ROM_QSTR(MP_QSTR_uctypes), MP_ROM_PTR(&mp_module_uctypes) },
#endif
// Extra constants as defined by a port
MICROPY_PORT_CONSTANTS
};
STATIC MP_DEFINE_CONST_MAP(mp_constants_map, mp_constants_table);
#endif
#if MICROPY_COMP_CONST_FOLDING
STATIC bool fold_constants(parser_t *parser, pt_t *pt, size_t pt_off, const rule_t *rule) {
(void)parser;
// this code does folding of arbitrary integer expressions, eg 1 + 2 * 3 + 4
// it does not do partial folding, eg 1 + 2 + x -> 3 + x
mp_int_t arg0;
if (rule->rule_id == RULE_expr
|| rule->rule_id == RULE_xor_expr
|| rule->rule_id == RULE_and_expr) {
// folding for binary ops: | ^ &
const byte *p = (byte*)pt->vv.buf + pt_off;
const byte *ptop = (byte*)pt->vv.buf + pt->vv.len;
if (*p != MP_PT_SMALL_INT) {
return false;
}
p = pt_get_small_int(p, &arg0);
while (p != ptop) {
if (*p != MP_PT_SMALL_INT) {
return false;
}
mp_int_t arg1;
p = pt_get_small_int(p, &arg1);
if (rule->rule_id == RULE_expr) {
// int | int
arg0 |= arg1;
} else if (rule->rule_id == RULE_xor_expr) {
// int ^ int
arg0 ^= arg1;
} else if (rule->rule_id == RULE_and_expr) {
// int & int
arg0 &= arg1;
}
}
} else if (rule->rule_id == RULE_shift_expr
|| rule->rule_id == RULE_arith_expr
|| rule->rule_id == RULE_term) {
// folding for binary ops: << >> + - * / % //
const byte *p = (byte*)pt->vv.buf + pt_off;
const byte *ptop = (byte*)pt->vv.buf + pt->vv.len;
if (*p != MP_PT_SMALL_INT) {
return false;
}
p = pt_get_small_int(p, &arg0);
while (p != ptop) {
p += 1; // it's a token
byte tok = *p++;
if (*p != MP_PT_SMALL_INT) {
return false;
}
mp_int_t arg1;
p = pt_get_small_int(p, &arg1);
if (tok == MP_TOKEN_OP_DBL_LESS) {
// int << int
if (arg1 >= (mp_int_t)BITS_PER_WORD
|| arg0 > (MP_SMALL_INT_MAX >> arg1)
|| arg0 < (MP_SMALL_INT_MIN >> arg1)) {
return false;
}
arg0 <<= arg1;
} else if (tok == MP_TOKEN_OP_DBL_MORE) {
// int >> int
if (arg1 >= (mp_int_t)BITS_PER_WORD) {
// Shifting to big amounts is underfined behavior
// in C and is CPU-dependent; propagate sign bit.
arg1 = BITS_PER_WORD - 1;
}
arg0 >>= arg1;
} else if (tok == MP_TOKEN_OP_PLUS) {
// int + int
arg0 += arg1;
} else if (tok == MP_TOKEN_OP_MINUS) {
// int - int
arg0 -= arg1;
} else if (tok == MP_TOKEN_OP_STAR) {
// int * int
if (mp_small_int_mul_overflow(arg0, arg1)) {
return false;
}
arg0 *= arg1;
} else if (tok == MP_TOKEN_OP_SLASH) {
// int / int
return false;
} else if (tok == MP_TOKEN_OP_PERCENT) {
// int % int
if (arg1 == 0) {
return false;
}
arg0 = mp_small_int_modulo(arg0, arg1);
} else {
assert(tok == MP_TOKEN_OP_DBL_SLASH); // should be
// int // int
if (arg1 == 0) {
return false;
}
arg0 = mp_small_int_floor_divide(arg0, arg1);
}
if (!MP_SMALL_INT_FITS(arg0)) {
return false;
}
}
} else if (rule->rule_id == RULE_factor_2) {
// folding for unary ops: + - ~
const byte *p = (byte*)pt->vv.buf + pt_off;
p += 1; // it's a token
byte tok = *p++;
if (*p != MP_PT_SMALL_INT) {
return false;
}
arg0 = pt_small_int_value(p);
if (tok == MP_TOKEN_OP_PLUS) {
// +int
} else if (tok == MP_TOKEN_OP_MINUS) {
// -int
arg0 = -arg0;
if (!MP_SMALL_INT_FITS(arg0)) {
return false;
}
} else {
assert(tok == MP_TOKEN_OP_TILDE); // should be
// ~int
arg0 = ~arg0;
}
#if 0&&MICROPY_COMP_CONST
} else if (rule->rule_id == RULE_expr_stmt) {
mp_parse_node_t pn1 = peek_result(parser, 0);
if (!MP_PARSE_NODE_IS_NULL(pn1)
&& !(MP_PARSE_NODE_IS_STRUCT_KIND(pn1, RULE_expr_stmt_augassign)
|| MP_PARSE_NODE_IS_STRUCT_KIND(pn1, RULE_expr_stmt_assign_list))) {
// this node is of the form <x> = <y>
mp_parse_node_t pn0 = peek_result(parser, 1);
if (MP_PARSE_NODE_IS_ID(pn0)
&& MP_PARSE_NODE_IS_STRUCT_KIND(pn1, RULE_power)
&& MP_PARSE_NODE_IS_ID(((mp_parse_node_struct_t*)pn1)->nodes[0])
&& MP_PARSE_NODE_LEAF_ARG(((mp_parse_node_struct_t*)pn1)->nodes[0]) == MP_QSTR_const
&& MP_PARSE_NODE_IS_STRUCT_KIND(((mp_parse_node_struct_t*)pn1)->nodes[1], RULE_trailer_paren)
&& MP_PARSE_NODE_IS_NULL(((mp_parse_node_struct_t*)pn1)->nodes[2])
) {
// code to assign dynamic constants: id = const(value)
// get the id
qstr id = MP_PARSE_NODE_LEAF_ARG(pn0);
// get the value
mp_parse_node_t pn_value = ((mp_parse_node_struct_t*)((mp_parse_node_struct_t*)pn1)->nodes[1])->nodes[0];
if (!MP_PARSE_NODE_IS_SMALL_INT(pn_value)) {
parser->parse_error = PARSE_ERROR_CONST;
return false;
}
mp_int_t value = MP_PARSE_NODE_LEAF_SMALL_INT(pn_value);
// store the value in the table of dynamic constants
mp_map_elem_t *elem = mp_map_lookup(&parser->consts, MP_OBJ_NEW_QSTR(id), MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
assert(elem->value == MP_OBJ_NULL);
elem->value = MP_OBJ_NEW_SMALL_INT(value);
// replace const(value) with value
pop_result(parser);
push_result_node(parser, pn_value);
// finished folding this assignment, but we still want it to be part of the tree
return false;
}
}
return false;
#endif
#if 0&&MICROPY_COMP_MODULE_CONST
} else if (rule->rule_id == RULE_power) {
mp_parse_node_t pn0 = peek_result(parser, 2);
mp_parse_node_t pn1 = peek_result(parser, 1);
mp_parse_node_t pn2 = peek_result(parser, 0);
if (!(MP_PARSE_NODE_IS_ID(pn0)
&& MP_PARSE_NODE_IS_STRUCT_KIND(pn1, RULE_trailer_period)
&& MP_PARSE_NODE_IS_NULL(pn2))) {
return false;
}
// id1.id2
// look it up in constant table, see if it can be replaced with an integer
mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pn1;
assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
qstr q_base = MP_PARSE_NODE_LEAF_ARG(pn0);
qstr q_attr = MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]);
mp_map_elem_t *elem = mp_map_lookup((mp_map_t*)&mp_constants_map, MP_OBJ_NEW_QSTR(q_base), MP_MAP_LOOKUP);
if (elem == NULL) {
return false;
}
mp_obj_t dest[2];
mp_load_method_maybe(elem->value, q_attr, dest);
if (!(MP_OBJ_IS_SMALL_INT(dest[0]) && dest[1] == MP_OBJ_NULL)) {
return false;
}
arg0 = MP_OBJ_SMALL_INT_VALUE(dest[0]);
#endif
} else {
return false;
}
// success folding this rule
pt_raw_truncate_at(pt, pt_off);
pt_add_kind_int(pt, MP_PT_SMALL_INT, arg0);
return true;
}
#endif
STATIC void pt_ins_rule(parser_t *parser, pt_t *pt, size_t pt_off, size_t src_line, const rule_t *rule, size_t num_args) {
(void)num_args;
// optimise away parenthesis around an expression if possible
if (rule->rule_id == RULE_atom_paren) {
// there should be just 1 arg for this rule
const byte *p = (byte*)pt->vv.buf + pt_off;
if (pt_is_null(p)) {
// need to keep parenthesis for ()
} else if (pt_is_rule(p, RULE_testlist_comp)) {
// need to keep parenthesis for (a, b, ...)
} else {
// parenthesis around a single expression, so it's just the expression
//printf("opt!\n");
return;
}
}
#if MICROPY_COMP_CONST_FOLDING
if (fold_constants(parser, pt, pt_off, rule)) {
// we folded this rule so return straight away
return;
}
#endif
#if 0
// TODO partial folding, eg 1 + 2 + x -> 3 + x
mp_int_t arg0;
if (rule->rule_id == RULE_expr
|| rule->rule_id == RULE_xor_expr
|| rule->rule_id == RULE_and_expr) {
// combined node folding for these rules
const byte *p = (byte*)pt->vv.buf + pt_off;
const byte *ptop = (byte*)pt->vv.buf + pt->vv.len;
if (*p != MP_PT_SMALL_INT) {
goto folding_fail;
}
p = pt_get_small_int(p, &arg0);
while (p != ptop) {
if (*p != MP_PT_SMALL_INT) {
goto folding_fail;
}
mp_int_t arg1;
p = pt_get_small_int(p, &arg1);
if (rule->rule_id == RULE_expr) {
// int | int
arg0 |= arg1;
} else if (rule->rule_id == RULE_xor_expr) {
// int ^ int
arg0 ^= arg1;
} else if (rule->rule_id == RULE_and_expr) {
// int & int
arg0 &= arg1;
}
if (!MP_SMALL_INT_FITS(arg0)) { // check needed?
goto folding_fail;
}
}
} else if (rule->rule_id == RULE_shift_expr
|| rule->rule_id == RULE_arith_expr
|| rule->rule_id == RULE_term) {
// combined node folding for these rules
const byte *p = (byte*)pt->vv.buf + pt_off;
const byte *ptop = (byte*)pt->vv.buf + pt->vv.len;
if (*p != MP_PT_SMALL_INT) {
goto folding_fail;
}
p = pt_get_small_int(p, &arg0);
while (p != ptop) {
p += 1; // it's a token
byte tok = *p++;
if (*p != MP_PT_SMALL_INT) {
goto folding_fail;
}
mp_int_t arg1;
p = pt_get_small_int(p, &arg1);
if (tok == MP_TOKEN_OP_DBL_LESS) {
// int << int
if (arg1 >= (mp_int_t)BITS_PER_WORD
|| arg0 > (MP_SMALL_INT_MAX >> arg1)
|| arg0 < (MP_SMALL_INT_MIN >> arg1)) {
goto folding_fail;
}
arg0 <<= arg1;
} else if (tok == MP_TOKEN_OP_DBL_MORE) {
// int >> int
if (arg1 >= (mp_int_t)BITS_PER_WORD) {
// Shifting to big amounts is underfined behavior
// in C and is CPU-dependent; propagate sign bit.
arg1 = BITS_PER_WORD - 1;
}
arg0 >>= arg1;
} else if (tok == MP_TOKEN_OP_PLUS) {
// int + int
arg0 += arg1;
} else if (tok == MP_TOKEN_OP_MINUS) {
// int - int
arg0 -= arg1;
} else if (tok == MP_TOKEN_OP_STAR) {
// int * int
if (mp_small_int_mul_overflow(arg0, arg1)) {
goto folding_fail;
}
arg0 *= arg1;
} else if (tok == MP_TOKEN_OP_SLASH) {
// int / int
goto folding_fail;
} else if (tok == MP_TOKEN_OP_PERCENT) {
// int % int
if (arg1 == 0) {
goto folding_fail;
}
arg0 = mp_small_int_modulo(arg0, arg1);
} else {
assert(tok == MP_TOKEN_OP_DBL_SLASH); // should be
// int // int
if (arg1 == 0) {
goto folding_fail;
}
arg0 = mp_small_int_floor_divide(arg0, arg1);
}
if (!MP_SMALL_INT_FITS(arg0)) {
goto folding_fail;
}
}
} else if (rule->rule_id == RULE_factor_2) {
const byte *p = (byte*)pt->vv.buf + pt_off;
p += 1; // it's a token
byte tok = *p++;
if (*p != MP_PT_SMALL_INT) {
goto folding_fail;
}
arg0 = pt_small_int_value(p);
if (tok == MP_TOKEN_OP_PLUS) {
// +int
} else if (tok == MP_TOKEN_OP_MINUS) {
// -int
arg0 = -arg0;
if (!MP_SMALL_INT_FITS(arg0)) {
goto folding_fail;
}
} else {
assert(tok == MP_TOKEN_OP_TILDE); // should be
// ~int
arg0 = ~arg0;
}
} else {
goto folding_fail;
}
// success folding this rule
pt_raw_truncate_at(pt, pt_off);
pt_add_kind_int(pt, MP_PT_SMALL_INT, arg0);
return;
folding_fail:;
#endif
int extra_node = 0;
/*
if (ADD_BLANK_NODE(rule)) {
extra_node = 1 + BYTES_PER_WORD; // for small int node
}
*/
size_t nbytes = pt->vv.len + extra_node - pt_off;
int nb1 = vuint_nbytes(src_line);
int nb2 = vuint_nbytes(nbytes);
byte *dest = (byte*)pt_raw_ins_blank(pt, pt_off, 1 + nb1 + nb2 + extra_node);
dest[0] = MP_PT_RULE_BASE + rule->rule_id;
vuint_store(dest + 1, nb1, src_line);
vuint_store(dest + 1 + nb1, nb2, nbytes);
// insert small int node for scope index
if (extra_node != 0) {
dest[1 + nb1 + nb2] = MP_PT_SMALL_INT;
size_t val = ++parser->cur_scope_id;
for (size_t i = 0; i < BYTES_PER_WORD; ++i) {
dest[1 + nb1 + nb2 + 1 + i] = val;
val >>= 8;
}
}
}
STATIC void make_node_const_object(parser_t *parser, pt_t *pt, mp_obj_t obj) {
int nb = vuint_nbytes(parser->co_used);
byte *buf = pt_raw_add_blank(pt, 1 + nb);
buf[0] = MP_PT_CONST_OBJECT;
vuint_store(buf + 1, nb, parser->co_used);
if (parser->co_used >= parser->co_alloc) {
// TODO use m_renew_maybe
size_t alloc = parser->co_alloc + 8;
parser->co_data = m_renew(mp_uint_t, parser->co_data, parser->co_alloc, alloc);
parser->co_alloc = alloc;
}
parser->co_data[parser->co_used++] = (mp_uint_t)obj;
}
STATIC void make_node_string_bytes(parser_t *parser, pt_t *pt, mp_token_kind_t tok, const char *str, size_t len) {
mp_obj_t o;
if (tok == MP_TOKEN_STRING) {
o = mp_obj_new_str(str, len, false);
} else {
o = mp_obj_new_bytes((const byte*)str, len);
}
make_node_const_object(parser, pt, o);
}
STATIC bool pt_add_token(parser_t *parser, pt_t *pt) {
mp_lexer_t *lex = parser->lexer;
if (lex->tok_kind == MP_TOKEN_NAME) {
qstr id = qstr_from_strn(lex->vstr.buf, lex->vstr.len);
#if MICROPY_COMP_CONST
// lookup identifier in table of dynamic constants
mp_map_elem_t *elem = mp_map_lookup(&parser->consts, MP_OBJ_NEW_QSTR(id), MP_MAP_LOOKUP);
if (elem != NULL) {
pt_add_kind_int(pt, MP_PT_SMALL_INT, MP_OBJ_SMALL_INT_VALUE(elem->value));
} else
#endif
{
pt_add_kind_qstr(pt, MP_PT_ID_BASE, id);
}
} else if (lex->tok_kind == MP_TOKEN_INTEGER) {
mp_obj_t o = mp_parse_num_integer(lex->vstr.buf, lex->vstr.len, 0, lex);
if (MP_OBJ_IS_SMALL_INT(o)) {
pt_add_kind_int(pt, MP_PT_SMALL_INT, MP_OBJ_SMALL_INT_VALUE(o));
} else {
make_node_const_object(parser, pt, o);
}
} else if (lex->tok_kind == MP_TOKEN_FLOAT_OR_IMAG) {
mp_obj_t o = mp_parse_num_decimal(lex->vstr.buf, lex->vstr.len, true, false, lex);
make_node_const_object(parser, pt, o);
} else if (lex->tok_kind == MP_TOKEN_STRING || lex->tok_kind == MP_TOKEN_BYTES) {
// join adjacent string/bytes literals
mp_token_kind_t tok_kind = lex->tok_kind;
vstr_t vstr;
vstr_init(&vstr, lex->vstr.len);
do {
vstr_add_strn(&vstr, lex->vstr.buf, lex->vstr.len);
mp_lexer_to_next(lex);
} while (lex->tok_kind == tok_kind);
if (lex->tok_kind == MP_TOKEN_STRING || lex->tok_kind == MP_TOKEN_BYTES) {
return false;
}
// Don't automatically intern all strings/bytes. doc strings (which are usually large)
// will be discarded by the compiler, and so we shouldn't intern them.
qstr qst = MP_QSTR_NULL;
if (vstr.len <= MICROPY_ALLOC_PARSE_INTERN_STRING_LEN) {
// intern short strings
qst = qstr_from_strn(vstr.buf, vstr.len);
} else {
// check if this string is already interned
qst = qstr_find_strn(vstr.buf, vstr.len);
}
if (qst != MP_QSTR_NULL) {
// qstr exists, make a leaf node
pt_add_kind_qstr(pt, tok_kind == MP_TOKEN_STRING ? MP_PT_STRING : MP_PT_BYTES, qst);
} else {
// not interned, make a node holding a pointer to the string/bytes data
make_node_string_bytes(parser, pt, tok_kind, vstr.buf, vstr.len);
}
vstr_clear(&vstr);
return true;
} else {
pt_add_kind_byte(pt, MP_PT_TOKEN, lex->tok_kind);
}
mp_lexer_to_next(lex);
return true;
}
const byte *pt_rule_extract_top(const byte *p, const byte **ptop) {
assert(*p >= MP_PT_RULE_BASE);
p++;
vuint_load(&p);
size_t nbytes = vuint_load(&p);
*ptop = p + nbytes;
return p;
}
const byte *pt_rule_extract(const byte *p, size_t *rule_id, size_t *src_line, const byte **ptop) {
assert(*p >= MP_PT_RULE_BASE);
*rule_id = *p++ - MP_PT_RULE_BASE;
*src_line = vuint_load(&p);
size_t nbytes = vuint_load(&p);
*ptop = p + nbytes;
return p;
}
bool pt_is_rule_empty(const byte *p) {
const byte *ptop;
p = pt_rule_extract_top(p, &ptop);
return p == ptop;
}
mp_parse_tree_t mp_parse(mp_lexer_t *lex, mp_parse_input_kind_t input_kind) {
// initialise parser and allocate memory for its stacks
parser_t parser;
parser.parse_error = PARSE_ERROR_NONE;
parser.rule_stack_alloc = MICROPY_ALLOC_PARSE_RULE_INIT;
parser.rule_stack_top = 0;
parser.rule_stack = m_new_maybe(rule_stack_t, parser.rule_stack_alloc);
parser.cur_scope_id = 0;
parser.co_alloc = 0;
parser.co_used = 0;
parser.co_data = NULL;
parser.lexer = lex;
parser.tree.chunk = NULL;
#if MICROPY_COMP_CONST
mp_map_init(&parser.consts, 0);
#endif
// check if we could allocate the stacks
if (parser.rule_stack == NULL) {
goto memory_error;
}
// work out the top-level rule to use, and push it on the stack
size_t top_level_rule;
switch (input_kind) {
case MP_PARSE_SINGLE_INPUT: top_level_rule = RULE_single_input; break;
case MP_PARSE_EVAL_INPUT: top_level_rule = RULE_eval_input; break;
default: top_level_rule = RULE_file_input;
}
push_rule(&parser, lex->tok_line, rules[top_level_rule], 0, 0);
// parse!
size_t n, i; // state for the current rule
size_t pt_off = 0; // state for the current rule
size_t rule_src_line; // source line for the first token matched by the current rule
bool backtrack = false;
const rule_t *rule = NULL;
pt_t *pt = pt_new();
for (;;) {
next_rule:
if (parser.rule_stack_top == 0 || parser.parse_error) {
break;
}
pop_rule(&parser, &rule, &i, &rule_src_line, &pt_off);
n = rule->act & RULE_ACT_ARG_MASK;
if (i == 0) {
pt_off = pt->vv.len;
}
/*
// debugging
printf("depth=%d ", parser.rule_stack_top);
for (int j = 0; j < parser.rule_stack_top; ++j) {
printf(" ");
}
printf("%s n=%d i=%d bt=%d\n", rule->rule_name, n, i, backtrack);
*/
switch (rule->act & RULE_ACT_KIND_MASK) {
case RULE_ACT_OR:
if (i > 0 && !backtrack) {
goto next_rule;
} else {
backtrack = false;
}
for (; i < n; ++i) {
uint16_t kind = rule->arg[i] & RULE_ARG_KIND_MASK;
if (kind == RULE_ARG_TOK) {
if (lex->tok_kind == (rule->arg[i] & RULE_ARG_ARG_MASK)) {
if (!pt_add_token(&parser, pt)) {
goto syntax_error;
}
goto next_rule;
}
} else {
assert(kind == RULE_ARG_RULE);
if (i + 1 < n) {
push_rule(&parser, rule_src_line, rule, i + 1, pt_off); // save this or-rule
}
push_rule_from_arg(&parser, rule->arg[i]); // push child of or-rule
goto next_rule;
}
}
backtrack = true;
break;
case RULE_ACT_AND: {
// failed, backtrack if we can, else syntax error
if (backtrack) {
assert(i > 0);
if ((rule->arg[i - 1] & RULE_ARG_KIND_MASK) == RULE_ARG_OPT_RULE) {
// an optional rule that failed, so continue with next arg
pt_add_null(pt);
backtrack = false;
} else {
// a mandatory rule that failed, so propagate backtrack
if (i > 1) {
// already eaten tokens so can't backtrack
goto syntax_error;
} else {
goto next_rule;
}
}
}
// progress through the rule
for (; i < n; ++i) {
switch (rule->arg[i] & RULE_ARG_KIND_MASK) {
case RULE_ARG_TOK: {
// need to match a token
mp_token_kind_t tok_kind = rule->arg[i] & RULE_ARG_ARG_MASK;
if (lex->tok_kind == tok_kind) {
// matched token
if (tok_kind == MP_TOKEN_NAME) {
pt_add_kind_qstr(pt, MP_PT_ID_BASE, qstr_from_strn(lex->vstr.buf, lex->vstr.len));
}
if (i == 0 && ADD_BLANK_NODE(rule)) {
pt_add_kind_int(pt, MP_PT_SMALL_INT, ++parser.cur_scope_id);
}
mp_lexer_to_next(lex);
} else {
// failed to match token
if (i > 0) {
// already eaten tokens so can't backtrack
goto syntax_error;
} else {
// this rule failed, so backtrack
backtrack = true;
goto next_rule;
}
}
break;
}
case RULE_ARG_RULE:
case RULE_ARG_OPT_RULE:
default:
push_rule(&parser, rule_src_line, rule, i + 1, pt_off); // save this and-rule
push_rule_from_arg(&parser, rule->arg[i]); // push child of and-rule
goto next_rule;
}
}
assert(i == n);
// matched the rule, so now build the corresponding parse_node
// count number of arguments for the parse_node
i = 0;
bool emit_rule = false;
/*
for (size_t x = 0; x < n; ++x) {
if ((rule->arg[x] & RULE_ARG_KIND_MASK) == RULE_ARG_TOK) {
mp_token_kind_t tok_kind = rule->arg[x] & RULE_ARG_ARG_MASK;
if (tok_kind >= MP_TOKEN_NAME) {
emit_rule = true;
}
if (tok_kind == MP_TOKEN_NAME) {
// only tokens which were names are pushed to stack
i += 1;
}
} else {
// rules are always pushed
i += 1;
}
}
*/
for (size_t x = 0; x < n; ++x) {
if ((rule->arg[x] & RULE_ARG_KIND_MASK) == RULE_ARG_TOK) {
mp_token_kind_t tok_kind = rule->arg[x] & RULE_ARG_ARG_MASK;
if (tok_kind >= MP_TOKEN_NAME) {
emit_rule = true;
}
}
}
for (const byte *p = (byte*)pt->vv.buf + pt_off; p < (byte*)pt->vv.buf + pt->vv.len;) {
i += 1;
p = pt_advance(p, true);
}
#if 0 && !MICROPY_ENABLE_DOC_STRING
// this code discards lonely statements, such as doc strings
if (input_kind != MP_PARSE_SINGLE_INPUT && rule->rule_id == RULE_expr_stmt && peek_result(&parser, 0) == MP_PARSE_NODE_NULL) {
mp_parse_node_t p = peek_result(&parser, 1);
if ((MP_PARSE_NODE_IS_LEAF(p) && !MP_PARSE_NODE_IS_ID(p)) || MP_PARSE_NODE_IS_STRUCT_KIND(p, RULE_string)) {
pop_result(&parser); // MP_PARSE_NODE_NULL
mp_parse_node_t pn = pop_result(&parser); // possibly RULE_string
if (MP_PARSE_NODE_IS_STRUCT(pn)) {
mp_parse_node_struct_t *pns = (mp_parse_node_struct_t *)pn;
if (MP_PARSE_NODE_STRUCT_KIND(pns) == RULE_string) {
m_del(char, (char*)pns->nodes[0], (mp_uint_t)pns->nodes[1]);
}
}
push_result_rule(&parser, rule_src_line, rules[RULE_pass_stmt], 0);
break;
}
}
#endif
// always emit these rules, even if they have only 1 argument
if (rule->rule_id == RULE_expr_stmt || rule->rule_id == RULE_yield_stmt) {
emit_rule = true;
}
// if a rule has the RULE_ACT_ALLOW_IDENT bit set then this
// rule should not be emitted if it has only 1 argument
if (rule->act & RULE_ACT_ALLOW_IDENT) {
emit_rule = false;
}
// always emit these rules, and add an extra blank node at the end (to be used by the compiler to store data)
if (ADD_BLANK_NODE(rule)) {
emit_rule = true;
// TODO
//add_result_node(&parser, MP_PARSE_NODE_NULL);
//i += 1;
}
// count number of non-null nodes
size_t num_not_null = 0;
size_t num_trail_null = 0;
{ const byte *p = (byte*)pt->vv.buf + pt_off;
for (size_t x = 0; x < i; ++x) {
if (*p != MP_PT_NULL) {
num_not_null += 1;
num_trail_null = 0;
} else {
num_trail_null += 1;
}
p = pt_advance(p, true);
}}
if (emit_rule || num_not_null != 1) {
// need to add rule when num_not_null == 0 for, eg, atom_paren, testlist_comp_3b
pt_del_tail_bytes(pt, num_trail_null); // remove trailing null nodes, they are store implicitly
pt_ins_rule(&parser, pt, pt_off, rule_src_line, rule, i - num_trail_null);
} else {
// single result, leave it on stack
const byte *p = (byte*)pt->vv.buf + pt_off;
for (size_t x = 0; x < i; ++x) {
if (*p == MP_PT_NULL) {
p = pt_del_byte(pt, p);
} else {
p = pt_advance(p, true);
}
}
}
break;
}
case RULE_ACT_LIST:
default: // nothing else
{
// n=2 is: item item*
// n=1 is: item (sep item)*
// n=3 is: item (sep item)* [sep]
bool had_trailing_sep;
if (backtrack) {
list_backtrack:
had_trailing_sep = false;
if (n == 2) {
if (i == 1) {
// fail on item, first time round; propagate backtrack
goto next_rule;
} else {
// fail on item, in later rounds; finish with this rule
backtrack = false;
}
} else {
if (i == 1) {
// fail on item, first time round; propagate backtrack
goto next_rule;
} else if ((i & 1) == 1) {
// fail on item, in later rounds; have eaten tokens so can't backtrack
if (n == 3) {
// list allows trailing separator; finish parsing list
had_trailing_sep = true;
backtrack = false;
} else {
// list doesn't allowing trailing separator; fail
goto syntax_error;
}
} else {
// fail on separator; finish parsing list
backtrack = false;
}
}
} else {
for (;;) {
size_t arg = rule->arg[i & 1 & n];
switch (arg & RULE_ARG_KIND_MASK) {
case RULE_ARG_TOK:
if (lex->tok_kind == (arg & RULE_ARG_ARG_MASK)) {
if (i & 1 & n) {
// separators which are tokens are not pushed to result stack
mp_lexer_to_next(lex);
} else {
pt_add_token(&parser, pt);
}
// got element of list, so continue parsing list
i += 1;
} else {
// couldn't get element of list
i += 1;
backtrack = true;
goto list_backtrack;
}
break;
case RULE_ARG_RULE:
rule_list_no_other_choice:
push_rule(&parser, rule_src_line, rule, i + 1, pt_off); // save this list-rule
push_rule_from_arg(&parser, arg); // push child of list-rule
goto next_rule;
default:
assert(0);
goto rule_list_no_other_choice; // to help flow control analysis
}
}
}
assert(i >= 1);
// compute number of elements in list, result in i
i -= 1;
if ((n & 1) && (rule->arg[1] & RULE_ARG_KIND_MASK) == RULE_ARG_TOK) {
// don't count separators when they are tokens
i = (i + 1) / 2;
}
if (i == 1) {
// list matched single item
if (had_trailing_sep) {
// if there was a trailing separator, make a list of a single item
pt_ins_rule(&parser, pt, pt_off, rule_src_line, rule, i);
} else {
// just leave single item on stack (ie don't wrap in a list)
}
} else {
pt_ins_rule(&parser, pt, pt_off, rule_src_line, rule, i);
}
break;
}
}
}
#if MICROPY_COMP_CONST
mp_map_deinit(&parser.consts);
#endif
#if 0
pt_show((const byte*)pt->vv.buf, (const byte*)pt->vv.buf + pt->vv.len);
{
size_t n_pool, n_qstr, n_str_data_bytes, n_total_bytes;
qstr_pool_info(&n_pool, &n_qstr, &n_str_data_bytes, &n_total_bytes);
printf("qstr pool: n_pool=" UINT_FMT ", n_qstr=" UINT_FMT ", n_str_data_bytes="
UINT_FMT ", n_total_bytes=" UINT_FMT "\n",
n_pool, n_qstr, n_str_data_bytes, n_total_bytes);
}
#endif
mp_obj_t exc;
if (parser.parse_error) {
#if MICROPY_COMP_CONST
if (parser.parse_error == PARSE_ERROR_CONST) {
exc = mp_obj_new_exception_msg(&mp_type_SyntaxError,
"constant must be an integer");
} else
#endif
{
assert(parser.parse_error == PARSE_ERROR_MEMORY);
memory_error:
exc = mp_obj_new_exception_msg(&mp_type_MemoryError,
"parser could not allocate enough memory");
}
parser.tree.root = NULL;
} else if (
lex->tok_kind != MP_TOKEN_END // check we are at the end of the token stream
|| pt->vv.len == 0 // check that we got a node (can fail on empty input)
) {
syntax_error:
if (lex->tok_kind == MP_TOKEN_INDENT) {
exc = mp_obj_new_exception_msg(&mp_type_IndentationError,
"unexpected indent");
} else if (lex->tok_kind == MP_TOKEN_DEDENT_MISMATCH) {
exc = mp_obj_new_exception_msg(&mp_type_IndentationError,
"unindent does not match any outer indentation level");
} else if (lex->tok_kind == MP_TOKEN_STRING || lex->tok_kind == MP_TOKEN_BYTES) {
exc = mp_obj_new_exception_msg(&mp_type_SyntaxError,
"cannot mix bytes and nonbytes literals");
} else {
exc = mp_obj_new_exception_msg(&mp_type_SyntaxError,
"invalid syntax");
}
parser.tree.root = NULL;
} else {
// no errors
//result_stack_show(parser);
//printf("rule stack alloc: %d\n", parser.rule_stack_alloc);
//printf("result stack alloc: %d\n", parser.result_stack_alloc);
//printf("number of parse nodes allocated: %d\n", num_parse_nodes_allocated);
// add number of scopes
pt_add_kind_int(pt, MP_PT_SMALL_INT, parser.cur_scope_id + 1);
// get the root parse node that we created
//assert(parser.result_stack_top == 1);
exc = MP_OBJ_NULL;
parser.tree.root = (byte*)pt->vv.buf;
parser.tree.co_data = parser.co_data;
}
// free the memory that we don't need anymore
m_del(rule_stack_t, parser.rule_stack, parser.rule_stack_alloc);
// we also free the lexer on behalf of the caller (see below)
if (exc != MP_OBJ_NULL) {
// had an error so raise the exception
// add traceback to give info about file name and location
// we don't have a 'block' name, so just pass the NULL qstr to indicate this
mp_obj_exception_add_traceback(exc, lex->source_name, lex->tok_line, MP_QSTR_NULL);
mp_lexer_free(lex);
nlr_raise(exc);
} else {
mp_lexer_free(lex);
return parser.tree;
}
}
void mp_parse_tree_clear(mp_parse_tree_t *tree) {
mp_parse_chunk_t *chunk = tree->chunk;
while (chunk != NULL) {
mp_parse_chunk_t *next = chunk->union_.next;
m_del(byte, chunk, sizeof(mp_parse_chunk_t) + chunk->alloc);
chunk = next;
}
}
#endif // MICROPY_ENABLE_COMPILER && MICROPY_USE_SMALL_HEAP_COMPILER
|