From 240c8378744d10753b3c75eafe12c1f45404ebce Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Sat, 2 Apr 2016 17:02:26 -0400 Subject: [PATCH] CLI: Added ::lexArguments --- src/CLI.cpp | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/CLI.h | 1 + 2 files changed, 66 insertions(+) diff --git a/src/CLI.cpp b/src/CLI.cpp index 99b44498..fd9efef2 100644 --- a/src/CLI.cpp +++ b/src/CLI.cpp @@ -149,6 +149,70 @@ void CLI::handleArg0 () _args.push_back (a); } +//////////////////////////////////////////////////////////////////////////////// +// All arguments must be individually and wholly recognized by the Lexer. Any +// argument not recognized is considered a Lexer::Type::word. +void CLI::lexArguments () +{ + // Note: Starts iterating at index 1, because ::handleArg0 has already + // processed it. + for (unsigned int i = 1; i < _original_args.size (); ++i) + { + bool quoted = Lexer::wasQuoted (_original_args[i].attribute ("raw")); + + std::string lexeme; + Lexer::Type type; + Lexer lex (_original_args[i].attribute ("raw")); + if (lex.token (lexeme, type) && + lex.isEOS ()) + { + A2 a (_original_args[i].attribute ("raw"), type); + if (quoted) + a.tag ("QUOTED"); + + if (_original_args[i].hasTag ("ORIGINAL")) + a.tag ("ORIGINAL"); + + _args.push_back (a); + } + else + { + std::string quote = "'"; + auto escaped = str_replace (_original_args[i].attribute ("raw"), quote, "\\'"); + + std::string::size_type cursor = 0; + std::string word; + if (Lexer::readWord (quote + escaped + quote, quote, cursor, word)) + { + Lexer::dequote (word); + A2 unknown (word, Lexer::Type::word); + if (Lexer::wasQuoted (_original_args[i].attribute ("raw"))) + unknown.tag ("QUOTED"); + + if (_original_args[i].hasTag ("ORIGINAL")) + unknown.tag ("ORIGINAL"); + + _args.push_back (unknown); + } + + // This branch may have no use-case. + else + { + A2 unknown (_original_args[i].attribute ("raw"), Lexer::Type::word); + unknown.tag ("UNKNOWN"); + + if (Lexer::wasQuoted (_original_args[i].attribute ("raw"))) + unknown.tag ("QUOTED"); + + if (_original_args[i].hasTag ("ORIGINAL")) + unknown.tag ("ORIGINAL"); + + _args.push_back (unknown); + } + } + } +} + //////////////////////////////////////////////////////////////////////////////// // Intended to be called after ::add() to perform the final analysis. void CLI::analyze () @@ -156,6 +220,7 @@ void CLI::analyze () // Process _original_args. _args.clear (); handleArg0 (); + lexArguments (); } //////////////////////////////////////////////////////////////////////////////// diff --git a/src/CLI.h b/src/CLI.h index 15ca6d21..17702d5a 100644 --- a/src/CLI.h +++ b/src/CLI.h @@ -64,6 +64,7 @@ public: private: void handleArg0 (); + void lexArguments (); public: std::multimap _entities {};