#ifndef CPPSEMIEXP_H // compile if CPPSEMIEXP_H is not in symbol table #define CPPSEMIEXP_H // put CPPSEMIEXP_H in symbol table ///////////////////////////////////////////////////////////////////////// // CppSemiExp.h - declares semi-expression which collects tokens // // ver 1.0 from input stream via a CToker instance // // // // Language: Visual C++, ver 7.0 // // Platform: Dell Dimension 8300, Windows XP Prof, SP 1 // // Application: CSE687 prototype for project #1, Spring 2004 // // Author: Jim Fawcett, CST 2-187, Syracuse University // // (315) 443-3948, jfawcett@twcny.rr.com // ///////////////////////////////////////////////////////////////////////// /* * Module Operations: * ================== * CppSemiExp.h Declares a class CSemiExp designed to collect tokens from * a std stream and assemble them into meaningful sequences. Semi-expressions. * end in single character tokens "{", "}", ";", or "\n" following a #. * * A semi-expression contains just the right amount of code for analysis * without requiring a lot of state management. * * Public Interface: * ================= * std::fstream in(someFile); * CToker toker(&in); * CSemiExp semi(&toker); // create CSemiExp instance * while(semi.get()) // extract next semi-expression * // do something with semi * int n = semi.length(); // read number of tokens * std::string tok = semi[2]; // read 3rd token's value * semi[1] = "test"; // change 2nd token's value * semi.show(); // display on cout * semi.clear(); // remove all tokens * */ /* * Maintenance History: * ==================== * ver 1.0 : 12 Jan 04 * - first release * * Build Process: * ============== * Required Files: * CppSemiExp.h, CppSemiExp.cpp, CppToker.h, CppToker.cpp * * Compiler Command: * cl /DTest_CPPSEMIEXP /EHsc CppSemiExp.cpp CppToker.cpp; */ // #include "CppToker.h" #include #include namespace Project1 { /////////////////////////////////////////////////////////////////////// // family of token filters class ITokenFilter { public: ITokenFilter(CToker *pToker) : _pToker(pToker) {} virtual bool present()=0; virtual bool extract(std::string &token)=0; protected: CToker *_pToker; }; class CCppCommentFilter : public ITokenFilter { public: CCppCommentFilter(CToker *pToker) : ITokenFilter(pToker) {} virtual bool present(); virtual bool extract(std::string &token); }; class CCCommentFilter : public ITokenFilter { public: CCCommentFilter(CToker *pToker) : ITokenFilter(pToker) {} virtual bool present(); virtual bool extract(std::string &token); }; class CDoubleQuoteFilter : public ITokenFilter { public: CDoubleQuoteFilter(CToker *pToker) : ITokenFilter(pToker) {} virtual bool present(); virtual bool extract(std::string &token); }; class CSingleQuoteFilter : public ITokenFilter { public: CSingleQuoteFilter(CToker *pToker) : ITokenFilter(pToker) {} virtual bool present(); virtual bool extract(std::string &token); }; // /////////////////////////////////////////////////////////////////////// // class representing semi-expressions class CSemiExp { public: CSemiExp(CToker *pToker=0); // constructor with default argument ~CSemiExp(); ////////////////////////////////////////////////////////// // Compiler generated copy constructor and assignment // operator work, so they are not provided here. bool get(); size_t length() const; std::string& operator[](unsigned int n); std::string operator[](unsigned int n) const; void insert(unsigned int i, const std::string &token); void push_back(const std::string &token); void erase(unsigned int i); void clear(); void show() const; void registerFilter(ITokenFilter *pITokenFilter); void registerDefaultFilters(); private: bool isTerminator(const std::string &tok) const; bool filter(std::string &token); void checkIndex(unsigned int); CToker *_pToker; std::vector _tok; std::list _filter; }; } #endif