Om
writer.cpp
Go to the documentation of this file.
1 
15 #ifndef Om_Language_Writer_
16 
17  #include "om/language/writer.hpp"
18 
19  #ifdef Om_Macro_Test_
20 
21  #ifndef Om_Macro_Precompilation_
22 
23  #include "boost/test/unit_test.hpp"
24 
25  #endif
26 
27 namespace Om {
28 
29  namespace Language {
30 
31  BOOST_AUTO_TEST_SUITE(WriterTest)
32 
33  BOOST_AUTO_TEST_CASE(GeneralTest) {
34  char const theCode[] = "\n\t {\n\t {\n\t }\n\t }\n\t";
35  std::string theResult;
36  {
38  std::back_insert_iterator<std::string>
39  > theCodePointSink(
40  std::back_inserter(theResult)
41  );
42  Writer theWriter(theCodePointSink);
43 
44  Om::Source::CodePointSource<> theCodePointSource(theCode);
45  Reader theReader(theCodePointSource);
46  theWriter.ParseElements(theReader);
47  }
48  BOOST_CHECK_EQUAL(
49  "\n\t {\n\t {\n\t }\n\t }\n\t",
50  theResult
51  );
52  }
53 
54  BOOST_AUTO_TEST_SUITE_END()
55 
56  }
57 
58 }
59 
60  #endif
61 
62 #else
63 
64  #include "om/language/operator.hpp"
67  #include "om/sink/sink.hpp"
69 
70 // MARK: - Om::Language::Writer
71 
72  #define Type_ \
73  Om::Language::Writer
74 
75 // MARK: public (non-static)
76 
77 inline Type_::Writer(
78  Om::Sink::Sink<CodePoint const> & theCodePointSink
79 ):
80 thisCodePointSink(theCodePointSink) {}
81 
82 inline void Type_::ParseElements(Reader & theReader) {
83  for (
84  ;
85  theReader;
86  theReader.Pop()
87  ) {
88  this->thisCodePointSink.Push(*theReader);
89  }
90 }
91 
92 inline void Type_::ParseQuotedElements(Reader & theReader) {
93  this->thisCodePointSink.Push(Symbol::theStartOperandSymbol);
94  this->ParseElements(theReader);
95  this->thisCodePointSink.Push(Symbol::theEndOperandSymbol);
96 }
97 
98 template <typename TheOperand>
99 inline void Type_::TakeOperand(TheOperand & theOperand) {
100  assert(
101  !theOperand.IsEmpty()
102  );
103  this->TakeQuotedProducer(
104  *theOperand.GetProgram()
105  );
106 }
107 
108 template <typename TheOperator>
109 inline void Type_::TakeOperator(TheOperator & theOperator) {
110  assert(
111  !theOperator.IsEmpty()
112  );
113  std::string const & theString = theOperator.GetString();
114  for (
116  theString.begin(),
117  theString.end()
118  );
119  theCodePointSource;
120  theCodePointSource.Pop()
121  ) {
122  switch (CodePoint const theCodePoint = *theCodePointSource) {
124  // Fall through.
126  // Fall through.
127  case Symbol::theEncodeOperatorSymbol:
128  this->thisCodePointSink.Push(Symbol::theEncodeOperatorSymbol);
129  // Fall through.
130  default:
131  this->thisCodePointSink.Push(theCodePoint);
132  // Fall through.
133  }
134  }
135 }
136 
137 template <typename TheProducer>
138 inline void Type_::TakeQuotedProducer(TheProducer & theProducer) {
139  this->thisCodePointSink.Push(Symbol::theStartOperandSymbol);
140  theProducer.GiveElements(*this);
141  this->thisCodePointSink.Push(Symbol::theEndOperandSymbol);
142 }
143 
144 template <typename TheSeparator>
145 inline void Type_::TakeSeparator(TheSeparator & theSeparator) {
146  assert(
147  !theSeparator.IsEmpty()
148  );
149  std::string const & theString = theSeparator.GetString();
150  for (
152  theString.begin(),
153  theString.end()
154  );
155  theCodePointSource;
156  theCodePointSource.Pop()
157  ) {
158  this->thisCodePointSink.Push(*theCodePointSource);
159  }
160 }
161 
162  #undef Type_
163 
164 #endif
A CodePoint Sink that pushes each code unit to the iterator.
Any object that items can be pushed to.
Definition: sink.hpp:31
A CodePoint Source that reads each code unit from the iterator.
virtual void Pop()
Pops the current item.
Om header file.
The Om library.
Definition: code_point.hpp:26
boost::locale::utf::code_point CodePoint
A UTF-8 code point.
Definition: code_point.hpp:32
#define Om_Language_Symbol_OperandSymbol_GetCases_()
Generates switch cases for each Om::Language::Symbol::OperandSymbol.
Om header file.
Om header file.
Om header file.
#define Om_Language_Symbol_SeparatorSymbol_GetCases_()
Generates switch cases for each Om::Language::Symbol::SeparatorSymbol.
Om header file.
Om header file.