classParserError<ExceptionendclassSentencedefinitialize(subject,verb,object)# remember we take Pair.new(:noun, "princess") structs and convert them@subject=subject.word@verb=verb.word@object=object.wordendenddefpeek(word_list)beginword_list.first.tokenrescuenilendenddefmatch(word_list,expecting)beginword=word_list.shiftifword.token==expectingwordelsenilendrescuenilendenddefskip(word_list,token)whilepeek(word_list)==tokenmatch(word_list,token)endenddefparse_verb(word_list)skip(word_list,:stop)ifpeek(word_list)==:verbreturnmatch(word_list,:verb)elseraiseParserError.new("Expected a verb next.")endenddefparse_object(word_list)skip(word_list,:stop)next_word=peek(word_list)ifnext_word==:nounreturnmatch(word_list,:noun)endifnext_word==:directionreturnmatch(word_list,:direction)elseraiseParserError.new("Expected a noun or direction next.")endenddefparse_subject(word_list,subj)verb=parse_verb(word_list)obj=parse_object(word_list)returnSentence.new(subj,verb,obj)enddefparse_sentence(word_list)skip(word_list,:stop)start=peek(word_list)ifstart==:nounsubj=match(word_list,:noun)returnparse_subject(word_list,subj)elsifstart==:verb# assume the subject is the player thenreturnparse_subject(word_list,Pair.new(:noun,"player"))elseraiseParserError.new("Must start with subject, object, or verb not: #{start}")endend