diff --git a/src/parser/A3t.cpp b/src/parser/A3t.cpp index 134a2641d..6ddcb7841 100644 --- a/src/parser/A3t.cpp +++ b/src/parser/A3t.cpp @@ -27,12 +27,24 @@ #include #include +#include +#include + +static const int minimumMatchLength = 3; //////////////////////////////////////////////////////////////////////////////// A3t::A3t (int argc, char** argv) { + _tree = new Tree ("root"); + if (! _tree) + throw std::string ("Failed to allocate memory for parse tree."); + for (int i = 0; i < argc; ++i) - _args.push_back (argv[i]); + { + Tree* branch = _tree->addBranch (new Tree (format ("arg{1}", i))); + branch->attribute ("raw", argv[i]); + branch->tag ("original"); + } } //////////////////////////////////////////////////////////////////////////////// @@ -43,6 +55,35 @@ A3t::~A3t () //////////////////////////////////////////////////////////////////////////////// Tree* A3t::parse () { - return NULL; + return _tree; } + +//////////////////////////////////////////////////////////////////////////////// +void A3t::identity (const std::string& name, const std::string& value) +{ + _entities.insert (std::pair (name, value)); +} + +//////////////////////////////////////////////////////////////////////////////// +// Search for 'value' in _entities, return category and canonicalized value. +bool A3t::canonicalize ( + std::string& canonicalized, + 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) + options.push_back (e->second); + + // Match against the options, throw away results. + std::vector matches; + return autoComplete (canonicalized, options, matches, minimumMatchLength) == 1 ? true : false; +} + //////////////////////////////////////////////////////////////////////////////// diff --git a/src/parser/A3t.h b/src/parser/A3t.h index 8add5050f..23dce99d0 100644 --- a/src/parser/A3t.h +++ b/src/parser/A3t.h @@ -27,9 +27,9 @@ #ifndef INCLUDED_A3T #define INCLUDED_A3T -#include -#include #include +#include +#include class A3t { @@ -37,9 +37,12 @@ public: A3t (int, char**); ~A3t (); Tree* parse (); + void identity (const std::string&, const std::string&); + bool canonicalize (std::string&, const std::string&, const std::string&) const; private: - std::vector _args; + Tree* _tree; + std::multimap _entities; }; #endif diff --git a/src/parser/args.cpp b/src/parser/args.cpp index fd3e8ec45..976649d97 100644 --- a/src/parser/args.cpp +++ b/src/parser/args.cpp @@ -39,6 +39,12 @@ int main (int argc, char** argv) try { A3t a3t (argc, argv); + + // Populate identity lists. + a3t.identity ("report", "list"); + a3t.identity ("readcmd", "info"); + a3t.identity ("writecmd", "modify"); + Tree* tree = a3t.parse (); if (tree) tree->dump (); diff --git a/src/parser/run b/src/parser/run index 2e2e252ce..190f620e0 100755 --- a/src/parser/run +++ b/src/parser/run @@ -1 +1,2 @@ -./parser $* grammar.bnf 123 mod pro:H +#./parser $* grammar.bnf 123 mod pro:H +./args 123 mod pro:H