SNAP Library 2.0, User Reference  2013-05-13 16:33:57
SNAP, a general purpose, high performance system for analysis and manipulation of large networks
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
lx.h
Go to the documentation of this file.
00001 #include "bd.h"
00002 
00004 // Lexical-Char-Definition
00005 typedef enum {lctUndef, lctSpace, lctNum, lctAlpha, lctSSym, lctTerm} TLxChTy;
00006 typedef enum {lcdtUsAscii, lcdtYuAscii} TLxChDefTy;
00007 
00008 ClassTP(TLxChDef, PLxChDef)//{
00009 private:
00010   TIntV ChTyV;
00011   TChV UcChV;
00012   void SetUcCh(const TStr& Str);
00013   void SetChTy(const TLxChTy& ChTy, const TStr& Str);
00014 public:
00015   TLxChDef(const TLxChDefTy& ChDefTy);
00016   static PLxChDef New(const TLxChDefTy& ChDefTy=lcdtUsAscii){
00017     return PLxChDef(new TLxChDef(ChDefTy));}
00018   TLxChDef(TSIn& SIn): ChTyV(SIn), UcChV(SIn){}
00019   static PLxChDef Load(TSIn& SIn){return new TLxChDef(SIn);}
00020   void Save(TSOut& SOut){ChTyV.Save(SOut); UcChV.Save(SOut);}
00021 
00022   TLxChDef& operator=(const TLxChDef& ChDef){
00023     ChTyV=ChDef.ChTyV; UcChV=ChDef.UcChV; return *this;}
00024 
00025   int GetChTy(const char& Ch) const {return ChTyV[Ch-TCh::Mn];}
00026   bool IsTerm(const char& Ch) const {return ChTyV[Ch-TCh::Mn]==TInt(lctTerm);}
00027   bool IsSpace(const char& Ch) const {return ChTyV[Ch-TCh::Mn]==TInt(lctSpace);}
00028   bool IsAlpha(const char& Ch) const {return ChTyV[Ch-TCh::Mn]==TInt(lctAlpha);}
00029   bool IsNum(const char& Ch) const {return ChTyV[Ch-TCh::Mn]==TInt(lctNum);}
00030   bool IsAlNum(const char& Ch) const {
00031     return (ChTyV[Ch-TCh::Mn]==TInt(lctAlpha))||(ChTyV[Ch-TCh::Mn]==TInt(lctNum));}
00032   char GetUc(const char& Ch) const {return UcChV[Ch-TCh::Mn];}
00033 
00034   bool IsNmStr(const TStr& Str) const;
00035   TStr GetUcStr(const TStr& Str) const;
00036 
00037   // standard entry points
00038   static PLxChDef GetChDef(const TLxChDefTy& ChDefTy=lcdtUsAscii);
00039 //  static TLxChDef& GetChDefRef(const TLxChDefTy& ChDefTy=lcdtUsAscii);
00040 };
00041 
00043 // Lexical-Symbols
00044 typedef enum {
00045   syUndef, syLn, syTab, syBool, syInt, syFlt, syStr, syIdStr, syQStr,
00046   syPeriod, syDPeriod, syComma, syColon, syDColon, sySemicolon,
00047   syPlus, syMinus, syAsterisk, sySlash, syPercent,
00048   syExclamation, syVBar, syAmpersand, syQuestion, syHash,
00049   syEq, syNEq, syLss, syGtr, syLEq, syGEq,
00050   syLParen, syRParen, syLBracket, syRBracket, syLBrace, syRBrace,
00051   syEoln, syEof,
00052   syMnRw, syRw1, syRw2, syRw3, syRw4, syRw5, syRw6, syRw7, syRw8, syRw9,
00053   syRw10, syRw11, syRw12, syRw13, syRw14, syRw15, syRw16, syRw17, syMxRw
00054 } TLxSym;
00055 
00056 class TLxSymStr{
00057 public:
00058   static const TStr UndefStr;
00059   static const TStr LnStr;
00060   static const TStr TabStr;
00061   static const TStr BoolStr;
00062   static const TStr IntStr;
00063   static const TStr FltStr;
00064   static const TStr StrStr;
00065   static const TStr IdStrStr;
00066   static const TStr QStrStr;
00067   static const TStr PeriodStr;
00068   static const TStr DPeriodStr;
00069   static const TStr CommaStr;
00070   static const TStr ColonStr;
00071   static const TStr DColonStr;
00072   static const TStr SemicolonStr;
00073   static const TStr PlusStr;
00074   static const TStr MinusStr;
00075   static const TStr AsteriskStr;
00076   static const TStr SlashStr;
00077   static const TStr PercentStr;
00078   static const TStr ExclamationStr;
00079   static const TStr VBarStr;
00080   static const TStr AmpersandStr;
00081   static const TStr QuestionStr;
00082   static const TStr HashStr;
00083   static const TStr EqStr;
00084   static const TStr NEqStr;
00085   static const TStr LssStr;
00086   static const TStr GtrStr;
00087   static const TStr LEqStr;
00088   static const TStr GEqStr;
00089   static const TStr LParenStr;
00090   static const TStr RParenStr;
00091   static const TStr LBracketStr;
00092   static const TStr RBracketStr;
00093   static const TStr LBraceStr;
00094   static const TStr RBraceStr;
00095   static const TStr EolnStr;
00096   static const TStr EofStr;
00097   static TStr GetSymStr(const TLxSym& Sym);
00098   static TLxSym GetSSym(const TStr& Str);
00099 public:
00100   static bool IsSep(const TLxSym& PrevSym, const TLxSym& Sym);
00101 };
00102 
00104 // Lexical-Input-Symbol-State
00105 class TILx;
00106 
00107 class TILxSymSt{
00108 private:
00109   TLxSym Sym;
00110   TStr Str, UcStr, CmtStr;
00111   bool Bool; int Int; double Flt;
00112   int SymLnN, SymLnChN, SymChN;
00113 public:
00114   TILxSymSt();
00115   TILxSymSt(const TILxSymSt& SymSt);
00116   TILxSymSt(TILx& Lx);
00117   TILxSymSt(TSIn&){Fail;}
00118   void Save(TSOut&){Fail;}
00119 
00120   void Restore(TILx& Lx);
00121 };
00122 
00124 // Lexical-Input
00125 typedef enum {
00126   iloCmtAlw, iloRetEoln, iloSigNum, iloUniStr, iloCsSens,
00127   iloExcept, iloTabSep, iloList, iloMx} TILxOpt;
00128 
00129 class TILx{
00130 private:
00131   PLxChDef ChDef;
00132   PSIn SIn;
00133   TSIn& RSIn;
00134   char PrevCh, Ch;
00135   int LnN, LnChN, ChN;
00136   TSStack<TILxSymSt> PrevSymStStack;
00137   TStrIntH RwStrH;
00138   bool IsCmtAlw, IsRetEoln, IsSigNum, IsUniStr, IsCsSens;
00139   bool IsExcept, IsTabSep, IsList;
00140   char GetCh(){
00141     Assert(Ch!=TCh::EofCh);
00142     PrevCh=Ch; LnChN++; ChN++;
00143     Ch=((RSIn.Eof()) ? TCh::EofCh : RSIn.GetCh());
00144     if (IsList){putchar(Ch);}
00145     return Ch;
00146   }
00147   char GetChX(){char Ch=GetChX(); printf("%c", Ch); return Ch;}
00148 public: // symbol state
00149   TLxSym Sym;
00150   TChA Str, UcStr, CmtStr;
00151   bool Bool; int Int; double Flt;
00152   int SymLnN, SymLnChN, SymChN;
00153   bool QuoteP;
00154   char QuoteCh;
00155 public:
00156   TILx(const PSIn& _SIn, const TFSet& OptSet=TFSet(),
00157    const TLxChDefTy& ChDefTy=lcdtUsAscii);
00158 
00159   TILx& operator=(const TILx&){Fail; return *this;}
00160 
00161   void SetOpt(const int& Opt, const bool& Val);
00162   TLxSym AddRw(const TStr& Str);
00163   TLxSym GetRw(const TStr& Str){
00164     return TLxSym(int(RwStrH.GetDat(Str)));}
00165   PSIn GetSIn(const char& SepCh);
00166   int GetLnN() const {return LnN;}
00167   bool IsBof() const {return ChN==-1;}
00168   bool IsEof() const {return Ch==TCh::EofCh;}
00169 
00170   TLxSym GetSym(const TFSet& Expect);
00171   TLxSym GetSym(){return GetSym(TFSet());}
00172   TLxSym GetSym(const TLxSym& Sym){return GetSym(TFSet()|Sym);}
00173   TLxSym GetSym(const TLxSym& Sym1, const TLxSym& Sym2){
00174     return GetSym(TFSet()|Sym1|Sym2);}
00175   TLxSym GetSym(const TLxSym& Sym1, const TLxSym& Sym2, const TLxSym& Sym3){
00176     return GetSym(TFSet()|Sym1|Sym2|Sym3);}
00177   TLxSym GetSym(const TLxSym& Sym1, const TLxSym& Sym2, const TLxSym& Sym3,
00178    const TLxSym& Sym4){
00179     return GetSym(TFSet()|Sym1|Sym2|Sym3|Sym4);}
00180   bool GetBool(){GetSym(TFSet()|syBool); return Bool;}
00181   int GetInt(){GetSym(TFSet()|syInt); return Int;}
00182   double GetFlt(){GetSym(TFSet()|syFlt); return Flt;}
00183   TStr GetStr(const TStr& _Str=TStr()){
00184     GetSym(TFSet()|syStr); IAssert(_Str.Empty()||(_Str==Str)); return Str;}
00185   TStr GetIdStr(const TStr& IdStr=TStr()){
00186     GetSym(TFSet()|syIdStr); IAssert(IdStr.Empty()||(IdStr==Str)); return Str;}
00187   TStr GetQStr(const TStr& QStr=TStr()){
00188     GetSym(TFSet()|syQStr); IAssert(QStr.Empty()||(Str==QStr)); return Str;}
00189   void GetEoln(){GetSym(TFSet()|syEoln);}
00190   TStr GetStrToCh(const char& ToCh);
00191   TStr GetStrToEolnOrCh(const char& ToCh);
00192   TStr GetStrToEoln(const bool& DoTrunc=false);
00193   TStr GetStrToEolnAndCh(const char& ToCh);
00194   void SkipToEoln();
00195   void SkipToSym(const TLxSym& SkipToSym){
00196     while (Sym!=SkipToSym){GetSym();}}
00197 
00198   void PutSym(const TILxSymSt& SymSt){PrevSymStStack.Push(TILxSymSt(SymSt));}
00199   void PutSym(){PrevSymStStack.Push(TILxSymSt(*this));}
00200   TLxSym PeekSym(){TLxSym NextSym=GetSym(); PutSym(); return NextSym;}
00201   TLxSym PeekSym(const int& Syms);
00202 
00203   TStr GetSymStr() const;
00204   TStr GetFPosStr() const;
00205   static TStr GetQStr(const TStr& Str, const bool& QuoteP, const char& QuoteCh);
00206 
00207   bool IsVar(const TStr& VarNm){
00208     GetSym(); bool Var=((Sym==syIdStr)&&(Str==VarNm)); PutSym(); return Var;}
00209   void GetVar(const TStr& VarNm,
00210    const bool& LBracket=false, const bool& NewLn=false){
00211     GetIdStr(VarNm); GetSym(syColon);
00212     if (LBracket){GetSym(syLBracket);} if (NewLn){GetEoln();}}
00213   void GetVarEnd(const bool& RBracket=false, const bool& NewLn=false){
00214     if (RBracket){GetSym(syRBracket);}
00215     if (NewLn){GetEoln();}}
00216   bool PeekVarEnd(const bool& RBracket=false, const bool& NewLn=false){
00217     if (RBracket){return PeekSym()==syRBracket;}
00218     if (NewLn){return PeekSym()==syEoln;} Fail; return false;}
00219   bool GetVarBool(const TStr& VarNm, const bool& NewLn=true){
00220     GetIdStr(VarNm); GetSym(syColon); bool Bool=GetBool();
00221     if (NewLn){GetEoln();} return Bool;}
00222   int GetVarInt(const TStr& VarNm, const bool& NewLn=true){
00223     GetIdStr(VarNm); GetSym(syColon); int Int=GetInt();
00224     if (NewLn){GetEoln();} return Int;}
00225   double GetVarFlt(const TStr& VarNm, const bool& NewLn=true){
00226     GetIdStr(VarNm); GetSym(syColon); double Flt=GetFlt();
00227     if (NewLn){GetEoln();} return Flt;}
00228   TStr GetVarStr(const TStr& VarNm, const bool& NewLn=true){
00229     GetIdStr(VarNm); GetSym(syColon); TStr Str=GetQStr();
00230     if (NewLn){GetEoln();} return Str;}
00231   TSecTm GetVarSecTm(const TStr& VarNm, const bool& NewLn=true){
00232     GetIdStr(VarNm); GetSym(syColon); TSecTm SecTm=TSecTm::LoadTxt(*this);
00233     if (NewLn){GetEoln();} return SecTm;}
00234   void GetVarBoolV(const TStr& VarNm, TBoolV& BoolV, const bool& NewLn=true);
00235   void GetVarIntV(const TStr& VarNm, TIntV& IntV, const bool& NewLn=true);
00236   void GetVarFltV(const TStr& VarNm, TFltV& FltV, const bool& NewLn=true);
00237   void GetVarStrV(const TStr& VarNm, TStrV& StrV, const bool& NewLn=true);
00238   void GetVarStrPrV(const TStr& VarNm, TStrPrV& StrPrV, const bool& NewLn=true);
00239   void GetVarStrVV(const TStr& VarNm, TVec<TStrV>& StrVV, const bool& NewLn=true);
00240 
00241   // file-of-lines
00242   static void GetLnV(const TStr& FNm, TStrV& LnV);
00243 };
00244 
00246 // Lexical-Output
00247 typedef enum {
00248   oloCmtAlw, oloFrcEoln, oloSigNum, oloUniStr,
00249   oloCsSens, oloTabSep, oloVarIndent, oloMx} TOLxOpt;
00250 
00251 class TOLx{
00252 private:
00253   PLxChDef ChDef;
00254   PSOut SOut;
00255   TSOut& RSOut;
00256   bool IsCmtAlw, IsFrcEoln, IsSigNum, IsUniStr;
00257   bool IsCsSens, IsTabSep, IsVarIndent;
00258   int VarIndentLev;
00259   TStrIntH RwStrH;
00260   TIntStrH RwSymH;
00261   TLxSym PrevSym;
00262   void PutSep(const TLxSym& Sym);
00263 public:
00264   TOLx(const PSOut& _SOut, const TFSet& OptSet,
00265    const TLxChDefTy& ChDefTy=lcdtUsAscii);
00266 
00267   TOLx& operator=(const TOLx&){Fail; return *this;}
00268 
00269   void SetOpt(const int& Opt, const bool& Val);
00270   TLxSym AddRw(const TStr& Str);
00271   PSOut GetSOut(const char& SepCh){
00272     RSOut.PutCh(SepCh); return SOut;}
00273 
00274   void PutSym(const TLxSym& Sym);
00275   void PutBool(const TBool& Bool){
00276     PutSep(syIdStr); RSOut.PutStr(TBool::GetStr(Bool));}
00277   void PutInt(const TInt& Int){
00278     if (!IsSigNum){Assert(int(Int)>=0);}
00279     PutSep(syInt); RSOut.PutStr(TInt::GetStr(Int));}
00280   void PutFlt(const TFlt& Flt, const int& Width=-1, const int& Prec=-1){
00281     if (!IsSigNum){Assert(Flt>=0);}
00282     PutSep(syFlt); RSOut.PutStr(TFlt::GetStr(Flt, Width, Prec));}
00283   void PutStr(const TStr& Str){
00284     if ((IsUniStr)&&(ChDef->IsNmStr(Str))){PutSep(syIdStr); RSOut.PutStr(Str);}
00285     else {PutSep(syStr); RSOut.PutCh('"'); RSOut.PutStr(Str); RSOut.PutCh('"');}}
00286   void PutIdStr(const TStr& Str, const bool& CheckIdStr=true){
00287     if (CheckIdStr){Assert(ChDef->IsNmStr(Str));}
00288     PutSep(syIdStr); RSOut.PutStr(Str);}
00289   void PutQStr(const TStr& Str){
00290     PutSep(syQStr); RSOut.PutCh('"'); RSOut.PutStr(Str); RSOut.PutCh('"');}
00291   void PutQStr(const TChA& ChA){
00292     PutSep(syQStr); RSOut.PutCh('"'); RSOut.PutStr(ChA); RSOut.PutCh('"');}
00293   void PutUQStr(const TStr& Str){
00294     PutSep(syIdStr); RSOut.PutStr(Str);}
00295   void PutLnCmt(const TStr& Str, const int& IndentLev=0){
00296     Assert(IsCmtAlw); PutStr(" // "); PutStr(Str); PutLn(IndentLev);}
00297   void PutParCmt(const TStr& Str){
00298     Assert(IsCmtAlw); PutStr(" /* "); PutStr(Str); PutStr(" */ ");}
00299   void PutIndent(const int& IndentLev){
00300     RSOut.PutCh(' ', IndentLev*2);}
00301   void PutTab() const {RSOut.PutCh(TCh::TabCh);}
00302   void PutLn(const int& IndentLev=0){
00303     Assert(IsFrcEoln);
00304     PutSep(syEoln); RSOut.PutLn(); RSOut.PutCh(' ', IndentLev*2);}
00305   void PutDosLn(const int& IndentLev=0){
00306     Assert(IsFrcEoln);
00307     PutSep(syEoln); RSOut.PutDosLn(); RSOut.PutCh(' ', IndentLev*2);}
00308 
00309   void PutVar(const TStr& VarNm, const bool& LBracket=false,
00310    const bool& NewLn=false, const bool& CheckIdStr=true){
00311     if (IsVarIndent){PutIndent(VarIndentLev);}
00312     PutIdStr(VarNm, CheckIdStr); PutSym(syColon);
00313     if (LBracket){PutSym(syLBracket);}
00314     if (NewLn){PutLn(); VarIndentLev++;}}
00315   void PutVarEnd(const bool& RBracket=false, const bool& NewLn=false){
00316     if (IsVarIndent){PutIndent(VarIndentLev-1);}
00317     if (RBracket){PutSym(syRBracket);}
00318     if (NewLn){PutLn(); VarIndentLev--;}}
00319   void PutVarBool(const TStr& VarNm, const bool& Bool,
00320    const bool& NewLn=true, const bool& CheckIdStr=true){
00321     if (IsVarIndent){PutIndent(VarIndentLev);}
00322     PutIdStr(VarNm, CheckIdStr); PutSym(syColon); PutBool(Bool);
00323     if (NewLn){PutLn();}}
00324   void PutVarInt(const TStr& VarNm, const int& Int,
00325    const bool& NewLn=true, const bool& CheckIdStr=true){
00326     if (IsVarIndent){PutIndent(VarIndentLev);}
00327     PutIdStr(VarNm, CheckIdStr); PutSym(syColon); PutInt(Int);
00328     if (NewLn){PutLn();}}
00329   void PutVarFlt(const TStr& VarNm, const double& Flt,
00330    const bool& NewLn=true, const bool& CheckIdStr=true){
00331     if (IsVarIndent){PutIndent(VarIndentLev);}
00332     PutIdStr(VarNm, CheckIdStr); PutSym(syColon); PutFlt(Flt);
00333     if (NewLn){PutLn();}}
00334   void PutVarStr(const TStr& VarNm, const TStr& Str,
00335    const bool& NewLn=true, const bool& CheckIdStr=true){
00336     if (IsVarIndent){PutIndent(VarIndentLev);}
00337     PutIdStr(VarNm, CheckIdStr); PutSym(syColon); PutQStr(Str);
00338     if (NewLn){PutLn();}}
00339   void PutVarSecTm(const TStr& VarNm, const TSecTm& SecTm,
00340    const bool& NewLn=true, const bool& CheckIdStr=true){
00341     if (IsVarIndent){PutIndent(VarIndentLev);}
00342     PutIdStr(VarNm, CheckIdStr); PutSym(syColon); SecTm.SaveTxt(*this);
00343     if (NewLn){PutLn();}}
00344   void PutVarBoolV(const TStr& VarNm, const TBoolV& BoolV,
00345    const bool& NewLn=true, const bool& CheckIdStr=true);
00346   void PutVarIntV(const TStr& VarNm, const TIntV& IntV,
00347    const bool& NewLn=true, const bool& CheckIdStr=true);
00348   void PutVarFltV(const TStr& VarNm, const TFltV& FltV,
00349    const bool& NewLn=true, const bool& CheckIdStr=true);
00350   void PutVarStrV(const TStr& VarNm, const TStrV& StrV,
00351    const bool& NewLn=true, const bool& CheckIdStr=true);
00352   void PutVarStrPrV(const TStr& VarNm, const TStrPrV& StrPrV,
00353    const bool& NewLn=true, const bool& CheckIdStr=true);
00354   void PutVarStrVV(const TStr& VarNm, const TVec<TStrV>& StrVV,
00355    const bool& NewLn=true, const bool& CheckIdStr=true);
00356 };
00357 
00359 // Preprocessor
00360 class TPreproc{
00361 private:
00362   PSIn SIn;
00363   TStrV SubstKeyIdV;
00364   char PrevCh, Ch;
00365   THash<TStr, TStrPrV> SubstIdToKeyIdValPrVH;
00366   char GetCh();
00367   bool IsSubstId(const TStr& SubstId, TStr& SubstValStr) const;
00368   UndefDefaultCopyAssign(TPreproc);
00369 public:
00370   TPreproc(const TStr& InFNm, const TStr& OutFNm,
00371    const TStr& SubstFNm, const TStrV& _SubstKeyIdV);
00372 
00373   static void Execute(const TStr& InFNm, const TStr& OutFNm,
00374    const TStr& InSubstFNm, const TStrV& SubstKeyIdV){
00375     TPreproc Preproc(InFNm, OutFNm, InSubstFNm, SubstKeyIdV);}
00376 };
00377 
00378 /* Sample Subst-File
00379 <SubstList>
00380 
00381 <Subst Id="TId">
00382   <Str Key="MSSQL">TId</Str>
00383   <Str Key="Oracle">NUMBER(15) NOT NULL</Str>
00384 </Subst>
00385 
00386 <Subst Id="TStr2NN">
00387   <Str Key="MSSQL">TStr2NN</Str>
00388   <Str Key="Oracle">VARCHAR2(2) NOT NULL</Str>
00389 </Subst>
00390 
00391 </SubstList>
00392 */