Lexer: readWords for quoted strings now retains the quotes

This commit is contained in:
Paul Beckingham 2015-07-08 09:03:48 -04:00
parent 1fed8c55f1
commit e6c4f48a48
2 changed files with 15 additions and 14 deletions

View file

@ -1237,26 +1237,27 @@ bool Lexer::isOneWord (const std::string& text)
// "'" // "'"
// "\"" // "\""
// 'one two' // 'one two'
// Result includes the quotes.
bool Lexer::readWord ( bool Lexer::readWord (
const std::string& text, const std::string& text,
const std::string& quotes, const std::string& quotes,
std::string::size_type& cursor, std::string::size_type& cursor,
std::string& word) std::string& word)
{ {
if (quotes.find (text[cursor]) == std::string::npos)
return false;
std::string::size_type eos = text.length (); std::string::size_type eos = text.length ();
int quote = text[cursor++];
word = quote;
int quote = 0;
if (quotes.find (text[cursor]) != std::string::npos)
quote = text[cursor++];
word = "";
int c; int c;
while ((c = text[cursor])) while ((c = text[cursor]))
{ {
// Quoted word ends on a quote. // Quoted word ends on a quote.
if (quote && quote == c) if (quote && quote == c)
{ {
++cursor; word += utf8_character (utf8_next_char (text, cursor));
break; break;
} }

View file

@ -37,7 +37,7 @@ Context context;
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
int main (int argc, char** argv) int main (int argc, char** argv)
{ {
UnitTest t (1061); UnitTest t (1067);
std::vector <std::pair <std::string, Lexer::Type>> tokens; std::vector <std::pair <std::string, Lexer::Type>> tokens;
std::string token; std::string token;
@ -215,7 +215,7 @@ int main (int argc, char** argv)
std::string::size_type cursor = 0; std::string::size_type cursor = 0;
std::string word; std::string word;
t.ok (Lexer::readWord ("'one two'", "'\"", cursor, word), "readWord ''one two'' --> true"); t.ok (Lexer::readWord ("'one two'", "'\"", cursor, word), "readWord ''one two'' --> true");
t.is (word, "one two", " word '" + word + "'"); t.is (word, "'one two'", " word '" + word + "'");
t.is ((int)cursor, 9, " cursor"); t.is ((int)cursor, 9, " cursor");
// static bool readWord (const std::string&, std::string::size_type&, std::string&); // static bool readWord (const std::string&, std::string::size_type&, std::string&);
@ -241,12 +241,12 @@ int main (int argc, char** argv)
std::string text = "one 'two' three\\ four"; std::string text = "one 'two' three\\ four";
cursor = 0; cursor = 0;
while (Lexer::readWord (text, cursor, word)) t.ok (Lexer::readWord (text, cursor, word), "readWord \"one 'two' three\\ four\" --> true");
{ t.is (word, "one", " word '" + word + "'");
t.diag ("'" + word + "'"); t.ok (Lexer::readWord (text, cursor, word), "readWord \"one 'two' three\\ four\" --> true");
while (Lexer::isWhitespace(text[cursor])) t.is (word, "'two'", " word '" + word + "'");
++cursor; t.ok (Lexer::readWord (text, cursor, word), "readWord \"one 'two' three\\ four\" --> true");
} t.is (word, "three four", " word '" + word + "'");
// Test all Lexer types. // Test all Lexer types.
#define NO {"",Lexer::Type::word} #define NO {"",Lexer::Type::word}