#ifndef TOK_H #define TOK_H /////////////////////////////////////////////////////////////// // tok.h - read words from ASCII file // // ver 1.2 // // 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.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(); 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); }; inline unsigned int token::lines() { return _lines; } inline unsigned int token::scopeLevel() { return _scopeLevel; } #endif