Skip to content
This repository
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 1530 lines (1220 sloc) 43.688 kb
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
%top{

/* ex: set ro ft=c: -*- buffer-read-only:t -*-
 * !!!!!!! DO NOT EDIT THIS FILE !!!!!!!
 *
 * This file is generated automatically by the Parrot build process
 * from the file compilers/imcc/imcc.l.
 *
 * Any changes made here will be lost!
 *
*/

/* HEADERIZER HFILE: none */
/* HEADERIZER STOP */

#ifndef __STDC_VERSION__
# define __STDC_VERSION__ 0
#endif

#include "imc.h"
#include "parser.h"

}

%{
/*
 * imcc.l
 *
 * Intermediate Code Compiler for Parrot
 *
 * Copyright (C) 2002 Melvin Smith <melvin.smith@mindspring.com>
 * Copyright (C) 2002-2008, Parrot Foundation.
 *
 * The tokenizer.
 *
 *
 */
/* static function declarations */
static void pop_parser_state(ARGMOD(imc_info_t *imcc), ARGMOD(void *yyscanner));

static struct macro_frame_t *new_frame(ARGMOD(imc_info_t *imcc));

static void define_macro(ARGMOD(imc_info_t *imcc), ARGIN(const char *name), ARGIN(const params_t *params),
        ARGIN(const char *expansion), int start_line);

static macro_t *find_macro(ARGMOD(imc_info_t *imcc), ARGIN(const char *name));

static void scan_string(macro_frame_t *frame, ARGIN(const char *expansion),
        ARGMOD(void *yyscanner));

static void scan_file(ARGMOD(imc_info_t *imcc), ARGIN(struct macro_frame_t *frame), PIOHANDLE file,
        ARGMOD(void *yyscanner));

static int destroy_frame(macro_frame_t *frame, ARGMOD(void *yyscanner));

static int yylex_skip(YYSTYPE *valp, ARGMOD(imc_info_t *imcc), ARGIN(const char *skip),
        ARGMOD(void *yyscanner));

static int read_macro(YYSTYPE *valp, ARGMOD(imc_info_t *imcc), ARGMOD(void *yyscanner));

static int expand_macro(ARGMOD(imc_info_t *imcc), ARGIN(const char *name), ARGMOD(void *yyscanner));

static void include_file(ARGMOD(imc_info_t *imcc), ARGIN(STRING *file_name), ARGMOD(void *yyscanner));

static int handle_identifier(ARGMOD(imc_info_t *imcc), YYSTYPE *valp, ARGIN(const char *id));

#define YY_DECL int yylex(YYSTYPE *valp, yyscan_t yyscanner, ARGMOD(imc_info_t *imcc))

#define YYCHOP() (yytext[--yyleng] = '\0')

#define SET_LINE_NUMBER (imcc->line = yylineno)

#define DUP_AND_RET(valp, token) \
  do { \
      if (valp) (valp)->s = mem_sys_strndup(yytext, yyleng); \
      return (token); \
  } while (0)

#define DUP_AND_RET_FREE(valp, token) \
  do { \
      if (valp) { \
          mem_sys_free((valp)->s); \
          (valp)->s = mem_sys_strndup(yytext, yyleng); \
          return (token); \
      } \
  } while (0)

#define YY_INPUT(buf, result, max_size) \
    (result) = Parrot_io_internal_read((Interp *)yyextra->interp, (PIOHANDLE)yyin, (buf), (max_size))

%}

%option reentrant
%option never-interactive
%option stack
%option yylineno

LETTER [a-zA-Z_@]
DIGIT [0-9]
DIGITS {DIGIT}+
HEX 0[xX][0-9A-Fa-f]+
OCT 0[oO][0-7]+
BIN 0[bB][01]+
DOT [.]
SIGN [-+]
FLOATNUM {SIGN}?(({DIGITS}{DOT}{DIGIT}*|{DOT}{DIGITS})([eE]{SIGN}?{DIGITS})?|{DIGITS}[eE]{SIGN}?{DIGITS})
LETTERDIGIT [a-zA-Z0-9_]
LABELLETTERDIGIT([a-zA-Z0-9_@])
ID {LETTER}{LABELLETTERDIGIT}*
DQ_STRING \"(\\.|[^"\\\n])*\"
ENCCHAR {LETTER}|{DIGIT}|"-"
ENCCHARS {ENCCHAR}*
ENC {LETTER}{ENCCHARS}":"
UNICODE {ENC}{DQ_STRING}
STRINGCONSTANT {SQ_STRING}|{DQ_STRING}
SQ_STRING \'[^'\n]*\'
EOL \r?\n
WS [\t\f\r\x1a ]
SP [ ]

%x emit
%x macro
%x pod
%x cmt1
%x cmt2
%x cmt3
%x cmt4
%x cmt5
%x heredoc1
%x heredoc2


%%
        /* for emacs "*/
        if (imcc->expect_pasm == 1 && !imcc->in_pod) {
            imcc->expect_pasm = 2;
            BEGIN(emit);
        }

        if (imcc->frames->s.pasm_file && YYSTATE == INITIAL &&
            !imcc->in_pod)
        {
            if (imcc->frames->s.pasm_file == 1) {
                BEGIN(emit);
                return EMIT;
            }

            return 0;
        }

<*>^{WS}* { SET_LINE_NUMBER; }

<heredoc1>.*{EOL} {
            SET_LINE_NUMBER;
            imcc->frames->heredoc_rest = mem_sys_strndup(yytext, yyleng);
            BEGIN(heredoc2);
    }

<heredoc2>{EOL} {
        /* heredocs have highest priority
         * arrange them before all wildcard state matches */

        /* Newline in the heredoc. Realloc and cat on. */
        imcc->heredoc_content =
            (char*)mem_sys_realloc(imcc->heredoc_content,
                            strlen(imcc->heredoc_content) + 3);
        strcpy(imcc->heredoc_content +
               strlen(imcc->heredoc_content), "\n");
    }

