001import java.util.StringTokenizer;
002
003/*
004 * This class is part of "World of Zuul". "World of Zuul" is a simple, 
005 * text based adventure game.
006 *
007 * This parser takes user input and tries to interpret it as a "Zuul"
008 * command. Every time it is called it takes a line as a String and
009 * tries to interpret the line as a two word command. It returns the command
010 * as an object of class Command.
011 *
012 * The parser has a set of known command words. It checks user input against
013 * the known commands, and if the input is not one of the known commands, it
014 * returns a command object that is marked as an unknown command.
015 * 
016 * @author  Michael Kolling and David J. Barnes
017 * @version 2.0 (Jan 2003) DB edited (2019)
018 */
019
020public class Parser 
021{
022
023    private CommandWords aCommandWords;  // holds all valid command words
024
025    /**
026     * Create a new Parser.
027     */
028    public Parser() 
029    {
030        this.aCommandWords = new CommandWords();
031    } // Parser()
032
033    /**
034     * Get a new command from the user. The command is read by
035     * parsing the 'inputLine'.
036     */
037    public Command getCommand( final String pInputLine ) 
038    {
039        String vWord1;
040        String vWord2;
041
042        StringTokenizer tokenizer = new StringTokenizer( pInputLine );
043
044        if ( tokenizer.hasMoreTokens() )
045            vWord1 = tokenizer.nextToken();      // get first word
046        else
047            vWord1 = null;
048
049        if ( tokenizer.hasMoreTokens() )
050            vWord2 = tokenizer.nextToken();      // get second word
051        else
052            vWord2 = null;
053
054        // note: we just ignore the rest of the input line.
055
056        // Now check whether this word is known. If so, create a command
057        // with it. If not, create a "null" command (for unknown command).
058
059        if ( this.aCommandWords.isCommand( vWord1 ) )
060            return new Command( vWord1, vWord2 );
061        else
062            return new Command( null, vWord2 );
063    } // getCommand(.)
064
065    /**
066     * Returns a String with valid command words.
067     */
068    public String getCommandString() // was showCommands()
069    {
070        return this.aCommandWords.getCommandList();
071    } // getCommandString()
072
073} // Parser