scanscalar.cpp 4.15 KB
Newer Older
1
2
3
4
5
6
7
8
#include "scanscalar.h"
#include "scanner.h"
#include "exp.h"
#include "exceptions.h"
#include "token.h"

namespace YAML
{
9
	// ScanScalar
10
11
12
13
14
15
16
17
18
19
	// . This is where the scalar magic happens.
	//
	// . We do the scanning in three phases:
	//   1. Scan until newline
	//   2. Eat newline
	//   3. Scan leading blanks.
	//
	// . Depending on the parameters given, we store or stop
	//   and different places in the above flow.
	std::string ScanScalar(Stream& INPUT, ScanScalarParams& params)
20
	{
21
		bool foundNonEmptyLine = false, pastOpeningBreak = false;
22
23
		bool emptyLine = false, moreIndented = false;
		std::string scalar;
24
		params.leadingSpaces = false;
25
26
27
28

		while(INPUT) {
			// ********************************
			// Phase #1: scan until line ending
29
			while(!params.end.Matches(INPUT) && !Exp::Break.Matches(INPUT)) {
30
31
32
				if(INPUT.peek() == EOF)
					break;

33
34
				// document indicator?
				if(INPUT.column == 0 && Exp::DocIndicator.Matches(INPUT)) {
35
					if(params.onDocIndicator == BREAK)
36
						break;
37
					else if(params.onDocIndicator == THROW)
38
39
40
41
						throw IllegalDocIndicator();
				}

				foundNonEmptyLine = true;
42
				pastOpeningBreak = true;
43

44
				// escaped newline? (only if we're escaping on slash)
45
				if(params.escape == '\\' && Exp::EscBreak.Matches(INPUT)) {
46
					int n = Exp::EscBreak.Match(INPUT);
47
					INPUT.eat(n);
48
49
50
51
					continue;
				}

				// escape this?
52
				if(INPUT.peek() == params.escape) {
53
					scalar += Exp::Escape(INPUT);
54
55
56
57
					continue;
				}

				// otherwise, just add the damn character
58
				scalar += INPUT.get();
59
60
61
62
			}

			// eof? if we're looking to eat something, then we throw
			if(INPUT.peek() == EOF) {
63
				if(params.eatEnd)
64
					throw IllegalEOF();
65
66
67
				break;
			}

68
			// doc indicator?
69
			if(params.onDocIndicator == BREAK && INPUT.column == 0 && Exp::DocIndicator.Matches(INPUT))
70
71
				break;

72
			// are we done via character match?
73
			int n = params.end.Match(INPUT);
74
			if(n >= 0) {
75
				if(params.eatEnd)
76
					INPUT.eat(n);
77
78
79
80
81
82
				break;
			}

			// ********************************
			// Phase #2: eat line ending
			n = Exp::Break.Match(INPUT);
83
			INPUT.eat(n);
84
85
86
87
88

			// ********************************
			// Phase #3: scan initial spaces

			// first the required indentation
89
			while(INPUT.peek() == ' ' && (INPUT.column < params.indent || (params.detectIndent && !foundNonEmptyLine)))
90
				INPUT.eat(1);
91

92
			// update indent if we're auto-detecting
93
94
			if(params.detectIndent && !foundNonEmptyLine)
				params.indent = std::max(params.indent, INPUT.column);
95

96
			// and then the rest of the whitespace
97
98
			while(Exp::Blank.Matches(INPUT)) {
				// we check for tabs that masquerade as indentation
99
				if(INPUT.peek() == '\t'&& INPUT.column < params.indent && params.onTabInIndentation == THROW)
100
101
					throw IllegalTabInIndentation();

102
				if(!params.eatLeadingWhitespace)
103
104
					break;

105
				INPUT.eat(1);
106
107
108
109
110
111
			}

			// was this an empty line?
			bool nextEmptyLine = Exp::Break.Matches(INPUT);
			bool nextMoreIndented = (INPUT.peek() == ' ');

112
			// for block scalars, we always start with a newline, so we should ignore it (not fold or keep)
113
114
115
116
117
118
			bool useNewLine = pastOpeningBreak;
			// and for folded scalars, we don't fold the very last newline to a space
			if(params.fold && !emptyLine && INPUT.column < params.indent)
				useNewLine = false;

			if(useNewLine) {
119
120
121
122
123
				if(params.fold && !emptyLine && !nextEmptyLine && !moreIndented && !nextMoreIndented)
					scalar += " ";
				else
					scalar += "\n";
			}
124
125
126

			emptyLine = nextEmptyLine;
			moreIndented = nextMoreIndented;
127
			pastOpeningBreak = true;
128
129

			// are we done via indentation?
130
131
			if(!emptyLine && INPUT.column < params.indent) {
				params.leadingSpaces = true;
132
				break;
133
			}
134
135
136
		}

		// post-processing
137
		if(params.trimTrailingSpaces) {
138
139
140
141
142
			unsigned pos = scalar.find_last_not_of(' ');
			if(pos < scalar.size())
				scalar.erase(pos + 1);
		}

143
		if(params.chomp <= 0) {
144
			unsigned pos = scalar.find_last_not_of('\n');
145
			if(params.chomp == 0 && pos + 1 < scalar.size())
146
				scalar.erase(pos + 2);
147
			else if(params.chomp == -1 && pos < scalar.size())
148
149
150
151
152
				scalar.erase(pos + 1);
		}

		return scalar;
	}
153
}