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 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 |
/*
* This software is derived from f2c (http://www.netlib.org/f2c) * * Copyright 1990, 1992 - 1997, 1999, 2000 by AT&T, Lucent Technologies * and Bellcore. * * See the LICENCE file for the permission notice and warranty disclaimer * provided by the f2c authors. * */ #include <f77crash/auxiliary.h> #include <f77crash/cursor.h> #include <f77crash/highlight.h> #include <f77crash/lex.h> #include <f77crash/tokens.h> #include <cassert> #include <cctype> #include <climits> #include <cstring> #include <fstream> #include <iostream> #include <vector> static bool initialized = false; enum Warn72 { Ignore = 0, Error = 1, FreeFormat = 2 }; static Warn72 warn72; static bool noExtension; static bool useBackspace; static bool shiftCase; static const int MAX_CODEBUFFER_SIZE = 10000; static const int MAX_CONTINUATIONS = 99; static const int MAX_NAMELENGTH = 50; static const char QUOTE_MARK = 2; static std::ifstream input; static std::string filename; static char *bufferBegin, *bufferEnd; enum CardType { STEOF = 1, STINITIAL = 2, STCONTINUE = 3 }; static CardType cardType; static char *cardNext, *cardEnd; long lineNumber; static long thisLine, previousLine; static char *nextCharacter = 0, *previousCharacter = 0, *lastCharacter; static char *lineStart[MAX_CONTINUATIONS]; namespace CrunchInfo { struct Pos { long line; int col; Pos() : line(0), col(0) {} Pos(long _line, int _col) : line(_line), col(_col) {} }; static std::vector<Pos> pos; static const char *begin, *last; static char **lineStart; static long lineNumber; static void init(const char *_begin, const char *_last, long _lineNumber, char *_lineStart[]) { begin = _begin; last = _last; lineStart = _lineStart; lineNumber = _lineNumber; pos.clear(); } static void store(const char *i, const char *j) { long k; long col = i - begin; for (k=0; i>=lineStart[k]; ++k) { col = i - lineStart[k]; } if ((unsigned int)(j-begin) >= pos.size()) { pos.resize(2*long(j-begin+1)); } pos[j-begin] = Pos(lineNumber+k, col); } static long getCurrentLine(const char *i) { long k; for (k=0; i>=lineStart[k]; ++k) { } return lineNumber+k; } static long getCurrentCol(const char *i) { long k; long col = i - begin; for (k=0; i>=lineStart[k]; ++k) { col = i - lineStart[k]; } return col; } static long getLine(const char *j) { if (j-begin>=0 && (unsigned int)(j-begin)<pos.size()) { return pos[j-begin].line; } return -1; } static long getCol(const char *j) { if (j-begin>=0 && (unsigned int)(j-begin)<pos.size()) { return pos[j-begin].col; } return -1; } } // namespace CrunchInfo static int statementNumber; static int nextStatementNumber; static int parenthesesLevel; static bool parenthesesSeen; static bool exposedComma, exposedEqual; static bool needWhileKey; enum LexState { NEWSTMT = 1, FIRSTTOKEN = 2, OTHERTOKEN = 3, RETEOS = 4 }; static LexState lexState = NEWSTMT; static int stkey; int yyStatementNumber; int tokenLength; char *token = 0; static int reservedTokenLength = 502; int newDeclaration; int needKeyword; bool inioctl; static char commentStart[1 << CHAR_BIT]; static char escapes[1 << CHAR_BIT]; struct Punctuation { char c; int value; }; static struct Punctuation punctuation[] = { { '(', SLPAR }, { ')', SRPAR }, { '=', SEQUALS }, { ',', SCOMMA }, { '+', SPLUS }, { '-', SMINUS }, { '*', SSTAR }, { '/', SSLASH }, { '$', SCURRENCY }, { ':', SCOLON }, { '<', SLT }, { '>', SGT }, { 0, 0 } }; struct DotOp { const char *name; int value; }; static struct DotOp dotOp[] = { { "and.", SAND }, { "or.", SOR }, { "not.", SNOT }, { "true.", STRUE }, { "false.", SFALSE }, { "eq.", SEQ }, { "ne.", SNE }, { "lt.", SLT }, { "le.", SLE }, { "gt.", SGT }, { "ge.", SGE }, { "neqv.", SNEQV }, { "eqv.", SEQV }, { 0, 0 } }; struct Key { const char *name; int value; }; static const Key *keyStart[26], *keyEnd[26]; static const Key key[] = { { "assign", SASSIGN }, { "automatic", SAUTOMATIC }, { "backspace", SBACKSPACE }, { "blockdata", SBLOCK }, { "byte", SBYTE }, { "call", SCALL }, { "character", SCHARACTER }, { "close", SCLOSE }, { "common", SCOMMON }, { "complex", SCOMPLEX }, { "continue", SCONTINUE }, { "data", SDATA }, { "dimension", SDIMENSION }, { "doubleprecision", SDOUBLE }, { "doublecomplex", SDCOMPLEX }, { "elseif", SELSEIF }, { "else", SELSE }, { "endfile", SENDFILE }, { "endif", SENDIF }, { "enddo", SENDDO }, { "end", SEND }, { "entry", SENTRY }, { "equivalence", SEQUIV }, { "external", SEXTERNAL }, { "format", SFORMAT }, { "function", SFUNCTION }, { "goto", SGOTO }, { "implicitnone", SIMPLICITNONE }, { "implicit", SIMPLICIT }, { "include", SINCLUDE }, { "inquire", SINQUIRE }, { "intrinsic", SINTRINSIC }, { "integer", SINTEGER }, { "logical", SLOGICAL }, { "namelist", SNAMELIST }, { "none", SUNDEFINED }, { "open", SOPEN }, { "parameter", SPARAM }, { "pause", SPAUSE }, { "print", SPRINT }, { "program", SPROGRAM }, { "punch", SPUNCH }, { "read", SREAD }, { "real", SREAL }, { "recursivesubroutine", SRECURSIVE }, { "return", SRETURN }, { "rewind", SREWIND }, { "save", SSAVE }, { "static", SSTATIC }, { "stop", SSTOP }, { "subroutine", SSUBROUTINE }, { "then", STHEN }, { "undefined", SUNDEFINED }, { "while", SWHILE }, { "write", SWRITE }, { 0, 0 } }; // // Forward declarations of internal functions // static void setCursor(int tokenId); static void adjustTokenLength(int newLength); static char escape(char c); static bool popInputFile(); static CardType getInputCards(); static CardType getNextInputCard(bool noContinuationAllowed); static void crunch(); static void analyze(); static int getKeyword(); static int getToken(bool intOnly); static int hexCheck(int key); // // Implementation // void initLex() { initAuxiliary(); initHighlight(); warn72 = Ignore; noExtension = false; useBackspace = true; shiftCase = true; // // Init buffer for storing source code, input cards, ... // bufferBegin = new char[MAX_CODEBUFFER_SIZE]; assert(bufferBegin); bufferEnd = bufferBegin + MAX_CODEBUFFER_SIZE - 1; cardNext = 0; cardEnd = bufferBegin; parenthesesLevel = 0; parenthesesSeen = false; // // Init lookup-table for characters starting comment // commentStart[(unsigned char)('c')] = 1; commentStart[(unsigned char)('C')] = 1; commentStart[(unsigned char)('*')] = 1; commentStart[(unsigned char)('!')] = 1; commentStart[(unsigned char)('#')] = 1; // // Init lookup-table for escaping characters // for (int i=0; i < (1<<CHAR_BIT); ++i) { escapes[i] = i; } escapes[(unsigned char)('b')] = '\b'; escapes[(unsigned char)('t')] = '\t'; escapes[(unsigned char)('n')] = '\n'; escapes[(unsigned char)('f')] = '\f'; escapes[(unsigned char)('r')] = '\r'; escapes[(unsigned char)('0')] = 0; // // Init lookup-table for Fortran keywords // for (int i = 0; i<26; ++i) { keyStart[i] = 0; } for (const Key *pk=key; pk->name; ++pk) { unsigned char j = letter(pk->name[0]); if (keyStart[j] == 0) { keyStart[j] = pk; } keyEnd[j] = pk; } // // Reserve memory for token string // adjustTokenLength(reservedTokenLength); initialized = true; } int yylex() { assert(initialized); static int tokenNumber; int returnValue; previousCharacter = nextCharacter; switch (lexState) { case NEWSTMT: // // need a new statement // returnValue = getInputCards(); if (returnValue == STEOF) { returnValue = SEOF; break; } crunch(); tokenNumber = 0; lexState = FIRSTTOKEN; yyStatementNumber = statementNumber; statementNumber = nextStatementNumber; tokenLength = 0; returnValue = SLABEL; break; first: case FIRSTTOKEN: // // first step on a statement // analyze(); lexState = OTHERTOKEN; tokenNumber = 1; returnValue = stkey; break; case OTHERTOKEN: // // return next token // if (nextCharacter > lastCharacter) { goto reteos; } ++tokenNumber; if ((stkey==SLOGIF || stkey==SELSEIF) && parenthesesLevel==0 && tokenNumber>3) { goto first; } if (stkey==SASSIGN && tokenNumber==3 && nextCharacter<lastCharacter && nextCharacter[0]=='t' && nextCharacter[1]=='o') { nextCharacter += 2; returnValue = STO; break; } if (tokenNumber == 2 && stkey == SDO) { returnValue = getToken(true); } else { returnValue = getToken(false); } break; reteos: case RETEOS: lexState = NEWSTMT; returnValue = SEOS; break; default: std::cerr << "Error in line " << lineNumber << ": impossible lexstate " << lexState << std::endl; exit(1); break; } setCursor(returnValue); return returnValue; } void yyerror(const char *s) { /* std::cerr << "Error in line " << lineNumber << ": " << s << std::endl; */ } void flushLine() { lexState = RETEOS; } static void setCursor(int tokenId) { cursor.fromLine = CrunchInfo::getLine(previousCharacter); cursor.fromCol = CrunchInfo::getCol(previousCharacter); cursor.toLine = CrunchInfo::getLine(nextCharacter-1); cursor.toCol = CrunchInfo::getCol(nextCharacter-1); cursor.tokenId = tokenId; cursor.statementNumber = yyStatementNumber; size_t len = 0; if (previousCharacter >= bufferBegin && previousCharacter <= nextCharacter && nextCharacter <= bufferEnd) { len = nextCharacter - previousCharacter; } cursor.content.replace(0, cursor.content.size(), previousCharacter, len); if (isHighlighted(tokenId)) { std::cout << getHighlightTag(tokenId) << ","; std::cout << cursor.fromLine-1 << "." << cursor.fromCol+6 << ":" << cursor.toLine-1 << "." << cursor.toCol+6 << ","; std::cout << yyStatementNumber << "," << tokenId << "," << cursor.content << std::endl; } } static void adjustTokenLength(int newLength) { while (reservedTokenLength < newLength) { reservedTokenLength = 2*reservedTokenLength + 2; } if ((token = (char *)(realloc(token, reservedTokenLength)))) { return; } std::cerr << "adjtoklen: realloc(" << reservedTokenLength << ") failure!" << std::endl; exit(2); } static char escape(char c) { typedef unsigned char uchar; return escapes[uchar(c)]; } // // Public interface // void doInputFile(const char *filename) { assert(cardNext==0); assert(cardEnd==bufferBegin); input.open(filename, std::ifstream::in); } static bool popInputFile() { return false; } static CardType getInputCards() { top: if (cardNext == 0) { cardNext = bufferBegin; cardType = getNextInputCard(true); statementNumber = nextStatementNumber; previousLine = thisLine; } if (cardType == STEOF) { if (popInputFile()) { goto top; } else { return STEOF; } } if (cardType == STCONTINUE) { lineNumber = thisLine; cardNext = 0; goto top; } // // Get rid of unused space at the head of the buffer */ // if (cardNext > bufferBegin) { const char *q = cardNext; char *p = bufferBegin; while (q < cardEnd) { *p++ = *q++; } cardEnd = p; } // // Be aware that the input (i.e. the string at the address cardNext) is NOT // NULL-terminated // // This loop merges all continuations into one long statement, AND puts the next // card to be read at the end of the buffer (i.e. it stores the look-ahead card // when there's room) // int numberOfContinuations = 0; for (;;) { cardNext = cardEnd; if (numberOfContinuations >= MAX_CONTINUATIONS || cardNext+66 > bufferEnd) { lineNumber = thisLine; std::cerr << "Error in line " << lineNumber << ": too many continuation lines" << std::endl; } lineStart[numberOfContinuations++] = cardNext; if ((cardType = getNextInputCard(false)) != STCONTINUE) { break; } if (numberOfContinuations == 20 && noExtension) { lineNumber = thisLine; std::cerr << "Error: more than 19 continuation lines." << std::endl; } } nextCharacter = bufferBegin; lastCharacter = cardNext - 1; lineNumber = previousLine; previousLine = thisLine; return(STINITIAL); } static CardType getNextInputCard(bool noContinuationAllowed) { assert(input.is_open()); bool ampersant; bool specialLine; // Special line - true when the line is allowed // to have more than 66 characters (e.g. the // "&" shorthand for continuation, use of a "\t" // to skip part of the label columns) static char a[6]; // Statement label buffer static char *aEnd = a+6; char buf72[24]; char *atend; char *p, *bEnd; int c; assert(cardNext!=0); top: cardEnd = cardNext; bEnd = cardNext + 66; ampersant = false; specialLine = false; assert(cardEnd<=bufferEnd); // // Handle the continuation shorthand of "&" in the first column, which stands // for " x" // if ((c=input.get()) == '&') { a[0] = c; a[1] = 0; a[5] = 'x'; ampersant = specialLine = true; bEnd = bufferEnd; p = aEnd; } // // Handle the Comment cards (a 'C', 'c', '*', or '!' in the first column). // else if (commentStart[c & ((1<<CHAR_BIT) - 1)]) { if (input.eof()) { return STEOF; } if (c == '#') { *cardEnd++ = c; while ((c=input.get()) != '\n') { if (c==EOF) { return STEOF; } else if (cardEnd<bufferEnd) { *cardEnd++ = c; } } ++thisLine; *cardEnd = 0; p = cardNext; if (cardNext[1]==' ') { p += 2; } else if (!strncmp(cardNext, "#line", 6)) { p += 6; } else { bad_cpp: lineNumber = thisLine; std::cerr << "Error in line " << lineNumber << std::endl; std::cerr << "Bad # line: " << cardNext << std::endl; goto top; } if (*p < '1' || *p > '9') { goto bad_cpp; } long L = *p - '0'; while ((c = *++p) >= '0' && c <= '9') { L = 10*L + c - '0'; } while (c == ' ') { c = *++p; } if (!c) { /* accept "# 1234" */ std::cerr << "TODO: Keeping track of changed line numbers" << std::endl; thisLine = L - 1; goto top; } if (c != '"') { goto bad_cpp; } char *newFilename = p+1; while (*++p != '"') { if (!*p) { goto bad_cpp; } } *p = 0; thisLine = L-1; std::cerr << "TODO: Keeping track of changed line numbers" << std::endl; if (!filename.c_str() || strcmp(filename.c_str(), newFilename)) { std::cerr << "TODO: Keeping track of changed file name" << std::endl; filename = std::string(newFilename); } goto top; } std::cout << "COMMENT," << thisLine << ".0:" << thisLine << ".-1" << std::endl; while (!input.eof() && ((c = input.get()) != '\n')) { } if (input.eof()) { if (c != '\n') { // To allow the line index to increment correctly return STEOF; } } ++thisLine; goto top; } // // Load buffer a with the statement label // else if (c!=EOF) { // a tab in columns 1-6 skips to column 7 input.unget(); for (p=a; p<aEnd && (c=input.get())!='\n' && c!=EOF; ) { if (c=='\t') { // The tab character translates into blank characters in the // statement label atend = p; while (p<aEnd) { *p++ = ' '; } specialLine = true; bEnd = bufferEnd; } else { *p++ = c; } } } // // By now we've read either a continuation character or the statement label // field // if (c==EOF) { return STEOF; } // // The next 'if' block handles lines that have fewer than 7 characters // if (c=='\n') { while (p<aEnd) { *p++ = ' '; } // // Blank out the buffer on lines which are not longer than 66 // characters if (! specialLine) { while (cardEnd<bEnd) { *cardEnd++ = ' '; } } } else { // read body of line if (warn72==FreeFormat) { specialLine = true; bEnd = bufferEnd; } while (cardEnd<bEnd && (c=input.get())!='\n' && c!=EOF ) { *cardEnd++ = c; } if (c==EOF) { return STEOF; } // Drop any extra characters on the input card; this usually means // those after column 72 if (c!='\n') { int i = 0; while( (c=input.get())!='\n' && c != EOF) { if (i<23 && c!='\r') { buf72[i++] = c; } } if (warn72!=Ignore && i && !specialLine) { buf72[i] = 0; if (i>=23) { strcpy(buf72+20, "..."); } lineNumber = thisLine + 1; std::cerr << "Error in line " << lineNumber << std::endl; std::cerr << "text after column 72: " << buf72 << std::endl; } if(c == EOF) { return STEOF; } } if (! specialLine) { while(cardEnd<bEnd) { *cardEnd++ = ' '; } } } // // The flow of control usually gets to this line (unless an earlier RETURN has // been taken) */ // ++thisLine; if (!isspace(a[5]) && a[5]!='0') { // Fortran 77 specifies that a 0 in column 6 // does not signify continuation if (!ampersant) { for (p=a; p<aEnd; ) { if (*p++=='!' && p!=aEnd) { goto initcheck; } } } if (noContinuationAllowed) { lineNumber = thisLine; std::cerr << "Error in line " << lineNumber << ": " << "illegal continuation card (starts \"" << std::string(a, 0, 6) << "\")" << std::endl; } else if (!ampersant && strncmp(a, " ", 5)) { lineNumber = thisLine; std::cerr << "Error in line " << lineNumber << ": " << "illegal continuation card (starts \"" << std::string(a, 0, 6) << "\")" << std::endl; } return STCONTINUE; } initcheck: for (p=a; p<atend; ++p) { if (!isspace(*p)) { if (*p++ != '!') { goto initline; } printf("COMMENT,%ld.%ld:%ld.-1\n", thisLine-1, p-a-1, thisLine-1); goto top; } } for (p=cardNext; p<cardEnd; ++p) { if (!isspace(*p)) { if (*p++ != '!') { goto initline; } printf("COMMENT,%ld.%ld:%ld.-1\n", thisLine-1, p-cardNext+5, thisLine-1); goto top; } } // // Skip over blank cards by reading the next one right away // goto top; initline: // // Set nextStatementNumber equal to the integer value of the statement label // nextStatementNumber = 0; bEnd = a + 5; for (p=a; p<bEnd; ++p) { if (!isspace(*p)) { if (isdigit(*p)) { nextStatementNumber = 10*nextStatementNumber + (*p - '0'); } else if (*p == '!') { printf("COMMENT,%ld.%ld:%ld.-1", thisLine-1, p-a-1, thisLine-1); cardEnd = cardNext; break; } else { lineNumber = thisLine; std::cerr << "Error in line " << lineNumber << ": " << "nondigit in statement label field \"" << std::string(a, 0, 5) << "\")" << std::endl; } } } return STINITIAL; } static void crunch() { // // i is the next input character to be looked at // j is the next output character // char *i, *j; char *previousString = bufferBegin; exposedComma = false; exposedEqual = false; parenthesesLevel = 0; parenthesesSeen = false; CrunchInfo::init(bufferBegin, lastCharacter, lineNumber, lineStart); int k = 0; for (i=j=bufferBegin; i<=lastCharacter; ++i) { if (isspace(*i)) { continue; } if (*i == '!') { long currLine = CrunchInfo::getCurrentLine(i); int currCol = CrunchInfo::getCurrentCol(i) + 7; printf("COMMENT,%ld.%d:%ld.-1\n", currLine-1, currCol-1, currLine-1); while (i >= lineStart[k]) { if (++k >= MAX_CONTINUATIONS) { std::cerr << "Error in line " << lineNumber << ": too many continuation lines" << " (MAX_CONTINUATIONS = " << MAX_CONTINUATIONS << ")" << std::endl; exit(1); } } i = lineStart[k] - 1; continue; } if (*i=='\'' || *i=='"') { // // Keep everything in a quoted string // int length = 0; char quote = *i; *j = QUOTE_MARK; CrunchInfo::store(i, j); for(;;) { if (++i > lastCharacter) { std::cerr << "Error in line " << lineNumber << ": unbalanced quotes; closing quote supplied" << std::endl; if (j >= lastCharacter) { j = lastCharacter - 1; } break; } if (*i == quote) { if (i<lastCharacter && i[1]==quote) { ++i; } else { break; } } else if(*i=='\\' && i<lastCharacter && useBackspace) { ++i; *i = escape(*i); } *++j = *i; CrunchInfo::store(i, j); length++; } if ((length = j - bufferBegin) > reservedTokenLength) { adjustTokenLength(length); } j[1] = QUOTE_MARK; CrunchInfo::store(i, j+1); j += 2; previousString = j; } else if ((*i=='h' || *i=='H') && j>previousString) { // // Test for Hollerith strings // char *j0 = j - 1; if (! isdigit(*j0)) { goto copychar; } int hollerithLength = *j0 - '0'; int ten = 10; char *j1 = previousString; if (j1 > bufferBegin && j1[-1] == QUOTE_MARK) { --j1; } if (j1+4 < j) { j1 = j-4; } for (;;) { if (j0-- <= j1) { goto copychar; } if (! isdigit(*j0 )) { break; } hollerithLength += ten * (*j0-'0'); ten*=10; } // // A Hollerith string must be preceded by a punctuation mark. // '*' is possible only as repetition factor in a data statement // not, in particular, in character*2h . // To avoid some confusion with missing commas in FORMAT statements, // treat a preceding string as a punctuation mark. // if (!(*j0=='*' && bufferBegin[0]=='d') && *j0!='/' && *j0!='(' && *j0!=',' && *j0!='=' && *j0!='.' && *j0 != QUOTE_MARK) { goto copychar; } int hollerithLength0 = hollerithLength; if (i+hollerithLength > lastCharacter) { std::cerr << "Error in line " << lineNumber << ": " << hollerithLength << "H too big" << std::endl; hollerithLength = lastCharacter - i; hollerithLength0 = -1; } j0[1] = QUOTE_MARK; j = j0 + 1; while (hollerithLength-- > 0) { if (++i > lastCharacter) { hol_overflow: if (hollerithLength0 >= 0) { std::cerr << "Error in line " << lineNumber << "escapes make " << hollerithLength0 << "H too big" << std::endl; } break; } if (*i == '\\' && useBackspace) { if (++i > lastCharacter) { goto hol_overflow; } *i = escape(*i); } *++j = *i; CrunchInfo::store(i, j); } j[1] = QUOTE_MARK; CrunchInfo::store(i, j+1); j+=2; previousString = j; } else { if (*i == '(') { ++parenthesesLevel; parenthesesSeen = true; } else if (*i == ')') { --parenthesesLevel; } else if (parenthesesLevel == 0) { if (*i == '=') { exposedEqual = true; } else if (*i == ',') { exposedComma = true; } } copychar: // // not a string or space -- copy, shifting case if necessary // if (shiftCase && isupper(*i)) { *j++ = tolower(*i); } else { *j++ = *i; } CrunchInfo::store(i, j-1); } } lastCharacter = j - 1; nextCharacter = bufferBegin; } static void analyze() { if (parenthesesLevel != 0) { std::cerr << "Error in line " << lineNumber << ": unbalanced parentheses, statement skipped" << std::endl; stkey = SUNKNOWN; // // prevent double error msg // lastCharacter = bufferBegin - 1; return; } if (nextCharacter+2<=lastCharacter && nextCharacter[0]=='i' && nextCharacter[1]=='f' && nextCharacter[2]=='(') { // // assignment or if statement -- look at character after balancing paren // char *i; parenthesesLevel = 1; for (i=nextCharacter+3 ; i<=lastCharacter; ++i) { if (*i == QUOTE_MARK) { while (*++i != QUOTE_MARK) { } } else if (*i == '(') { ++parenthesesLevel; } else if (*i == ')') { if (--parenthesesLevel == 0) { break; } } } if (i >= lastCharacter) { stkey = SLOGIF; } else if (i[1] == '=') { stkey = SLET; } else if (isdigit(i[1])) { stkey = SARITHIF; } else { stkey = SLOGIF; } if (stkey != SLET) { nextCharacter += 2; } } else if (exposedEqual) { // // may be an assignment // if (exposedComma && nextCharacter<lastCharacter && nextCharacter[0]=='d' && nextCharacter[1]=='o') { stkey = SDO; nextCharacter += 2; } else { stkey = SLET; } } else if (parenthesesSeen && (nextCharacter + 7 < lastCharacter) // screen out "double..." early && nextCharacter[2] != 'u' && nextCharacter[0] == 'd' && nextCharacter[1] == 'o' && ((nextCharacter[2] >= '0' && nextCharacter[2] <= '9') || nextCharacter[2] == ',' || nextCharacter[2] == 'w')) { stkey = SDO; nextCharacter += 2; needWhileKey = true; } else { // // otherwise search for keyword // stkey = getKeyword(); if (stkey==SGOTO && lastCharacter>=nextCharacter) { if (nextCharacter[0]=='(') { stkey = SCOMPGOTO; } else if (isAlpha_(*nextCharacter)) { stkey = SASGOTO; } } } parenthesesLevel = 0; } static int getKeyword() { if (! isAlpha_(*nextCharacter)) { return SUNKNOWN; } unsigned char k = letter(nextCharacter[0]); if (const Key *pk = keyStart[k]) { for (const Key *pend = keyEnd[k]; pk<=pend; ++pk ) { const char *i = pk->name; char *j = nextCharacter; while (*++i==*++j && *i!='\0') { } if (*i=='\0' && j<=lastCharacter+1) { nextCharacter = j; return pk->value; } } } return SUNKNOWN; } static int getToken(bool intOnly) { static const char *expMinus = "X**-Y treated as X**(-Y)", *expPlus = "X**+Y treated as X**(+Y)"; bool haveDot = false; bool haveExp = false; bool haveDouble = false; char *p, *i; int c = (unsigned char)(*nextCharacter); if (c == QUOTE_MARK) { ++nextCharacter; p = token; while (*nextCharacter != QUOTE_MARK) { *p++ = *nextCharacter++; } tokenLength = p - token; *p = 0; // // allow octal, binary, hex constants of the form 'abc'x (etc.) // int value; if (++nextCharacter <= lastCharacter && isAlpha_(value = (unsigned char)(*nextCharacter))) { ++nextCharacter; return hexCheck(value); } return SHOLLERITH; } if (needKeyword) { needKeyword = false; return getKeyword(); } for (Punctuation *pp=punctuation; pp->c; ++pp) { if (c == pp->c) { int value = pp->value; if (++nextCharacter <= lastCharacter) { switch (c) { case '/': switch (*nextCharacter) { case '/': nextCharacter++; value = SCONCAT; break; case '=': goto sne; default: if (newDeclaration && parenthesesLevel == 0) { value = SSLASHD; } } return value; case '*': if (*nextCharacter == '*') { nextCharacter++; if (noExtension && nextCharacter <= lastCharacter) { switch (*nextCharacter) { case '-': std::cerr << "Error in line " << lineNumber << expMinus << std::endl; break; case '+': std::cerr << "Error in line " << lineNumber << expPlus << std::endl; } } return SPOWER; } break; case '<': switch(*nextCharacter) { case '=': nextCharacter++; value = SLE; break; case '>': sne: nextCharacter++; value = SNE; } goto extchk; case '=': if (*nextCharacter == '=') { nextCharacter++; value = SEQ; goto extchk; } break; case '>': if (*nextCharacter == '=') { nextCharacter++; value = SGE; } extchk: if (noExtension) { std::cerr << "Error in line " << lineNumber << "Fortran 8x comparison operator" << std::endl; } return value; } } else if (c == '/' && newDeclaration && parenthesesLevel == 0) { return SSLASHD; } switch (value) { case SLPAR: ++parenthesesLevel; break; case SRPAR: --parenthesesLevel; } return value; } } if (c == '.') { if (nextCharacter >= lastCharacter) { goto badchar; } else if (isdigit(nextCharacter[1])) { goto numconst; } else { const char *j; for (DotOp *pd=dotOp; (j=pd->name) ; ++pd) { for (i=nextCharacter+1; i<=lastCharacter; ++i) { if (*i != *j) { break; } else if (*i != '.') { ++j; } else { nextCharacter = i+1; return pd->value; } } } goto badchar; } } if (isAlpha_(c)) { p = token; *p++ = *nextCharacter++; while (nextCharacter<=lastCharacter) { if (isAlphaNum_((unsigned char)(*nextCharacter))) { *p++ = *nextCharacter++; } else { break; } } tokenLength = p - token; *p = 0; if (needWhileKey) { needKeyword = 0; if (tokenLength == 5 && nextCharacter <= lastCharacter && *nextCharacter == '(' // ) && !strcmp(token,"while")) { return SWHILE; } } if (inioctl && nextCharacter<=lastCharacter && *nextCharacter=='=') { ++nextCharacter; return SNAMEEQ; } if (tokenLength>8 && !strncmp(token, "function", 8) && isAlpha_((unsigned char)(token[8])) && nextCharacter < lastCharacter && nextCharacter[0]=='(' && (nextCharacter[1]==')' || isAlpha_((unsigned char)(nextCharacter[1])))) { nextCharacter -= (tokenLength - 8); return SFUNCTION; } if (tokenLength > MAX_NAMELENGTH) { char buff[2*MAX_NAMELENGTH+50]; if (tokenLength >= MAX_NAMELENGTH+10) { sprintf(buff, "name %.*s... too long, truncated to %.*s", MAX_NAMELENGTH+6, token, MAX_NAMELENGTH, token); } else { sprintf(buff, "name %s too long, truncated to %.*s", token, MAX_NAMELENGTH, token); } std::cerr << "Error in line " << lineNumber << ": " << buff << std::endl; tokenLength = MAX_NAMELENGTH; token[MAX_NAMELENGTH] = '\0'; } if (tokenLength==1 && *nextCharacter==QUOTE_MARK) { int value = token[0]; ++nextCharacter; for (p=token; *nextCharacter!=QUOTE_MARK; ) { *p++ = *nextCharacter++; } ++nextCharacter; tokenLength = p - token; *p = 0; return hexCheck(value); } return(SNAME); } if (isdigit(c)) { // // Check for NAG's special hex constant // if ((nextCharacter[1] == '#' && nextCharacter < lastCharacter) || (nextCharacter[2] == '#' && isdigit(nextCharacter[1]) && lastCharacter - nextCharacter >= 2)) { int radix = atoi(nextCharacter); p = 0; if (*++nextCharacter != '#') { ++nextCharacter; } if (radix != 2 && radix != 8 && radix != 16) { std::cerr << "Error in line " << lineNumber << ":invalid base " << radix << " for constant, defaulting to hex" << std::endl; radix = 16; } if (++nextCharacter > lastCharacter) { goto badchar; } for (p = token; hexToInt(*nextCharacter) < radix;) { *p++ = *nextCharacter++; if (nextCharacter > lastCharacter) { break; } } tokenLength = p - token; *p = 0; return (radix == 16) ? SHEXCON : ((radix == 8) ? SOCTCON : SBITCON); } } else { goto badchar; } numconst: char *n1; for (n1 = nextCharacter; nextCharacter<=lastCharacter; ++nextCharacter) { if (*nextCharacter == '.') { if (haveDot) { break; } else if (nextCharacter+2<=lastCharacter && isAlpha_((unsigned char)(nextCharacter[1])) && isAlpha_((unsigned char)(nextCharacter[2]))) { break; } else { haveDot = true; } } else if (! isdigit((unsigned char)(*nextCharacter))) { if (!intOnly && (*nextCharacter=='d' || *nextCharacter=='e') ) { p = nextCharacter; haveExp = true; if (*nextCharacter == 'd') { haveDouble = true; } if (nextCharacter<lastCharacter) { if (nextCharacter[1]=='+' || nextCharacter[1]=='-') { ++nextCharacter; } } if (! isdigit(*++nextCharacter)) { nextCharacter = p; haveDouble = haveExp = false; break; } for (++nextCharacter; nextCharacter<=lastCharacter && isdigit((unsigned char)(*nextCharacter)); ++nextCharacter); } break; } } p = token; i = n1; while (i < nextCharacter) { *p++ = *i++; } tokenLength = p - token; *p = 0; if (haveDouble) { return SDCON; } if (haveDot || haveExp) { return SRCON; } return SICON; badchar: bufferBegin[0] = *nextCharacter++; return SUNKNOWN; } static int hexCheck(int key) { static const char *kindName[3] = { "hexadecimal", "octal", "binary" }; int radix, kind; switch (key) { case 'z': case 'Z': case 'x': case 'X': radix = 16; key = SHEXCON; kind = 0; break; case 'o': case 'O': radix = 8; key = SOCTCON; kind= 1; break; case 'b': case 'B': radix = 2; key = SBITCON; kind = 2; break; default: std::cerr << "Error in line " << lineNumber << ": bad bit identifier" << std::endl;; return SNAME; } for (char *p = token; *p; p++) { if (hexToInt(*p) >= radix) { std::cerr << "Error in line " << lineNumber << ": invalid " << kindName[kind] << " character" << std::endl; break; } } return key; } |