]> git.proxmox.com Git - mirror_edk2.git/blobdiff - EdkCompatibilityPkg/Other/Maintained/Tools/Pccts/h/ATokenBuffer.cpp
Add in the 1st version of ECP.
[mirror_edk2.git] / EdkCompatibilityPkg / Other / Maintained / Tools / Pccts / h / ATokenBuffer.cpp
diff --git a/EdkCompatibilityPkg/Other/Maintained/Tools/Pccts/h/ATokenBuffer.cpp b/EdkCompatibilityPkg/Other/Maintained/Tools/Pccts/h/ATokenBuffer.cpp
new file mode 100644 (file)
index 0000000..a7b10c7
--- /dev/null
@@ -0,0 +1,345 @@
+/* ANTLRTokenBuffer.C\r
+ *\r
+ * SOFTWARE RIGHTS\r
+ *\r
+ * We reserve no LEGAL rights to the Purdue Compiler Construction Tool\r
+ * Set (PCCTS) -- PCCTS is in the public domain.  An individual or\r
+ * company may do whatever they wish with source code distributed with\r
+ * PCCTS or the code generated by PCCTS, including the incorporation of\r
+ * PCCTS, or its output, into commerical software.\r
+ *\r
+ * We encourage users to develop software with PCCTS.  However, we do ask\r
+ * that credit is given to us for developing PCCTS.  By "credit",\r
+ * we mean that if you incorporate our source code into one of your\r
+ * programs (commercial product, research project, or otherwise) that you\r
+ * acknowledge this fact somewhere in the documentation, research report,\r
+ * etc...  If you like PCCTS and have developed a nice tool with the\r
+ * output, please mention that you developed it using PCCTS.  In\r
+ * addition, we ask that this header remain intact in our source code.\r
+ * As long as these guidelines are kept, we expect to continue enhancing\r
+ * this system and expect to make other tools available as they are\r
+ * completed.\r
+ *\r
+ * ANTLR 1.33\r
+ * Terence Parr\r
+ * Parr Research Corporation\r
+ * with Purdue University and AHPCRC, University of Minnesota\r
+ * 1989-1998\r
+ */\r
+\r
+typedef int ANTLRTokenType;  // fool AToken.h into compiling\r
+\r
+class ANTLRParser;          /* MR1 */\r
+\r
+#define ANTLR_SUPPORT_CODE\r
+\r
+#include "pcctscfg.h"\r
+\r
+#include ATOKENBUFFER_H\r
+typedef ANTLRAbstractToken *_ANTLRTokenPtr;\r
+\r
+#if defined(DBG_TBUF)||defined(DBG_TBUF_MARK_REW)\r
+static unsigned char test[1000];\r
+#endif\r
+\r
+#ifdef DBG_REFCOUNTTOKEN\r
+int ANTLRCommonToken::ctor = 0;\r
+int ANTLRCommonToken::dtor = 0;\r
+#endif\r
+\r
+ANTLRTokenBuffer::\r
+ANTLRTokenBuffer(ANTLRTokenStream *_input, int _k, int _chunk_size_formal) /* MR14 */\r
+{\r
+  this->input = _input;\r
+  this->k = _k;\r
+  buffer_size = chunk_size = _chunk_size_formal;\r
+  buffer = (_ANTLRTokenPtr *)\r
+       calloc(chunk_size+1,sizeof(_ANTLRTokenPtr ));\r
+  if ( buffer == NULL ) {\r
+    panic("cannot alloc token buffer");\r
+  }\r
+  buffer++;        // leave the first elem empty so tp-1 is valid ptr\r
+\r
+  tp = &buffer[0];\r
+  last = tp-1;\r
+  next = &buffer[0];\r
+  num_markers = 0;\r
+  end_of_buffer = &buffer[buffer_size-1];\r
+  // BUGBUG -- threshold = &buffer[(int)(buffer_size*(1.0/2.0))];\r
+  threshold = &buffer[(int)(buffer_size / 2)];\r
+  _deleteTokens = 1;   // assume we delete tokens\r
+  parser=NULL;        // MR5 - uninitialized reference\r
+}\r
+\r
+static void f() {;}\r
+ANTLRTokenBuffer::\r
+~ANTLRTokenBuffer()\r
+{\r
+  f();\r
+  // Delete all remaining tokens (from 0..last inclusive)\r
+  if ( _deleteTokens )\r
+  {\r
+    _ANTLRTokenPtr *z;\r
+    for (z=buffer; z<=last; z++)\r
+    {\r
+      (*z)->deref();\r
+//      z->deref();\r
+#ifdef DBG_REFCOUNTTOKEN\r
+          fprintf(stderr, "##########dtor: deleting token '%s' (ref %d)\n",\r
+              ((ANTLRCommonToken *)*z)->getText(), (*z)->nref());\r
+#endif\r
+      if ( (*z)->nref()==0 )\r
+      {\r
+        delete (*z);\r
+      }\r
+    }\r
+  }\r
+\r
+  if ( buffer!=NULL ) free((char *)(buffer-1));\r
+}\r
+\r
+#if defined(DBG_TBUF)||defined(DBG_TBUF_MARK_REW)\r
+#include "pccts_stdio.h"\r
+PCCTS_NAMESPACE_STD\r
+#endif\r
+\r
+_ANTLRTokenPtr ANTLRTokenBuffer::\r
+getToken()\r
+{\r
+  if ( tp <= last )  // is there any buffered lookahead still to be read?\r
+  {\r
+    return *tp++;  // read buffered lookahead\r
+  }\r
+  // out of buffered lookahead, get some more "real"\r
+  // input from getANTLRToken()\r
+  if ( num_markers==0 )\r
+  {\r
+    if( next > threshold )\r
+    {\r
+#ifdef DBG_TBUF\r
+fprintf(stderr,"getToken: next > threshold (high water is %d)\n", threshold-buffer);\r
+#endif\r
+      makeRoom();\r
+    }\r
+  }\r
+  else {\r
+    if ( next > end_of_buffer )\r
+    {\r
+#ifdef DBG_TBUF\r
+fprintf(stderr,"getToken: next > end_of_buffer (size is %d)\n", buffer_size);\r
+#endif\r
+      extendBuffer();\r
+    }\r
+  }\r
+  *next = getANTLRToken();\r
+  (*next)->ref();        // say we have a copy of this pointer in buffer\r
+  last = next;\r
+  next++;\r
+  tp = last;\r
+  return *tp++;\r
+}\r
+\r
+void ANTLRTokenBuffer::\r
+rewind(int pos)\r
+{\r
+#if defined(DBG_TBUF)||defined(DBG_TBUF_MARK_REW)\r
+  fprintf(stderr, "rewind(%d)[nm=%d,from=%d,%d.n=%d]\n", pos, num_markers, tp-buffer,pos,test[pos]);\r
+  test[pos]--;\r
+#endif\r
+  tp = &buffer[pos];\r
+  num_markers--;\r
+}\r
+\r
+/*\r
+ * This function is used to specify that the token pointers read\r
+ * by the ANTLRTokenBuffer should be buffered up (to be reused later).\r
+ */\r
+int ANTLRTokenBuffer::\r
+mark()\r
+{\r
+#if defined(DBG_TBUF)||defined(DBG_TBUF_MARK_REW)\r
+  test[tp-buffer]++;\r
+  fprintf(stderr,"mark(%d)[nm=%d,%d.n=%d]\n",tp-buffer,num_markers+1,tp-buffer,test[tp-buffer]);\r
+#endif\r
+  num_markers++;\r
+  return tp - buffer;\r
+}\r
+\r
+/*\r
+ * returns the token pointer n positions ahead.\r
+ * This implies that bufferedToken(1) gets the NEXT symbol of lookahead.\r
+ * This is used in conjunction with the ANTLRParser lookahead buffer.\r
+ *\r
+ * No markers are set or anything.  A bunch of input is buffered--that's all.\r
+ * The tp pointer is left alone as the lookahead has not been advanced\r
+ * with getToken().  The next call to getToken() will find a token\r
+ * in the buffer and won't have to call getANTLRToken().\r
+ *\r
+ * If this is called before a consume() is done, how_many_more_i_need is\r
+ * set to 'n'.\r
+ */\r
+_ANTLRTokenPtr ANTLRTokenBuffer::\r
+bufferedToken(int n)\r
+{\r
+//  int how_many_more_i_need = (last-tp < 0) ? n : n-(last-tp)-1;\r
+  int how_many_more_i_need = (tp > last) ? n : n-(last-tp)-1;\r
+  // Make sure that at least n tokens are available in the buffer\r
+#ifdef DBG_TBUF\r
+  fprintf(stderr, "bufferedToken(%d)\n", n);\r
+#endif\r
+  for (int i=1; i<=how_many_more_i_need; i++)\r
+  {\r
+    if ( next > end_of_buffer )  // buffer overflow?\r
+    {\r
+      extendBuffer();\r
+    }\r
+    *next = getANTLRToken();\r
+    (*next)->ref();    // say we have a copy of this pointer in buffer\r
+    last = next;\r
+    next++;\r
+  }\r
+  return tp[n - 1];\r
+}\r
+\r
+/* If no markers are set, the none of the input needs to be saved (except\r
+ * for the lookahead Token pointers).  We save only k-1 token pointers as\r
+ * we are guaranteed to do a getANTLRToken() right after this because otherwise\r
+ * we wouldn't have needed to extend the buffer.\r
+ *\r
+ * If there are markers in the buffer, we need to save things and so\r
+ * extendBuffer() is called.\r
+ */\r
+void ANTLRTokenBuffer::\r
+makeRoom()\r
+{\r
+#ifdef DBG_TBUF\r
+  fprintf(stderr, "in makeRoom.................\n");\r
+  fprintf(stderr, "num_markers==%d\n", num_markers);\r
+#endif\r
+/*\r
+  if ( num_markers == 0 )\r
+  {\r
+*/\r
+#ifdef DBG_TBUF\r
+    fprintf(stderr, "moving lookahead and resetting next\n");\r
+\r
+    _ANTLRTokenPtr *r;\r
+    fprintf(stderr, "tbuf = [");\r
+    for (r=buffer; r<=last; r++)\r
+    {\r
+      if ( *r==NULL ) fprintf(stderr, " xxx");\r
+      else fprintf(stderr, " '%s'", ((ANTLRCommonToken *)*r)->getText());\r
+    }\r
+    fprintf(stderr, " ]\n");\r
+\r
+    fprintf(stderr,\r
+    "before: tp=%d, last=%d, next=%d, threshold=%d\n",tp-buffer,last-buffer,next-buffer,threshold-buffer);\r
+#endif\r
+\r
+    // Delete all tokens from 0..last-(k-1) inclusive\r
+    if ( _deleteTokens )\r
+    {\r
+      _ANTLRTokenPtr *z;\r
+      for (z=buffer; z<=last-(k-1); z++)\r
+      {\r
+        (*z)->deref();\r
+//        z->deref();\r
+#ifdef DBG_REFCOUNTTOKEN\r
+          fprintf(stderr, "##########makeRoom: deleting token '%s' (ref %d)\n",\r
+              ((ANTLRCommonToken *)*z)->getText(), (*z)->nref());\r
+#endif\r
+        if ( (*z)->nref()==0 )\r
+        {\r
+          delete (*z);\r
+        }\r
+      }\r
+    }\r
+\r
+    // reset the buffer to initial conditions, but move k-1 symbols\r
+    // to the beginning of buffer and put new input symbol at k\r
+    _ANTLRTokenPtr *p = buffer, *q = last-(k-1)+1;\r
+//    ANTLRAbstractToken **p = buffer, **q = end_of_buffer-(k-1)+1;\r
+#ifdef DBG_TBUF\r
+    fprintf(stderr, "lookahead buffer = [");\r
+#endif\r
+    for (int i=1; i<=(k-1); i++)\r
+    {\r
+      *p++ = *q++;\r
+#ifdef DBG_TBUF\r
+      fprintf(stderr,\r
+      " '%s'", ((ANTLRCommonToken *)buffer[i-1])->getText());\r
+#endif\r
+    }\r
+#ifdef DBG_TBUF\r
+    fprintf(stderr, " ]\n");\r
+#endif\r
+    next = &buffer[k-1];\r
+    tp = &buffer[k-1];  // tp points to what will be filled in next\r
+    last = tp-1;\r
+#ifdef DBG_TBUF\r
+    fprintf(stderr,\r
+    "after: tp=%d, last=%d, next=%d\n",\r
+    tp-buffer, last-buffer, next-buffer);\r
+#endif\r
+/*\r
+  }\r
+  else {\r
+    extendBuffer();\r
+  }\r
+*/\r
+}\r
+\r
+/* This function extends 'buffer' by chunk_size and returns with all\r
+ * pointers at the same relative positions in the buffer (the buffer base\r
+ * address could have changed in realloc()) except that 'next' comes\r
+ * back set to where the next token should be stored.  All other pointers\r
+ * are untouched.\r
+ */\r
+void\r
+ANTLRTokenBuffer::\r
+extendBuffer()\r
+{\r
+  int save_last = last-buffer, save_tp = tp-buffer, save_next = next-buffer;\r
+#ifdef DBG_TBUF\r
+  fprintf(stderr, "extending physical buffer\n");\r
+#endif\r
+  buffer_size += chunk_size;\r
+  buffer = (_ANTLRTokenPtr *)\r
+    realloc((char *)(buffer-1),\r
+        (buffer_size+1)*sizeof(_ANTLRTokenPtr ));\r
+  if ( buffer == NULL ) {\r
+    panic("cannot alloc token buffer");\r
+  }\r
+  buffer++;        // leave the first elem empty so tp-1 is valid ptr\r
+\r
+  tp = buffer + save_tp;  // put the pointers back to same relative position\r
+  last = buffer + save_last;\r
+  next = buffer + save_next;\r
+  end_of_buffer = &buffer[buffer_size-1];\r
+  // BUGBUG -- threshold = &buffer[(int)(buffer_size*(1.0/2.0))];\r
+  threshold = &buffer[(int)(buffer_size / 2)];\r
+\r
+/*\r
+  // zero out new token ptrs so we'll know if something to delete in buffer\r
+  ANTLRAbstractToken **p = end_of_buffer-chunk_size+1;\r
+  for (; p<=end_of_buffer; p++) *p = NULL;\r
+*/\r
+}\r
+\r
+ANTLRParser * ANTLRTokenBuffer::        // MR1\r
+setParser(ANTLRParser *p) {          // MR1\r
+  ANTLRParser  *old=parser;          // MR1\r
+  parser=p;              // MR1\r
+  input->setParser(p);            // MR1\r
+  return old;              // MR1\r
+}                // MR1\r
+                // MR1\r
+ANTLRParser * ANTLRTokenBuffer::        // MR1\r
+getParser() {              // MR1\r
+  return parser;            // MR1\r
+}                // MR1\r
+\r
+/* to avoid having to link in another file just for the smart token ptr\r
+ * stuff, we include it here.  Ugh.\r
+ */\r
+#include ATOKPTR_C\r