emitter.cpp 21.6 KB
Newer Older
1
#include "yaml-cpp/emitter.h"
2
3
4
#include "emitterstate.h"
#include "emitterutils.h"
#include "indentation.h"
5
#include "yaml-cpp/exceptions.h"
6
7
8
9
10
11
12
13
14
15
16
17
#include <sstream>

namespace YAML
{	
	Emitter::Emitter(): m_pState(new EmitterState)
	{
	}
	
	Emitter::~Emitter()
	{
	}
	
18
	const char *Emitter::c_str() const
19
	{
20
		return m_stream.str();
21
22
	}
	
23
	unsigned Emitter::size() const
24
	{
25
		return m_stream.pos();
26
27
28
29
30
31
32
33
34
35
36
37
38
39
	}
	
	// state checking
	bool Emitter::good() const
	{
		return m_pState->good();
	}
	
	const std::string Emitter::GetLastError() const
	{
		return m_pState->GetLastError();
	}

	// global setters
40
41
	bool Emitter::SetOutputCharset(EMITTER_MANIP value)
	{
42
		return m_pState->SetOutputCharset(value, FmtScope::Global);
43
44
	}

45
46
	bool Emitter::SetStringFormat(EMITTER_MANIP value)
	{
47
		return m_pState->SetStringFormat(value, FmtScope::Global);
48
49
50
51
52
	}
	
	bool Emitter::SetBoolFormat(EMITTER_MANIP value)
	{
		bool ok = false;
53
		if(m_pState->SetBoolFormat(value, FmtScope::Global))
54
			ok = true;
55
		if(m_pState->SetBoolCaseFormat(value, FmtScope::Global))
56
			ok = true;
57
		if(m_pState->SetBoolLengthFormat(value, FmtScope::Global))
58
59
60
61
62
63
			ok = true;
		return ok;
	}
	
	bool Emitter::SetIntBase(EMITTER_MANIP value)
	{
64
		return m_pState->SetIntFormat(value, FmtScope::Global);
65
66
67
68
	}
	
	bool Emitter::SetSeqFormat(EMITTER_MANIP value)
	{
69
		return m_pState->SetFlowType(GroupType::Seq, value, FmtScope::Global);
70
71
72
73
74
	}
	
	bool Emitter::SetMapFormat(EMITTER_MANIP value)
	{
		bool ok = false;
75
		if(m_pState->SetFlowType(GroupType::Map, value, FmtScope::Global))
76
			ok = true;
77
		if(m_pState->SetMapKeyFormat(value, FmtScope::Global))
78
79
80
81
82
83
			ok = true;
		return ok;
	}
	
	bool Emitter::SetIndent(unsigned n)
	{
84
		return m_pState->SetIndent(n, FmtScope::Global);
85
86
87
88
	}
	
	bool Emitter::SetPreCommentIndent(unsigned n)
	{
89
		return m_pState->SetPreCommentIndent(n, FmtScope::Global);
90
91
92
93
	}
	
	bool Emitter::SetPostCommentIndent(unsigned n)
	{
94
		return m_pState->SetPostCommentIndent(n, FmtScope::Global);
95
	}
96
97
98
    
    bool Emitter::SetFloatPrecision(unsigned n)
    {
99
        return m_pState->SetFloatPrecision(n, FmtScope::Global);
100
101
102
103
    }

    bool Emitter::SetDoublePrecision(unsigned n)
    {
104
        return m_pState->SetDoublePrecision(n, FmtScope::Global);
105
    }
106
107
108
109
110
111
112
113
114

	// SetLocalValue
	// . Either start/end a group, or set a modifier locally
	Emitter& Emitter::SetLocalValue(EMITTER_MANIP value)
	{
		if(!good())
			return *this;
		
		switch(value) {
115
116
117
118
119
120
			case BeginDoc:
				EmitBeginDoc();
				break;
			case EndDoc:
				EmitEndDoc();
				break;
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
			case BeginSeq:
				EmitBeginSeq();
				break;
			case EndSeq:
				EmitEndSeq();
				break;
			case BeginMap:
				EmitBeginMap();
				break;
			case EndMap:
				EmitEndMap();
				break;
			case Key:
				EmitKey();
				break;
			case Value:
				EmitValue();
				break;
139
140
141
			case TagByKind:
				EmitKindTag();
				break;
142
143
144
			case Newline:
				EmitNewline();
				break;
145
146
147
148
149
150
151
152
153
			default:
				m_pState->SetLocalValue(value);
				break;
		}
		return *this;
	}
	