<heredoc2>([^\r\n]|\r[^\n])* {
        SET_LINE_NUMBER;
        /* Are we at the end of the heredoc? */
        if (STREQ(imcc->heredoc_end, yytext)) {
            /* End of the heredoc. */
            yyguts_t * const yyg = (yyguts_t *)yyscanner;
            const int len = strlen(imcc->heredoc_content);

            /* delim */
            imcc->heredoc_content[len] =
                imcc->heredoc_content[0];

            imcc->heredoc_content[len + 1] = 0;

            mem_sys_free(imcc->heredoc_end);
            imcc->heredoc_end = NULL;

            imcc->frames->buffer = YY_CURRENT_BUFFER;
            valp->s =
                imcc->heredoc_content;

            yy_pop_state(yyscanner);
            yy_scan_string(imcc->frames->heredoc_rest, yyscanner);

            return STRINGC;
        }
        else {
            /* Part of the heredoc. Realloc and cat the line on. */
            imcc->heredoc_content =
                (char *)mem_sys_realloc(imcc->heredoc_content,
                                strlen(imcc->heredoc_content) +
                                strlen(yytext) + 2);
            strcpy(imcc->heredoc_content +
                   strlen(imcc->heredoc_content), yytext);
        }
    }

<cmt2>[^"]+ {
        STRING *str;

        yy_pop_state(yyscanner);
        yy_push_state(cmt3, yyscanner);

        str = Parrot_str_new(imcc->interp, yytext,0);
        imcc->frames->s.file = str;
        imcc->cur_unit->file = str;

        return FILECOMMENT;
    }

<cmt3>["] {
        yy_pop_state(yyscanner);
        yy_push_state(cmt4, yyscanner);
    }

<cmt1>{DIGITS} {
        yylineno = imcc->line = atoi(yytext);
        yy_pop_state(yyscanner);
        yy_push_state(cmt4, yyscanner);
        return LINECOMMENT;
    }

<cmt4>.*{EOL} {
        yy_pop_state(yyscanner);
    }

<INITIAL,emit>{EOL} {
        if (imcc->expect_pasm == 2)
            BEGIN(INITIAL);

        imcc->expect_pasm = 0;

        return '\n';
    }

<INITIAL,emit># {
        yy_push_state(cmt5, yyscanner);
    }

<cmt5>.*{EOL} {
        if (imcc->expect_pasm == 2)
            BEGIN(INITIAL);
        else
            yy_pop_state(yyscanner);

        imcc->expect_pasm = 0;

        return '\n';
    }


<INITIAL,emit,macro>^"=cut"{EOL} {
    /* this is a stand-alone =cut, but we're not in POD mode, so ignore. */
    SET_LINE_NUMBER;
}

<INITIAL,emit,macro>^"=".*{EOL} {
        SET_LINE_NUMBER;
        imcc->in_pod = 1;
        yy_push_state(pod, yyscanner);
    }

<pod>^"=cut"{EOL} {
        SET_LINE_NUMBER;
        imcc->in_pod = 0;
        yy_pop_state(yyscanner);
    }

<pod>.* { SET_LINE_NUMBER; }

<pod>{EOL} { /* ignore */ }

<INITIAL,emit>".annotate" return ANNOTATE;
<INITIAL,emit>".lex" return LEXICAL;
".set_arg" return ARG;
".sub" { SET_LINE_NUMBER; return SUB; }
".end" return ESUB;
".begin_call" return PCC_BEGIN;
".end_call" return PCC_END;
".call" return PCC_CALL;
".invocant" return INVOCANT;
<emit,INITIAL>".pcc_sub" return PCC_SUB;
".begin_return" return PCC_BEGIN_RETURN;
".end_return" return PCC_END_RETURN;
".begin_yield" return PCC_BEGIN_YIELD;
".end_yield" return PCC_END_YIELD;

<emit,INITIAL>":method" return METHOD;
<emit,INITIAL>":multi" return MULTI;
<emit,INITIAL>":main" return MAIN;
<emit,INITIAL>":load" return LOAD;
<emit,INITIAL>":init" return INIT;
<emit,INITIAL>":immediate" return IMMEDIATE;
<emit,INITIAL>":postcomp" return POSTCOMP;
<emit,INITIAL>":tag" return SUBTAG;
<emit,INITIAL>":anon" return ANON;
<emit,INITIAL>":outer" return OUTER;
<emit,INITIAL>":lex" return NEED_LEX;
<emit,INITIAL>":vtable" return VTABLE_METHOD;
<emit,INITIAL>":nsentry" return NS_ENTRY;
":instanceof" return SUB_INSTANCE_OF;
":subid" return SUBID;

".get_result" return RESULT;
".get_results" return GET_RESULTS;
".yield" return YIELDT;
".set_yield" return SET_YIELD;
".return" return RETURN;
".set_return" return SET_RETURN;
".tailcall" return TAILCALL;
<emit,INITIAL>".loadlib" return LOADLIB;

":flat" return ADV_FLAT;
":slurpy" return ADV_SLURPY;
":optional" return ADV_OPTIONAL;
":opt_flag" return ADV_OPT_FLAG;
":named" return ADV_NAMED;
"=>" return ADV_ARROW;
":invocant" return ADV_INVOCANT;
":call_sig" return ADV_CALL_SIG;

<emit,INITIAL>".namespace" return NAMESPACE;
<emit,INITIAL>".HLL" return HLL;

".local" return LOCAL;
<emit,INITIAL>".const" return CONST;
".globalconst" return GLOBAL_CONST;
".param" return PARAM;
"goto" return GOTO;
"if" return IF;
"unless" return UNLESS;
"null" return PNULL;
"int" return INTV;
"num" return FLOATV;

"pmc" return PMCV;
"string" return STRINGV;
"<<" return SHIFT_LEFT;
">>" return SHIFT_RIGHT;
">>>" return SHIFT_RIGHT_U;
"&&" return LOG_AND;
"||" return LOG_OR;
"~~" return LOG_XOR;
"<" return RELOP_LT;
"<=" return RELOP_LTE;
">" return RELOP_GT;
">=" return RELOP_GTE;
"==" return RELOP_EQ;
"!=" return RELOP_NE;
"**" return POW;

{WS}+"."{WS}+ return CONCAT;
"." return DOT;

