#ifndef TOK_H #define TOK_H /////////////////////////////////////////////////////////////// // tok.h - read words from ASCII file // // ver 1.6 // // Language: Visual C, ver 6.0 // // Platform: Micron Dual Pentium Pro 200, Win NT 4.0 // // Application: CSE784 Example // // Author: Jim Fawcett, CST 2-187, Syracuse Univ. // // (315) 443-3948, fawcett@ecs.syr.edu // /////////////////////////////////////////////////////////////// /* Module Operations ================= This tok class provides one public function which extracts words from an existing file stream. A word is a sequence of characters delimited by whitespace. punctuator sequences are separated from alphanumberic characters; and certain punctuator characters are collected as single character tokens, e.g.: '{', '}', '(', ')', '[', ']', ';' The tokenizer is used in the following way: ifstream in(filename); token toker(in); string tok; while(getTok(tok)) { // do things with collected tokens } int nl = toker.lines(); // number of '\n' chars int sl = toker.scopeLevel(); // net number of '{' and '}' chars All newlines, '\n', are counted. Braces, '{' and '}', are counted only if not in comments or quoted strings. */ // /////////////////////////////////////////////////////////////// // build process // /////////////////////////////////////////////////////////////// // // // Required Files: // // tok.h, tok.c // // // // Compile Command: // // cl -GX -DTEST_TOK tok.cpp // // // /////////////////////////////////////////////////////////////// /* Maintenance History =================== ver 1.6 : 15 Jan 2002 - added token::good() to public interface ver 1.5 : 30 Sep 2001 - cosmetic changes to this header file and test stub ver 1.4 : 21 Apr 2000 - tok now announces end of file while in comment - added option to terminate comment on * and / event if nested (like VC++ does by default) ver 1.3 : 10 Dec 1999 - changed charType(ch) to return underscore as a char instead of a punctuator ver 1.2 : 11 Sep 1999 - added counts for lines and braces ver 1.1 : 26 Apr 1999 - fixed bug in test stub, e.g., added in.close() and in.clear() at end of file loop ver 1.0 : 20 Sep 1998 - first release */ // /////////////////////////////////////////////////////////////// // declarations // /////////////////////////////////////////////////////////////// #include #include using namespace std; class token { public: token(istream &input); bool getTok(string &buffer); unsigned int lines(); unsigned int scopeLevel(); void nestedComments(bool pred); bool good(); private: istream ∈ // source of tokens unsigned int _lines; // number of newlines unsigned int _scopeLevel; // net number of open/closed braces enum charType { Char, NewLine, White, Punct }; charType type(char ch); int delim(char ch); int isComment(const string &buffer); int isWhite(const string &buffer); int eatWhite(string &buffer); int eatComment(string &buffer); int isQuote(const string &buffer); int collectQuote(string &buffer); bool _nestedComments; }; inline unsigned int token::lines() { return _lines; } inline unsigned int token::scopeLevel() { return _scopeLevel; } inline void token::nestedComments(bool pred) { _nestedComments = pred; } inline bool token::good() { return in.good(); } #endif