#ifndef CPPTOKER_H // compile if CPPTOKER_H is not in symbol table #define CPPTOKER_H // put CPPTOKER_H in symbol table ///////////////////////////////////////////////////////////////////////// // CppToker.h - declares tokenizer which extracts words from a stream // // ver 1.0 // // 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: * ================== * CppToker.h Declares a class CToker designed to extract words from a * std stream. Each word is either an identifier or punctuator token. * Identifiers consist of alphabetic characters, digits, and underscores. * Punctuators are composed of all other non-whitespace characters. A * specific set of punctuation characters are designated as single character * tokens. All other non-identifiers consist of multiple punctuators. * * Public Interface: * ================= * std::fstream in(someFile); * CToker toker(&in); // create toker * std::string tok; * while((tok = toker.getTok()) != "") // read tokens * // do something with tok * std::istream *pStr = toker.getStream(); // get pointer to stream * std::istream *pOld = toker.setStream(pNew); // give toker new stream * std::string curr = toker.curr(); // get last token extracted * std::string prev = toker.prev(); // get one before that */ /* * Maintenance History: * ==================== * ver 1.0 : 12 Jan 04 * - first release * * Build Process: * ============== * Required Files: * CppToker.h, CppToker.cpp * * Compiler Command: * cl /DTest_CPPTOKER /EHsc CppToker.cpp; */ // /* * Design Notes: * ============= * This module illustrates many things we will be covering this semester, some * rather unusual things, for purposes of illustration: * - the type std::basic_string appears in this header file. You will * usually see the type std::string in most C++ code, which is equivalent. * This syntax makes it clear that the standard string is a template-based * type, using the 8 bit Char type as part of its definition. We will be * saying much more about this later in the course. * - The CToker class has a private helper class, CTokBuffer, and declares a * reference to that type. This prevents CToker class from participating * in copy or assignment operations. That is consistent with my intended * abstraction for this class. * - I wanted to keep CTokBuffer out of the abstraction of the CToker class * For that reason its declaration is in the implementation file, * CppToker.cpp, instead of in this header file. A consequence of that is * that the CToker class cannot hold an object of CTokBuffer, because its * size is not known to the compiler yet. It could hold either a pointer * or a reference to its CTokBuffer instance. I chose to use a reference * so that I could use object semantics rather than pointer semantics in * the CToker member function implementations. */ #include #include #include #include namespace Project1 { /////////////////////////////////////////////////////////////////////// // class CToker - extracts tokens from a std stream // - uses a private CTokBuffer class to help build // tokens from stream class CToker { public: CToker(std::basic_istream *pStream=0); ~CToker(void); std::istream* setStream(std::basic_istream *pStream); std::basic_string getTok(); std::istream* getStream(); std::basic_string prev(); std::basic_string curr(); private: std::istream *_pStream; // forward reference to helper class class CTokBuffer; // reference type below will prevent CToker objects // being copied or assigned CTokBuffer &tokBuffer; std::basic_string prevTok; std::basic_string currTok; }; } #endif