"+=" return PLUS_ASSIGN;
"-=" return MINUS_ASSIGN;
"*=" return MUL_ASSIGN;
"/=" return DIV_ASSIGN;
"%=" return MOD_ASSIGN;
"//" return FDIV;
"//=" return FDIV_ASSIGN;
"&=" return BAND_ASSIGN;
"|=" return BOR_ASSIGN;
"~=" return BXOR_ASSIGN;
">>=" return SHR_ASSIGN;
"<<=" return SHL_ASSIGN;
">>>=" return SHR_U_ASSIGN;
".=" return CONCAT_ASSIGN;

<emit,INITIAL>".macro_const" {
        char *macro_name = NULL;
        int start_cond = YY_START;
        int macro_exists = 0;
        int c;
        int start_line;

        BEGIN(macro);
        c = yylex_skip(valp, imcc, " ", yyscanner);

        if (c != IDENTIFIER)
            IMCC_fataly(imcc, EXCEPTION_SYNTAX_ERROR,
               "Constant names must be identifiers");

        imcc->cur_macro_name = macro_name = valp->s;
        start_line = imcc->line;

        c = yylex_skip(valp, imcc, " ", yyscanner);

        if (c != INTC && c != FLOATC && c != STRINGC && c != REG)
            IMCC_fataly(imcc, EXCEPTION_SYNTAX_ERROR,
                "Constant '%s' value must be a number, "
                "stringliteral or register", macro_name);

        /* macro_name becomes a hash key
         * the first one needs to remain; destroying the hash frees it
         * subsequent macro_names need destruction here to avoid leaks */
        if (find_macro(imcc, macro_name))
            macro_exists = 1;

        define_macro(imcc, macro_name, NULL, valp->s, start_line);
        mem_sys_free(valp->s);

        /* don't leak these */
        if (macro_exists)
            mem_sys_free(macro_name);

        imcc->cur_macro_name = NULL;

        BEGIN(start_cond);
        return MACRO;
    }

<emit,INITIAL>".macro" {
        /* the initial whitespace catcher misses this one */
        SET_LINE_NUMBER;
        return read_macro(valp, imcc, yyscanner);
    }

<emit,INITIAL>".include" {
        const int c = yylex(valp, yyscanner, imcc);
        STRING *filename;

        if (c != STRINGC)
            return c;

        /* STRINGCs have a mem_sys_strdup()ed valp->s */
        mem_sys_free(valp->s);
        YYCHOP();
        filename = Parrot_str_new(imcc->interp, yytext + 1, 0);
        include_file(imcc, filename, yyscanner);
    }

<emit,INITIAL>{ID}"$:" {
        if (valp) {
            char *label;
            size_t len;

            YYCHOP();
            YYCHOP();

            if (!imcc->frames || !imcc->frames->label)
                    IMCC_fataly(imcc, EXCEPTION_SYNTAX_ERROR, "missing space?");

            len = yyleng + 10;
            label = (char *)mem_sys_allocate(len);
            snprintf(label, len, "%s%d", yytext, imcc->frames->label);

            /* XXX: free valp->s if it exists? */
            valp->s = label;
        }

        return LABEL;
    }

<emit,INITIAL>{ID}"$" {
        if (valp) {
            char *label;
            size_t len;
            YYCHOP();

            if (!imcc->frames || !imcc->frames->label)
                IMCC_fataly(imcc, EXCEPTION_SYNTAX_ERROR, "missing space?");

            len = yyleng + 10;
            label = (char *)mem_sys_allocate(len);
            snprintf(label, len, "%s%d", yytext, imcc->frames->label);

            /* XXX: free valp->s if it exists? */
            valp->s = label;
        }

        return IDENTIFIER;
     }

<emit,INITIAL>"," return COMMA;

<emit,INITIAL>{ID}":" {
        /* trim last ':' */
        YYCHOP();

        if (valp)
            valp->s = mem_sys_strndup(yytext, yyleng);

        return LABEL;
    }

<emit,INITIAL>{DOT}{LETTER}{LETTERDIGIT}* {
        char * const macro_name = mem_sys_strndup(yytext + 1, yyleng - 1);
        int failed = expand_macro(imcc, macro_name, yyscanner);
        mem_sys_free(macro_name);
        if (!failed) {
            yyless(1);
            return DOT;
        }
    }



<*>{FLOATNUM} DUP_AND_RET(valp, FLOATC);
<*>{SIGN}?{DIGIT}+ DUP_AND_RET(valp, INTC);
<*>{HEX} DUP_AND_RET(valp, INTC);
<*>{BIN} DUP_AND_RET(valp, INTC);
<*>{OCT} DUP_AND_RET(valp, INTC);

<*>{STRINGCONSTANT} {
        valp->s = mem_sys_strndup(yytext, yyleng);

        return STRINGC;
    }

<*>"<<"{STRINGCONSTANT} {
        macro_frame_t *frame;

        /* Save the string we want to mark the end of the heredoc and snip
           off newline and quote. */
        if (imcc->frames->heredoc_rest)
            IMCC_fataly(imcc, EXCEPTION_SYNTAX_ERROR, "nested heredoc not supported");
        imcc->heredoc_end = mem_sys_strndup(yytext + 3, yyleng - 3);
        imcc->heredoc_end[strlen(imcc->heredoc_end) - 1] = 0;

        if (!strlen(imcc->heredoc_end))
            IMCC_fataly(imcc, EXCEPTION_SYNTAX_ERROR, "empty heredoc delimiter");

        frame = new_frame(imcc);
        frame->s.next = (parser_state_t *)imcc->frames;
        imcc->frames = frame;

        /* Start slurping up the heredoc. */
        imcc->heredoc_content = (char *)mem_sys_allocate(2);

        /* preserve delim */
        imcc->heredoc_content[0] = yytext[2];

        /* eos */
        imcc->heredoc_content[1] = 0;
        yy_push_state(heredoc1, yyscanner);
    }

<*>{UNICODE} {
        /* charset:"..." */
        valp->s = mem_sys_strndup(yytext, yyleng);

        /* this is actually not unicode but a string with a charset */
        return USTRINGC;
    }

