/* Copyright (c) 1999, 2000 by Kevin Forchione.  All Rights Reserved. */
/*
 *  TADS ADV.T/STD.T LIBRARY EXTENSION
 *  PARSEWORD.T				
 *  version 1.0
 *
 *	    This TADS file is intended to assist in the conversion of 
 *      player input strings into a list of words for easier use by 
 *      preparse routines.
 *
 *----------------------------------------------------------------------
 *  REQUIREMENTS
 *
 *      + HTML TADS 2.2.6 or later
 *      + Requires ADV.T and STD.T
 *      + Should be #included after ADV.T and STD.T.
 *
 *----------------------------------------------------------------------
 *  IMPORTANT LIBRARY INTERFACE AND MODIFICATION
 *
 *      None.
 *
 *----------------------------------------------------------------------
 *  COPYRIGHT NOTICE
 *
 *  	You may modify and use this file in any way you want, provided that
 *		if you redistribute modified copies of this file in source form, the
 *   	copies must include the original copyright notice (including this
 *   	paragraph), and must be clearly marked as modified from the original
 *   	version.
 *
 *------------------------------------------------------------------------------
 *  REVISION HISTORY
 *
 *		99-03-10 programming begun.
 */

#define __PARSEWORD_MODULE_
 
parseWord: function;
parsePunct: function;

/*
 *	parseWord: function
 *
 *	This function takes in a player's input stream
 *	and returns a list consisting of 'words' (the player's
 *	input as separated by spaces, but not separating 
 *	out punctuation.) It then goes on to call parsePunct()
 *	which creates a list consisting of words and punctuation
 *	marks.
 */
parseWord: function( inline )
{
	local istr := lower( inline ), j, len, pos, w, wlst := [], fstr := [ ',' '.' ';' '?' '!' ];

	len := length( istr );
	while ( len )
	{
		pos := find( istr, ' ' );
		if ( pos = nil )
		     pos := length( istr ) + 1;
		w := substr( istr, 1, pos-1 );
		wlst += [ w ];
		istr := substr( istr, pos+1, len );
		len := length( istr );
	}
	for ( j := 1; j <= length( fstr ); j++ )
	{
		wlst := parsePunct( wlst, fstr[ j ] );
	}
	return wlst;
}

/*
 *	parsePunct: function
 *
 *	This function is called by parseWord and separates
 *	out the punctuation marks in the word list. For instance
 *	the statement <Doctor, give me the book.> will result in the
 *	following word list from parseWord: [ 'doctor,' 'give' 'me' 'the' 'book.']
 *	parsePunct will return  the following list:
 *	[ 'doctor'  ','  'give' 'me' 'the' 'book'  '.' ]
 */
parsePunct: function( lst, fstr )
{
	local i, lenstr, lenword, pos, w, wlst := [];

	lenstr := length( lst );
	for ( i := 1; i <= lenstr; i++ )
	{
		pos := find( lst[ i ], fstr );
		if ( pos = nil )
		{
			wlst += lst[ i ];
			continue;
		}
		lenword := length( lst[ i ] );
		w := substr( lst[ i ], 1, pos-1 );
		wlst += [ w ];
		w := substr( lst[ i ], pos, 1 );
		wlst += [ w ];
		if ( pos < lenword )
		{
			w := substr( lst[ i ], pos+1, lenword-pos );
			wlst += [ w ];
		}
	}
	return wlst;
}
