home *** CD-ROM | disk | FTP | other *** search
/ gdead.berkeley.edu / gdead.berkeley.edu.tar / gdead.berkeley.edu / pub / cad-tools / ciftomann.tar / CD / parser.h < prev    next >
C/C++ Source or Header  |  1988-01-28  |  7KB  |  243 lines

  1. /*
  2.  * parser.h
  3.  *
  4.  * Copyright -C- 1981 Kenneth H. Keller, Giles C. Billingsley
  5.  * sccsid "%W%  %G%"
  6.  *
  7.  *     KIC is a graphics editor that was developed by the integrated
  8.  * circuits group of the Electronics Research Laboratory and the
  9.  * Department of Electrical Engineering and Computer Sciences at
  10.  * the University of California, Berkeley, California.  The program
  11.  * KIC is available free of charge to any interested party.
  12.  * The sale, resale, or use of this program for profit without the
  13.  * express written consent of the Department of Electrical Engineering
  14.  * and Computer Sciences, University of California, Berkeley, California,
  15.  * is forbidden.
  16.  */
  17.  
  18.  
  19. /*
  20.  * Fast CIF parser's data structures.
  21.  * 
  22.  */
  23.  
  24. #include "macros.h"
  25. #include <stdio.h>
  26.  
  27. /*
  28.  * 24*80+1=1921 characters of string.
  29.  */
  30. #define    PSTRINGSIZE    1921
  31.  
  32.  
  33. /*
  34.  * Status of parse when parser returns.
  35.  */
  36. char PStatus[80*3+1];
  37.  
  38. /*
  39.  * CIF file desc.
  40.  */
  41. FILE *PCIFFileDesc;
  42.  
  43. /*
  44.  * string for input line buffering
  45.  */
  46. char PString[PSTRINGSIZE];
  47.  
  48. /*
  49.  * Global token variables.
  50.  */
  51. int PChar;
  52. int PInt;
  53. int PReturned;
  54.  
  55. /*
  56.  * Ousterhout's path package.
  57.  */
  58. FILE *POpen();
  59.  
  60. /*
  61.  * Values routines return.
  62.  */
  63. #define PSUCCEEDED        1    /* successful return */
  64. #define PFAILED            2    /* parser failed */
  65. #define PNOTAPPLICABLE        3    /* parser failed due to syntax */
  66.  
  67. /*
  68.  * Arguments to handle an EOF in PCharacter and PInteger
  69.  */
  70. #define PFAILONEOF        1
  71. #define PDONTFAILONEOF        2
  72.  
  73. /*
  74.  * Arguments to specify characters to be ignored by PWhiteSpace
  75.  */
  76. #define PSTRIPWHITESPACE1    1    /* strip blanks, tabs, commas, or    */
  77.                     /* new lines.                        */
  78. #define PSTRIPWHITESPACE2    2    /* strip everything but upper case,  */
  79.                     /* hyphens, digits, parens, and ;'s. */
  80. #define PSTRIPWHITESPACE3    3    /* strip everything but digits,      */
  81.                     /* hyphens, parens, and ;'s.         */
  82. #define PLEAVEWHITESPACE    4
  83.  
  84.  
  85.  
  86. #define PCharacter(Returned,WhiteSpaceControl,EOFControl){        \
  87.     /* PWhiteSpace returns either PSUCCEEDED or PFAILED */        \
  88.     PWhiteSpace(Returned,WhiteSpaceControl,EOFControl);            \
  89.     if(Returned != PFAILED){                        \
  90.         PChar = getc(PCIFFileDesc);                    \
  91.         if(PChar == EOF){                        \
  92.         if(EOFControl != PDONTFAILONEOF) {                \
  93.                 PErrorEOF();                        \
  94.                 Returned = PFAILED;                    \
  95.                 }                            \
  96.         }                                \
  97.         }                                \
  98.     }
  99.  
  100. #define PWhiteSpace(Returned,WhiteSpaceControl,EOFControl){        \
  101.     Returned = PSUCCEEDED;                         \
  102.     if(WhiteSpaceControl == PSTRIPWHITESPACE1){                \
  103.         while((PChar = getc(PCIFFileDesc)) == ' ' Or PChar == '\t' Or    \
  104.         PChar == '\n' Or PChar == ','){                \
  105.             if(PChar == EOF){                        \
  106.             if(EOFControl != PDONTFAILONEOF){            \
  107.                     PErrorEOF();                    \
  108.                     Returned = PFAILED;                    \
  109.                     }                            \
  110.             break;                            \
  111.             }                            \
  112.         }                                \
  113.         ungetc((char)PChar,PCIFFileDesc);                \
  114.         }                                \
  115.     elif(WhiteSpaceControl == PSTRIPWHITESPACE2){            \
  116.         while((PChar = getc(PCIFFileDesc)) != '-' And PChar != '('     \
  117.         And PChar != ')' And (PChar < 'A' Or PChar > 'Z')        \
  118.         And    (PChar < '0' Or PChar > '9') And PChar != ';'){        \
  119.             if(PChar == EOF){                        \
  120.             if(EOFControl != PDONTFAILONEOF) {            \
  121.                     PErrorEOF();                    \
  122.                     Returned = PFAILED;                    \
  123.                     }                            \
  124.             break;                            \
  125.             }                            \
  126.         }                                \
  127.         ungetc((char)PChar,PCIFFileDesc);                \
  128.         }                                \
  129.     elif(WhiteSpaceControl == PSTRIPWHITESPACE3){            \
  130.         while((PChar = getc(PCIFFileDesc)) != '-' And PChar != '('    \
  131.         And PChar != ')' And (PChar< '0' Or PChar > '9') And    \
  132.         PChar != ';'){                        \
  133.             if(PChar == EOF){                        \
  134.             if(EOFControl != PDONTFAILONEOF){            \
  135.                     PErrorEOF();                    \
  136.                     Returned = PFAILED;                    \
  137.                     }                            \
  138.             break;                            \
  139.             }                            \
  140.         }                                \
  141.         ungetc((char)PChar,PCIFFileDesc);                \
  142.         }                                \
  143.     }
  144.  
  145. #define PWhiteSpace1(Returned,EOFControl){                \
  146.     Returned = PSUCCEEDED;                         \
  147.     while((PChar = getc(PCIFFileDesc)) == ' ' Or PChar == '\t' Or    \
  148.     PChar == '\n' Or PChar == ','){                    \
  149.         if(PChar == EOF){                        \
  150.         if(EOFControl != PDONTFAILONEOF){                \
  151.                 PErrorEOF();                        \
  152.                 Returned = PFAILED;                    \
  153.                 }                            \
  154.         break;                            \
  155.         }                                \
  156.     }                                \
  157.     ungetc((char)PChar,PCIFFileDesc);                    \
  158.     }
  159.  
  160. #define PWhiteSpace2(Returned,EOFControl){                \
  161.     Returned = PSUCCEEDED;                         \
  162.     while((PChar = getc(PCIFFileDesc)) != '-' And PChar != '('         \
  163.     And PChar != ')' And (PChar < 'A' Or PChar > 'Z')        \
  164.     And (PChar < '0' Or PChar > '9') And PChar != ';'){        \
  165.         if(PChar == EOF){                        \
  166.         if(EOFControl != PDONTFAILONEOF) {                \
  167.                 PErrorEOF();                        \
  168.                 Returned = PFAILED;                    \
  169.                 }                            \
  170.         break;                            \
  171.         }                                \
  172.     }                                \
  173.     ungetc((char)PChar,PCIFFileDesc);                    \
  174.     }
  175.  
  176. #define PWhiteSpace3(Returned,EOFControl){                \
  177.     Returned = PSUCCEEDED;                         \
  178.     while((PChar = getc(PCIFFileDesc)) != '-' And PChar != '('        \
  179.     And PChar != ')' And (PChar< '0' Or PChar > '9') And        \
  180.     PChar != ';'){                            \
  181.         if(PChar == EOF){                        \
  182.         if(EOFControl != PDONTFAILONEOF){                \
  183.                 PErrorEOF();                        \
  184.                 Returned = PFAILED;                    \
  185.                 }                            \
  186.         break;                            \
  187.         }                                \
  188.     }                                \
  189.     ungetc((char)PChar,PCIFFileDesc);                    \
  190.     }
  191.  
  192. #define PLookAhead(Returned, WhiteSpaceControl, For){            \
  193.     /* PCharaceter will return either PSUCCEEDED or PFAILED */        \
  194.     PCharacter(Returned,WhiteSpaceControl,PFAILONEOF);            \
  195.     if(PChar != For && Returned != PFAILED){                \
  196.         ungetc((char)PChar,PCIFFileDesc);                \
  197.     }                                \
  198.     }
  199.  
  200. #define PLookForSemi(Returned){                        \
  201.     /* PWhiteSpace returns either PSUCCEEDED or PFAILED */        \
  202.     PWhiteSpace3(Returned,PFAILONEOF);                    \
  203.     if(Returned != PFAILED){                        \
  204.         PChar = getc(PCIFFileDesc);                    \
  205.         if(PChar == EOF){                        \
  206.             PErrorEOF();                        \
  207.             Returned = PFAILED;                        \
  208.         }                                \
  209.         elif(PChar != ';'){                        \
  210.             ungetc((char)PChar,PCIFFileDesc);                \
  211.         }                                \
  212.     }                                \
  213.     }
  214.  
  215. #define PInteger(Returned,EOFControl) {                    \
  216.     loop{                                \
  217.         PWhiteSpace3(PReturned,EOFControl);                \
  218.         if(PReturned == PFAILED){                    \
  219.             Returned = PFAILED;                        \
  220.         break;                            \
  221.         }                                \
  222.     if((PChar >= '0' And PChar <= '9') Or                \
  223.         (PChar == '-' Or PChar == '+')) {                \
  224.             /* read integer */                        \
  225.             if((fscanf(PCIFFileDesc,"%d",&PInt)) == EOF){        \
  226.             if(EOFControl != PDONTFAILONEOF){            \
  227.                     PChar = EOF;                    \
  228.                     PErrorEOF();                    \
  229.                     Returned = PFAILED;                    \
  230.                     }                            \
  231.             }                            \
  232.             Returned = PSUCCEEDED;                    \
  233.         break;                            \
  234.         }                                \
  235.         /* drop unwanted characters */                    \
  236.         PCharacter(PReturned,PSTRIPWHITESPACE1,EOFControl);        \
  237.         if(PReturned == PFAILED){                    \
  238.             Returned = PFAILED;                        \
  239.             break;                            \
  240.         }                                \
  241.         }                                \
  242.     }
  243.