student.adventure
Class Parser

java.lang.Object
  extended by student.adventure.Parser

public class Parser
extends java.lang.Object

This parser reads user input and tries to interpret it as an "Adventure" command. Every time it is called it reads a line from the terminal and tries to interpret the line as a two word command. It returns the command as an object of class Command. The parser has a set of known command words. It checks user input against the known commands, and if the input is not one of the known commands, it returns a command object that is marked as an unknown command.

Version:
1.1 (December 2002)
Author:
Michael Kolling and David J. Barnes

Constructor Summary
Parser()
          Create a new Parser object connected to System.in.
 
Method Summary
 CommandWords commandWords()
          Access the set of valid command words.
 Command getCommand()
          Get the next command from the input sequence.
 java.lang.String promptString()
          Get the prompt string printed before reading each command.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Parser

public Parser()
Create a new Parser object connected to System.in.

Method Detail

getCommand

public Command getCommand()
Get the next command from the input sequence. This includes printing the prompt, reading a line, grabbing the first and second (if there are more than one) words from that line, using the first word to look up commands in the commandWords(), and pushing the second word into the resulting command (if there was a second word). Any words on the line past the first two are ignored.

Returns:
the next command

promptString

public java.lang.String promptString()
Get the prompt string printed before reading each command. A subclass can override this method to change the prompt.

Returns:
the prompt used for user input

commandWords

public CommandWords commandWords()
Access the set of valid command words.

Returns:
The map from Strings to Commands defining the verbs this parser understands