1 | /* |
---|
2 | * Copyright (c) 2014 International Characters. |
---|
3 | * This software is licensed to the public under the Open Software License 3.0. |
---|
4 | * icgrep is a trademark of International Characters. |
---|
5 | */ |
---|
6 | |
---|
7 | #ifndef RE_SEQ_H |
---|
8 | #define RE_SEQ_H |
---|
9 | |
---|
10 | #include "re_re.h" |
---|
11 | #include <llvm/Support/Casting.h> |
---|
12 | |
---|
13 | namespace re { |
---|
14 | |
---|
15 | class Seq : public Vector { |
---|
16 | public: |
---|
17 | static inline bool classof(const RE * re) { |
---|
18 | return re->getClassTypeId() == ClassTypeId::Seq; |
---|
19 | } |
---|
20 | static inline bool classof(const void *) { |
---|
21 | return false; |
---|
22 | } |
---|
23 | virtual ~Seq() {} |
---|
24 | protected: |
---|
25 | friend Seq * makeSeq(); |
---|
26 | template<typename iterator> friend RE * makeSeq(iterator, iterator); |
---|
27 | Seq() |
---|
28 | : Vector(ClassTypeId::Seq) { |
---|
29 | |
---|
30 | } |
---|
31 | Seq(iterator begin, iterator end) |
---|
32 | : Vector(ClassTypeId::Seq, begin, end) { |
---|
33 | |
---|
34 | } |
---|
35 | template<typename itr> void flatten(itr begin, itr end); |
---|
36 | }; |
---|
37 | |
---|
38 | inline Seq * makeSeq() { |
---|
39 | return new Seq(); |
---|
40 | } |
---|
41 | |
---|
42 | template<typename itr> |
---|
43 | void Seq::flatten(itr begin, itr end) { |
---|
44 | for (auto i = begin; i != end; ++i) { |
---|
45 | if (LLVM_UNLIKELY(llvm::isa<Seq>(*i))) { |
---|
46 | flatten<Seq::iterator>(llvm::cast<Seq>(*i)->begin(), llvm::cast<Seq>(*i)->end()); |
---|
47 | } else { |
---|
48 | push_back(*i); |
---|
49 | } |
---|
50 | } |
---|
51 | } |
---|
52 | |
---|
53 | template<typename itr> |
---|
54 | inline RE * makeSeq(itr begin, itr end) { |
---|
55 | if (LLVM_UNLIKELY(std::distance(begin, end) == 0)) { |
---|
56 | return makeSeq(); |
---|
57 | } else { |
---|
58 | Seq * seq = makeSeq(); |
---|
59 | seq->flatten(begin, end); |
---|
60 | if (seq->size() == 1) { |
---|
61 | return seq->front(); |
---|
62 | } |
---|
63 | return seq; |
---|
64 | } |
---|
65 | } |
---|
66 | |
---|
67 | inline RE * makeSeq(RE::InitializerList list) { |
---|
68 | return makeSeq(list.begin(), list.end()); |
---|
69 | } |
---|
70 | |
---|
71 | } |
---|
72 | |
---|
73 | #endif // RE_SEQ_H |
---|
74 | |
---|
75 | |
---|
76 | |
---|
77 | |
---|