9 #include "utf8_string.h"
10 #include "literal_data.h"
14 using namespace utakata;
15 using namespace utakata::literal;
16 using namespace utakata::utf8_string;
17 using namespace utakata::syntax;
19 // 原則としてそれぞれ単純に呼びだされるのみ。
23 TreeNode (node_pair p) : list(), datum (p) {}
25 std::vector<smart_ptr<TreeNode> > list;
28 Tree::node_pair datum;
31 ////////////////////////////
32 // syntax::Tree::iterator //
33 ////////////////////////////
35 syntax::Tree::iterator::iterator () : current_()
39 syntax::Tree::iterator::iterator (const Tree::iterator& i)
44 syntax::Tree::iterator& syntax::Tree::iterator::operator=(const Tree::iterator& rh)
51 void syntax::Tree::iterator::swap (const Tree::iterator& t)
53 current_ = t.current_;
56 Tree::iterator& Tree::iterator::operator++ ()
58 // car,cdr をそれぞれ巡回する。 car,cdr それぞれに移動する場合には、
59 // it を元の tree に渡して取得する。
61 // cons -> cdr に繋がっている cons
62 // ただし、 cdr に繋がっている cons が literal の場合、以降は進むことが
63 // 無く、また list として判定されることはない。
69 std::pair<DatumID, smart_ptr<literal::Literal> >& Tree::iterator::operator*()
71 // 現在の current_が指す node のリテラルを返す。
72 return (*current_)->datum;
75 std::pair<DatumID, smart_ptr<literal::Literal> >* Tree::iterator::operator->()
77 // 現在の current_が指す node のリテラルを返す。
78 return &((*current_)->datum);
81 bool Tree::iterator::operator==(const Tree::iterator& rh)
83 // node_が指す先が同一かどうかを返す。
84 // 但し、自身か相手のいずれかが nil である場合だけは例外的に、単純に DatumID のみの
86 return current_ == rh.current_;
93 Tree::Tree () : node_()
96 smart_ptr<TreeNode> p(new TreeNode(makeNilList()));
97 // 最後尾には常にnilがいることにする。
101 Tree::iterator Tree::begin ()
105 it.current_ = node_.begin();
109 Tree::iterator Tree::end ()
113 it.current_ = node_.end();
117 void Tree::push_back(node_pair datum)
119 // 渡されたdatumを、現在保持しているデータ型に追加する。
120 // 末尾には常にnilがいるので、その一つ前に設定することにする。
121 smart_ptr<TreeNode> p(new TreeNode(datum));
122 node_.insert(--(node_.end()), p);
125 void Tree::push_back(Tree::iterator it, Tree::iterator it2,
128 // itとit2までを、datumで設定されたdatumidであるcompound datum
130 smart_ptr<TreeNode> p(new TreeNode(datum));
131 p->list.insert(p->list.end(), it.current_, it2.current_);
132 node_.insert(--(node_.end()), p);
135 Tree::iterator Tree::in_compound_begin(Tree::iterator it)
137 // itがcompound datumである場合に、compound datumの内部リストを返す。
138 if (isCompoundDatum(it))
141 ret.current_ = (*it.current_)->list.begin();
146 // 違う場合には、endを返すことにする。
151 Tree::iterator Tree::in_compound_end(Tree::iterator it)
153 // itがcompound datumである場合に、compound datumの内部リストを返す。
154 if (isCompoundDatum(it))
157 ret.current_ = (*it.current_)->list.end();
162 // 違う場合には、endを返すことにする。
167 //////////////////////
168 // helper functions //
169 //////////////////////
171 bool syntax::isCompoundDatum(Tree::iterator it)
173 if (it->first == syntax::DatumID::list ||
174 it->first == syntax::DatumID::vector ||
175 it->first == syntax::DatumID::byteVector)
185 std::pair<DatumID, smart_ptr<literal::Literal> > syntax::makeCompoundDatum (
188 // datum_id が list である node_pair を返す。
189 return std::pair<DatumID, smart_ptr<literal::Literal> >(id, smart_ptr<literal::Literal>());
192 std::pair<DatumID, smart_ptr<literal::Literal> > syntax::makeNilList ()
194 // datum_id が nil である node_pair を返す。
195 return std::pair<DatumID, smart_ptr<literal::Literal> >(
196 DatumID (DatumID::nil), smart_ptr<literal::Literal>());
199 std::pair<DatumID, smart_ptr<literal::Literal> > syntax::makeLexemeDatum (smart_ptr<literal::Literal> l,
202 // datum_id が各 lexeme であり、 literal として l を持つ
204 return std::pair<DatumID, smart_ptr<literal::Literal> >(
208 std::pair<DatumID, smart_ptr<literal::Literal> > syntax::makeAbbreviation (smart_ptr<literal::Literal> l)
210 // datum_id が literal であり、 literal として l を持つ
212 return std::pair<DatumID, smart_ptr<literal::Literal> >(
213 DatumID (DatumID::abbreviations), l);