	Emitter& Emitter::SetLocalIndent(const _Indent& indent)
	{
154
		m_pState->SetIndent(indent.value, FmtScope::Local);
155
156
157
		return *this;
	}

158
159
160
    Emitter& Emitter::SetLocalPrecision(const _Precision& precision)
    {
        if(precision.floatPrecision >= 0)
161
            m_pState->SetFloatPrecision(precision.floatPrecision, FmtScope::Local);
162
        if(precision.doublePrecision >= 0)
163
            m_pState->SetDoublePrecision(precision.doublePrecision, FmtScope::Local);
164
165
166
        return *this;
    }

167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
	// GotoNextPreAtomicState
	// . Runs the state machine, emitting if necessary, and returns 'true' if done (i.e., ready to emit an atom)
	bool Emitter::GotoNextPreAtomicState()
	{
		if(!good())
			return true;
		
		unsigned curIndent = m_pState->GetCurIndent();
		
		EMITTER_STATE curState = m_pState->GetCurState();
		switch(curState) {
				// document-level
			case ES_WAITING_FOR_DOC:
				m_pState->SwitchState(ES_WRITING_DOC);
				return true;
			case ES_WRITING_DOC:
				return true;
184
			case ES_DONE_WITH_DOC:
185
186
				EmitBeginDoc();
				return false;
187
188
189
190
				
				// block sequence
			case ES_WAITING_FOR_BLOCK_SEQ_ENTRY:
				m_stream << IndentTo(curIndent) << "-";
191
				m_pState->RequireSoftSeparation();
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
				m_pState->SwitchState(ES_WRITING_BLOCK_SEQ_ENTRY);
				return true;
			case ES_WRITING_BLOCK_SEQ_ENTRY:
				return true;
			case ES_DONE_WITH_BLOCK_SEQ_ENTRY:
				m_stream << '\n';
				m_pState->SwitchState(ES_WAITING_FOR_BLOCK_SEQ_ENTRY);
				return false;
				
				// flow sequence
			case ES_WAITING_FOR_FLOW_SEQ_ENTRY:
				m_pState->SwitchState(ES_WRITING_FLOW_SEQ_ENTRY);
				return true;
			case ES_WRITING_FLOW_SEQ_ENTRY:
				return true;
			case ES_DONE_WITH_FLOW_SEQ_ENTRY:
208
				EmitSeparationIfNecessary();
209
				m_stream << ',';
210
				m_pState->RequireSoftSeparation();
211
212
213
214
215
216
217
218
219
220
				m_pState->SwitchState(ES_WAITING_FOR_FLOW_SEQ_ENTRY);
				return false;
				
				// block map
			case ES_WAITING_FOR_BLOCK_MAP_ENTRY:
				m_pState->SetError(ErrorMsg::EXPECTED_KEY_TOKEN);
				return true;
			case ES_WAITING_FOR_BLOCK_MAP_KEY:
				if(m_pState->CurrentlyInLongKey()) {
					m_stream << IndentTo(curIndent) << '?';
221
					m_pState->RequireSoftSeparation();
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
				}
				m_pState->SwitchState(ES_WRITING_BLOCK_MAP_KEY);
				return true;
			case ES_WRITING_BLOCK_MAP_KEY:
				return true;
			case ES_DONE_WITH_BLOCK_MAP_KEY:
				m_pState->SetError(ErrorMsg::EXPECTED_VALUE_TOKEN);
				return true;
			case ES_WAITING_FOR_BLOCK_MAP_VALUE:
				m_pState->SwitchState(ES_WRITING_BLOCK_MAP_VALUE);
				return true;
			case ES_WRITING_BLOCK_MAP_VALUE:
				return true;
			case ES_DONE_WITH_BLOCK_MAP_VALUE:
				m_pState->SetError(ErrorMsg::EXPECTED_KEY_TOKEN);
				return true;
				
				// flow map
			case ES_WAITING_FOR_FLOW_MAP_ENTRY:
				m_pState->SetError(ErrorMsg::EXPECTED_KEY_TOKEN);
				return true;
			case ES_WAITING_FOR_FLOW_MAP_KEY:
244
				EmitSeparationIfNecessary();
245
246
247
				m_pState->SwitchState(ES_WRITING_FLOW_MAP_KEY);
				if(m_pState->CurrentlyInLongKey()) {
					m_stream << '?';
248
					m_pState->RequireSoftSeparation();
249
250
251
252
253
254
255
256
				}
				return true;
			case ES_WRITING_FLOW_MAP_KEY:
				return true;
			case ES_DONE_WITH_FLOW_MAP_KEY:
				m_pState->SetError(ErrorMsg::EXPECTED_VALUE_TOKEN);
				return true;
			case ES_WAITING_FOR_FLOW_MAP_VALUE:
257
				EmitSeparationIfNecessary();
258
				m_stream << ':';
259
				m_pState->RequireSoftSeparation();
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
				m_pState->SwitchState(ES_WRITING_FLOW_MAP_VALUE);
				return true;
			case ES_WRITING_FLOW_MAP_VALUE:
				return true;
			case ES_DONE_WITH_FLOW_MAP_VALUE:
				m_pState->SetError(ErrorMsg::EXPECTED_KEY_TOKEN);
				return true;
			default:
				assert(false);
		}

		assert(false);
		return true;
	}
	
