cse466.parser
Class Parser

java.lang.Object
  |
  +--cse466.parser.Parser

public class Parser
extends java.lang.Object

This class implements the functionality needed for parsing the input file and generating the action list that will later be executed.


Field Summary
protected  java.io.BufferedReader _br
          The buffered reader that is reading the file
protected  int _lineNumber
          The current line number being parsed
 
Constructor Summary
protected Parser(java.io.BufferedReader br)
          Initializes the current line, and assigns the buffered reader to be the one the rest of the parser will use
 
Method Summary
protected  java.util.List doParsing(boolean ignoreBadAMValues)
          Runs the parser and appends the incoming actions to the list of actions.
protected  java.lang.String nextLine()
          Grab the next line from the file, but if it hits the end of the file it will throw an IOException saying that it was expecting more.
protected  java.lang.String nextLineEndOk()
          Grab the next line from the file.
protected  cse466.actions.Action parseDelayAction(java.lang.String line)
          Creates a DelayAction out of the input line.
static java.util.List parseFile(java.lang.String filename, boolean ignoreBadAMValues)
          Parses the file specified and gives back the ordered list of actions declared in the file.
protected  cse466.actions.Action parseNextAction(boolean ignoreBadAMValues)
          Grab the next line and figure out what type of action it is (delay or packet) then delegate the rest of the parsing to the appropriate method.
protected  cse466.actions.Action parsePacketAction(java.lang.String line, boolean ignoreBadAMValues)
          Parses the sequence of lines that make up a @link PacketAction.
protected  java.lang.String[] splitInTwo(java.lang.String s)
          A helper function that splits a string into two space delimited strings and then trims them.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

_lineNumber

protected int _lineNumber
The current line number being parsed


_br

protected java.io.BufferedReader _br
The buffered reader that is reading the file

Constructor Detail

Parser

protected Parser(java.io.BufferedReader br)
Initializes the current line, and assigns the buffered reader to be the one the rest of the parser will use

Parameters:
br - The buffered reader that will be used for all the file reading
Method Detail

splitInTwo

protected java.lang.String[] splitInTwo(java.lang.String s)
                                 throws ParserException
A helper function that splits a string into two space delimited strings and then trims them. It will throw a parser exception if it can't split the string.

Parameters:
s - the string to split
Returns:
a two element array of strings that are the result of trimming and splitting the input string
Throws:
ParserException - if it cannot figure out how to split the string

nextLineEndOk

protected java.lang.String nextLineEndOk()
                                  throws java.io.IOException
Grab the next line from the file. This one allows the end of the file to be reached, and returns null in that case.

Returns:
the next line in the file
Throws:
java.io.IOException - if one occurs from trying to read the file

nextLine

protected java.lang.String nextLine()
                             throws java.io.IOException
Grab the next line from the file, but if it hits the end of the file it will throw an IOException saying that it was expecting more. This is useful if you're in the middle of parsing a multi-line structure.

Returns:
The next line in the file
Throws:
java.io.IOException - if the end of the file was reached or if there was a problem trying to read the file.

parseDelayAction

protected cse466.actions.Action parseDelayAction(java.lang.String line)
                                          throws ParserException
Creates a DelayAction out of the input line. Since the delay action fits on a single line, it does not do any file reading of its own. The format of the delay action in the file is:

  delay timeInMilliseconds
  

Parameters:
line - The line of the file that contains the delay specification.
Returns:
The Action that represents the delay
Throws:
ParserException - if there was something wrong with the line of input.

parsePacketAction

protected cse466.actions.Action parsePacketAction(java.lang.String line,
                                                  boolean ignoreBadAMValues)
                                           throws ParserException,
                                                  java.io.IOException
Parses the sequence of lines that make up a @link PacketAction. A PacketAction simply consists of a packet to send. These packets can be any of the packets in the cse466.packets package. The format of a packet action is:

  packet packetType
  fieldName value
  fieldName value
  end
  
Note that you must have as many fieldname/value pairs as the packet has fields.

Parameters:
line - The line that contains the start of the packet action (packet packetType).
ignoreBadAMValues - if true, it will let you put whatever amType you want to put in the packet. If false, it will throw a
Returns:
The action that represents sending the given packet
Throws:
ParserException - if the input was malformed
java.io.IOException - if there was a problem reading from the file.

parseNextAction

protected cse466.actions.Action parseNextAction(boolean ignoreBadAMValues)
                                         throws java.io.IOException,
                                                ParserException
Grab the next line and figure out what type of action it is (delay or packet) then delegate the rest of the parsing to the appropriate method.

Parameters:
ignoreBadAMValues - if true, will let you use whatever am values you want otherwise, it forces you to use the ones that correspond to the packets.
Returns:
The action that should be run for this line
Throws:
ParserException - if the input file is malformed
java.io.IOException - if there was a problem reading from the file

doParsing

protected java.util.List doParsing(boolean ignoreBadAMValues)
                            throws java.io.IOException,
                                   ParserException
Runs the parser and appends the incoming actions to the list of actions.

Parameters:
ignoreBadAMValues - if it should ignore when the amValue does not match the amValue that the packet is supposed to have
Returns:
the ordered list of actions to be executed
Throws:
ParserException - on malformed input
java.io.IOException - if there was a problem reading from the file

parseFile

public static java.util.List parseFile(java.lang.String filename,
                                       boolean ignoreBadAMValues)
                                throws java.io.IOException,
                                       ParserException
Parses the file specified and gives back the ordered list of actions declared in the file.

Parameters:
filename - The name of the file to parse
ignoreBadAMValues - if true, it will ignore the case where the packet is receiving an amValue that does not correspond to the type of packet being created.
Returns:
The list of actions to be executed in order
Throws:
java.io.IOException - if there was a problem reading the file
ParserException - if there was malformed input