Modules | Files | Inheritance Tree | Inheritance Graph | Name Index | Config
File: Synopsis/Parser/C++/occ/ptree-core.h
    1| /*
    2|   Copyright (C) 1997-2000 Shigeru Chiba, University of Tsukuba.
    3| 
    4|   Permission to use, copy, distribute and modify this software and   
    5|   its documentation for any purpose is hereby granted without fee,        
    6|   provided that the above copyright notice appear in all copies and that 
    7|   both that copyright notice and this permission notice appear in 
    8|   supporting documentation.
    9| 
   10|   Shigeru Chiba makes no representations about the suitability of this 
   11|   software for any purpose.  It is provided "as is" without express or
   12|   implied warranty.
   13| */
   14| 
   15| #ifndef _ptree_core_h
   16| #define _ptree_core_h
   17| 
   18| #include <iosfwd>
   19| #include "types.h"
   20| 
   21| #define NIL     ((Ptre
   22| 
   23| class ProgramString;
   24| class Walker;
   25| class TypeInfo;
   26| 
   27| #ifdef __opencxx
   28| metaclass QuoteClass Ptree;           // get qMake() available
   29| #endif
   30| 
   31| class OCXXMOP Ptree : public LightObject {
   32| public:
   33|     virtual bool IsLeaf() = nil;
   34|     bool Eq(char);
   35|     bool Eq(char*);
   36|     bool Eq(char*, int);
   37|     bool Eq(Ptree* p) { return Eq(thisp); }
   38| 
   39|     void Display();
   40|     void Display2(std::ostream&);
   41|     virtual void Print(std::ostream&, intint) = nil;
   42|     int Write(std::ostream&);
   43|     virtual int Write(std::ostream&, int) = nil;
   44|     void PrintIndent(std::ostream&, int);
   45| 
   46|     charToString();
   47|     virtual void WritePS(ProgramString&) = nil;
   48| 
   49|     charGetPosition() { return data.leaf.position; }
   50|     int GetLength() { return data.leaf.length; }
   51| 
   52|     PtreeCar() { return data.nonleaf.child; }
   53|     PtreeCdr() { return data.nonleaf.next; }
   54|     PtreeCadr() { return Cdr()->Car(); }
   55|     PtreeCddr() { return Cdr()->Cdr(); }
   56|     PtreeCa_ar();
   57|     void SetCar(Ptree* p) { data.nonleaf.child = p; }
   58|     void SetCdr(Ptree* p) { data.nonleaf.next = p; }
   59| 
   60|     charLeftMost();
   61|     charRightMost();
   62| 
   63|     virtual int What();
   64|     bool IsA(int);
   65|     bool IsA(intint);
   66|     bool IsA(intintint);
   67| 
   68|     virtual PtreeTranslate(Walker*);
   69|     virtual void Typeof(Walker*, TypeInfo&);
   70| 
   71|     virtual charGetEncodedType();
   72|     virtual charGetEncodedName();
   73| 
   74|     PtreeLast() { return Last(this); }
   75|     PtreeFirst() { return First(this); }
   76|     PtreeRest() { return Rest(this); }
   77|     PtreeSecond() { return Second(this); }
   78|     PtreeThird() { return Third(this); }
   79|     PtreeNth(int n) { return Nth(thisn); }
   80|     int Length() { return Length(this); }
   81|     PtreeListTail(int n) { return ListTail(thisn); }
   82| 
   83| // static members
   84| 
   85|     static bool Eq(Ptree*, char);
   86|     static bool Eq(Ptree*, char*);
   87|     static bool Eq(Ptree*, char*, int);
   88|     static bool Eq(Ptree*, Ptree*);
   89|     static bool Equiv(Ptree*, Ptree*);
   90|     static bool Equal(Ptree*, Ptree*);
   91|     static PtreeLast(Ptree*);
   92|     static PtreeFirst(Ptree*);
   93|     static PtreeRest(Ptree*);
   94|     static PtreeSecond(Ptree*);
   95|     static PtreeThird(Ptree*);
   96|     static PtreeNth(Ptree*, int);
   97|     static int Length(Ptree*);
   98|     static PtreeListTail(Ptree*, int);
   99| 
  100|     static PtreeCons(Ptree*, Ptree*);
  101|     static PtreeList();
  102|     static PtreeList(Ptree*);
  103|     static PtreeList(Ptree*, Ptree*);
  104|     static PtreeList(Ptree*, Ptree*, Ptree*);
  105|     static PtreeList(Ptree*, Ptree*, Ptree*, Ptree*);
  106|     static PtreeList(Ptree*, Ptree*, Ptree*, Ptree*, Ptree*);
  107|     static PtreeList(Ptree*, Ptree*, Ptree*, Ptree*, Ptree*, Ptree*);
  108|     static PtreeList(Ptree*, Ptree*, Ptree*, Ptree*, Ptree*, Ptree*,
  109|                Ptree*);
  110|     static PtreeList(Ptree*, Ptree*, Ptree*, Ptree*, Ptree*, Ptree*,
  111|                       Ptree*, Ptree*);
  112|     static PtreeCopyList(Ptree*);
  113|     static PtreeAppend(Ptree*, Ptree*);
  114|     static PtreeReplaceAll(Ptree*, Ptree*, Ptree*);
  115|     static PtreeSubst(Ptree*, Ptree*, Ptree*);
  116|     static PtreeSubst(Ptree*, Ptree*, Ptree*, Ptree*, Ptree*);
  117|     static PtreeSubst(Ptree*, Ptree*, Ptree*, Ptree*,
  118|                       Ptree*, Ptree*, Ptree*);
  119|     static PtreeShallowSubst(Ptree*, Ptree*, Ptree*);
  120|     static PtreeShallowSubst(Ptree*, Ptree*, Ptree*, Ptree*, Ptree*);
  121|     static PtreeShallowSubst(Ptree*, Ptree*, Ptree*, Ptree*,
  122|                              Ptree*, Ptree*, Ptree*);
  123|     static PtreeShallowSubst(Ptree*, Ptree*, Ptree*, Ptree*,
  124|                              Ptree*, Ptree*, Ptree*, Ptree*, Ptree*);
  125|     static PtreeSubstSublist(Ptree*, Ptree*, Ptree*);
  126| 
  127|     /* they cause side-effect */
  128|     static PtreeSnoc(Ptree*, Ptree*);
  129|     static PtreeNconc(Ptree*, Ptree*);
  130|     static PtreeNconc(Ptree*, Ptree*, Ptree*);
  131| 
  132| // in pattern.cc
  133| public:
  134|     static bool Match(Ptree*, char*, ...);
  135|     static PtreeMake(const char* pat, ...);
  136|     static PtreeMakeStatement(const char* pat, ...);
  137|     static PtreeGenSym();
  138| 
  139|     static PtreeqMake(char*);
  140|     static PtreeqMakeStatement(char*);
  141| 
  142|     bool Reify(unsigned int&);
  143|     bool Reify(char*&);
  144| 
  145|     static charIntegerToString(sintint&);   // not documen
  146| 
  147| private:
  148|     static charMatchPat(Ptree*, char*);
  149|     static charMatchList(Ptree*, char*);
  150|     static charMatchWord(Ptree*, char*);
  151| 
  152| public:
  153|     // if this is TRUE, Print() shows an encoded type and name.
  154|     static bool show_encoded;
  155| 
  156| protected:
  157|     union {
  158|         struct {
  159|             Ptreechild;
  160|             Ptreenext;
  161|         nonleaf;
  162|         struct {
  163|             charposition;
  164|             int  length;
  165|         leaf;
  166|     }data;
  167| 
  168|     friend class NonLeaf;
  169| };
  170| 
  171| inline std::ostreamoperator << (std::ostream& s, Ptree* p)
  172| {
  173|     p->Write(s);
  174|     return s;
  175| }
  176| 
  177| // in ptree.cc
  178| 
  179| class OCXXMOP PtreeIter : public LightObject {
  180| public:
  181|     PtreeIter(Ptree* p) { ptree = p; }
  182|     Ptreeoperator () () { return Pop(); }
  183|     PtreePop();
  184|     bool Next(Ptree*&);
  185|     void Reset(Ptree* p) { ptree = p; }
  186| 
  187|     Ptreeoperator * () { return This(); }
  188|     Ptreeoperator ++ () { Pop(); return This(); }
  189|     Ptreeoperator ++ (int) { return Pop(); }  // postfix
  190|     PtreeThis();
  191|     bool Empty() { return bool(ptree == nil); }
  192| 
  193| private:
  194|     Ptreeptree;
  195| };
  196| 
  197| class OCXXMOP PtreeArray : public LightObject {
  198| public:
  199|     PtreeArray(int = 8);
  200|     uint Number() { return num; }
  201|     Ptree*& operator [] (uint index) { return Ref(index); }
  202|     Ptree*& Ref(uint index);
  203|     void Append(Ptree*);
  204|     void Clear() { num = 0; }
  205|     PtreeAll();
  206| 
  207| private:
  208|     uint numsize;
  209|     Ptree** array;
  210|     Ptreedefault_buf[8];
  211| };
  212| 
  213| // PtreeHead is used to implement Ptree::qMake()
  214| // The implementation is in pattern.cc
  215| 
  216| class OCXXMOP PtreeHead : public LightObject {
  217| public:
  218|     PtreeHead() { ptree = nil; }
  219|     operator Ptree* () { return ptree; }
  220|     PtreeHeadoperator + (Ptree*);
  221|     PtreeHeadoperator + (const char*);
  222|     PtreeHeadoperator + (char*);
  223|     PtreeHeadoperator + (char);
  224|     PtreeHeadoperator + (int);
  225| 
  226| private:
  227|     PtreeAppend(Ptree*, Ptree*);
  228|     PtreeAppend(Ptree*, char*, int);
  229| 
  230| private:
  231|     Ptreeptree;
  232| };
  233| 
  234| // error messages
  235| 
  236| void OCXXMOP MopErrorMessage(char* method_name, char* msg);
  237| void OCXXMOP MopErrorMessage2(char* msg1, char* msg2);
  238| void OCXXMOP MopWarningMessage(char* method_name, char* msg);
  239| void OCXXMOP MopWarningMessage2(char* msg1, char* msg2);
  240| void OCXXMOP MopMoreWarningMessage(char* msg1, char* msg2 = nil);
  241| 
  242| #endif  /* _ptree_core_h */