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     * @param pInputLine Lis la String entrée.
037     * @return une commande
038     */
039    public Command getCommand( final String pInputLine ) 
040    {
041        String vWord1;
042        String vWord2;
043
044        StringTokenizer tokenizer = new StringTokenizer( pInputLine );
045
046        if ( tokenizer.hasMoreTokens() )
047            vWord1 = tokenizer.nextToken();      // get first word
048        else
049            vWord1 = null;
050
051        if ( tokenizer.hasMoreTokens() )
052            vWord2 = tokenizer.nextToken();      // get second word
053        else
054            vWord2 = null;
055
056        // note: we just ignore the rest of the input line.
057
058        // Now check whether this word is known. If so, create a command
059        // with it. If not, create a "null" command (for unknown command).
060
061        if ( this.aCommandWords.isCommand( vWord1 ) )
062            return new Command( vWord1, vWord2 );
063        else
064            return new Command( null, vWord2 );
065    } // getCommand(.)
066
067    /**
068     * Returns a String with valid command words.
069     * @return la String avec les commandes valides.
070     */
071    public String getCommandString() // was showCommands()
072    {
073        return this.aCommandWords.getCommandList();
074    } // getCommandString()
075
076} // Parser