? |
Definition at line 804 of file cmt_pattern.cxx.
References cmt_vector< Template >::add(), add(), apply(), Use::current(), cmt_string::erase(), Symbol::expand(), cmt_string::find(), Pattern::find(), Use::get_package_name(), Cmt::get_quiet(), Pattern::line, Template::name, name, cmt_string::npos, CmtError::pattern_not_found, replacements, CmtError::set(), cmt_string::size(), cmt_vector< cmt_string >::size(), cmt_string::substr(), CmtSystem::testenv(), and Template::value.
Referenced by KwdApplyPattern::action().
00805 {
00806
00807
00808
00809
00810
00811
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
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 }
|