Home | History | Annotate | Download | only in h
      1 #ifndef PBLACKBOX_H
      2 #define PBLACKBOX_H
      3 
      4 /*
      5  * SOFTWARE RIGHTS
      6  *
      7  * We reserve no LEGAL rights to the Purdue Compiler Construction Tool
      8  * Set (PCCTS) -- PCCTS is in the public domain.  An individual or
      9  * company may do whatever they wish with source code distributed with
     10  * PCCTS or the code generated by PCCTS, including the incorporation of
     11  * PCCTS, or its output, into commerical software.
     12  *
     13  * We encourage users to develop software with PCCTS.  However, we do ask
     14  * that credit is given to us for developing PCCTS.  By "credit",
     15  * we mean that if you incorporate our source code into one of your
     16  * programs (commercial product, research project, or otherwise) that you
     17  * acknowledge this fact somewhere in the documentation, research report,
     18  * etc...  If you like PCCTS and have developed a nice tool with the
     19  * output, please mention that you developed it using PCCTS.  In
     20  * addition, we ask that this header remain intact in our source code.
     21  * As long as these guidelines are kept, we expect to continue enhancing
     22  * this system and expect to make other tools available as they are
     23  * completed.
     24  *
     25  * ANTLR 1.33
     26  * Terence Parr
     27  * Parr Research Corporation
     28  * with Purdue University and AHPCRC, University of Minnesota
     29  * 1989-2000
     30  */
     31 
     32 /* Completely rewritten by Chris Uzdavinis (chris (at) atdesk.com) for MR23 */
     33 
     34 #include "pcctscfg.h"
     35 
     36 #include "pccts_iostream.h"
     37 
     38 PCCTS_NAMESPACE_STD
     39 
     40 //  MR20 Added #include for "DLexerBase.h"
     41 
     42 #include "DLexerBase.h"
     43 
     44 //
     45 //  The default buffer size of the lexer is given by the
     46 //   second argument of the lexer's ctor.  It is optional
     47 //   and defaults to 2000
     48 //
     49 
     50 template<class Lexer, class Parser, class Token>
     51 class DllExportPCCTS ParserBlackBox {
     52 private:
     53   // no copy construction allowed
     54   ParserBlackBox(ParserBlackBox const &);
     55 
     56   // no copy assignment allowed
     57   ParserBlackBox & operator=(ParserBlackBox const &);
     58 
     59 protected:
     60   DLGFileInput *in;
     61   Lexer *scan;
     62   _ANTLRTokenPtr tok;
     63   ANTLRTokenBuffer *pipe;
     64   Parser *_parser;
     65   FILE *file;
     66   int openByBlackBox;    /* MR21 Don't close what we haven't opened */
     67 public:
     68 
     69   ParserBlackBox(FILE *f)
     70     : in(0)
     71     , scan(0)
     72     , tok(0)
     73     , pipe(0)
     74     , _parser(0)
     75     , file(0)
     76     , openByBlackBox(0)
     77   {
     78     if (f == NULL)
     79     {
     80       cerr << "invalid file pointer\n";
     81     }
     82     else
     83     {
     84       openByBlackBox = 0;     /* MR21a */
     85       file = f;
     86       in = new DLGFileInput(f);
     87       scan = new Lexer(in);
     88       pipe = new ANTLRTokenBuffer(scan);
     89       tok = new Token;
     90       scan->setToken(tok);
     91       _parser = new Parser(pipe);
     92       _parser->init();
     93     }
     94   }
     95   ParserBlackBox(char *fname)
     96     : in(0)
     97     , scan(0)
     98     , tok(0)
     99     , pipe(0)
    100     , _parser(0)
    101     , file(0)
    102     , openByBlackBox(0)
    103   {
    104     FILE *f = fopen(fname, "r");
    105     if ( f==NULL ) {
    106       openByBlackBox = 0;
    107       cerr << "cannot open " << fname << "\n"; return;
    108     }
    109     else {
    110       openByBlackBox = 1;
    111       file = f;
    112       in = new DLGFileInput(f);
    113       scan = new Lexer(in);
    114       pipe = new ANTLRTokenBuffer(scan);
    115       tok = new Token;
    116       scan->setToken(tok);
    117       _parser = new Parser(pipe);
    118       _parser->init();
    119     }
    120   }
    121 
    122   ~ParserBlackBox()
    123   {
    124     delete in; delete scan; delete pipe; delete _parser; delete tok;
    125     if (1 == openByBlackBox) {
    126       fclose(file);
    127     }
    128   }
    129 
    130   Parser *parser()	   { return _parser; }
    131   Lexer  *getLexer()     { return scan; }
    132 };
    133 
    134 #endif
    135