Permalink
Browse files

Fixed infinite loop on parser error.

  • Loading branch information...
1 parent ae4a849 commit 998d526893e5ed5286bbf7df89c1270eb0216543 @munificent committed Jul 2, 2010
Showing with 31 additions and 3 deletions.
  1. +6 −0 src/Base/Queue.h
  2. +3 −2 src/Repl.cpp
  3. +1 −1 src/Syntax/FinchParser.h
  4. +7 −0 src/Syntax/Lexer.cpp
  5. +4 −0 src/Syntax/Lexer.h
  6. +5 −0 src/Syntax/Parser.cpp
  7. +5 −0 src/Syntax/Parser.h
View
@@ -27,6 +27,12 @@ namespace Finch
// Gets the maximum number of items the queue can hold.
int Capacity() const { return Size; }
+ // Clears the entire queue.
+ void Clear()
+ {
+ while (!IsEmpty()) Dequeue();
+ }
+
// Adds the given item to the head of the queue.
void Enqueue(const T & item)
{
View
@@ -37,7 +37,7 @@ namespace Finch
#ifdef HACK_ROOT_BASE_PATH
const char* baseLibPath = "base/main.fin";
#else
- const char* baseLibPath = "../../base/main.fin";
+ const char* baseLibPath = "../../base/main.fin";
#endif
Script::Execute(baseLibPath, interpreter);
@@ -49,7 +49,8 @@ namespace Finch
if (expr.IsNull())
{
- cout << "parse error" << endl;
+ lexer.Reset();
+ parser.Reset();
continue;
}
@@ -12,7 +12,7 @@ namespace Finch
class ILineReader;
// Parser for the Finch grammar.
- class FinchParser : private Parser
+ class FinchParser : public Parser
{
public:
FinchParser(ITokenSource & tokens)
@@ -6,6 +6,13 @@
namespace Finch
{
+ void Lexer::Reset()
+ {
+ mState = LEX_NEED_LINE;
+ mIndex = 0;
+ mTokenStart = 0;
+ }
+
Token Lexer::ReadToken()
{
Token token;
View
@@ -20,6 +20,10 @@ namespace Finch
mTokenStart(0)
{}
+ // Puts the lexer back into its starting state. Useful to get it back
+ // into a known state after a parse error.
+ void Reset();
+
// Lexes and returns the next full Token read from the source. If the
// ILineReader is out of lines, this will return an EOF Token.
virtual Token ReadToken();
@@ -6,6 +6,11 @@
namespace Finch
{
+ void Parser::Reset()
+ {
+ mRead.Clear();
+ }
+
bool Parser::LookAhead(TokenType type)
{
FillLookAhead(1);
View
@@ -12,6 +12,11 @@ namespace Finch
// Base class for a generic recursive descent parser.
class Parser
{
+ public:
+ // Discards any Tokens in the queue and puts the parser back into a
+ // blank state.
+ void Reset();
+
protected:
Parser(ITokenSource & tokens)
: mTokens(tokens)

0 comments on commit 998d526

Please sign in to comment.