	// PreAtomicWrite
	// . Depending on the emitter state, write to the stream to get it
	//   in position to do an atomic write (e.g., scalar, sequence, or map)
	void Emitter::PreAtomicWrite()
	{
		if(!good())
			return;
		
		while(!GotoNextPreAtomicState())
			;
	}
	
	// PostAtomicWrite
	// . Clean up
	void Emitter::PostAtomicWrite()
	{
		if(!good())
			return;
		
		EMITTER_STATE curState = m_pState->GetCurState();
		switch(curState) {
				// document-level
			case ES_WRITING_DOC:
				m_pState->SwitchState(ES_DONE_WITH_DOC);
				break;

				// block seq
			case ES_WRITING_BLOCK_SEQ_ENTRY:
				m_pState->SwitchState(ES_DONE_WITH_BLOCK_SEQ_ENTRY);
				break;
				
				// flow seq
			case ES_WRITING_FLOW_SEQ_ENTRY:
				m_pState->SwitchState(ES_DONE_WITH_FLOW_SEQ_ENTRY);
				break;
				
				// block map
			case ES_WRITING_BLOCK_MAP_KEY:
313
314
				if(!m_pState->CurrentlyInLongKey()) {
					m_stream << ':';
315
					m_pState->RequireSoftSeparation();
316
				}
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
				m_pState->SwitchState(ES_DONE_WITH_BLOCK_MAP_KEY);
				break;
			case ES_WRITING_BLOCK_MAP_VALUE:
				m_pState->SwitchState(ES_DONE_WITH_BLOCK_MAP_VALUE);
				break;
				
				// flow map
			case ES_WRITING_FLOW_MAP_KEY:
				m_pState->SwitchState(ES_DONE_WITH_FLOW_MAP_KEY);
				break;
			case ES_WRITING_FLOW_MAP_VALUE:
				m_pState->SwitchState(ES_DONE_WITH_FLOW_MAP_VALUE);
				break;
			default:
				assert(false);
		};
				
		m_pState->ClearModifiedSettings();
	}
	
