Main Page | Class Hierarchy | Class List | File List | Class Members | File Members

ApplyPattern Class Reference

#include <cmt_pattern.h>

Collaboration diagram for ApplyPattern:

Collaboration graph
[legend]
List of all members.

Public Types

typedef cmt_vector< ApplyPatternApplyPatternVector

Public Member Functions

 ApplyPattern ()
 ~ApplyPattern ()
void show () const
void apply_globals () const
void apply () const

Static Public Member Functions

void action (const CmtSystem::cmt_string_vector &words, Use *use)
ApplyPatternadd (const cmt_string &name, Use *use)

Public Attributes

cmt_string name
Useuse
cmt_vector< Templatereplacements

Member Typedef Documentation

typedef cmt_vector<ApplyPattern> ApplyPattern::ApplyPatternVector
 

Definition at line 112 of file cmt_pattern.h.


Constructor & Destructor Documentation

ApplyPattern::ApplyPattern  ) 
 

Definition at line 1020 of file cmt_pattern.cxx.

01021 {
01022 }

ApplyPattern::~ApplyPattern  ) 
 

Definition at line 1025 of file cmt_pattern.cxx.

01026 {
01027 }


Member Function Documentation

void ApplyPattern::action const CmtSystem::cmt_string_vector words,
Use use
[static]
 

Definition at line 804 of file cmt_pattern.cxx.

References cmt_vector< Template >::add(), add(), apply(), CmtSystem::cmt_string_vector, Use::current(), cmt_string::erase(), Symbol::expand(), cmt_string::find(), Pattern::find(), Use::get_package_name(), Cmt::get_quiet(), Pattern::line, Template::name, name, replacements, CmtError::set(), cmt_string::size(), cmt_vector< T >::size(), cmt_string::substr(), CmtSystem::testenv(), and Template::value.

Referenced by KwdApplyPattern::action().

00805 {
00806     //
00807     // Expected syntax is
00808     //
00809     // apply_pattern <pattern-name> [ <template>=<value> ... ]
00810     //   or just
00811     // <pattern-name> [ <template>=<value> ... ]
00812     //
00813 
00814   int first_word = 0;
00815 
00816   if (words[0] == "apply_pattern") first_word = 1;
00817   else first_word = 0;
00818 
00819   if (words.size () < (first_word + 1)) return;
00820 
00821   if (use == 0) use = &(Use::current());
00822 
00823   cmt_string name = words[first_word];
00824   Symbol::expand (name);
00825 
00826   if (name == "") return;
00827 
00828   Pattern* p = Pattern::find (name);
00829   if (p == 0) 
00830     {
00831       CmtError::set (CmtError::pattern_not_found, name);
00832       return;
00833     }
00834 
00835   ApplyPattern* apply_pattern = add (name, use);
00836 
00837   /*
00838     We then look for all <name>=<value> pairs
00839    */
00840   enum
00841     {
00842       need_template,
00843       need_equal,
00844       need_value,
00845       can_add,
00846       in_error
00847     } state = need_template;
00848 
00849   cmt_string tname;
00850   cmt_string tvalue;
00851 
00852   for (int i = (first_word + 1); i < words.size (); i++)
00853     {
00854       cmt_string s = words[i];
00855 
00856       if (CmtSystem::testenv ("CMTTESTPATTERN"))
00857         {
00858           cout << "ApplyPattern::action> " << name << " s=[" << s << "] state=" << state << endl;
00859         }
00860 
00861       int pos = cmt_string::npos;
00862 
00863       switch (state)
00864         {
00865         case need_template:
00866           pos = s.find ("=");
00867 
00868           tname = s;
00869           tvalue = "";
00870 
00871           if (pos == cmt_string::npos)
00872             {
00873               state = need_equal;
00874             }
00875           else
00876             {
00877               s.substr (0, pos, tname);
00878               s.substr (pos + 1, tvalue);
00879 
00880               if (CmtSystem::testenv ("CMTTESTPATTERN"))
00881                 {
00882                   cout << "ApplyPattern::action-1> n=[" << tname << "] v=[" << tvalue << "]" << endl;
00883                 }
00884               
00885               if (tvalue == "")
00886                 {
00887                   state = need_value;
00888                 }
00889               else
00890                 {                 
00891                   state = can_add;
00892                 }
00893             }
00894           break;
00895         case need_equal:
00896           pos = s.find ("=");
00897 
00898           tvalue = "";
00899 
00900           if (pos != 0)
00901             {
00902               state = in_error;
00903               if (!Cmt::get_quiet ())
00904                 {
00905                   cerr << "#CMT> Warning: bad syntax in apply_pattern " << name
00906                        << " (missing '=' separator)";
00907 
00908                   if (use != 0) cerr << " (from " << use->get_package_name () << ")";
00909 
00910                   cerr << endl;
00911                 }
00912               break;
00913             }
00914           else
00915             {
00916               s.substr (pos + 1, tvalue);
00917 
00918               if (tvalue == "")
00919                 {
00920                   state = need_value;
00921                 }
00922               else
00923                 {                 
00924                   state = can_add;
00925                 }
00926             }
00927           break;
00928         case need_value:
00929 
00930           pos = s.find ("=");
00931 
00932           if (pos == cmt_string::npos)
00933             {
00934               tvalue = s;
00935               state = can_add;
00936             }
00937           else
00938             {
00939               tname = s;
00940               tvalue = "";
00941 
00942               s.substr (0, pos, tname);
00943               s.substr (pos + 1, tvalue);
00944 
00945               if (CmtSystem::testenv ("CMTTESTPATTERN"))
00946                 {
00947                   cout << "ApplyPattern::action-2> n=[" << tname << "] v=[" << tvalue << "]" << endl;
00948                 }
00949               
00950               if (tvalue == "")
00951                 {
00952                   state = need_value;
00953                 }
00954               else
00955                 {                 
00956                   state = can_add;
00957                 }
00958             }
00959 
00960           break;
00961         }
00962 
00963       if (state == can_add)
00964         {
00965           state = need_template;
00966 
00967               if (CmtSystem::testenv ("CMTTESTPATTERN"))
00968                 {
00969                   cout << "ApplyPattern::action-3> n=[" << tname << "] v=[" << tvalue << "]" << endl;
00970                 }
00971 
00972           cmt_string tsearch = "<";
00973           tsearch += tname;
00974           tsearch += ">";
00975 
00976           if (p->line.find (tsearch) == cmt_string::npos)
00977             {
00978               if (!Cmt::get_quiet ())
00979                 {
00980                   cerr << "#CMT> Warning: template <" << tname << "> not expected in pattern " << name;
00981                   if (use != 0) cerr << " (from " << use->get_package_name () << ")";
00982                   cerr << endl;
00983                 }
00984             }
00985 
00986           Template& t = apply_pattern->replacements.add ();
00987 
00988           t.name = tname;
00989           t.value = tvalue;
00990 
00991           int size = t.value.size ();
00992 
00993           if (size >= 2)
00994             {
00995               if (((t.value[0] == '"') && (t.value[size - 1] == '"')) ||
00996                   ((t.value[0] == '\'') && (t.value[size - 1] == '\'')))
00997                 {
00998                   t.value.erase (size - 1);
00999                   t.value.erase (0, 1);
01000                 }
01001             }
01002         }
01003     }
01004 
01005   apply_pattern->apply ();
01006 }

