Give byte offset on regex parse errors

This commit is contained in:
Robin Jadoul 2016-05-30 17:08:30 +02:00
parent e425e9ff81
commit 652ee18461
3 changed files with 13 additions and 12 deletions

View File

@ -3,6 +3,7 @@
#define RE_H
#include "Lexesis/automata.h"
#include "RegexLexer.h"
#include <memory>
#include <stdexcept>
@ -154,7 +155,7 @@ namespace lxs {
class SyntaxError : public std::runtime_error
{
public:
SyntaxError(const char* w) : std::runtime_error(w) {}
SyntaxError(RegexLexer& lex, const std::string w) : std::runtime_error((std::to_string(lex.getByteOffset()) + ": " + w)) {}
};
} //namespace lxs

View File

@ -1,5 +1,5 @@
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/config.h.in ${CMAKE_CURRENT_BINARY_DIR}/config.h @ONLY)
include_directories(${CMAKE_CURRENT_BINARY_DIR})
include_directories(${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR})
add_library(Lexesis-backends
backends/cpp.cpp

View File

@ -270,7 +270,7 @@ namespace lxs {
case RegexLexer::STAR:
if (stk.empty())
throw SyntaxError("Cannot apply kleene star to empty regex");
throw SyntaxError(lex, "Cannot apply kleene star to empty regex");
n = std::make_shared<StarRE>(stk.top());
stk.pop();
stk.push(n);
@ -278,7 +278,7 @@ namespace lxs {
case RegexLexer::PLUS:
if (stk.empty())
throw SyntaxError("Cannot apply kleene plus to empty regex");
throw SyntaxError(lex, "Cannot apply kleene plus to empty regex");
n = stk.top();
stk.pop();
n = std::make_shared<ConcatRE>(n, std::make_shared<StarRE>(n));
@ -287,7 +287,7 @@ namespace lxs {
case RegexLexer::QUESTIONMARK:
if (stk.empty())
throw SyntaxError("Cannot apply '?' to empty regex");
throw SyntaxError(lex, "Cannot apply '?' to empty regex");
n = std::make_shared<PlusRE>(stk.top(), std::make_shared<EpsilonRE>());
stk.pop();
stk.push(n);
@ -295,7 +295,7 @@ namespace lxs {
case RegexLexer::PIPE:
if (stk.empty())
throw SyntaxError("Invalid regex: nothing to the left of '|'");
throw SyntaxError(lex, "Invalid regex: nothing to the left of '|'");
if (stk.size() > 1)
compactStack(stk), compress(stk);
n = std::make_shared<PlusRE>(stk.top(), parseRE(lex, exit_by_closed_paren, inside_parens));
@ -307,28 +307,28 @@ namespace lxs {
else if (stk.size() == 2)
return compress(stk), stk.top();
else
throw SyntaxError("Invalid regex");
throw SyntaxError(lex, "Invalid regex");
}
break;
case RegexLexer::LPAREN:
n = parseRE(lex, exit_by_closed_paren, true);
if (!exit_by_closed_paren) {
throw SyntaxError("Unclosed parenthesis");
throw SyntaxError(lex, "Unclosed parenthesis");
}
stk.push(n);
break;
case RegexLexer::RPAREN:
if (!inside_parens)
throw SyntaxError("Unopened parenthesis");
throw SyntaxError(lex, "Unopened parenthesis");
exit_by_closed_paren = true;
if (stk.size() == 1)
return stk.top();
else if (stk.size() == 2)
return compress(stk), stk.top();
throw SyntaxError("Could not parse regex, nothing inside parentheses");
throw SyntaxError(lex, "Could not parse regex, nothing inside parentheses");
case RegexLexer::CHAR:
stk.push(std::make_shared<SingleRE>(tok.content[0]));
@ -339,7 +339,7 @@ namespace lxs {
break;
case RegexLexer::ERROR:
throw SyntaxError((std::to_string(lex.getByteOffset()) + ": Error on character: " + tok.content).c_str());
throw SyntaxError(lex, "Error on character: " + tok.content);
case RegexLexer::ignore: case RegexLexer::nonmatching:
//Just ignore these
@ -352,7 +352,7 @@ namespace lxs {
return stk.top();
else if (stk.size() == 2)
return compress(stk), stk.top();
throw SyntaxError("Could not parse regex");
throw SyntaxError(lex, "Could not parse regex");
}
}