"vscode:/vscode.git/clone" did not exist on "9fe6407ef575a86bf41368aa92cf7f1f69e7f9d7"
scanscalar.cpp 3.83 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;
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
42
						throw IllegalDocIndicator();
				}

				foundNonEmptyLine = true;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

111
112
			// TODO: for block scalars, we always start with a newline, so we should fold OR keep that

113
			if(params.fold && !emptyLine && !nextEmptyLine && !moreIndented && !nextMoreIndented)
114
115
				scalar += " ";
			else
116
				scalar += "\n";
117
118
119
120
121

			emptyLine = nextEmptyLine;
			moreIndented = nextMoreIndented;

			// are we done via indentation?
122
123
			if(!emptyLine && INPUT.column < params.indent) {
				params.leadingSpaces = true;
124
				break;
125
			}
126
127
128
		}

		// post-processing
129
		if(params.trimTrailingSpaces) {
130
131
132
133
134
			unsigned pos = scalar.find_last_not_of(' ');
			if(pos < scalar.size())
				scalar.erase(pos + 1);
		}

135
		if(params.chomp <= 0) {
136
			unsigned pos = scalar.find_last_not_of('\n');
137
			if(params.chomp == 0 && pos + 1 < scalar.size())
138
				scalar.erase(pos + 2);
139
			else if(params.chomp == -1 && pos < scalar.size())
140
141
142
143
144
				scalar.erase(pos + 1);
		}

		return scalar;
	}
145
}