ApplyPattern * ApplyPattern::add const cmt_string name,
Use use
[static]
 

Definition at line 1009 of file cmt_pattern.cxx.

References cmt_vector< T >::add(), Use::apply_patterns, name, and use.

Referenced by action().

01010 {
01011   ApplyPattern& a = use->apply_patterns.add ();
01012 
01013   a.name = name;
01014   a.use  = use;
01015 
01016   return (&a);
01017 }

void ApplyPattern::apply  )  const
 

Definition at line 1050 of file cmt_pattern.cxx.

References Pattern::apply(), Use::current(), Pattern::find(), Pattern::global, name, replacements, CmtSystem::testenv(), and use.

Referenced by action().

01051 {
01052   if (CmtSystem::testenv ("CMTTESTPATTERN"))
01053     {
01054       cout << "ApplyPattern::apply> " << name << endl;
01055     }
01056 
01057   Pattern* p = Pattern::find (name);
01058   if (p == 0) 
01059     {
01060       if (CmtSystem::testenv ("CMTTESTPATTERN"))
01061         {
01062           cout << "ApplyPattern::apply> " << name << " not found" << endl;
01063         }
01064 
01065       return;
01066     }
01067 
01068   if (p->global) return;
01069   
01070   Use* u = use;
01071   if (u == 0) u = &(Use::current ());
01072 
01073   p->apply (u, replacements);
01074 }

void ApplyPattern::apply_globals  )  const
 

void ApplyPattern::show  )  const
 

Definition at line 1030 of file cmt_pattern.cxx.

References Use::current(), Pattern::expand(), Pattern::find(), name, cmt_string::replace_all(), replacements, and use.

Referenced by Pattern::show(), and Pattern::show_all_applied_patterns().

01031 {
01032   cmt_string replacement = "";
01033 
01034   Pattern* p = Pattern::find (name);
01035   if (p == 0) return;
01036 
01037   Use* u = use;
01038   if (u == 0) u = &(Use::current ());
01039 
01040   p->expand (u, replacements, replacement);
01041 
01042   if (replacement != "")
01043     {
01044       replacement.replace_all ("\"", "");
01045       cout << replacement;
01046     }
01047 }


Member Data Documentation

cmt_string ApplyPattern::name
 

Definition at line 127 of file cmt_pattern.h.

Referenced by action(), add(), apply(), show(), Pattern::show(), and Pattern::show_all_applied_patterns().

cmt_vector<Template> ApplyPattern::replacements
 

Definition at line 129 of file cmt_pattern.h.

Referenced by action(), apply(), and show().

Use* ApplyPattern::use
 

Definition at line 128 of file cmt_pattern.h.

Referenced by add(), apply(), and show().


The documentation for this class was generated from the following files:
Generated on Mon May 2 10:25:26 2005 for CMT by doxygen 1.3.5