	// EmitSeparationIfNecessary
	void Emitter::EmitSeparationIfNecessary()
	{
		if(!good())
			return;
		
343
		if(m_pState->RequiresSoftSeparation())
344
			m_stream << ' ';
345
346
		else if(m_pState->RequiresHardSeparation())
			m_stream << '\n';
347
348
349
		m_pState->UnsetSeparation();
	}
	
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
	// EmitBeginDoc
	void Emitter::EmitBeginDoc()
	{
		if(!good())
			return;
		
		EMITTER_STATE curState = m_pState->GetCurState();
		if(curState != ES_WAITING_FOR_DOC && curState != ES_WRITING_DOC && curState != ES_DONE_WITH_DOC) {
			m_pState->SetError("Unexpected begin document");
			return;
		}
		
		if(curState == ES_WRITING_DOC || curState == ES_DONE_WITH_DOC)
			m_stream << '\n';		
		m_stream << "---\n";

		m_pState->UnsetSeparation();
		m_pState->SwitchState(ES_WAITING_FOR_DOC);
	}
	
	// EmitEndDoc
	void Emitter::EmitEndDoc()
	{
		if(!good())
			return;

		
		EMITTER_STATE curState = m_pState->GetCurState();
		if(curState != ES_WAITING_FOR_DOC && curState != ES_WRITING_DOC && curState != ES_DONE_WITH_DOC) {
			m_pState->SetError("Unexpected end document");
			return;
		}
		
		if(curState == ES_WRITING_DOC || curState == ES_DONE_WITH_DOC)
			m_stream << '\n';		
		m_stream << "...\n";
		
		m_pState->UnsetSeparation();
		m_pState->SwitchState(ES_WAITING_FOR_DOC);
	}

391
392
393
394
395
396
397
398
399
400
401
402
	// EmitBeginSeq
	void Emitter::EmitBeginSeq()
	{
		if(!good())
			return;
		
		// must have a long key if we're emitting a sequence
		m_pState->StartLongKey();
		
		PreAtomicWrite();
		
		EMITTER_STATE curState = m_pState->GetCurState();
403
		EMITTER_MANIP flowType = m_pState->GetFlowType(GroupType::Seq);
404
		if(flowType == Block) {
405
406
407
408
			if(curState == ES_WRITING_BLOCK_SEQ_ENTRY ||
			   curState == ES_WRITING_BLOCK_MAP_KEY || curState == ES_WRITING_BLOCK_MAP_VALUE ||
			   curState == ES_WRITING_DOC
			) {
409
410
411
412
				if(m_pState->RequiresHardSeparation() || curState != ES_WRITING_DOC) {
					m_stream << "\n";
					m_pState->UnsetSeparation();
				}
413
414
415
416
417
418
419
420
421
			}
			m_pState->PushState(ES_WAITING_FOR_BLOCK_SEQ_ENTRY);
		} else if(flowType == Flow) {
			EmitSeparationIfNecessary();
			m_stream << "[";
			m_pState->PushState(ES_WAITING_FOR_FLOW_SEQ_ENTRY);
		} else
			assert(false);

422
		m_pState->BeginGroup(GroupType::Seq);
423
424
425
426
427
428
429
430
	}
	
	// EmitEndSeq
	void Emitter::EmitEndSeq()
	{
		if(!good())
			return;
		
431
		if(m_pState->GetCurGroupType() != GroupType::Seq)
432
433
434
			return m_pState->SetError(ErrorMsg::UNEXPECTED_END_SEQ);
		
		EMITTER_STATE curState = m_pState->GetCurState();
435
436
		FlowType::value flowType = m_pState->GetCurGroupFlowType();
		if(flowType == FlowType::Block) {
437
438
439
440
			// Note: block sequences are *not* allowed to be empty, but we convert it
			//       to a flow sequence if it is
			assert(curState == ES_DONE_WITH_BLOCK_SEQ_ENTRY || curState == ES_WAITING_FOR_BLOCK_SEQ_ENTRY);
			if(curState == ES_WAITING_FOR_BLOCK_SEQ_ENTRY) {
441
442
				// Note: only one of these will actually output anything for a given situation
				EmitSeparationIfNecessary();
443
				unsigned curIndent = m_pState->GetCurIndent();
444
445
446
				m_stream << IndentTo(curIndent);

				m_stream << "[]";
447
			}
448
		} else if(flowType == FlowType::Flow) {
449
450
			// Note: flow sequences are allowed to be empty
			assert(curState == ES_DONE_WITH_FLOW_SEQ_ENTRY || curState == ES_WAITING_FOR_FLOW_SEQ_ENTRY);
451
			m_stream << "]";
452
453
454
455
		} else
			assert(false);
		
		m_pState->PopState();
456
		m_pState->EndGroup(GroupType::Seq);
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472

		PostAtomicWrite();
	}
	
