summaryrefslogtreecommitdiff
path: root/src/lib/eolian_cxx
diff options
context:
space:
mode:
authorVitor Sousa <vitorsousa@expertisesolutions.com.br>2019-03-22 18:34:29 -0300
committerVitor Sousa <vitorsousa@expertisesolutions.com.br>2019-03-26 16:42:18 -0300
commit2b86e84b5660245b394b4ca1c02b73a8d1989265 (patch)
tree0768dcd23cdf2624356cfe316686ee7015c364ad /src/lib/eolian_cxx
parenta4ae92d8dd2d937f5242ed85d8a8fda4912e1a34 (diff)
eolian_cxx: rework context code, also more flexible scope indentation generator
Summary: Context management classes now store constant copies of the tags they are supposed to hold. So, they are immutable objects now. Functions that manipulate context create new object copies as needed. `constexpr` was added for class constructor and functions. Indentation generator now used four space by default. Also, the amount of desired spaces can now be specified at call site. Added helper methods to return 'indented' and 'unindented' version of a given indentation generator object. Test Plan: ninja test Reviewers: felipealmeida, lauromoura Reviewed By: lauromoura Subscribers: cedric, #reviewers, #committers Tags: #efl Differential Revision: https://phab.enlightenment.org/D8467
Diffstat (limited to '')
-rw-r--r--src/lib/eolian_cxx/grammar/context.hpp58
-rw-r--r--src/lib/eolian_cxx/grammar/indentation.hpp35
2 files changed, 65 insertions, 28 deletions
diff --git a/src/lib/eolian_cxx/grammar/context.hpp b/src/lib/eolian_cxx/grammar/context.hpp
index e4839200bc..6fb171ab66 100644
--- a/src/lib/eolian_cxx/grammar/context.hpp
+++ b/src/lib/eolian_cxx/grammar/context.hpp
@@ -5,33 +5,50 @@ namespace efl { namespace eolian { namespace grammar {
5 5
6struct context_null {}; 6struct context_null {};
7 7
8template <typename Tag, typename Tail = context_null> 8template <typename Tag, typename Tail = context_null>
9struct context_cons 9struct context_cons
10{ 10{
11 Tag tag; 11 typename std::remove_reference<Tag>::type const tag;
12 Tail const& tail; 12 typename std::remove_reference<Tail>::type const tail;
13};
14
15template <typename Tag>
16struct context_cons<Tag, context_null>
17{
18 Tag tag;
19 context_null tail;
20}; 13};
21 14
22template <typename NewTag, typename Tag, typename Tail> 15template <typename NewTag, typename Tag, typename Tail>
23context_cons<NewTag, context_cons<Tag, Tail>> 16constexpr context_cons<NewTag, context_cons<Tag, Tail>>
24context_add_tag(NewTag tag, context_cons<Tag, Tail> const& context) 17context_add_tag(NewTag const& tag, context_cons<Tag, Tail> const& context)
25{ 18{
26 return context_cons<NewTag, context_cons<Tag, Tail>>{tag, context}; 19 return context_cons<NewTag, context_cons<Tag, Tail>>{tag, context};
27} 20}
28template <typename NewTag> 21template <typename NewTag>
29context_cons<NewTag, context_null> 22constexpr context_cons<NewTag, context_null>
30context_add_tag(NewTag tag, context_null context) 23context_add_tag(NewTag const& tag, context_null context)
31{ 24{
32 return context_cons<NewTag, context_null>{tag, context}; 25 return context_cons<NewTag, context_null>{tag, context};
33} 26}
34 27
28template <typename Tag, typename SameTag, typename Tail>
29constexpr context_cons<SameTag, Tail>
30context_replace_tag(Tag const& tag, context_cons<SameTag, Tail> const& context
31 , typename std::enable_if<std::is_same<Tag, SameTag>::value>::type* = nullptr)
32{
33 return {tag, context.tail};
34}
35
36template <typename Tag, typename OtherTag, typename Tail>
37constexpr context_cons<OtherTag, Tail>
38context_replace_tag(Tag const& tag, context_cons<OtherTag, Tail> const& context
39 , typename std::enable_if<!std::is_same<Tag, OtherTag>::value>::type* = nullptr)
40{
41 return {context.tag, context_replace_tag(tag, context.tail)};
42}
43
44template <typename Tag>
45constexpr context_null
46context_replace_tag(Tag const& tag, context_null const&)
47{
48 static_assert(std::is_same<Tag, context_null>::value, "Tag type not available in this generation context");
49 return tag;
50}
51
35template <typename Tag, typename Context> 52template <typename Tag, typename Context>
36struct tag_check; 53struct tag_check;
37template <typename Tag, typename Tail> 54template <typename Tag, typename Tail>
@@ -42,25 +59,26 @@ template <typename Tag, typename OtherTag, typename Context>
42struct tag_check<Tag, context_cons<OtherTag, Context>> : tag_check<Tag, Context> {}; 59struct tag_check<Tag, context_cons<OtherTag, Context>> : tag_check<Tag, Context> {};
43 60
44template <typename Tag, typename SameTag, typename Tail> 61template <typename Tag, typename SameTag, typename Tail>
45Tag const& context_find_tag(context_cons<SameTag, Tail> const& context 62constexpr Tag const& context_find_tag(context_cons<SameTag, Tail> const& context
46 , typename std::enable_if<std::is_same<Tag, SameTag>::value>::type* = nullptr) 63 , typename std::enable_if<std::is_same<Tag, SameTag>::value>::type* = nullptr)
47{ 64{
48 return context.tag; 65 return context.tag;
49} 66}
50 67
51template <typename Tag, typename OtherTag, typename Tail> 68template <typename Tag, typename OtherTag, typename Tail>
52Tag const& context_find_tag(context_cons<OtherTag, Tail> const& context 69constexpr Tag const& context_find_tag(context_cons<OtherTag, Tail> const& context
53 , typename std::enable_if<!std::is_same<Tag, OtherTag>::value>::type* = nullptr) 70 , typename std::enable_if<!std::is_same<Tag, OtherTag>::value>::type* = nullptr)
54{ 71{
55 return context_find_tag<Tag>(context.tail); 72 return context_find_tag<Tag>(context.tail);
56} 73}
57 74
58template <typename Tag, typename OtherTag, typename Tail> 75template <typename Tag>
59Tag const& context_find_tag(context_null const&) 76constexpr Tag const& context_find_tag(context_null const& context)
60{ 77{
61 throw std::logic_error("Context for generation not available"); 78 static_assert(std::is_same<Tag, context_null>::value, "Tag type not available in this generation context");
79 return context;
62} 80}
63 81
64} } } 82} } }
65 83
66#endif 84#endif
diff --git a/src/lib/eolian_cxx/grammar/indentation.hpp b/src/lib/eolian_cxx/grammar/indentation.hpp
index 897be03c3b..347974fa01 100644
--- a/src/lib/eolian_cxx/grammar/indentation.hpp
+++ b/src/lib/eolian_cxx/grammar/indentation.hpp
@@ -8,22 +8,36 @@ namespace efl { namespace eolian { namespace grammar {
8 8
9struct scope_tab_generator 9struct scope_tab_generator
10{ 10{
11 scope_tab_generator(int n) 11 constexpr scope_tab_generator(int n, int m = 4)
12 : n(n) {} 12 : n(n)
13 13 , m(m)
14 {}
15 constexpr scope_tab_generator(scope_tab_generator const&) = default;
16 scope_tab_generator& operator=(scope_tab_generator const&) = default;
17
14 template <typename OutputIterator, typename Context> 18 template <typename OutputIterator, typename Context>
15 bool generate(OutputIterator sink, attributes::unused_type, Context const&) const 19 bool generate(OutputIterator sink, attributes::unused_type, Context const&) const
16 { 20 {
17 for(int i = 0; i != n; ++i) 21 for(int i = 0; i != n; ++i)
18 { 22 {
19 *sink++ = ' '; 23 for(int j = 0; j != m; ++j)
20 *sink++ = ' '; 24 *sink++ = ' ';
21 *sink++ = ' ';
22 } 25 }
23 return true; 26 return true;
24 } 27 }
25 28
29 constexpr scope_tab_generator inc(int nplus = 1) const
30 {
31 return {n+nplus, m};
32 }
33
34 constexpr scope_tab_generator dec(int nminus = 1) const
35 {
36 return {n-nminus, m};
37 }
38
26 int n; 39 int n;
40 int m;
27}; 41};
28 42
29template <> 43template <>
@@ -33,9 +47,14 @@ struct is_generator<scope_tab_generator> : std::true_type {};
33 47
34struct scope_tab_terminal 48struct scope_tab_terminal
35{ 49{
36 scope_tab_generator operator()(int n) const 50 scope_tab_generator operator()(int n, int m = 4) const
51 {
52 return {n, m};
53 }
54
55 operator scope_tab_generator() const
37 { 56 {
38 return scope_tab_generator(n); 57 return {1};
39 } 58 }
40} const scope_tab = {}; 59} const scope_tab = {};
41 60