From 1b19414178804361826fbd2ba645087c8d09eea6 Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Mon, 26 May 2014 15:05:56 -0400 Subject: [PATCH] Parser - ::findCommand now checks for any abbreviated command. If it finds either an exact match, or no ambiguity, it checks again to determine READCMD, WRITECMD or HELPER. --- src/Context.cpp | 2 ++ src/Parser.cpp | 50 ++++++++++++++++++++++++++++++------------------- src/Parser.h | 1 + 3 files changed, 34 insertions(+), 19 deletions(-) diff --git a/src/Context.cpp b/src/Context.cpp index d1bc7d0d4..80041dc43 100644 --- a/src/Context.cpp +++ b/src/Context.cpp @@ -156,6 +156,8 @@ int Context::initialize (int argc, const char** argv) std::map ::iterator cmd; for (cmd = commands.begin (); cmd != commands.end (); ++cmd) { + parser.entity ("cmd", cmd->first); + if (cmd->first[0] == '_') parser.entity ("helper", cmd->first); else if (cmd->second->read_only ()) diff --git a/src/Parser.cpp b/src/Parser.cpp index 9d10e70e8..53ecbce08 100644 --- a/src/Parser.cpp +++ b/src/Parser.cpp @@ -165,6 +165,29 @@ void Parser::entity (const std::string& name, const std::string& value) _entities.insert (std::pair (name, value)); } +//////////////////////////////////////////////////////////////////////////////// +// Search for exact 'value' in _entities category. +bool Parser::exactMatch ( + const std::string& category, + const std::string& value) const +{ + // Find the category. + std::pair ::const_iterator, std::multimap ::const_iterator> c; + c = _entities.equal_range (category); + + // Extract a list of entities for category. + std::vector options; + std::multimap ::const_iterator e; + for (e = c.first; e != c.second; ++e) + { + // Shortcut: if an exact match is found, success. + if (value == e->second) + return true; + } + + return false; +} + //////////////////////////////////////////////////////////////////////////////// // Search for 'value' in _entities category, return canonicalized value. bool Parser::canonicalize ( @@ -278,31 +301,20 @@ void Parser::findCommand () if (! (*i)->hasTag ("?")) continue; - if (canonicalize (command, "writecmd", (*i)->attribute ("raw"))) + if (canonicalize (command, "cmd", (*i)->attribute ("raw"))) { (*i)->unTag ("?"); (*i)->tag ("CMD"); - (*i)->tag ("WRITECMD"); (*i)->attribute ("canonical", command); - break; - } - else if (canonicalize (command, "readcmd", (*i)->attribute ("raw"))) - { - (*i)->unTag ("?"); - (*i)->tag ("CMD"); - (*i)->tag ("READCMD"); - (*i)->attribute ("canonical", command); - break; - } + if (exactMatch ("writecmd", (*i)->attribute ("raw"))) + (*i)->tag ("WRITECMD"); + else if (exactMatch ("writecmd", (*i)->attribute ("raw"))) + (*i)->tag ("READCMD"); + else if (exactMatch ("writecmd", (*i)->attribute ("raw"))) + (*i)->tag ("HELPER"); - else if (canonicalize (command, "helper", (*i)->attribute ("raw"))) - { - (*i)->unTag ("?"); - (*i)->tag ("CMD"); - (*i)->tag ("HELPER"); - (*i)->attribute ("canonical", command); - break; + return; } } } diff --git a/src/Parser.h b/src/Parser.h index 02b670183..0f8440fbe 100644 --- a/src/Parser.h +++ b/src/Parser.h @@ -43,6 +43,7 @@ public: Tree* tree (); Tree* parse (); void entity (const std::string&, const std::string&); + bool exactMatch (const std::string&, const std::string&) const; bool canonicalize (std::string&, const std::string&, const std::string&) const; void findBinary ();