	// EmitBeginMap
	void Emitter::EmitBeginMap()
	{
		if(!good())
			return;
		
		// must have a long key if we're emitting a map
		m_pState->StartLongKey();

		PreAtomicWrite();

		EMITTER_STATE curState = m_pState->GetCurState();
473
		EMITTER_MANIP flowType = m_pState->GetFlowType(GroupType::Map);
474
		if(flowType == Block) {
475
476
477
478
			if(curState == ES_WRITING_BLOCK_SEQ_ENTRY ||
			   curState == ES_WRITING_BLOCK_MAP_KEY || curState == ES_WRITING_BLOCK_MAP_VALUE ||
			   curState == ES_WRITING_DOC
			) {
479
				if(m_pState->RequiresHardSeparation() || (curState != ES_WRITING_DOC && curState != ES_WRITING_BLOCK_SEQ_ENTRY)) {
480
481
482
					m_stream << "\n";
					m_pState->UnsetSeparation();
				}
483
484
485
486
487
488
489
490
491
			}
			m_pState->PushState(ES_WAITING_FOR_BLOCK_MAP_ENTRY);
		} else if(flowType == Flow) {
			EmitSeparationIfNecessary();
			m_stream << "{";
			m_pState->PushState(ES_WAITING_FOR_FLOW_MAP_ENTRY);
		} else
			assert(false);
		
492
		m_pState->BeginGroup(GroupType::Map);
493
494
495
496
497
498
499
500
	}
	
	// EmitEndMap
	void Emitter::EmitEndMap()
	{
		if(!good())
			return;
		
501
		if(m_pState->GetCurGroupType() != GroupType::Map)
502
503
504
			return m_pState->SetError(ErrorMsg::UNEXPECTED_END_MAP);

		EMITTER_STATE curState = m_pState->GetCurState();
505
506
		FlowType::value flowType = m_pState->GetCurGroupFlowType();
		if(flowType == FlowType::Block) {
507
508
509
510
			// Note: block sequences are *not* allowed to be empty, but we convert it
			//       to a flow sequence if it is
			assert(curState == ES_DONE_WITH_BLOCK_MAP_VALUE || curState == ES_WAITING_FOR_BLOCK_MAP_ENTRY);
			if(curState == ES_WAITING_FOR_BLOCK_MAP_ENTRY) {
511
512
				// Note: only one of these will actually output anything for a given situation
				EmitSeparationIfNecessary();
513
				unsigned curIndent = m_pState->GetCurIndent();
514
515
				m_stream << IndentTo(curIndent);
				m_stream << "{}";
516
			}
517
		} else if(flowType == FlowType::Flow) {
518
519
			// Note: flow maps are allowed to be empty
			assert(curState == ES_DONE_WITH_FLOW_MAP_VALUE || curState == ES_WAITING_FOR_FLOW_MAP_ENTRY);
520
			EmitSeparationIfNecessary();
521
			m_stream << "}";
522
523
524
525
		} else
			assert(false);
		
		m_pState->PopState();
526
		m_pState->EndGroup(GroupType::Map);
527
528
529
530
531
532
533
534
535
536
537
		
		PostAtomicWrite();
	}
	
