scanscalar.cpp 4.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include "crt.h"
#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)
	{
		bool foundNonEmptyLine = false, pastOpeningBreak = false;
		bool emptyLine = false, moreIndented = false;
jbeder's avatar
jbeder committed
24
25
		int foldedNewlineCount = 0;
		bool foldedNewlineStartedMoreIndented = false;
26
27
28
29
30
31
32
		std::string scalar;
		params.leadingSpaces = false;

		while(INPUT) {
			// ********************************
			// Phase #1: scan until line ending
			while(!params.end.Matches(INPUT) && !Exp::Break.Matches(INPUT)) {
33
				if(!INPUT)
34
35
36
					break;

				// document indicator?
37
				if(INPUT.column() == 0 && Exp::DocIndicator.Matches(INPUT)) {
38
39
40
					if(params.onDocIndicator == BREAK)
						break;
					else if(params.onDocIndicator == THROW)
41
						throw ParserException(INPUT.mark(), ErrorMsg::DOC_IN_SCALAR);
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
				}

				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);
					continue;
				}

				// escape this?
				if(INPUT.peek() == params.escape) {
					scalar += Exp::Escape(INPUT);
					continue;
				}

				// otherwise, just add the damn character
				scalar += INPUT.get();
			}

			// eof? if we're looking to eat something, then we throw
65
			if(!INPUT) {
66
				if(params.eatEnd)
67
					throw ParserException(INPUT.mark(), ErrorMsg::EOF_IN_SCALAR);
68
69
70
71
				break;
			}

			// doc indicator?
72
			if(params.onDocIndicator == BREAK && INPUT.column() == 0 && Exp::DocIndicator.Matches(INPUT))
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
				break;

			// are we done via character match?
			int n = params.end.Match(INPUT);
			if(n >= 0) {
				if(params.eatEnd)
					INPUT.eat(n);
				break;
			}

			// ********************************
			// Phase #2: eat line ending
			n = Exp::Break.Match(INPUT);
			INPUT.eat(n);

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

			// first the required indentation
92
			while(INPUT.peek() == ' ' && (INPUT.column() < params.indent || (params.detectIndent && !foundNonEmptyLine)))
93
94
95
96
				INPUT.eat(1);

			// update indent if we're auto-detecting
			if(params.detectIndent && !foundNonEmptyLine)
97
				params.indent = std::max(params.indent, INPUT.column());
98
99
100
101

			// and then the rest of the whitespace
			while(Exp::Blank.Matches(INPUT)) {
				// we check for tabs that masquerade as indentation
102
103
				if(INPUT.peek() == '\t'&& INPUT.column() < params.indent && params.onTabInIndentation == THROW)
					throw ParserException(INPUT.mark(), ErrorMsg::TAB_IN_INDENTATION);
104
105
106
107
108
109
110
111
112
113

				if(!params.eatLeadingWhitespace)
					break;

				INPUT.eat(1);
			}

			// was this an empty line?
			bool nextEmptyLine = Exp::Break.Matches(INPUT);
			bool nextMoreIndented = (INPUT.peek() == ' ');
jbeder's avatar
jbeder committed
114
115
			if(params.fold && foldedNewlineCount == 0 && nextEmptyLine)
				foldedNewlineStartedMoreIndented = moreIndented;
116
117

			// for block scalars, we always start with a newline, so we should ignore it (not fold or keep)
118
			if(pastOpeningBreak) {
jbeder's avatar
jbeder committed
119
120
121
122
123
124
125
126
127
128
129
130
131
132
				if(params.fold) {
					if(!emptyLine && !nextEmptyLine && !moreIndented && !nextMoreIndented && INPUT.column() >= params.indent)
						scalar += " ";
					else if(nextEmptyLine)
						foldedNewlineCount++;
					else
						scalar += "\n";
					
					if(!nextEmptyLine && foldedNewlineCount > 0) {
						if(foldedNewlineStartedMoreIndented || nextMoreIndented)
							scalar += std::string("\n", foldedNewlineCount);
						foldedNewlineCount = 0;
					}
				} else {
133
					scalar += "\n";
jbeder's avatar
jbeder committed
134
				}
135
136
137
138
139
140
141
			}

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

			// are we done via indentation?
142
			if(!emptyLine && INPUT.column() < params.indent) {
143
144
145
146
147
148
149
				params.leadingSpaces = true;
				break;
			}
		}

		// post-processing
		if(params.trimTrailingSpaces) {
150
			std::size_t pos = scalar.find_last_not_of(' ');
151
152
153
154
			if(pos < scalar.size())
				scalar.erase(pos + 1);
		}

155
		if(params.chomp == STRIP || params.chomp == CLIP) {
156
			std::size_t pos = scalar.find_last_not_of('\n');
157
			if(params.chomp == CLIP && pos + 1 < scalar.size())
158
				scalar.erase(pos + 2);
159
			else if(params.chomp == STRIP && pos < scalar.size())
160
161
162
163
164
165
				scalar.erase(pos + 1);
		}

		return scalar;
	}
}