Lexer: Added ::decomposeSubstitution and more flexible ::dequote

- ::dequote can now be given a string of valid quote characters, which defaults
  to '".
- ::decomposeSubstitution properly parses the /from/to/g construct allowing for
  escaped characters (\/).
- The 'g' at the end of a substitution is now considered to be a string of flag
  characters, which may contain 'g'. No other flag values are currently
  supported.
This commit is contained in:
Paul Beckingham 2015-07-11 16:40:52 -04:00
parent 1c1422370a
commit 1bef45ff47
2 changed files with 41 additions and 6 deletions

View file

@ -324,14 +324,14 @@ bool Lexer::isPunctuation (int c)
}
////////////////////////////////////////////////////////////////////////////////
void Lexer::dequote (std::string& input)
void Lexer::dequote (std::string& input, const std::string& quotes)
{
int quote = input[0];
size_t len = input.length ();
if ((quote == '\'' || quote == '"') &&
quote == input[len - 1])
if (quotes.find (quote) != std::string::npos)
{
input = input.substr (1, len - 2);
size_t len = input.length ();
if (input[0] == input[len - 1])
input = input.substr (1, len - 2);
}
}
@ -1403,3 +1403,37 @@ bool Lexer::decomposePair (
}
////////////////////////////////////////////////////////////////////////////////
// / <from> / <to> / [<flags>]
bool Lexer::decomposeSubstitution (
const std::string& text,
std::string& from,
std::string& to,
std::string& flags)
{
std::string parsed_from;
std::string::size_type cursor = 0;
if (readWord (text, "/", cursor, parsed_from) &&
parsed_from.length ())
{
--cursor;
std::string parsed_to;
if (readWord (text, "/", cursor, parsed_to))
{
std::string parsed_flags = text.substr (cursor);
if (parsed_flags.find ("/") == std::string::npos)
{
dequote (parsed_from, "/");
dequote (parsed_to, "/");
from = parsed_from;
to = parsed_to;
flags = parsed_flags;
return true;
}
}
}
return false;
}
////////////////////////////////////////////////////////////////////////////////

View file

@ -74,11 +74,12 @@ public:
static bool isPunctuation (int);
static bool isAllDigits (const std::string&);
static bool isOneWord (const std::string&);
static void dequote (std::string&);
static void dequote (std::string&, const std::string& quotes = "'\"");
static bool wasQuoted (const std::string&);
static bool readWord (const std::string&, const std::string&, std::string::size_type&, std::string&);
static bool readWord (const std::string&, std::string::size_type&, std::string&);
static bool decomposePair (const std::string&, std::string&, std::string&, std::string&, std::string&);
static bool decomposeSubstitution (const std::string&, std::string&, std::string&, std::string&);
static int hexToInt (int);
static int hexToInt (int, int);
static int hexToInt (int, int, int, int);