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(this, p); }
   38| 
   39|     void Display();
   40|     void Display2(std::ostream&);
   41|     virtual void Print(std::ostream&, int, int) = nil;
   42|     int Write(std::ostream&);
   43|     virtual int Write(std::ostream&, int) = nil;
   44|     void PrintIndent(std::ostream&, int);
   45| 
   46|     char* ToString();
   47|     virtual void WritePS(ProgramString&) = nil;
   48| 
   49|     char* GetPosition() { return data.leaf.position; }
   50|     int GetLength() { return data.leaf.length; }
   51| 
   52|     Ptree* Car() { return data.nonleaf.child; }
   53|     Ptree* Cdr() { return data.nonleaf.next; }
   54|     Ptree* Cadr() { return Cdr()->Car(); }
   55|     Ptree* Cddr() { return Cdr()->Cdr(); }
   56|     Ptree* Ca_ar();
   57|     void SetCar(Ptree* p) { data.nonleaf.child = p; }
   58|     void SetCdr(Ptree* p) { data.nonleaf.next = p; }
   59| 
   60|     char* LeftMost();
   61|     char* RightMost();
   62| 
   63|     virtual int What();
   64|     bool IsA(int);
   65|     bool IsA(int, int);
   66|     bool IsA(int, int, int);
   67| 
   68|     virtual Ptree* Translate(Walker*);
   69|     virtual void Typeof(Walker*, TypeInfo&);
   70| 
   71|     virtual char* GetEncodedType();
   72|     virtual char* GetEncodedName();
   73| 
   74|     Ptree* Last() { return Last(this); }
   75|     Ptree* First() { return First(this); }
   76|     Ptree* Rest() { return Rest(this); }
   77|     Ptree* Second() { return Second(this); }
   78|     Ptree* Third() { return Third(this); }
   79|     Ptree* Nth(int n) { return Nth(this, n); }
   80|     int Length() { return Length(this); }
   81|     Ptree* ListTail(int n) { return ListTail(this, n); }
   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 Ptree* Last(Ptree*);
   92|     static Ptree* First(Ptree*);
   93|     static Ptree* Rest(Ptree*);
   94|     static Ptree* Second(Ptree*);
   95|     static Ptree* Third(Ptree*);
   96|     static Ptree* Nth(Ptree*, int);
   97|     static int Length(Ptree*);
   98|     static Ptree* ListTail(Ptree*, int);
   99| 
  100|     static Ptree* Cons(Ptree*, Ptree*);
  101|     static Ptree* List();
  102|     static Ptree* List(Ptree*);
  103|     static Ptree* List(Ptree*, Ptree*);
  104|     static Ptree* List(Ptree*, Ptree*, Ptree*);
  105|     static Ptree* List(Ptree*, Ptree*, Ptree*, Ptree*);
  106|     static Ptree* List(Ptree*, Ptree*, Ptree*, Ptree*, Ptree*);
  107|     static Ptree* List(Ptree*, Ptree*, Ptree*, Ptree*, Ptree*, Ptree*);
  108|     static Ptree* List(Ptree*, Ptree*, Ptree*, Ptree*, Ptree*, Ptree*,
  109|                Ptree*);
  110|     static Ptree* List(Ptree*, Ptree*, Ptree*, Ptree*, Ptree*, Ptree*,
  111|                       Ptree*, Ptree*);
  112|     static Ptree* CopyList(Ptree*);
  113|     static Ptree* Append(Ptree*, Ptree*);
  114|     static Ptree* ReplaceAll(Ptree*, Ptree*, Ptree*);
  115|     static Ptree* Subst(Ptree*, Ptree*, Ptree*);
  116|     static Ptree* Subst(Ptree*, Ptree*, Ptree*, Ptree*, Ptree*);
  117|     static Ptree* Subst(Ptree*, Ptree*, Ptree*, Ptree*,
  118|                       Ptree*, Ptree*, Ptree*);
  119|     static Ptree* ShallowSubst(Ptree*, Ptree*, Ptree*);
  120|     static Ptree* ShallowSubst(Ptree*, Ptree*, Ptree*, Ptree*, Ptree*);
  121|     static Ptree* ShallowSubst(Ptree*, Ptree*, Ptree*, Ptree*,
  122|                              Ptree*, Ptree*, Ptree*);
  123|     static Ptree* ShallowSubst(Ptree*, Ptree*, Ptree*, Ptree*,
  124|                              Ptree*, Ptree*, Ptree*, Ptree*, Ptree*);
  125|     static Ptree* SubstSublist(Ptree*, Ptree*, Ptree*);
  126| 
  127|     /* they cause side-effect */
  128|     static Ptree* Snoc(Ptree*, Ptree*);
  129|     static Ptree* Nconc(Ptree*, Ptree*);
  130|     static Ptree* Nconc(Ptree*, Ptree*, Ptree*);
  131| 
  132| // in pattern.cc
  133| public:
  134|     static bool Match(Ptree*, char*, ...);
  135|     static Ptree* Make(const char* pat, ...);
  136|     static Ptree* MakeStatement(const char* pat, ...);
  137|     static Ptree* GenSym();
  138| 
  139|     static Ptree* qMake(char*);
  140|     static Ptree* qMakeStatement(char*);
  141| 
  142|     bool Reify(unsigned int&);
  143|     bool Reify(char*&);
  144| 
  145|     static char* IntegerToString(sint, int&);   // not documen
  146| 
  147| private:
  148|     static char* MatchPat(Ptree*, char*);
  149|     static char* MatchList(Ptree*, char*);
  150|     static char* MatchWord(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|             Ptree* child;
  160|             Ptree* next;
  161|         } nonleaf;
  162|         struct {
  163|             char* position;
  164|             int  length;
  165|         } leaf;
  166|     }data;
  167| 
  168|     friend class NonLeaf;
  169| };
  170| 
  171| inline std::ostream& operator << (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|     Ptree* operator () () { return Pop(); }
  183|     Ptree* Pop();
  184|     bool Next(Ptree*&);
  185|     void Reset(Ptree* p) { ptree = p; }
  186| 
  187|     Ptree* operator * () { return This(); }
  188|     Ptree* operator ++ () { Pop(); return This(); }
  189|     Ptree* operator ++ (int) { return Pop(); }  // postfix
  190|     Ptree* This();
  191|     bool Empty() { return bool(ptree == nil); }
  192| 
  193| private:
  194|     Ptree* ptree;
  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|     Ptree* All();
  206| 
  207| private:
  208|     uint num, size;
  209|     Ptree** array;
  210|     Ptree* default_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|     PtreeHead& operator + (Ptree*);
  221|     PtreeHead& operator + (const char*);
  222|     PtreeHead& operator + (char*);
  223|     PtreeHead& operator + (char);
  224|     PtreeHead& operator + (int);
  225| 
  226| private:
  227|     Ptree* Append(Ptree*, Ptree*);
  228|     Ptree* Append(Ptree*, char*, int);
  229| 
  230| private:
  231|     Ptree* ptree;
  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 */