	// EmitKey
	void Emitter::EmitKey()
	{
		if(!good())
			return;
		
		EMITTER_STATE curState = m_pState->GetCurState();
538
        FlowType::value flowType = m_pState->GetCurGroupFlowType();
539
540
541
542
		if(curState != ES_WAITING_FOR_BLOCK_MAP_ENTRY && curState != ES_DONE_WITH_BLOCK_MAP_VALUE
		   && curState != ES_WAITING_FOR_FLOW_MAP_ENTRY && curState != ES_DONE_WITH_FLOW_MAP_VALUE)
			return m_pState->SetError(ErrorMsg::UNEXPECTED_KEY_TOKEN);

543
		if(flowType == FlowType::Block) {
544
545
546
547
			if(curState == ES_DONE_WITH_BLOCK_MAP_VALUE)
				m_stream << '\n';
			unsigned curIndent = m_pState->GetCurIndent();
			m_stream << IndentTo(curIndent);
548
			m_pState->UnsetSeparation();
549
			m_pState->SwitchState(ES_WAITING_FOR_BLOCK_MAP_KEY);
550
		} else if(flowType == FlowType::Flow) {
551
			EmitSeparationIfNecessary();
552
553
			if(curState == ES_DONE_WITH_FLOW_MAP_VALUE) {
				m_stream << ',';
554
				m_pState->RequireSoftSeparation();
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
			}
			m_pState->SwitchState(ES_WAITING_FOR_FLOW_MAP_KEY);
		} else
			assert(false);
		
		if(m_pState->GetMapKeyFormat() == LongKey)
			m_pState->StartLongKey();
		else if(m_pState->GetMapKeyFormat() == Auto)
			m_pState->StartSimpleKey();
		else
			assert(false);
	}
	
	// EmitValue
	void Emitter::EmitValue()
	{
		if(!good())
			return;

		EMITTER_STATE curState = m_pState->GetCurState();
575
		FlowType::value flowType = m_pState->GetCurGroupFlowType();
576
577
578
		if(curState != ES_DONE_WITH_BLOCK_MAP_KEY && curState != ES_DONE_WITH_FLOW_MAP_KEY)
			return m_pState->SetError(ErrorMsg::UNEXPECTED_VALUE_TOKEN);

579
		if(flowType == FlowType::Block) {
580
			if(m_pState->CurrentlyInLongKey()) {
581
				m_stream << '\n';
582
583
				m_stream << IndentTo(m_pState->GetCurIndent());
				m_stream << ':';
584
				m_pState->RequireSoftSeparation();
585
			}
586
			m_pState->SwitchState(ES_WAITING_FOR_BLOCK_MAP_VALUE);
587
		} else if(flowType == FlowType::Flow) {
588
589
590
591
592
			m_pState->SwitchState(ES_WAITING_FOR_FLOW_MAP_VALUE);
		} else
			assert(false);
	}

593
594
595
596
597
	// EmitNewline
	void Emitter::EmitNewline()
	{
		if(!good())
			return;
598

599
		if(CanEmitNewline()) {
600
			m_stream << '\n';
601
602
			m_pState->UnsetSeparation();
		}
603
604
605
606
	}

	bool Emitter::CanEmitNewline() const
	{
607
608
		FlowType::value flowType = m_pState->GetCurGroupFlowType();
		if(flowType == FlowType::Block && m_pState->CurrentlyInLongKey())
609
610
611
612
			return true;

		EMITTER_STATE curState = m_pState->GetCurState();
		return curState != ES_DONE_WITH_BLOCK_MAP_KEY && curState != ES_WAITING_FOR_BLOCK_MAP_VALUE && curState != ES_WRITING_BLOCK_MAP_VALUE;
613
614
	}

615
616
617
618
619
620
621
622
623
	// *******************************************************************************************
	// overloads of Write
	