<emit,INITIAL>\$I[0-9]+ {
        if (valp) (valp)->s = yytext;
        if (imcc->state->pasm_file)
            IMCC_fataly(imcc, EXCEPTION_SYNTAX_ERROR,
                "'%s' is not a valid register name in pasm mode", yytext);
        return IREG;
    }

<emit,INITIAL>\$N[0-9]+ {
        if (valp)
            (valp)->s = yytext;
        if (imcc->state->pasm_file)
            IMCC_fataly(imcc, EXCEPTION_SYNTAX_ERROR,
                "'%s' is not a valid register name in pasm mode", yytext);
        return NREG;
    }

<emit,INITIAL>\$S[0-9]+ {
        if (valp)
            (valp)->s = yytext;
        if (imcc->state->pasm_file)
            IMCC_fataly(imcc, EXCEPTION_SYNTAX_ERROR,
                "'%s' is not a valid register name in pasm mode", yytext);
        return SREG;
    }

<emit,INITIAL>\$P[0-9]+ {
        if (valp)
            (valp)->s = yytext;
        if (imcc->state->pasm_file)
            IMCC_fataly(imcc, EXCEPTION_SYNTAX_ERROR,
                "'%s' is not a valid register name in pasm mode", yytext);
        return PREG;
    }

<emit,INITIAL>\$[a-zA-Z0-9]+ {
        IMCC_fataly(imcc, EXCEPTION_SYNTAX_ERROR,
            "'%s' is not a valid register name", yytext);
    }

<emit,INITIAL>[ISNP]{DIGIT}{DIGIT}? {
        if (imcc->state->pasm_file == 0)
            IMCC_fataly(imcc, EXCEPTION_SYNTAX_ERROR,
            "'%s' is only a valid register name in PASM mode", yytext);

        if (valp)
            valp->s = mem_sys_strndup(yytext, yyleng);

        return REG;
    }


<emit,INITIAL>{ID} { return handle_identifier(imcc, valp, yytext); }

<emit,INITIAL>{WS}+ /* skip */;

<emit,cmt1,cmt2,cmt3,cmt4,cmt5,INITIAL>. {
        /* catch all except for state macro */
        return yytext[0];
    }

<emit><<EOF>> {
        BEGIN(INITIAL);

        if (imcc->frames->s.pasm_file) {
            imcc->frames->s.pasm_file = 2;
            return EOM;
        }

        return 0;
    }

<INITIAL><<EOF>> yyterminate();

<macro>".endm" {
        /* the initial whitespace catcher misses this one */
        SET_LINE_NUMBER;
        DUP_AND_RET(valp, ENDM);
}

<macro>{WS}*{EOL} {
        DUP_AND_RET(valp, '\n');
    }

<macro>"$"{ID}":" return LABEL;

<macro>".label"{WS}+ {

        if (yylex(valp, yyscanner, imcc) != LABEL)
                IMCC_fataly(imcc, EXCEPTION_SYNTAX_ERROR, "LABEL expected");

        if (valp) {
            char *label;
            size_t len;
            YYCHOP();

            len = strlen(imcc->cur_macro_name) + yyleng + 15;
            label = (char *)mem_sys_allocate(len);

            snprintf(label, len, "local__%s__%s__$:",
                imcc->cur_macro_name, yytext+1);

            if (valp->s)
                mem_sys_free(valp->s);
            valp->s = label;
        }

        return LABEL;
    }

<macro>".$"{ID} {
    if (valp) {
        if (!imcc->cur_macro_name) {
            if (valp->s)
                mem_sys_free(valp->s);
            IMCC_fataly(imcc, EXCEPTION_SYNTAX_ERROR,
                "Invalid LABEL outside of macro");
        }
        else {
            const char * const fmt = "local__%s__%s__$";
            const size_t fmtlen = strlen(fmt) - (2 * strlen("%s"));
            const size_t len = strlen(imcc->cur_macro_name)
                                + yyleng + fmtlen;
            char * const label = (char *)mem_sys_allocate(len);

            /* skip over ".$" prefix with the +2 */
            snprintf(label, len, fmt,
                imcc->cur_macro_name, yytext + 2);

            if (valp->s)
                mem_sys_free(valp->s);
            valp->s = label;
        }
    }

    return IDENTIFIER;
}

<macro>{WS}+ DUP_AND_RET(valp, ' ');
<macro>[SNIP]{DIGITS} DUP_AND_RET(valp, REG);
<macro>"$"[SNIP]{DIGITS} DUP_AND_RET(valp, REG);
<macro>{ID} DUP_AND_RET(valp, IDENTIFIER);
<macro>{DOT}{ID} DUP_AND_RET(valp, MACRO);
<macro>. DUP_AND_RET(valp, yytext[0]);
<macro><<EOF>> yyterminate();

%%

#ifdef yywrap
# undef yywrap
#endif

int yywrap(void* yyscanner) {
    /* Add code here to open next source file and start scanning
     * yywrap returns 0 if scanning is to continue */
    imc_info_t * imcc = yyget_extra(yyscanner);
    yyguts_t * const yyg = (yyguts_t *)yyscanner;

    if (!imcc->interp) {
        fprintf(stderr, "Argh, interp not found\n");
        exit(1);
    }

    yy_delete_buffer(YY_CURRENT_BUFFER, yyscanner);

    /* pop old frame */
    if (imcc->frames->s.next) {
        pop_parser_state(imcc, yyscanner);
        if (YYSTATE == INITIAL || YYSTATE == emit)
            BEGIN(imcc->frames->s.pasm_file ? emit : INITIAL);
        return 0;
    }

    return 1;
}

static macro_frame_t *
new_frame(ARGMOD(imc_info_t *imcc)) {
    macro_frame_t * const tmp = mem_gc_allocate_zeroed_typed(imcc->interp, macro_frame_t);

    tmp->label = imcc->unique_count;
    imcc->unique_count++;
    tmp->s.line = imcc->line;
    tmp->s.handle = PIO_INVALID_HANDLE;

    if (imcc->frames) {
        tmp->s.pasm_file = imcc->frames->s.pasm_file;
        if (imcc->frames->s.file)
            tmp->s.file = imcc->frames->s.file;
    }

    tmp->s.interp = imcc->interp;

    return tmp;
}

