From 930c8137dfb6fa45fc65f29408c7f1aa531e13aa Mon Sep 17 00:00:00 2001 From: Mike Cifelli Date: Wed, 14 Dec 2016 13:09:41 -0500 Subject: [PATCH] Created a package for s-expressions and continued refactoring the parser --- src/eval/APPLY.java | 3 + src/eval/ATOM.java | 4 + src/eval/CAR.java | 3 + src/eval/CDR.java | 3 + src/eval/COND.java | 3 + src/eval/CONS.java | 3 + src/eval/DEFUN.java | 4 + src/eval/DIVIDE.java | 4 + src/eval/EQ.java | 4 + src/eval/EQUAL.java | 4 + src/eval/EQUALSP.java | 5 + src/eval/EVAL.java | 5 + src/eval/EXIT.java | 3 + src/eval/FUNCALL.java | 3 + src/eval/GREATERP.java | 5 + src/eval/LAMBDA.java | 3 + src/eval/LENGTH.java | 4 + src/eval/LESSP.java | 5 + src/eval/LET.java | 3 + src/eval/LIST.java | 3 + src/eval/LISTP.java | 4 + src/eval/LOAD.java | 10 +- src/eval/LambdaExpression.java | 2 + src/eval/LispFunction.java | 2 + src/eval/MINUS.java | 4 + src/eval/MULTIPLY.java | 3 + src/eval/NULL.java | 4 + src/eval/PLUS.java | 3 + src/eval/PRINT.java | 3 + src/eval/QUOTE.java | 3 + src/eval/SETF.java | 3 + src/eval/SYMBOL_FUNCTION.java | 3 + src/eval/SymbolTable.java | 2 + src/eval/UDFunction.java | 4 + src/main/LispInterpreter.java | 5 +- src/parser/LispParser.java | 43 ++++---- src/{parser => sexpression}/Atom.java | 2 +- src/{parser => sexpression}/Cons.java | 2 +- src/{parser => sexpression}/LispNumber.java | 2 +- src/{parser => sexpression}/LispString.java | 2 +- src/{parser => sexpression}/Nil.java | 2 +- src/{parser => sexpression}/SExpression.java | 2 +- src/{parser => sexpression}/Symbol.java | 2 +- test/parser/LispParserTester.java | 107 +++++++++++-------- 44 files changed, 215 insertions(+), 78 deletions(-) rename src/{parser => sexpression}/Atom.java (97%) rename src/{parser => sexpression}/Cons.java (99%) rename src/{parser => sexpression}/LispNumber.java (98%) rename src/{parser => sexpression}/LispString.java (96%) rename src/{parser => sexpression}/Nil.java (99%) rename src/{parser => sexpression}/SExpression.java (99%) rename src/{parser => sexpression}/Symbol.java (97%) diff --git a/src/eval/APPLY.java b/src/eval/APPLY.java index f7fa819..cad1663 100644 --- a/src/eval/APPLY.java +++ b/src/eval/APPLY.java @@ -7,6 +7,9 @@ package eval; import parser.*; +import sexpression.Cons; +import sexpression.SExpression; +import sexpression.Symbol; /** * APPLY represents the APPLY function in Lisp. diff --git a/src/eval/ATOM.java b/src/eval/ATOM.java index fbee068..0b5ca1c 100644 --- a/src/eval/ATOM.java +++ b/src/eval/ATOM.java @@ -7,6 +7,10 @@ package eval; import parser.*; +import sexpression.Cons; +import sexpression.Nil; +import sexpression.SExpression; +import sexpression.Symbol; /** * ATOM represents the ATOM function in Lisp. diff --git a/src/eval/CAR.java b/src/eval/CAR.java index 3b75078..ca49f98 100644 --- a/src/eval/CAR.java +++ b/src/eval/CAR.java @@ -7,6 +7,9 @@ package eval; import parser.*; +import sexpression.Cons; +import sexpression.SExpression; +import sexpression.Symbol; /** * CAR represents the CAR function in Lisp. diff --git a/src/eval/CDR.java b/src/eval/CDR.java index 0cc281e..932a4f2 100644 --- a/src/eval/CDR.java +++ b/src/eval/CDR.java @@ -7,6 +7,9 @@ package eval; import parser.*; +import sexpression.Cons; +import sexpression.SExpression; +import sexpression.Symbol; /** * CDR represents the CDR function in Lisp. diff --git a/src/eval/COND.java b/src/eval/COND.java index 9bd4e79..6fdc096 100644 --- a/src/eval/COND.java +++ b/src/eval/COND.java @@ -7,6 +7,9 @@ package eval; import parser.*; +import sexpression.Cons; +import sexpression.Nil; +import sexpression.SExpression; /** * COND represents the COND form in Lisp. diff --git a/src/eval/CONS.java b/src/eval/CONS.java index db967a7..7c4e592 100644 --- a/src/eval/CONS.java +++ b/src/eval/CONS.java @@ -7,6 +7,9 @@ package eval; import parser.*; +import sexpression.Cons; +import sexpression.SExpression; +import sexpression.Symbol; /** * CONS represents the CONS function in Lisp. diff --git a/src/eval/DEFUN.java b/src/eval/DEFUN.java index c435737..ffa5bf7 100644 --- a/src/eval/DEFUN.java +++ b/src/eval/DEFUN.java @@ -7,6 +7,10 @@ package eval; import parser.*; +import sexpression.Cons; +import sexpression.SExpression; +import sexpression.Symbol; + import java.util.HashMap; /** diff --git a/src/eval/DIVIDE.java b/src/eval/DIVIDE.java index 0794f57..a1f9c5d 100644 --- a/src/eval/DIVIDE.java +++ b/src/eval/DIVIDE.java @@ -7,6 +7,10 @@ package eval; import parser.*; +import sexpression.Cons; +import sexpression.LispNumber; +import sexpression.SExpression; +import sexpression.Symbol; /** * DIVIDE represents the '/' function in Lisp. diff --git a/src/eval/EQ.java b/src/eval/EQ.java index 0227525..5939a75 100644 --- a/src/eval/EQ.java +++ b/src/eval/EQ.java @@ -7,6 +7,10 @@ package eval; import parser.*; +import sexpression.Cons; +import sexpression.Nil; +import sexpression.SExpression; +import sexpression.Symbol; /** * EQ represents the EQ function in Lisp. diff --git a/src/eval/EQUAL.java b/src/eval/EQUAL.java index 0e4e02e..4cf0c1d 100644 --- a/src/eval/EQUAL.java +++ b/src/eval/EQUAL.java @@ -7,6 +7,10 @@ package eval; import parser.*; +import sexpression.Cons; +import sexpression.Nil; +import sexpression.SExpression; +import sexpression.Symbol; /** * EQUAL represents the EQUAL function in Lisp. diff --git a/src/eval/EQUALSP.java b/src/eval/EQUALSP.java index 1c6fb65..f5c13c8 100644 --- a/src/eval/EQUALSP.java +++ b/src/eval/EQUALSP.java @@ -7,6 +7,11 @@ package eval; import parser.*; +import sexpression.Cons; +import sexpression.LispNumber; +import sexpression.Nil; +import sexpression.SExpression; +import sexpression.Symbol; /** * EQUALSP represents the '=' function in Lisp. diff --git a/src/eval/EVAL.java b/src/eval/EVAL.java index 2eacfc2..512c8ab 100644 --- a/src/eval/EVAL.java +++ b/src/eval/EVAL.java @@ -7,6 +7,11 @@ package eval; import parser.*; +import sexpression.Cons; +import sexpression.Nil; +import sexpression.SExpression; +import sexpression.Symbol; + import java.util.HashMap; /** diff --git a/src/eval/EXIT.java b/src/eval/EXIT.java index 1def13f..b057b41 100644 --- a/src/eval/EXIT.java +++ b/src/eval/EXIT.java @@ -1,6 +1,9 @@ package eval; import parser.*; +import sexpression.Cons; +import sexpression.SExpression; +import sexpression.Symbol; public class EXIT extends LispFunction { diff --git a/src/eval/FUNCALL.java b/src/eval/FUNCALL.java index 395dd88..50c8bdd 100644 --- a/src/eval/FUNCALL.java +++ b/src/eval/FUNCALL.java @@ -7,6 +7,9 @@ package eval; import parser.*; +import sexpression.Cons; +import sexpression.SExpression; +import sexpression.Symbol; /** * FUNCALL represents the FUNCALL function in Lisp. diff --git a/src/eval/GREATERP.java b/src/eval/GREATERP.java index 2dfc7aa..a1cda2c 100644 --- a/src/eval/GREATERP.java +++ b/src/eval/GREATERP.java @@ -7,6 +7,11 @@ package eval; import parser.*; +import sexpression.Cons; +import sexpression.LispNumber; +import sexpression.Nil; +import sexpression.SExpression; +import sexpression.Symbol; /** * GREATERP represents the '>' function in Lisp. diff --git a/src/eval/LAMBDA.java b/src/eval/LAMBDA.java index eaec557..ebe5721 100644 --- a/src/eval/LAMBDA.java +++ b/src/eval/LAMBDA.java @@ -7,6 +7,9 @@ package eval; import parser.*; +import sexpression.Cons; +import sexpression.SExpression; +import sexpression.Symbol; /** * LAMBDA represents the LAMBDA form in Lisp. diff --git a/src/eval/LENGTH.java b/src/eval/LENGTH.java index dcab21b..b201333 100644 --- a/src/eval/LENGTH.java +++ b/src/eval/LENGTH.java @@ -7,6 +7,10 @@ package eval; import parser.*; +import sexpression.Cons; +import sexpression.LispNumber; +import sexpression.SExpression; +import sexpression.Symbol; /** * LENGTH represents the LENGTH function in Lisp. diff --git a/src/eval/LESSP.java b/src/eval/LESSP.java index 162d8c6..866946f 100644 --- a/src/eval/LESSP.java +++ b/src/eval/LESSP.java @@ -7,6 +7,11 @@ package eval; import parser.*; +import sexpression.Cons; +import sexpression.LispNumber; +import sexpression.Nil; +import sexpression.SExpression; +import sexpression.Symbol; /** * LESSP represents the '<' function in Lisp. diff --git a/src/eval/LET.java b/src/eval/LET.java index becdbdd..f489a6e 100644 --- a/src/eval/LET.java +++ b/src/eval/LET.java @@ -7,6 +7,9 @@ package eval; import parser.*; +import sexpression.Cons; +import sexpression.Nil; +import sexpression.SExpression; /** * LET represents the LET form in Lisp. diff --git a/src/eval/LIST.java b/src/eval/LIST.java index d801e6e..182bbdb 100644 --- a/src/eval/LIST.java +++ b/src/eval/LIST.java @@ -7,6 +7,9 @@ package eval; import parser.*; +import sexpression.Cons; +import sexpression.Nil; +import sexpression.SExpression; /** * LIST represents the LIST function in Lisp. diff --git a/src/eval/LISTP.java b/src/eval/LISTP.java index b2ad0ce..19e412a 100644 --- a/src/eval/LISTP.java +++ b/src/eval/LISTP.java @@ -7,6 +7,10 @@ package eval; import parser.*; +import sexpression.Cons; +import sexpression.Nil; +import sexpression.SExpression; +import sexpression.Symbol; /** * LISTP represents the LISTP function in Lisp. diff --git a/src/eval/LOAD.java b/src/eval/LOAD.java index 1ce3f57..f7d7621 100644 --- a/src/eval/LOAD.java +++ b/src/eval/LOAD.java @@ -7,6 +7,12 @@ package eval; import parser.*; +import sexpression.Cons; +import sexpression.LispString; +import sexpression.Nil; +import sexpression.SExpression; +import sexpression.Symbol; + import java.io.*; /** @@ -64,9 +70,9 @@ public class LOAD extends LispFunction { } // attempt to evaluate all the S-expressions contained in the file - while (! parser.eof()) { + while (! parser.isEof()) { try { - SExpression sexpr = parser.getSExpr(); + SExpression sexpr = parser.getNextSExpression(); EVAL.eval(sexpr); } catch (RuntimeException e) { diff --git a/src/eval/LambdaExpression.java b/src/eval/LambdaExpression.java index 570d162..2b787c7 100644 --- a/src/eval/LambdaExpression.java +++ b/src/eval/LambdaExpression.java @@ -7,6 +7,8 @@ package eval; import parser.*; +import sexpression.Cons; +import sexpression.SExpression; /** * This class represents a Lisp FUNCTION in the PL-Lisp implementation. diff --git a/src/eval/LispFunction.java b/src/eval/LispFunction.java index c861fa2..b86870a 100644 --- a/src/eval/LispFunction.java +++ b/src/eval/LispFunction.java @@ -7,6 +7,8 @@ package eval; import parser.*; +import sexpression.Cons; +import sexpression.SExpression; /** * A LispFunction is an internal representation of a built-in diff --git a/src/eval/MINUS.java b/src/eval/MINUS.java index a913624..ec3e6b3 100644 --- a/src/eval/MINUS.java +++ b/src/eval/MINUS.java @@ -7,6 +7,10 @@ package eval; import parser.*; +import sexpression.Cons; +import sexpression.LispNumber; +import sexpression.SExpression; +import sexpression.Symbol; /** * MINUS represents the '-' function in Lisp. diff --git a/src/eval/MULTIPLY.java b/src/eval/MULTIPLY.java index ac15950..c461a30 100644 --- a/src/eval/MULTIPLY.java +++ b/src/eval/MULTIPLY.java @@ -7,6 +7,9 @@ package eval; import parser.*; +import sexpression.Cons; +import sexpression.LispNumber; +import sexpression.SExpression; /** * MULTIPLY represents the '*' function in Lisp. diff --git a/src/eval/NULL.java b/src/eval/NULL.java index fd30367..b1d1a0f 100644 --- a/src/eval/NULL.java +++ b/src/eval/NULL.java @@ -7,6 +7,10 @@ package eval; import parser.*; +import sexpression.Cons; +import sexpression.Nil; +import sexpression.SExpression; +import sexpression.Symbol; /** * NULL represents the NULL function in Lisp. diff --git a/src/eval/PLUS.java b/src/eval/PLUS.java index 144f46c..33e8d42 100644 --- a/src/eval/PLUS.java +++ b/src/eval/PLUS.java @@ -7,6 +7,9 @@ package eval; import parser.*; +import sexpression.Cons; +import sexpression.LispNumber; +import sexpression.SExpression; /** * PLUS represents the '+' function in Lisp. diff --git a/src/eval/PRINT.java b/src/eval/PRINT.java index f9fedb5..407544c 100644 --- a/src/eval/PRINT.java +++ b/src/eval/PRINT.java @@ -7,6 +7,9 @@ package eval; import parser.*; +import sexpression.Cons; +import sexpression.SExpression; +import sexpression.Symbol; /** * PRINT represents the PRINT function in Lisp. diff --git a/src/eval/QUOTE.java b/src/eval/QUOTE.java index 960ab3e..53cd348 100644 --- a/src/eval/QUOTE.java +++ b/src/eval/QUOTE.java @@ -7,6 +7,9 @@ package eval; import parser.*; +import sexpression.Cons; +import sexpression.SExpression; +import sexpression.Symbol; /** * QUOTE represents the QUOTE form in Lisp. diff --git a/src/eval/SETF.java b/src/eval/SETF.java index 90c25d4..4ddd8ec 100644 --- a/src/eval/SETF.java +++ b/src/eval/SETF.java @@ -7,6 +7,9 @@ package eval; import parser.*; +import sexpression.Cons; +import sexpression.SExpression; +import sexpression.Symbol; /** * SETF represents the SETF form in Lisp. diff --git a/src/eval/SYMBOL_FUNCTION.java b/src/eval/SYMBOL_FUNCTION.java index 4a591ee..02ff8c5 100644 --- a/src/eval/SYMBOL_FUNCTION.java +++ b/src/eval/SYMBOL_FUNCTION.java @@ -7,6 +7,9 @@ package eval; import parser.*; +import sexpression.Cons; +import sexpression.SExpression; +import sexpression.Symbol; /** * SYMBOL_FUNCTION represents the SYMBOL-FUNCTION function in diff --git a/src/eval/SymbolTable.java b/src/eval/SymbolTable.java index aa72b28..844095c 100644 --- a/src/eval/SymbolTable.java +++ b/src/eval/SymbolTable.java @@ -7,6 +7,8 @@ package eval; import parser.*; +import sexpression.SExpression; + import java.util.HashMap; /** diff --git a/src/eval/UDFunction.java b/src/eval/UDFunction.java index 44e02c8..9828a8e 100644 --- a/src/eval/UDFunction.java +++ b/src/eval/UDFunction.java @@ -7,6 +7,10 @@ package eval; import parser.*; +import sexpression.Cons; +import sexpression.SExpression; +import sexpression.Symbol; + import java.util.ArrayList; /** diff --git a/src/main/LispInterpreter.java b/src/main/LispInterpreter.java index abfbb26..08dafe5 100644 --- a/src/main/LispInterpreter.java +++ b/src/main/LispInterpreter.java @@ -7,6 +7,7 @@ package main; import parser.*; +import sexpression.SExpression; import eval.*; import error.ErrorManager; import error.LispException; @@ -62,9 +63,9 @@ public class LispInterpreter { System.out.print(PROMPT); } - while (! parser.eof()) { + while (! parser.isEof()) { try { - SExpression sexpr = parser.getSExpr(); + SExpression sexpr = parser.getNextSExpression(); String result = MessageFormat.format("{0}{1}{2}", ANSI_GREEN, EVAL.eval(sexpr), ANSI_RESET); LispInterpreter.erasePrompt(interactive); diff --git a/src/parser/LispParser.java b/src/parser/LispParser.java index c94d661..5d2e4e6 100644 --- a/src/parser/LispParser.java +++ b/src/parser/LispParser.java @@ -3,10 +3,9 @@ package parser; import java.io.InputStream; import error.LispException; -import parser.MalformedSExpressionException.EofEncounteredException; -import parser.MalformedSExpressionException.StartsWithRightParenthesisException; -import parser.MalformedSExpressionException.UnrecognizedTokenException; +import parser.MalformedSExpressionException.*; import scanner.LispScanner; +import sexpression.*; import token.Eof; import token.Token; @@ -18,37 +17,37 @@ public class LispParser { private LispScanner scanner; private Token nextToken; - private boolean nextTokenStored; + private boolean isNextTokenStored; public LispParser(InputStream inputStream, String fileName) { scanner = new LispScanner(inputStream, fileName); nextToken = null; - nextTokenStored = false; + isNextTokenStored = false; } - public boolean eof() { - if (!nextTokenStored) { - try { - nextToken = scanner.getNextToken(); - nextTokenStored = true; - } catch (LispException e) { - // this method should give the illusion of not actually reading - // a token, so we ignore any exceptions (they will be thrown - // the next time the 'getSExpr' method is called) - - if (nextToken == null) - return false; - } - } + public boolean isEof() { + if (!isNextTokenStored) + storeNextToken(); return nextToken instanceof Eof; } - public SExpression getSExpr() { - if (!nextTokenStored) + private void storeNextToken() { + try { + nextToken = scanner.getNextToken(); + isNextTokenStored = true; + } catch (LispException e) { + // This method should give the illusion of not actually reading + // a token, so we ignore any exception. It will be thrown when we + // return the next sExpression. + } + } + + public SExpression getNextSExpression() { + if (!isNextTokenStored) nextToken = scanner.getNextToken(); else - nextTokenStored = false; + isNextTokenStored = false; return sExpr(); } diff --git a/src/parser/Atom.java b/src/sexpression/Atom.java similarity index 97% rename from src/parser/Atom.java rename to src/sexpression/Atom.java index 465de81..3fd4596 100644 --- a/src/parser/Atom.java +++ b/src/sexpression/Atom.java @@ -4,7 +4,7 @@ * Assignment: Lisp Parser */ -package parser; +package sexpression; /** * This class represents an ATOM in the PL-Lisp implementation. diff --git a/src/parser/Cons.java b/src/sexpression/Cons.java similarity index 99% rename from src/parser/Cons.java rename to src/sexpression/Cons.java index 2402a99..ae749da 100644 --- a/src/parser/Cons.java +++ b/src/sexpression/Cons.java @@ -4,7 +4,7 @@ * Assignment: Lisp Parser */ -package parser; +package sexpression; /** * This class represents a Lisp CONS cell in the PL-Lisp implementation. diff --git a/src/parser/LispNumber.java b/src/sexpression/LispNumber.java similarity index 98% rename from src/parser/LispNumber.java rename to src/sexpression/LispNumber.java index 34c3bde..eaa5abc 100644 --- a/src/parser/LispNumber.java +++ b/src/sexpression/LispNumber.java @@ -4,7 +4,7 @@ * Assignment: Lisp Parser */ -package parser; +package sexpression; /** * This class represents a NUMBER in the PL-Lisp implementation. diff --git a/src/parser/LispString.java b/src/sexpression/LispString.java similarity index 96% rename from src/parser/LispString.java rename to src/sexpression/LispString.java index 66b56a3..b559cee 100644 --- a/src/parser/LispString.java +++ b/src/sexpression/LispString.java @@ -4,7 +4,7 @@ * Assignment: Lisp Parser */ -package parser; +package sexpression; /** * This class represents a STRING in the PL-Lisp implementation. diff --git a/src/parser/Nil.java b/src/sexpression/Nil.java similarity index 99% rename from src/parser/Nil.java rename to src/sexpression/Nil.java index 5344053..b831e2e 100644 --- a/src/parser/Nil.java +++ b/src/sexpression/Nil.java @@ -4,7 +4,7 @@ * Assignment: Lisp Parser */ -package parser; +package sexpression; /** * This class represents NIL in the PL-Lisp implementation. diff --git a/src/parser/SExpression.java b/src/sexpression/SExpression.java similarity index 99% rename from src/parser/SExpression.java rename to src/sexpression/SExpression.java index bb8aaa4..a40c7bb 100644 --- a/src/parser/SExpression.java +++ b/src/sexpression/SExpression.java @@ -4,7 +4,7 @@ * Assignment: Lisp Parser */ -package parser; +package sexpression; /** * This is the base class for memory in the PL-Lisp implementation. diff --git a/src/parser/Symbol.java b/src/sexpression/Symbol.java similarity index 97% rename from src/parser/Symbol.java rename to src/sexpression/Symbol.java index 96f1a96..381966d 100644 --- a/src/parser/Symbol.java +++ b/src/sexpression/Symbol.java @@ -4,7 +4,7 @@ * Assignment: Lisp Parser */ -package parser; +package sexpression; /** * This class represents a SYMBOL in the PL-Lisp implementation. diff --git a/test/parser/LispParserTester.java b/test/parser/LispParserTester.java index 1bb0581..60dbb1f 100644 --- a/test/parser/LispParserTester.java +++ b/test/parser/LispParserTester.java @@ -7,6 +7,8 @@ import java.io.InputStream; import org.junit.Test; import error.LispException; +import sexpression.Nil; +import sexpression.SExpression; import testutils.TestUtilities; public class LispParserTester { @@ -79,7 +81,7 @@ public class LispParserTester { String input = ""; LispParser parser = createLispParser(input); - assertTrue(parser.eof()); + assertTrue(parser.isEof()); } @Test @@ -87,40 +89,40 @@ public class LispParserTester { String input = "abc"; LispParser parser = createLispParser(input); - assertFalse(parser.eof()); + assertFalse(parser.isEof()); } @Test public void testEofMethod_ReturnsTrueAfterSomeInput() { String input = "(yyz 9 9 9)"; LispParser parser = createLispParser(input); - parser.getSExpr(); + parser.getNextSExpression(); - assertTrue(parser.eof()); + assertTrue(parser.isEof()); } @Test public void testEofMethod_ReturnsFalseAfterMultipleExpressions() { String input = "()()()"; LispParser parser = createLispParser(input); - assertFalse(parser.eof()); - parser.getSExpr(); - assertFalse(parser.eof()); - parser.getSExpr(); - assertFalse(parser.eof()); + assertFalse(parser.isEof()); + parser.getNextSExpression(); + assertFalse(parser.isEof()); + parser.getNextSExpression(); + assertFalse(parser.isEof()); } @Test public void testEofMethod_ReturnsTrueAfterMultipleExpressions() { String input = "()()()"; LispParser parser = createLispParser(input); - assertFalse(parser.eof()); - parser.getSExpr(); - assertFalse(parser.eof()); - parser.getSExpr(); - assertFalse(parser.eof()); - parser.getSExpr(); - assertTrue(parser.eof()); + assertFalse(parser.isEof()); + parser.getNextSExpression(); + assertFalse(parser.isEof()); + parser.getNextSExpression(); + assertFalse(parser.isEof()); + parser.getNextSExpression(); + assertTrue(parser.isEof()); } @Test @@ -128,7 +130,7 @@ public class LispParserTester { String input = "()"; LispParser parser = createLispParser(input); - assertNil(parser.getSExpr()); + assertNil(parser.getNextSExpression()); } @Test @@ -136,7 +138,7 @@ public class LispParserTester { String input = "12"; LispParser parser = createLispParser(input); - assertNumber(parser.getSExpr()); + assertNumber(parser.getNextSExpression()); } @Test @@ -144,7 +146,7 @@ public class LispParserTester { String input = "identifier1"; LispParser parser = createLispParser(input); - assertSymbol(parser.getSExpr()); + assertSymbol(parser.getNextSExpression()); } @Test @@ -152,7 +154,7 @@ public class LispParserTester { String input = "\"string\""; LispParser parser = createLispParser(input); - assertString(parser.getSExpr()); + assertString(parser.getNextSExpression()); } @Test @@ -160,7 +162,7 @@ public class LispParserTester { String input = "(1 2)"; LispParser parser = createLispParser(input); - assertList(parser.getSExpr()); + assertList(parser.getNextSExpression()); } @Test @@ -168,8 +170,8 @@ public class LispParserTester { String input = "'quoted"; LispParser parser = createLispParser(input); - assertList(parser.getSExpr()); - assertTrue(parser.eof()); + assertList(parser.getNextSExpression()); + assertTrue(parser.isEof()); } @Test @@ -177,8 +179,8 @@ public class LispParserTester { String input = "(defun f (x) \n (print \n (list \"x is \" x) \n ) \n )"; LispParser parser = createLispParser(input); - assertList(parser.getSExpr()); - assertTrue(parser.eof()); + assertList(parser.getNextSExpression()); + assertTrue(parser.isEof()); } @Test @@ -186,12 +188,12 @@ public class LispParserTester { String input = "(setf x 2) x \"hi\" () 29"; LispParser parser = createLispParser(input); - assertList(parser.getSExpr()); - assertSymbol(parser.getSExpr()); - assertString(parser.getSExpr()); - assertNil(parser.getSExpr()); - assertNumber(parser.getSExpr()); - assertTrue(parser.eof()); + assertList(parser.getNextSExpression()); + assertSymbol(parser.getNextSExpression()); + assertString(parser.getNextSExpression()); + assertNil(parser.getNextSExpression()); + assertNumber(parser.getNextSExpression()); + assertTrue(parser.isEof()); } @Test @@ -199,11 +201,11 @@ public class LispParserTester { String input = "()"; LispParser parser = createLispParser(input); - parser.eof(); - parser.eof(); + parser.isEof(); + parser.isEof(); - assertNil(parser.getSExpr()); - assertTrue(parser.eof()); + assertNil(parser.getNextSExpression()); + assertTrue(parser.isEof()); } @Test(expected = LispException.class) @@ -211,7 +213,7 @@ public class LispParserTester { String input = "["; LispParser parser = createLispParser(input); - parser.getSExpr(); + parser.getNextSExpression(); } @Test(expected = LispException.class) @@ -219,7 +221,7 @@ public class LispParserTester { String input = "\"string"; LispParser parser = createLispParser(input); - parser.getSExpr(); + parser.getNextSExpression(); } @Test(expected = LispException.class) @@ -227,7 +229,7 @@ public class LispParserTester { String input = "(bad list"; LispParser parser = createLispParser(input); - parser.getSExpr(); + parser.getNextSExpression(); } @Test(expected = LispException.class) @@ -235,7 +237,7 @@ public class LispParserTester { String input = ")"; LispParser parser = createLispParser(input); - parser.getSExpr(); + parser.getNextSExpression(); } @Test(expected = LispException.class) @@ -244,27 +246,42 @@ public class LispParserTester { LispParser parser = createLispParser(input); try { - parser.eof(); + parser.isEof(); } catch (LispException e) { fail("Exception thrown too early"); } - parser.getSExpr(); + parser.getNextSExpression(); } @Test(expected = LispException.class) public void givenBadCharacterAfterValidToken_ThrowsExceptionAtTheCorrectTime() { - String input = "id[]"; + String input = "id["; LispParser parser = createLispParser(input); try { - parser.getSExpr(); - parser.eof(); + parser.getNextSExpression(); + parser.isEof(); } catch (LispException e) { fail("Exception thrown too early"); } - parser.getSExpr(); + parser.getNextSExpression(); } + @Test + public void afterException_ReturnsEofCorrectly() { + String input = "id["; + LispParser parser = createLispParser(input); + parser.getNextSExpression(); + parser.isEof(); + + try { + parser.getNextSExpression(); + fail("Expected LispException"); + } catch (LispException e) { + } + + assertTrue(parser.isEof()); + } }