	Emitter& Emitter::Write(const std::string& str)
	{
		if(!good())
			return *this;
		
		// literal scalars must use long keys
624
		if(m_pState->GetStringFormat() == Literal && m_pState->GetCurGroupFlowType() != FlowType::Flow)
625
626
627
628
629
			m_pState->StartLongKey();
		
		PreAtomicWrite();
		EmitSeparationIfNecessary();
		
630
		bool escapeNonAscii = m_pState->GetOutputCharset() == EscapeNonAscii;
631
		EMITTER_MANIP strFmt = m_pState->GetStringFormat();
632
        FlowType::value flowType = m_pState->GetCurGroupFlowType();
633
634
635
636
		unsigned curIndent = m_pState->GetCurIndent();

		switch(strFmt) {
			case Auto:
637
				Utils::WriteString(m_stream, str, flowType == FlowType::Flow, escapeNonAscii);
638
639
640
641
642
643
644
645
				break;
			case SingleQuoted:
				if(!Utils::WriteSingleQuotedString(m_stream, str)) {
					m_pState->SetError(ErrorMsg::SINGLE_QUOTED_CHAR);
					return *this;
				}
				break;
			case DoubleQuoted:
646
				Utils::WriteDoubleQuotedString(m_stream, str, escapeNonAscii);
647
648
				break;
			case Literal:
649
650
				if(flowType == FlowType::Flow)
					Utils::WriteString(m_stream, str, flowType == FlowType::Flow, escapeNonAscii);
651
652
653
654
655
656
657
658
659
660
				else
					Utils::WriteLiteralString(m_stream, str, curIndent + m_pState->GetIndent());
				break;
			default:
				assert(false);
		}
		
		PostAtomicWrite();
		return *this;
	}
661

662
	void Emitter::PreWriteIntegralType(std::stringstream& str)
663
664
665
666
667
668
669
670
671
672
	{
		PreAtomicWrite();
		EmitSeparationIfNecessary();
		
		EMITTER_MANIP intFmt = m_pState->GetIntFormat();
		switch(intFmt) {
			case Dec:
				str << std::dec;
				break;
			case Hex:
673
                str << "0x";
674
675
				str << std::hex;
				break;
676
677
			case Oct:
                str << "0";
678
679
680
681
682
				str << std::oct;
				break;
			default:
				assert(false);
		}
683
	}
684

685
	void Emitter::PreWriteStreamable(std::stringstream&)
686
687
688
689
690
	{
		PreAtomicWrite();
		EmitSeparationIfNecessary();
	}

691
692
693
694
695
696
697
698
699
700
    unsigned Emitter::GetFloatPrecision() const
    {
        return m_pState->GetFloatPrecision();
    }
    
    unsigned Emitter::GetDoublePrecision() const
    {
        return m_pState->GetDoublePrecision();
    }

701
702
	void Emitter::PostWriteIntegralType(const std::stringstream& str)
	{
703
704
705
		m_stream << str.str();
		PostAtomicWrite();
	}
706

707
708
709
710
711
712
	void Emitter::PostWriteStreamable(const std::stringstream& str)
	{
		m_stream << str.str();
		PostAtomicWrite();
	}

713
714
715
716
717
718
719
	const char *Emitter::ComputeFullBoolName(bool b) const
	{
		const EMITTER_MANIP mainFmt = (m_pState->GetBoolLengthFormat() == ShortBool ? YesNoBool : m_pState->GetBoolFormat());
		const EMITTER_MANIP caseFmt = m_pState->GetBoolCaseFormat();
		switch(mainFmt) {
			case YesNoBool:
				switch(caseFmt) {
720
721
722
723
					case UpperCase: return b ? "YES" : "NO";
					case CamelCase: return b ? "Yes" : "No";
					case LowerCase: return b ? "yes" : "no";
					default: break;
724
				}
725
				break;
726
727
			case OnOffBool:
				switch(caseFmt) {
728
729
730
731
					case UpperCase: return b ? "ON" : "OFF";
					case CamelCase: return b ? "On" : "Off";
					case LowerCase: return b ? "on" : "off";
					default: break;
732
				}
733
734
				break;
			case TrueFalseBool:
735
				switch(caseFmt) {
736
737
738
739
					case UpperCase: return b ? "TRUE" : "FALSE";
					case CamelCase: return b ? "True" : "False";
					case LowerCase: return b ? "true" : "false";
					default: break;
740
				}
741
742
743
				break;
			default:
				break;
744
		}
745
		return b ? "y" : "n"; // should never get here, but it can't hurt to give these answers
746
	}
747

748
749
750
751
752
753
754
	Emitter& Emitter::Write(bool b)
	{
		if(!good())
			return *this;
		
		PreAtomicWrite();
		EmitSeparationIfNecessary();
755
756
757
	
		const char *name = ComputeFullBoolName(b);
		if(m_pState->GetBoolLengthFormat() == ShortBool)
758
759
760
			m_stream << name[0];
		else
			m_stream << name;
761

762
763
764
765
		PostAtomicWrite();
		return *this;
	}

Jesse Beder's avatar
Jesse Beder committed
766
767
768
769
770
771
772
773
774
775
776
777
778
779
	Emitter& Emitter::Write(char ch)
	{
		if(!good())
			return *this;
		
		PreAtomicWrite();
		EmitSeparationIfNecessary();
		
		Utils::WriteChar(m_stream, ch);
		
		PostAtomicWrite();
		return *this;
	}

780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
	Emitter& Emitter::Write(const _Alias& alias)
	{
		if(!good())
			return *this;
		
		PreAtomicWrite();
		EmitSeparationIfNecessary();
		if(!Utils::WriteAlias(m_stream, alias.content)) {
			m_pState->SetError(ErrorMsg::INVALID_ALIAS);
			return *this;
		}
		PostAtomicWrite();
		return *this;
	}
	