static void
scan_string(macro_frame_t *frame, ARGIN(const char *expansion), void *yyscanner)
{
    yyguts_t * const yyg = (yyguts_t *)yyscanner;
    imc_info_t * imcc = yyget_extra(yyscanner);

    frame->buffer = YY_CURRENT_BUFFER;
    frame->s.next = (parser_state_t *)imcc->frames;
    imcc->frames = frame;

    /* start at the effective *starting line* of the macro */
    imcc->line = frame->s.line - 2;

    yy_scan_string(expansion, yyscanner);
}

static int
destroy_frame(struct macro_frame_t *frame, void *yyscanner)
{
    YY_BUFFER_STATE buffer = frame->buffer;
    int ret = 0;
    int i;

    for (i = 0; i < frame->expansion.num_param; i++) {
        mem_sys_free(frame->expansion.name[i]);
        frame->expansion.name[i] = NULL;
    }

    if (frame->heredoc_rest) {
        mem_sys_free(frame->heredoc_rest);
        frame->heredoc_rest = NULL;
    }
    else {
        ret = frame->s.line;
    }

    mem_sys_free(frame);

    if (buffer != NULL)
        yy_switch_to_buffer(buffer, yyscanner);

    return ret;
}

static int
yylex_skip(YYSTYPE *valp, ARGMOD(imc_info_t *imcc), const char *skip, void *yyscanner)
{
    int c;
    const char *p;
    yyguts_t * const yyg = (yyguts_t *)yyscanner;

    do {
        c = yylex(valp, yyscanner, imcc);
        p = skip;

        while (*p && c != *p)
            p++;

        /* leave loop early if it gets found */
        if (*p == '\0')
            break;

        /* free any mem_sys_strdup()ed strings */
        if (yytext)
            mem_sys_free(valp->s);
    } while (*p != '\0');

    if (c)
        DUP_AND_RET_FREE(valp, c);

    return c;
}

static char*
read_braced(YYSTYPE *valp, ARGMOD(imc_info_t *imcc), const char *macro_name,
             char *current, void *yyscanner)
{
    YYSTYPE val;
    size_t len = strlen(current);
    int c = yylex(&val, yyscanner, imcc);
    int count = 0;

    while (c != '}' || count > 0) {

        if (c == '}')
            count--;
        else if (c == '{')
            count++;

        if (c <= 0)
            IMCC_fataly(imcc, EXCEPTION_SYNTAX_ERROR,
                        "End of file reached while reading arguments in '%s'",
                        macro_name);

        len += strlen(val.s);
        current = (char *)mem_sys_realloc(current, len + 1);
        strcat(current,val.s);

        mem_sys_free(val.s);
        val.s = NULL;
        c = yylex(&val, yyscanner, imcc);
    }

    if (valp) {
        if (valp->s)
            mem_sys_free(valp->s);
        *valp = val;
    }
    else
        mem_sys_free(val.s);

    return current;
}

static int
read_params(YYSTYPE *valp, ARGMOD(imc_info_t *imcc), params_t *params,
             ARGIN(const char *macro_name), int need_id, void *yyscanner)
{
    YYSTYPE val;
    size_t len = 0;
    char *current = mem_sys_strndup("", 0);
    yyguts_t *yyg = (yyguts_t *)yyscanner;
    int c = yylex_skip(&val, imcc, " \n", yyscanner);

    params->num_param = 0;

    while (c != ')') {
        if (YYSTATE == heredoc2)
            IMCC_fataly(imcc, EXCEPTION_SYNTAX_ERROR,
                        "Heredoc in macro '%s' not allowed", macro_name);

        if (c <= 0)
            IMCC_fataly(imcc, EXCEPTION_SYNTAX_ERROR,
                        "End of file reached while reading arguments in '%s'",
                        macro_name);
        else if (c == ',') {
            if (params->num_param == MAX_PARAM)
                IMCC_fataly(imcc, EXCEPTION_SYNTAX_ERROR,
                            "More than %d params in '%s'",
                            MAX_PARAM, macro_name);

            params->name[params->num_param++] = current;
            current = mem_sys_strndup("", 0);
            len = 0;

            if (val.s)
                mem_sys_free(val.s);
            c = yylex_skip(&val, imcc, " \n", yyscanner);
        }
        else if (need_id && (*current || c != IDENTIFIER) && c != ' ') {
            IMCC_fataly(imcc, EXCEPTION_SYNTAX_ERROR,
                        "Parameter definition in '%s' must be IDENTIFIER",
                        macro_name);
        }
        else if (c == '{') {
            current = read_braced(&val, imcc, macro_name, current, yyscanner);
            mem_sys_free(val.s);
            c = yylex_skip(&val, imcc, " \n", yyscanner);
            len = strlen(current);
        }
        else {
            if (!need_id || c != ' ') {
                len += strlen(val.s);
                current = (char *)mem_sys_realloc(current, len + 1);
                strcat(current, val.s);
            }

            mem_sys_free(val.s);
            val.s = NULL;
            c = yylex(&val, yyscanner, imcc);
        }
    }

    params->name[params->num_param++] = current;

    if (valp)
        *valp = val;
    else
        mem_sys_free(val.s);

    return c;
}

