scanscalar.cpp 5.12 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;
jbeder's avatar
jbeder committed
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
			while(!params.end.Matches(INPUT) && !Exp::Break.Matches(INPUT)) {
35
				if(!INPUT)
36
37
38
					break;

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

				foundNonEmptyLine = true;
				pastOpeningBreak = true;

				// escaped newline? (only if we're escaping on slash)
				if(params.escape == '\\' && Exp::EscBreak.Matches(INPUT)) {
					int n = Exp::EscBreak.Match(INPUT);
					INPUT.eat(n);
53
					lastNonWhitespaceChar = scalar.size();
54
55
56
57
58
59
					continue;
				}

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

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

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

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

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

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

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

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

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

				if(!params.eatLeadingWhitespace)
					break;

				INPUT.eat(1);
			}

			// was this an empty line?
			bool nextEmptyLine = Exp::Break.Matches(INPUT);
124
			bool nextMoreIndented = Exp::Blank.Matches(INPUT);
125
			if(params.fold == FOLD_BLOCK && foldedNewlineCount == 0 && nextEmptyLine)
jbeder's avatar
jbeder committed
126
				foldedNewlineStartedMoreIndented = moreIndented;
127
128

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

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

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

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

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

		return scalar;
	}
}