/* * File: scanner.h * --------------- * This file is the interface to a package that divides * a line into individual "tokens". A token is defined * to be either * * 1. a string of consecutive letters and digits representing * a word, or * * 2. a one-character string representing a separator * character, such as a space or a punctuation mark. * * To use this package, you must first call * * InitScanner(line); * * where line is the string (typically a line returned by * GetLine) that is to be divided into tokens. To retrieve * each token in turn, you call * * token = GetNextToken(); * * When the last token has been read, the predicate function * AtEndOfLine returns TRUE, so that the loop structure * * while (!AtEndOfLine()) { * token = GetNextToken(); * . . . process the token . . . * } * * serves as an idiom for processing each token on the line. * * Further details for each function are given in the * individual descriptions below. */ #ifndef _scanner_h #define _scanner_h #include #include #include "genlib.h" #include "strlib.h" #include "scanner.h" void InitScanner(string line); string GetNextToken(void); bool AtEndOfLine(void); /* * Private variables * ----------------- * buffer -- Private copy of the string passed to InitScanner * buflen -- Length of the buffer, saved for efficiency * cpos -- Current character position in the buffer */ static string buffer; static int buflen; static int cpos; /* * Function: InitScanner * --------------------- * All this function has to do is initialize the private * variables used in the package. */ void InitScanner(string line) { buffer = line; buflen = StringLength(buffer); cpos = 0; } /* * Function: GetNextToken * ---------------------- * The implementation of GetNextToken follows its behavioral * description as given in the interface: if the next character * is alphanumeric (i.e., a letter or digit), the function * searches to find an unbroken string of such characters and * returns the entire string. If the current character is not * a letter or digit, a one-character string containing that * character is returned. */ string GetNextToken(void) { char ch; int start; if (cpos >= buflen) Error("No more tokens"); ch = IthChar(buffer, cpos); if (isalnum(ch)) { start = cpos; while (cpos < buflen && isalnum(IthChar(buffer, cpos))) { cpos++; } return (SubString(buffer, start, cpos - 1)); } else { cpos++; return (CharToString(ch)); } } /* * Function: AtEndOfLine * --------------------- * This implementation compares the current buffer position * against the saved length. */ bool AtEndOfLine(void) { return (cpos >= buflen); } #endif