static int
read_macro(YYSTYPE *valp, ARGMOD(imc_info_t *imcc), void *yyscanner)
{
    int c, start_line;
    params_t params;
    yyguts_t * const yyg = (yyguts_t *)yyscanner;
    int start_cond = YY_START;
    size_t buffer_size = 0;
    size_t buffer_used = 0;

    BEGIN(macro);

    c = yylex_skip(valp, imcc, " ", yyscanner);

    if (c != IDENTIFIER)
        IMCC_fataly(imcc, EXCEPTION_SYNTAX_ERROR, "Macro names must be identifiers");

    imcc->cur_macro_name = valp->s;
    start_line = imcc->line;

    memset(&params, 0, sizeof (params_t));

    /* white space is allowed between macro and opening paren) */
    c = yylex_skip(valp, imcc, " ", yyscanner);

    if (c == '(') {
        mem_sys_free(valp->s);
        valp->s = NULL;

        c = read_params(NULL, imcc, &params,
                        imcc->cur_macro_name, 1, yyscanner);

        c = yylex(valp, yyscanner, imcc);
    }

    while (c != ENDM) {
        int elem_len;

        if (c <= 0) {
            mem_sys_free(valp->s);
            IMCC_fataly(imcc, EXCEPTION_SYNTAX_ERROR,
                        "File ended before macro '%s' was complete",
                        imcc->cur_macro_name);
        }

        if (valp->s) {
            elem_len = strlen(valp->s);

            if (buffer_used) {
                if (buffer_used + elem_len > buffer_size) {
                    buffer_size += elem_len;
                    buffer_size <<= 1;

                    imcc->macro_buffer =
                        (char *)mem_sys_realloc(imcc->macro_buffer,
                            buffer_size);
                }
            }
            else {
                buffer_size = (elem_len << 1) > 1024 ? elem_len << 1 : 1024;

                imcc->macro_buffer =
                    (char *)mem_sys_allocate_zeroed(buffer_size);
            }

            strcat(imcc->macro_buffer, valp->s);
            buffer_used += elem_len;

            mem_sys_free(valp->s);
            valp->s = NULL;
        }
        c = yylex(valp, yyscanner, imcc);
    }

    mem_sys_free(valp->s);
    valp->s = NULL;

    BEGIN(start_cond);

    define_macro(imcc, imcc->cur_macro_name,
                 &params, imcc->macro_buffer, start_line);

    mem_sys_free(imcc->macro_buffer);
    imcc->macro_buffer = NULL;
    imcc->cur_macro_name = NULL;

    return MACRO;
}

static char *
find_macro_param(ARGMOD(imc_info_t *imcc), const char *name)
{
    macro_frame_t *f;

    for (f = imcc->frames; f; f = (macro_frame_t *)f->s.next) {
        if (f->params) {
            int i;
            for (i = 0; i < f->params->num_param; i++) {
                if (STREQ(f->params->name[i], name))
                    return f->expansion.name[i];
            }
        }
    }

    return NULL;
}

static void
define_macro(ARGMOD(imc_info_t *imcc), ARGIN(const char *name),
        ARGIN(const params_t *params), ARGIN(const char *expansion), int start_line)
{
    DECL_CONST_CAST;

    macro_t *m = find_macro(imcc, name);

    if (m) {
        mem_sys_free(m->expansion);
        m->expansion = NULL;
    }
    else {
        m = mem_gc_allocate_zeroed_typed(imcc->interp, macro_t);

        if (!imcc->macros)
            imcc->macros = Parrot_hash_new_cstring_hash(imcc->interp);
        Parrot_hash_put(imcc->interp, imcc->macros,
            PARROT_const_cast(char *, name), m);
    }

    if (params)
        m->params = *params;
    else
        memset(&m->params, 0, sizeof (params_t));

    m->expansion = mem_sys_strdup(expansion);
    m->line = start_line;
}

static macro_t *
find_macro(ARGMOD(imc_info_t *imcc), const char *name)
{
    if (!imcc->macros)
        return NULL;

    return (macro_t *)Parrot_hash_get(imcc->interp, imcc->macros, name);
}

static int
expand_macro(ARGMOD(imc_info_t *imcc), ARGIN(const char *name), void *yyscanner)
{
    yyguts_t * const yyg = (yyguts_t *)yyscanner;
    const char * const expansion = find_macro_param(imcc, name);
    macro_t *m;

    if (expansion) {
        macro_frame_t * const frame = new_frame(imcc);

        /* When an error occurs, then report it as being in a macro */
        frame->is_macro = 1;
        scan_string(frame, expansion, yyscanner);
        return 1;
    }

    m = find_macro(imcc, name);
    if (m) {
        int i, c, start_cond;

        macro_frame_t * frame = new_frame(imcc);
        frame->params = &m->params;

        /* When an error occurs, then report it as being in a macro */
        frame->is_macro = 1;

        frame->s.file = Parrot_str_new(imcc->interp, name, 0);

        /* whitespace can be safely ignored */
        do {
#ifdef __cplusplus
            c = yyinput(yyscanner);
#else
            c = input(yyscanner);
#endif
        } while (c == ' ' || c == '\t');

        if (c != '(') {
            if (m->params.num_param != 0)
                IMCC_fataly(imcc, EXCEPTION_SYNTAX_ERROR,
                            "Macro '%s' needs %d arguments",
                            name, m->params.num_param);
            unput(c);
            scan_string(frame, m->expansion, yyscanner);
            return 1;
        }

        start_cond = YY_START;
        BEGIN(macro);

        read_params(NULL, imcc, &frame->expansion, name, 0, yyscanner);

        BEGIN(start_cond);

        if (frame->expansion.num_param == 0 && m->params.num_param == 1) {
            frame->expansion.name[0] = mem_sys_strndup("", 0);
            frame->expansion.num_param = 1;
        }

        if (frame->expansion.num_param != m->params.num_param) {
            IMCC_fataly(imcc, EXCEPTION_SYNTAX_ERROR,
                        "Macro '%s' requires %d arguments, but %d given",
                        name, m->params.num_param, frame->expansion.num_param);
        }

        /* expand arguments */
        for (i = 0; i < frame->expansion.num_param; i++) {
            char * const current = frame->expansion.name[i];

            /* parameter of outer macro */
            if (current[0] == '.') {
                const char * const s = find_macro_param(imcc, current + 1);

                if (s) {
                    frame->expansion.name[i] = mem_sys_strdup(s);
                    mem_sys_free(current);
                }

            }
            else {
                const size_t len = strlen(current);
                if (len && (current[len - 1] == '$')) { /* local label */
                    const size_t slen = len + 10;
                    char * const s = (char *)mem_sys_allocate(slen);

                    current[len - 1] = '\0';

                    snprintf(s, slen, "%s%d", current, imcc->frames->label);

                    frame->expansion.name[i] = s;
                    mem_sys_free(current);
                }
            }
        }

        scan_string(frame, m->expansion, yyscanner);

        return 1;
    }

    return 0;
}

