summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFelipe Magno de Almeida <felipe@expertisesolutions.com.br>2016-01-17 14:09:55 -0200
committerFelipe Magno de Almeida <felipe@expertisesolutions.com.br>2016-01-17 14:09:55 -0200
commit05e204c96b3328dc457da6acd64cbc11e8c2fb34 (patch)
tree0049181f0f734c99c2447fbca2230ac08b50f479
parent23c7900eec27e9b873f09d5071fd27b8f4eb8495 (diff)
eolian-js: Modified formating to replace verb position in method namesdevs/felipealmeida/js
Formatting now checks if the last function is a known verb and changes its position to the first word of the method if it is. Making the method use the same coding standard as node.js.
-rw-r--r--src/bin/eolian_js/eolian/js/format.hh176
-rw-r--r--src/bin/eolian_js/main.cc24
2 files changed, 173 insertions, 27 deletions
diff --git a/src/bin/eolian_js/eolian/js/format.hh b/src/bin/eolian_js/eolian/js/format.hh
index a07d541e14..5e940d12ba 100644
--- a/src/bin/eolian_js/eolian/js/format.hh
+++ b/src/bin/eolian_js/eolian/js/format.hh
@@ -11,24 +11,170 @@ namespace eolian { namespace js {
11 11
12namespace format { 12namespace format {
13 13
14std::string generic(std::string const& in) 14const char* verbs[] =
15 {
16 "add",
17 "get",
18 "is",
19 "del",
20 "thaw",
21 "freeze",
22 "save",
23 "wait",
24 "eject",
25 "raise",
26 "lower",
27 "load",
28 "dup",
29 "reset",
30 "unload",
31 "close",
32 "set",
33 "interpolate",
34 "has",
35 "grab",
36 "check",
37 "find",
38 "ungrab",
39 "unset",
40 "clear",
41 "pop",
42 "new",
43 "peek",
44 "push",
45 "update",
46 "show",
47 "move",
48 "hide",
49 "calculate",
50 "resize",
51 "attach",
52 "pack",
53 "unpack"
54 };
55
56const char* not_verbs[] =
57 {
58 "below",
59 "above",
60 "name",
61 "unfreezable",
62 "value",
63 "r",
64 "g",
65 "b",
66 "a",
67 "finalize",
68 "destructor",
69 "to",
70 "circle",
71 "rect",
72 "path",
73 "commands",
74 "type",
75 "colorspace"
76 "op",
77 "type",
78 "properties",
79 "status",
80 "status",
81 "relative",
82 "ptr",
83 "pair",
84 "pos"
85 };
86
87std::string format_method(std::string const& in)
15{ 88{
16 std::string s = in; 89 std::string r;
17 auto i = s.find('_'); 90 std::string::const_iterator current = in.begin(), last = in.end();
18 while (i != std::string::npos) 91 do
19 { 92 {
20 if (i <= 0 || i+1 >= s.size() || 93 std::string::const_iterator word_end = std::find(current, last, '_');
21 !::isalnum(s[i-1]) || !::isalnum(s[i+1])) 94 if(word_end == last)
22 { 95 {
23 EINA_CXX_DOM_LOG_WARN(eolian::js::domain) << "Entity '" << in 96 bool found_verb = false, found_not_verb = false;
24 << "' can't be conveniently converted to a JavaScript name."; 97 std::string v(current, word_end);
25 return in; 98 v[0] = std::toupper(v[0]);
26 } 99 for(const char** verb = &format::verbs[0]; verb != &format::verbs
27 s[i+1] = static_cast<char>(::toupper(s[i+1])); 100 [sizeof(format::verbs)/sizeof(format::verbs[0])]; ++verb)
28 s.erase(i, 1); 101 {
29 i = s.find('_', i); 102 if(!std::lexicographical_compare
103 (current, word_end, *verb, *verb + std::strlen(*verb))
104 && !std::lexicographical_compare
105 (*verb, *verb + std::strlen(*verb), current, word_end))
106 {
107 found_verb = true;
108 }
109 }
110 if(!found_verb)
111 {
112 for(const char** not_verb = &format::not_verbs[0]; not_verb != &format::not_verbs
113 [sizeof(format::not_verbs)/sizeof(format::not_verbs[0])]; ++not_verb)
114 {
115 if(!std::lexicographical_compare
116 (current, word_end, *not_verb, *not_verb + std::strlen(*not_verb))
117 && !std::lexicographical_compare
118 (*not_verb, *not_verb + std::strlen(*not_verb), current, word_end))
119 {
120 found_not_verb = true;
121 }
122 }
123 if(!found_not_verb)
124 EINA_CXX_DOM_LOG_WARN(eolian::js::domain)
125 << "Last word is NOT a not-verb " << v << std::endl;
126 }
127 if(found_verb || !found_not_verb)
128 r = v + r;
129 else
130 r += v;
131 current = last;
132 }
133 else
134 {
135 r += std::toupper(*current++);
136 std::copy(current, word_end, std::back_inserter(r));
137 current = word_end + 1;
138 }
30 } 139 }
31 return s; 140 while(current != last);
141
142 EINA_CXX_DOM_LOG_DBG(eolian::js::domain)
143 << "Formatted method " << r << " with input " << in << std::endl;
144
145 return r;
146}
147
148std::string format_field(std::string const& in)
149{
150 return format_method(in);
151}
152
153std::string format_class(std::string const& in)
154{
155 std::string r;
156 std::string::const_iterator current = in.begin(), last = in.end();
157 std::copy_if(current, last, std::back_insert_iterator<std::string>(r),
158 [] (char c)
159 {
160 return c != '_';
161 });
162 return r;
163}
164
165std::string format_namespace(std::string const& in)
166{
167 return format_class(in);
168}
169
170std::string format_struct(std::string const& in)
171{
172 return format_class(in);
173}
174
175std::string format_enum(std::string const& in)
176{
177 return format_class(in);
32} 178}
33 179
34std::string constant(std::string in) 180std::string constant(std::string in)
diff --git a/src/bin/eolian_js/main.cc b/src/bin/eolian_js/main.cc
index bc575fdf8e..b144a45ca4 100644
--- a/src/bin/eolian_js/main.cc
+++ b/src/bin/eolian_js/main.cc
@@ -647,7 +647,7 @@ int main(int argc, char** argv)
647 { 647 {
648 k = "::efl::eina::js::nonclass_cls_name_getter"; 648 k = "::efl::eina::js::nonclass_cls_name_getter";
649 } 649 }
650 ss << " prototype_->SetAccessor(::efl::eina::js::compatibility_new<v8::String>(isolate_, \"" << format::generic(field_name) << "\"),\n"; 650 ss << " prototype_->SetAccessor(::efl::eina::js::compatibility_new<v8::String>(isolate_, \"" << format::format_field(field_name) << "\"),\n";
651 ss << " static_cast<v8::AccessorGetterCallback>(&::efl::eo::js::get_struct_member<" << struct_c_name << ", decltype(" << member_ref << "), &" << member_ref << ", " << k << ">),\n"; 651 ss << " static_cast<v8::AccessorGetterCallback>(&::efl::eo::js::get_struct_member<" << struct_c_name << ", decltype(" << member_ref << "), &" << member_ref << ", " << k << ">),\n";
652 ss << " static_cast<v8::AccessorSetterCallback>(&::efl::eo::js::set_struct_member<" << struct_c_name << ", " << field_type_tag_name << ", decltype(" << member_ref << "), &" << member_ref << ", " << k << ">));\n"; 652 ss << " static_cast<v8::AccessorSetterCallback>(&::efl::eo::js::set_struct_member<" << struct_c_name << ", " << field_type_tag_name << ", decltype(" << member_ref << "), &" << member_ref << ", " << k << ">));\n";
653 } 653 }
@@ -659,11 +659,11 @@ int main(int argc, char** argv)
659 if (comma) 659 if (comma)
660 ss << ", "; 660 ss << ", ";
661 comma = true; 661 comma = true;
662 ss << '"' << format::generic(&*ns_it) << '"'; 662 ss << '"' << format::format_namespace(&*ns_it) << '"';
663 } 663 }
664 ss << "}, isolate, global);\n"; 664 ss << "}, isolate, global);\n";
665 ss << " ::efl::eo::js::register_struct<" << struct_c_name << ">(isolate, \"" 665 ss << " ::efl::eo::js::register_struct<" << struct_c_name << ">(isolate, \""
666 << format::generic(struct_name) << "\", \"" << struct_type_full_name << "\", to_export, fields_func);\n"; 666 << format::format_struct(struct_name) << "\", \"" << struct_type_full_name << "\", to_export, fields_func);\n";
667 ss << " }\n"; 667 ss << " }\n";
668 668
669 structs_ss << ss.str(); 669 structs_ss << ss.str();
@@ -717,10 +717,10 @@ int main(int argc, char** argv)
717 member_name = eolian_function_name_get(function); 717 member_name = eolian_function_name_get(function);
718 break; 718 break;
719 case EOLIAN_PROP_SET: 719 case EOLIAN_PROP_SET:
720 member_name = std::string("set_") + eolian_function_name_get(function); 720 member_name = eolian_function_name_get(function) + std::string("_set");
721 break; 721 break;
722 case EOLIAN_PROP_GET: 722 case EOLIAN_PROP_GET:
723 member_name = std::string("get_") + eolian_function_name_get(function); 723 member_name = eolian_function_name_get(function) + std::string("_get");
724 break; 724 break;
725 case EOLIAN_PROPERTY: 725 case EOLIAN_PROPERTY:
726 EINA_CXX_DOM_LOG_ERR(eolian::js::domain) << "EOLIAN_PROPERTY function type is invalid at this point"; 726 EINA_CXX_DOM_LOG_ERR(eolian::js::domain) << "EOLIAN_PROPERTY function type is invalid at this point";
@@ -739,7 +739,7 @@ int main(int argc, char** argv)
739 { 739 {
740 if(! ::eolian_function_is_constructor(function, klass)) 740 if(! ::eolian_function_is_constructor(function, klass))
741 ss << " prototype->Set( ::efl::eina::js::compatibility_new<v8::String>(isolate, \"" 741 ss << " prototype->Set( ::efl::eina::js::compatibility_new<v8::String>(isolate, \""
742 << format::generic(name) << "\")\n" 742 << format::format_method(name) << "\")\n"
743 << " , ::efl::eina::js::compatibility_new<v8::FunctionTemplate>(isolate, &efl::eo::js::call_function\n" 743 << " , ::efl::eina::js::compatibility_new<v8::FunctionTemplate>(isolate, &efl::eo::js::call_function\n"
744 << " , efl::eo::js::call_function_data<\n" 744 << " , efl::eo::js::call_function_data<\n"
745 << " ::efl::eina::_mpl::tuple_c<std::size_t"; 745 << " ::efl::eina::_mpl::tuple_c<std::size_t";
@@ -1005,7 +1005,7 @@ int main(int argc, char** argv)
1005 os << " register_" << lower_case_class_name << "_from_constructor(isolate, constructor, &constructor_from_eo);\n"; 1005 os << " register_" << lower_case_class_name << "_from_constructor(isolate, constructor, &constructor_from_eo);\n";
1006 1006
1007 os << " constructor->SetClassName( ::efl::eina::js::compatibility_new<v8::String>(isolate, \"" 1007 os << " constructor->SetClassName( ::efl::eina::js::compatibility_new<v8::String>(isolate, \""
1008 << format::generic(class_name) 1008 << format::format_class(class_name)
1009 << "\"));\n"; 1009 << "\"));\n";
1010 1010
1011 os << " auto to_export = ::efl::eo::js::get_namespace({"; 1011 os << " auto to_export = ::efl::eo::js::get_namespace({";
@@ -1017,13 +1017,13 @@ int main(int argc, char** argv)
1017 if (comma) 1017 if (comma)
1018 os << ", "; 1018 os << ", ";
1019 comma = true; 1019 comma = true;
1020 os << '"' << format::generic(&*ns_it) << '"'; 1020 os << '"' << format::format_namespace(&*ns_it) << '"';
1021 } 1021 }
1022 } 1022 }
1023 os << "}, isolate, global);\n"; 1023 os << "}, isolate, global);\n";
1024 1024
1025 os << " to_export->Set( ::efl::eina::js::compatibility_new<v8::String>(isolate, \"" 1025 os << " to_export->Set( ::efl::eina::js::compatibility_new<v8::String>(isolate, \""
1026 << format::generic(class_name) << "\")" 1026 << format::format_class(class_name) << "\")"
1027 << ", constructor->GetFunction());\n"; 1027 << ", constructor->GetFunction());\n";
1028 1028
1029 1029
@@ -1031,7 +1031,7 @@ int main(int argc, char** argv)
1031 os << " v8::Handle<v8::FunctionTemplate> constructor = ::efl::eina::js::compatibility_new<v8::FunctionTemplate>\n"; 1031 os << " v8::Handle<v8::FunctionTemplate> constructor = ::efl::eina::js::compatibility_new<v8::FunctionTemplate>\n";
1032 os << " (isolate, &efl::eo::js::construct_from_eo);\n"; 1032 os << " (isolate, &efl::eo::js::construct_from_eo);\n";
1033 os << " constructor->SetClassName( ::efl::eina::js::compatibility_new<v8::String>(isolate, \"" 1033 os << " constructor->SetClassName( ::efl::eina::js::compatibility_new<v8::String>(isolate, \""
1034 << format::generic(class_name) 1034 << format::format_class(class_name)
1035 << "\"));\n"; 1035 << "\"));\n";
1036 os << " v8::Local<v8::ObjectTemplate> instance = " 1036 os << " v8::Local<v8::ObjectTemplate> instance = "
1037 << "register_" << lower_case_class_name << "_from_constructor(isolate, constructor, &constructor_from_eo);\n"; 1037 << "register_" << lower_case_class_name << "_from_constructor(isolate, constructor, &constructor_from_eo);\n";
@@ -1058,12 +1058,12 @@ int main(int argc, char** argv)
1058 if (comma) 1058 if (comma)
1059 os << ", "; 1059 os << ", ";
1060 comma = true; 1060 comma = true;
1061 os << '"' << format::generic(&*ns_it) << '"'; 1061 os << '"' << format::format_namespace(&*ns_it) << '"';
1062 } 1062 }
1063 os << "}, isolate, global);\n"; 1063 os << "}, isolate, global);\n";
1064 os << " v8::Handle<v8::Object> enum_obj = efl::eina::js::compatibility_new<v8::Object>(isolate);\n"; 1064 os << " v8::Handle<v8::Object> enum_obj = efl::eina::js::compatibility_new<v8::Object>(isolate);\n";
1065 os << " to_export->Set(efl::eina::js::compatibility_new<v8::String>(isolate, \"" 1065 os << " to_export->Set(efl::eina::js::compatibility_new<v8::String>(isolate, \""
1066 << format::generic(enum_name) << "\"), enum_obj);\n"; 1066 << format::format_enum(enum_name) << "\"), enum_obj);\n";
1067 for (efl::eina::iterator<Eolian_Enum_Type_Field> ef(::eolian_type_enum_fields_get(tp)) 1067 for (efl::eina::iterator<Eolian_Enum_Type_Field> ef(::eolian_type_enum_fields_get(tp))
1068 , ef_end; ef != ef_end; ++ef) 1068 , ef_end; ef != ef_end; ++ef)
1069 { 1069 {