GME  13
ATokenBuffer.h
Go to the documentation of this file.
00001 /* ANTLRTokenBuffer.h
00002  *
00003  * SOFTWARE RIGHTS
00004  *
00005  * We reserve no LEGAL rights to the Purdue Compiler Construction Tool
00006  * Set (PCCTS) -- PCCTS is in the public domain.  An individual or
00007  * company may do whatever they wish with source code distributed with
00008  * PCCTS or the code generated by PCCTS, including the incorporation of
00009  * PCCTS, or its output, into commerical software.
00010  *
00011  * We encourage users to develop software with PCCTS.  However, we do ask
00012  * that credit is given to us for developing PCCTS.  By "credit",
00013  * we mean that if you incorporate our source code into one of your
00014  * programs (commercial product, research project, or otherwise) that you
00015  * acknowledge this fact somewhere in the documentation, research report,
00016  * etc...  If you like PCCTS and have developed a nice tool with the
00017  * output, please mention that you developed it using PCCTS.  In
00018  * addition, we ask that this header remain intact in our source code.
00019  * As long as these guidelines are kept, we expect to continue enhancing
00020  * this system and expect to make other tools available as they are
00021  * completed.
00022  *
00023  * ANTLR 1.33
00024  * Terence Parr
00025  * Parr Research Corporation
00026  * with Purdue University and AHPCRC, University of Minnesota
00027  * 1989-1998
00028  */
00029 
00030 #ifndef ATOKENBUFFER_H_GATE
00031 #define ATOKENBUFFER_H_GATE
00032 
00033 #include "config.h"
00034 #include ATOKEN_H
00035 #include ATOKENSTREAM_H
00036 #include <stdlib.h>
00037 
00038 /*
00039  * The parser is "attached" to an ANTLRTokenBuffer via interface
00040  * functions: getToken() and bufferedToken().  The object that actually
00041  * consumes characters and constructs tokens is connected to the
00042  * ANTLRTokenBuffer via interface function ANTLRTokenStream::getToken();
00043  * where ANTLRTokenStream is really just a behavior (class with no data).
00044  * C++ does not have this abstraction and hence we simply have come up
00045  * with a fancy name for "void *".  See the note in ANTLRTokenStream.h on
00046  * the "behavior" of ANTLRTokenStream.
00047  */
00048 
00049 class ANTLRParser;                                      // MR1
00050 
00051 class ANTLRTokenBuffer {
00052 protected:
00053         ANTLRTokenStream *input;        // where do I get tokens
00054         int buffer_size;
00055         int chunk_size;
00056         int num_markers;
00057         int k;                          // Need at least this many tokens in buffer
00058         _ANTLRTokenPtr *buffer; // buffer used for arbitrary lookahead
00059         _ANTLRTokenPtr *tp;        // pts into buffer; current token ptr
00060         _ANTLRTokenPtr *last;      // pts to last valid token in buffer
00061         _ANTLRTokenPtr *next;      // place to put token from getANTLRToken()
00062         _ANTLRTokenPtr *end_of_buffer;
00063         /* when you try to write a token past this and there are no markers
00064            set, then move k-1 tokens back to the beginning of the buffer.
00065            We want to stay away from the end of the buffer because we have
00066            to extend it if a marker is set and we reach the end (we cannot
00067            move tokens to the beginning of the buffer in this case).
00068          */
00069         _ANTLRTokenPtr *threshold;
00070         unsigned char _deleteTokens;
00071 
00072         // This function is filled in by the subclass; it initiates fetch of input
00073         virtual _ANTLRTokenPtr getANTLRToken() { return input->getToken(); }
00074         void makeRoom();
00075         void extendBuffer();
00076 
00077 public:
00078         ANTLRTokenBuffer(ANTLRTokenStream *in, int k=1, int chksz=50);
00079         virtual ~ANTLRTokenBuffer();
00080         virtual _ANTLRTokenPtr getToken();
00081         virtual void rewind(int pos);
00082         virtual int mark();
00083         virtual _ANTLRTokenPtr bufferedToken(int i);
00084 
00085         void noGarbageCollectTokens()   { _deleteTokens=0; }
00086         void garbageCollectTokens()             { _deleteTokens=1; }
00087 
00088         virtual int bufferSize() { return buffer_size; }
00089         virtual int minTokens() { return k; }
00090         virtual void setMinTokens(int k_new) { k = k_new; }
00091 
00092         virtual void panic(char *msg) { exit(PCCTS_EXIT_FAILURE); }
00093 protected:                                              // MR1
00094         ANTLRParser     *parser;                        // MR1
00095 public:                                                 // MR1
00096         ANTLRParser     *setParser(ANTLRParser *p);     // MR1
00097         ANTLRParser     *getParser();                       // MR1
00098     ANTLRTokenStream *getLexer() const {    // MR12
00099       return input;}                        // MR12
00100 };
00101 
00102 #endif