static void
include_file(ARGMOD(imc_info_t *imcc), ARGIN(STRING *file_name), void *yyscanner)
{
    yyguts_t * const yyg = (yyguts_t *)yyscanner;
    macro_frame_t * const frame = new_frame(imcc);
    STRING *s = Parrot_locate_runtime_file_str(imcc->interp,
                                    file_name, PARROT_RUNTIME_FT_INCLUDE);
    char *ext;
    PIOHANDLE file;

    if (STRING_IS_NULL(s)
    || (file = Parrot_io_internal_open(imcc->interp, s, PIO_F_READ)) == PIO_INVALID_HANDLE) {
        IMCC_fataly(imcc, EXCEPTION_EXTERNAL_ERROR,
            "No such file or directory '%Ss'", file_name);
    }

    frame->s.file = file_name;
    frame->s.handle = file;

    /* TODO: We do checks like this elsewhere. Create a utility function to
             check file type */
    if (imcc_string_ends_with(imcc, file_name, ".pasm")) {
        frame->s.pasm_file = 1;
        BEGIN(emit);
    }
    else if (imcc_string_ends_with(imcc, file_name, ".pir")) {
        frame->s.pasm_file = 0;
        BEGIN(INITIAL);
    }

    scan_file(imcc, frame, file, yyscanner);
}

static void
scan_file(ARGMOD(imc_info_t *imcc), macro_frame_t *frame, PIOHANDLE file,
        void *yyscanner)
{
    yyguts_t * const yyg = (yyguts_t *)yyscanner;
    const int oldline = imcc->line;
    frame->buffer = YY_CURRENT_BUFFER;
    frame->s.next = (parser_state_t *)imcc->frames;
    imcc->frames = frame;
    imcc->state = (parser_state_t *)imcc->frames;

    /* let the start of line rule increment this to 1 */
    imcc->line = 0;

    yy_switch_to_buffer(yy_create_buffer((FILE *)file, YY_BUF_SIZE, yyscanner),
        yyscanner);

    imcc->line = oldline;
}

void
IMCC_push_parser_state(ARGMOD(imc_info_t *imcc), STRING *filename,
        int is_file, int is_pasm)
{
    macro_frame_t * const frame = new_frame(imcc);
    frame->s.next = (parser_state_t *)imcc->frames;
    imcc->frames = frame;
    frame->s.line = imcc->line = 1;
    imcc->state = (parser_state_t *)imcc->frames;
    if (is_file)
        imcc->state->file = filename;
    else
        imcc->state->file = Parrot_str_new_constant(imcc->interp, "(file unknown)");
    imcc->state->pasm_file = is_pasm;
}

static void
pop_parser_state(ARGMOD(imc_info_t *imcc), void *yyscanner)
{
    macro_frame_t * const tmp = imcc->frames;
    if (tmp) {
        int l;
        if (tmp->s.handle != PIO_INVALID_HANDLE)
            Parrot_io_internal_close(imcc->interp, tmp->s.handle);

        imcc->frames =
            (macro_frame_t *)imcc->frames->s.next;

        l = destroy_frame(tmp, yyscanner);

        if (l)
            imcc->line = l;
    }

    imcc->state = (parser_state_t *)imcc->frames;
}

void
IMCC_pop_parser_state(ARGMOD(imc_info_t *imcc), void *yyscanner)
{
    pop_parser_state(imcc, yyscanner);
}

PIOHANDLE
determine_input_file_type(ARGMOD(imc_info_t * imcc), ARGIN(STRING *sourcefile))
{
    PIOHANDLE handle;

    if (!STRING_length(sourcefile))
        IMCC_fatal_standalone(imcc, 1, "main: No source file specified.\n");

    if (STRING_length(sourcefile) == 1
            && STRING_ord(imcc->interp, sourcefile, 0) == '-') {
        handle = Parrot_io_internal_std_os_handle(imcc->interp, PIO_STDIN_FILENO);

        if ((FILE *)handle == NULL) {
            /*
             * We have to dup the handle because the stdin fd is 0 on UNIX and
             * lex would think it's a NULL FILE pointer and reset it to the
             * stdin FILE pointer.
             */
            handle = Parrot_io_internal_dup(imcc->interp, handle);
        }
    }
    else {
        if (Parrot_file_stat_intval(imcc->interp, sourcefile, STAT_ISDIR))
            Parrot_ex_throw_from_c_args(imcc->interp, NULL, EXCEPTION_EXTERNAL_ERROR,
                "imcc_compile_file: '%Ss' is a directory\n", sourcefile);

        handle = Parrot_io_internal_open(imcc->interp, sourcefile, PIO_F_READ);
        if (handle == PIO_INVALID_HANDLE)
            IMCC_fatal_standalone(imcc, EXCEPTION_EXTERNAL_ERROR,
                                  "Error reading source file %Ss.\n",
                                  sourcefile);
        if (imcc_string_ends_with(imcc, sourcefile, ".pasm"))
            SET_STATE_PASM_FILE(imcc);
    }

    if (imcc->verbose) {
        IMCC_info(imcc, 1, "debug = 0x%x, optim = 0x%x\n", imcc->debug,
                  imcc->optimizer_level);
        IMCC_info(imcc, 1, "Reading %Ss\n", sourcefile);
    }

    return handle;
}


static PIOHANDLE
imcc_setup_input(ARGMOD(imc_info_t * imcc), yyscan_t yyscanner,
        ARGIN(STRING *source), ARGIN(const char *source_c), int is_file)
{
    if (is_file) {
        PIOHANDLE file = determine_input_file_type(imcc, source);
        imc_yyin_set(file, yyscanner);
        yy_switch_to_buffer(
            yy_create_buffer((FILE *)file, YY_BUF_SIZE, yyscanner),
            yyscanner);
        return file;
    }
    else {
        yy_scan_string(source_c, yyscanner);
        return PIO_INVALID_HANDLE;
    }
}

static void
imcc_cleanup_input(ARGMOD(imc_info_t * imcc), PIOHANDLE file,
        ARGIN(char *source_c), int is_file)
{
    if (is_file)
        Parrot_io_internal_close(imcc->interp, file);

    Parrot_str_free_cstring(source_c);
}

