Browse Source

Reader: Improve error reporting

master
Riyyi 2 years ago
parent
commit
9fa6314378
  1. 2
      src/lexer.h
  2. 54
      src/reader.cpp
  3. 2
      src/reader.h

2
src/lexer.h

@ -33,7 +33,7 @@ struct Token {
At, // @
String, // "foobar"
Keyword, // :keyword
Value, // numbers, "true", "false", and "nil", symbols
Value, // number, "true", "false", "nil", symbol
Comment, // ;
Error,
};

54
src/reader.cpp

@ -38,32 +38,18 @@ void Reader::read()
m_node = readImpl();
// TODO: Move these to the appropriate functions
// Error checking
if (m_invalid_syntax) {
Error::the().addError("invalid read syntax: '" + std::string(1, m_error_character) + "'");
return;
}
if (m_is_unbalanced) {
Error::the().addError("expected '" + std::string(1, m_error_character) + "', got EOF");
if (Error::the().hasOtherError()) {
return;
}
// Check for multiple expressions
if (!isEOF()) {
Token::Type type = peek().type;
switch (type) {
case Token::Type::ParenOpen: // (
case Token::Type::ParenClose: // )
case Token::Type::String:
case Token::Type::Value:
Error::the().addError("more than one sexp in input");
break;
case Token::Type::Comment:
break;
default:
Error::the().addError("unknown error");
Error::the().addError("more than one sexp in input");
break;
};
}
@ -79,28 +65,25 @@ ASTNode* Reader::readImpl()
case Token::Type::Special: // ~@
return readSpliceUnquote();
break;
case Token::Type::ParenOpen: // (
return readList();
break;
case Token::Type::ParenClose: // )
Error::the().addError("invalid read syntax: ')'");
return nullptr;
break;
case Token::Type::BracketOpen: // [
return readVector();
break;
case Token::Type::BracketClose: // ]
m_invalid_syntax = true;
m_error_character = ']';
Error::the().addError("invalid read syntax: ']'");
return nullptr;
break;
case Token::Type::BraceOpen: // {
return readHashMap();
break;
case Token::Type::BraceClose: // }
m_invalid_syntax = true;
m_error_character = '}';
return nullptr;
break;
case Token::Type::ParenOpen: // (
return readList();
break;
case Token::Type::ParenClose: // )
m_invalid_syntax = true;
m_error_character = ')';
Error::the().addError("invalid read syntax: '}'");
return nullptr;
break;
case Token::Type::Quote: // '
@ -166,8 +149,8 @@ ASTNode* Reader::readList()
}
if (!consumeSpecific(Token { .type = Token::Type::ParenClose })) { // )
m_error_character = ')';
m_is_unbalanced = true;
Error::the().addError("expected ')', got EOF");
return nullptr;
}
return list;
@ -183,8 +166,7 @@ ASTNode* Reader::readVector()
}
if (!consumeSpecific(Token { .type = Token::Type::BracketClose })) { // ]
m_error_character = ']';
m_is_unbalanced = true;
Error::the().addError("expected ']', got EOF");
}
return vector;
@ -218,8 +200,7 @@ ASTNode* Reader::readHashMap()
}
if (!consumeSpecific(Token { .type = Token::Type::BraceClose })) { // }
m_error_character = '}';
m_is_unbalanced = true;
Error::the().addError("expected '}', got EOF");
}
return hash_map;
@ -316,8 +297,7 @@ ASTNode* Reader::readString()
// Unbalanced string
if (symbol.size() < 2 || symbol.front() != '"' || symbol.back() != '"') {
m_error_character = '"';
m_is_unbalanced = true;
Error::the().addError("expected '\"', got EOF");
}
return new String(symbol);

2
src/reader.h

@ -46,7 +46,7 @@ private:
ASTNode* readDeref(); // @
ASTNode* readString(); // "foobar"
ASTNode* readKeyword(); // :keyword
ASTNode* readValue(); // true, false, nil
ASTNode* readValue(); // number, "true", "false", "nil", symbol
void dumpImpl(ASTNode* node);

Loading…
Cancel
Save