	Emitter& Emitter::Write(const _Anchor& anchor)
	{
		if(!good())
			return *this;
		
		PreAtomicWrite();
		EmitSeparationIfNecessary();
		if(!Utils::WriteAnchor(m_stream, anchor.content)) {
			m_pState->SetError(ErrorMsg::INVALID_ANCHOR);
			return *this;
		}
806
		m_pState->RequireHardSeparation();
807
808
809
810
		// Note: no PostAtomicWrite() because we need another value for this node
		return *this;
	}
	
811
812
813
814
	Emitter& Emitter::Write(const _Tag& tag)
	{
		if(!good())
			return *this;
815

816
817
		PreAtomicWrite();
		EmitSeparationIfNecessary();
818
819
820
821
822
823
824
825
826
827
		
		bool success = false;
		if(tag.type == _Tag::Type::Verbatim)
			success = Utils::WriteTag(m_stream, tag.content, true);
		else if(tag.type == _Tag::Type::PrimaryHandle)
			success = Utils::WriteTag(m_stream, tag.content, false);
		else
			success = Utils::WriteTagWithPrefix(m_stream, tag.prefix, tag.content);
		
		if(!success) {
828
			m_pState->SetError(ErrorMsg::INVALID_TAG);
829
			return *this;
830
		}
831
		
832
		m_pState->RequireHardSeparation();
833
		// Note: no PostAtomicWrite() because we need another value for this node
834
		return *this;
835
	}
836

837
838
	void Emitter::EmitKindTag()
	{
839
		Write(LocalTag(""));
840
841
	}

842
843
844
845
846
	Emitter& Emitter::Write(const _Comment& comment)
	{
		if(!good())
			return *this;
		
847
848
		if(m_stream.col() > 0)
			m_stream << Indentation(m_pState->GetPreCommentIndent());
849
		Utils::WriteComment(m_stream, comment.content, m_pState->GetPostCommentIndent());
850
851
852
		m_pState->RequireHardSeparation();
		m_pState->ForceHardSeparation();
		
853
854
		return *this;
	}
855
856
857
858
859
860
861
862
863
864
865
866

	Emitter& Emitter::Write(const _Null& /*null*/)
	{
		if(!good())
			return *this;
		
		PreAtomicWrite();
		EmitSeparationIfNecessary();
		m_stream << "~";
		PostAtomicWrite();
		return *this;
	}
867

868
	Emitter& Emitter::Write(const Binary& binary)
869
	{
870
871
		Write(SecondaryTag("binary"));

872
873
874
875
876
		if(!good())
			return *this;
		
		PreAtomicWrite();
		EmitSeparationIfNecessary();
877
		Utils::WriteBinary(m_stream, binary);
878
879
880
		PostAtomicWrite();
		return *this;
	}
881
}
882