Regex parsing: add escape characters

This commit is contained in:
Robin Jadoul 2016-04-25 10:43:06 +02:00
parent efb88f2519
commit 90ff409281
1 changed files with 50 additions and 1 deletions

View File

@ -119,6 +119,44 @@ namespace lxs {
stk.push(tp);
}
std::shared_ptr<RE> parseEscapeChar(char c) {
switch (c)
{
case '\\':
case '*':
case '+':
case '|':
case '(':
case ')':
case '[':
case ']':
case '?':
case '.':
case '\'':
case '"':
break;
case 'n':
c = '\n'; break;
case 'r':
c = '\r'; break;
case 'b':
c = '\b'; break;
case 't':
c = '\t'; break;
case 's':
c = ' '; break;
case 'a':
c = '\a'; break;
case 'f':
c = '\f'; break;
case 'v':
c = '\v'; break;
default:
throw SyntaxError(("Invalid escape sequence: \\" + std::string(1, c)).c_str());
}
return std::make_shared<SingleRE>(c);
}
std::shared_ptr<RE> parseRE(string& input, size_t& idx)
{
stack<std::shared_ptr<RE> > stk;
@ -131,9 +169,20 @@ namespace lxs {
idx++;
if (idx >= input.length())
throw SyntaxError("Escape sequence at the end of the string");
//TODO: escape chars
else
throw SyntaxError(("invalid escape sequence: \\" + string(1, input[idx])).c_str());
stk.push(parseEscapeChar(input[idx++]));
break;
case '[':
//TODO: parse character classes
break;
case '.':
//TODO: any character
case ']':
throw SyntaxError("Unopened ']'");
break;
case '*':