Parsing a CSV line

Create a finite state machine describing the behavior for a class that has the responsibility to parse a single line from a Comma Separated Values file. The file will have the structure given below. Be sure to label states with a descriptive name in the context of parsing a single line from a CSV file. All transitions should be labeled with the event that triggers the transition, and show actions that will be executed, if appropriate, before entering the next state. Be sure to define behavior that is robust to format errors in the line being parsed.

  1. A line break indicates the end of a line.
  2. Empty fields are allowed.
  3. Two fields on a line are separated by a comma. There is no comma after the last field on the line.
  4. Double-quotes may or may not enclose a field. Double-quotes enclosing a field are not part of the field value.
  5. Commas can only be present in a field if the field is enclosed in double-quotes. A double-quote within an enclosed field must be escaped with a double-quote, i.e. the double-quote within the field is represented by two double-quotes in a row. A double-quote in a non-enclosed field is considered a regular character.
  6. A line ending in a comma indicates an empty last field on the line. A line with just a line break is a blank line with no fields. A line that has a single empty field must use an enclosed field with no characters between the enclosing double-quote characters to indicate that single empty field.

Notes