INTVAL
imcc_compile_buffer_safe(ARGMOD(imc_info_t *imcc), yyscan_t yyscanner,
        ARGIN(STRING *source), int is_file, SHIM(int is_pasm))
{
    yyguts_t * const yyg = (yyguts_t *)yyscanner;
    YY_BUFFER_STATE volatile buffer;
    char * source_c = Parrot_str_to_cstring(imcc->interp, source);
    PIOHANDLE file;
    INTVAL success = 0;

    imcc->frames->s.next = NULL;
    buffer = YY_CURRENT_BUFFER;

    file = imcc_setup_input(imcc, yyscanner, source, source_c, is_file);
    emit_open(imcc);
    success = imcc_run_compilation(imcc, yyscanner);
    imcc_cleanup_input(imcc, file, source_c, is_file);

    if (buffer)
        yy_switch_to_buffer(buffer, yyscanner);
    return success;
}

static void
do_a_better_error_message(imc_info_t * imcc, SHIM(void * yyscanner))
{
    STRING * loc;
    imcc->error_code = IMCC_PARSEFAIL_EXCEPTION;
    if (imcc->frames && imcc->frames->is_macro)
        loc = Parrot_sprintf_c(imcc->interp, "in macro '.%Ss' line %d",
                imcc->frames->s.file, imcc->line);
    else
        loc = Parrot_sprintf_c(imcc->interp, "in file '%Ss' line %d",
                imcc->frames->s.file, imcc->line);
    imcc->error_message = Parrot_sprintf_c(imcc->interp,
        "Unexpected parser exit. Unknown syntax error.\n"
        "\tLast line reported is %d\n"
        "\tLast file reported is %S", imcc->line, loc);
}

INTVAL
imcc_run_compilation(ARGMOD(imc_info_t *imcc), void *yyscanner) {
    /* TODO: Kill this stuff and use Parrot exceptions exclusively */
    IMCC_TRY(imcc->jump_buf, imcc->error_code) {
        if (yyparse(yyscanner, imcc)) {
            imcc->error_code = IMCC_PARSEFAIL_EXCEPTION;
            return 0;
        }

        imc_compile_all_units(imcc);
        return 1;
    }

    IMCC_CATCH(IMCC_FATAL_EXCEPTION) {
        imcc->error_code = IMCC_FATAL_EXCEPTION;
    }

    IMCC_CATCH(IMCC_FATALY_EXCEPTION) {
        imcc->error_code = IMCC_FATALY_EXCEPTION;
    }

    IMCC_END_TRY;
    return 0;
}

void
IMCC_print_inc(ARGMOD(imc_info_t *imcc))
{
    macro_frame_t *f;
    STRING *old = imcc->frames->s.file;

    if (imcc->frames && imcc->frames->is_macro)
        IMCC_warning(imcc, "\n\tin macro '.%Ss' line %d\n",
                imcc->frames->s.file, imcc->line);
    else
        IMCC_warning(imcc, "\n\tin file '%Ss' line %d\n",
                imcc->frames->s.file, imcc->line);


    for (f = imcc->frames; f; f = (macro_frame_t *)f->s.next) {
        if (!STRING_equal(imcc->interp, f->s.file, old)) {
            IMCC_warning(imcc, "\tincluded from '%Ss' line %d\n",
                    f->s.file, f->s.line);
        }

        old = f->s.file;
    }
}

/*

void
set_filename(ARGMOD(imc_info_t *imcc), char * const filename)

Function to set the C<filename> as specified using the C<.line> directive.
The parser needs to call back into the lexer (this file), because the
parser does not have access to the lexer's private bits.

*/

void
set_filename(ARGMOD(imc_info_t *imcc), char * const filename)
{
    STRING *str = Parrot_str_new(imcc->interp, filename, 0);

    imcc->frames->s.file = str;

    /* in case .line is used outside a .sub, then this
     * can't be done; hence the check.
     * The mem_sys_strdup() is done, as the original #line implementation
     * duplicated the string twice as well; one for the
     * frames->s.file and one for cur_unit->file.
     * During the parse, the STRINGC is already mem_sys_strdup()ed once.
     */
    if (imcc->cur_unit)
        imcc->cur_unit->file = str;
}

/* Functions to set and get yyin, as we can't decorate it for export
   (since it is defined in a file generated by yacc/bison). */
void imc_yyin_set(PIOHANDLE new_yyin, void *yyscanner)
{
    yyguts_t * const yyg = (yyguts_t *)yyscanner;
    yyg->yyin_r = (FILE *)new_yyin;
}

PIOHANDLE imc_yyin_get(void *yyscanner)
{
    const yyguts_t * const yyg = (yyguts_t *)yyscanner;
    return (PIOHANDLE)yyg->yyin_r;
}

/* return true if scanner is at EOF */
int at_eof(yyscan_t yyscanner)
{
    yyguts_t * const yyg = (yyguts_t *)yyscanner;
    return yyg->yy_hold_char == '\0';
}

static int
handle_identifier(ARGMOD(imc_info_t *imcc), YYSTYPE *valp, const char *text)
{
    if (!imcc->is_def) {
        SymReg *r = find_sym(imcc, text);

        if (r && (r->type & (VTIDENTIFIER|VT_CONSTP))) {
            valp->sr = r;
            return VAR;
        }

        if (imcc->cur_unit
                && imcc->cur_unit->instructions
                && (r = imcc->cur_unit->instructions->symregs[0])
                && r->pcc_sub)
        {
            if (((r->pcc_sub->pragma & P_METHOD)
            || (imcc->cur_unit->is_vtable_method))
            && !strcmp(text, "self")) {
                valp->sr = mk_ident(imcc, "self", 'P', VTIDENTIFIER);
                imcc->cur_unit->type |= IMC_HAS_SELF;
                return VAR;
            }
        }
    }

    valp->s = mem_sys_strdup(text);
    return (!imcc->is_def && is_op(imcc, valp->s) ? PARROT_OP : IDENTIFIER);
}

/*
 * Local variables:
 * mode: c
 * c-file-style: "parrot"
 * End:
 * vim: expandtab shiftwidth=4 cinoptions='\:2=2' :
 */
Something went wrong with that request. Please try again.