scanscalar.cpp 5.23 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include "scanscalar.h"
#include "scanner.h"
#include "exp.h"
#include "exceptions.h"
#include "token.h"

namespace YAML
{
	// ScanScalar
	// . 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)
	{
21
22
		bool foundNonEmptyLine = false;
		bool pastOpeningBreak = (params.fold == FOLD_FLOW);
23
		bool emptyLine = false, moreIndented = false;
24
25
		int foldedNewlineCount = 0;
		bool foldedNewlineStartedMoreIndented = false;
26
27
28
29
30
31
		std::string scalar;
		params.leadingSpaces = false;

		while(INPUT) {
			// ********************************
			// Phase #1: scan until line ending
32
33
			
			std::size_t lastNonWhitespaceChar = scalar.size();
34
			bool escapedNewline = false;
35
			while(!params.end.Matches(INPUT) && !Exp::Break.Matches(INPUT)) {
36
				if(!INPUT)
37
38
39
					break;

				// document indicator?
40
				if(INPUT.column() == 0 && Exp::DocIndicator.Matches(INPUT)) {
41
42
43
					if(params.onDocIndicator == BREAK)
						break;
					else if(params.onDocIndicator == THROW)
44
						throw ParserException(INPUT.mark(), ErrorMsg::DOC_IN_SCALAR);
45
46
47
48
49
50
51
				}

				foundNonEmptyLine = true;
				pastOpeningBreak = true;

				// escaped newline? (only if we're escaping on slash)
				if(params.escape == '\\' && Exp::EscBreak.Matches(INPUT)) {
52
53
					// eat escape character and get out (but preserve trailing whitespace!)
					INPUT.get();
54
					lastNonWhitespaceChar = scalar.size();
55
56
					escapedNewline = true;
					break;
57
58
59
60
61
				}

				// escape this?
				if(INPUT.peek() == params.escape) {
					scalar += Exp::Escape(INPUT);
62
					lastNonWhitespaceChar = scalar.size();
63
64
65
66
					continue;
				}

				// otherwise, just add the damn character
67
68
69
70
				char ch = INPUT.get();
				scalar += ch;
				if(ch != ' ' && ch != '\t')
					lastNonWhitespaceChar = scalar.size();
71
72
73
			}

			// eof? if we're looking to eat something, then we throw
74
			if(!INPUT) {
75
				if(params.eatEnd)
76
					throw ParserException(INPUT.mark(), ErrorMsg::EOF_IN_SCALAR);
77
78
79
80
				break;
			}

			// doc indicator?
81
			if(params.onDocIndicator == BREAK && INPUT.column() == 0 && Exp::DocIndicator.Matches(INPUT))
82
83
84
85
86
87
88
89
90
				break;

			// are we done via character match?
			int n = params.end.Match(INPUT);
			if(n >= 0) {
				if(params.eatEnd)
					INPUT.eat(n);
				break;
			}
91
92
93
94
95
			
			// do we remove trailing whitespace?
			if(params.fold == FOLD_FLOW)
				scalar.erase(lastNonWhitespaceChar);
			
96
97
98
99
100
101
102
103
104
			// ********************************
			// Phase #2: eat line ending
			n = Exp::Break.Match(INPUT);
			INPUT.eat(n);

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

			// first the required indentation
105
			while(INPUT.peek() == ' ' && (INPUT.column() < params.indent || (params.detectIndent && !foundNonEmptyLine)))
106
107
108
109
				INPUT.eat(1);

			// update indent if we're auto-detecting
			if(params.detectIndent && !foundNonEmptyLine)
110
				params.indent = std::max(params.indent, INPUT.column());
111
112
113
114

			// and then the rest of the whitespace
			while(Exp::Blank.Matches(INPUT)) {
				// we check for tabs that masquerade as indentation
115
116
				if(INPUT.peek() == '\t'&& INPUT.column() < params.indent && params.onTabInIndentation == THROW)
					throw ParserException(INPUT.mark(), ErrorMsg::TAB_IN_INDENTATION);
117
118
119
120
121
122
123
124
125

				if(!params.eatLeadingWhitespace)
					break;

				INPUT.eat(1);
			}

			// was this an empty line?
			bool nextEmptyLine = Exp::Break.Matches(INPUT);
126
			bool nextMoreIndented = Exp::Blank.Matches(INPUT);
127
			if(params.fold == FOLD_BLOCK && foldedNewlineCount == 0 && nextEmptyLine)
128
				foldedNewlineStartedMoreIndented = moreIndented;
129
130

			// for block scalars, we always start with a newline, so we should ignore it (not fold or keep)
131
			if(pastOpeningBreak) {
132
133
				switch(params.fold) {
					case DONT_FOLD:
134
						scalar += "\n";
135
136
137
138
139
140
141
						break;
					case FOLD_BLOCK:
						if(!emptyLine && !nextEmptyLine && !moreIndented && !nextMoreIndented && INPUT.column() >= params.indent)
							scalar += " ";
						else if(nextEmptyLine)
							foldedNewlineCount++;
						else
142
							scalar += "\n";
143
144
145
146
147
148
149
150
151
152
153
						
						if(!nextEmptyLine && foldedNewlineCount > 0) {
							scalar += std::string(foldedNewlineCount - 1, '\n');
							if(foldedNewlineStartedMoreIndented || nextMoreIndented)
								scalar += "\n";
							foldedNewlineCount = 0;
						}
						break;
					case FOLD_FLOW:
						if(nextEmptyLine)
							scalar += "\n";
154
						else if(!emptyLine && !nextEmptyLine && !escapedNewline)
155
156
							scalar += " ";
						break;
157
				}
158
159
160
161
162
163
164
			}

			emptyLine = nextEmptyLine;
			moreIndented = nextMoreIndented;
			pastOpeningBreak = true;

			// are we done via indentation?
165
			if(!emptyLine && INPUT.column() < params.indent) {
166
167
168
169
170
171
172
				params.leadingSpaces = true;
				break;
			}
		}

		// post-processing
		if(params.trimTrailingSpaces) {
173
			std::size_t pos = scalar.find_last_not_of(' ');
174
175
176
177
			if(pos < scalar.size())
				scalar.erase(pos + 1);
		}

178
		if(params.chomp == STRIP || params.chomp == CLIP) {
179
			std::size_t pos = scalar.find_last_not_of('\n');
180
			if(params.chomp == CLIP && pos + 1 < scalar.size())
181
				scalar.erase(pos + 2);
182
			else if(params.chomp == STRIP && pos < scalar.size())
183
184
185
186
187
188
				scalar.erase(pos + 1);
		}

		return scalar;
	}
}