"cacheflow/git@developer.sourcefind.cn:SIYIXNI/vllm.git" did not exist on "a90c97d72705f57b589062a2e09917dd9d27e389"
scanscalar.cpp 6.97 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
	std::string ScanScalar(Stream& INPUT, ScanScalarInfo& info)
11
	{
12
		bool foundNonEmptyLine = false;
13
14
		bool emptyLine = false, moreIndented = false;
		std::string scalar;
15
		info.leadingSpaces = false;
16
17
18
19

		while(INPUT) {
			// ********************************
			// Phase #1: scan until line ending
20
			while(!info.end.Matches(INPUT) && !Exp::Break.Matches(INPUT)) {
21
22
23
				if(INPUT.peek() == EOF)
					break;

24
25
26
27
28
29
30
31
32
33
				// document indicator?
				if(INPUT.column == 0 && Exp::DocIndicator.Matches(INPUT)) {
					if(info.onDocIndicator == BREAK)
						break;
					else if(info.onDocIndicator == THROW)
						throw IllegalDocIndicator();
				}

				foundNonEmptyLine = true;

34
				// escaped newline? (only if we're escaping on slash)
35
				if(info.escape == '\\' && Exp::EscBreak.Matches(INPUT)) {
36
					int n = Exp::EscBreak.Match(INPUT);
37
					INPUT.Eat(n);
38
39
40
41
					continue;
				}

				// escape this?
42
				if(INPUT.peek() == info.escape) {
43
					scalar += Exp::Escape(INPUT);
44
45
46
47
					continue;
				}

				// otherwise, just add the damn character
48
				scalar += INPUT.GetChar();
49
50
51
52
			}

			// eof? if we're looking to eat something, then we throw
			if(INPUT.peek() == EOF) {
53
				if(info.eatEnd)
54
					throw IllegalEOF();
55
56
57
				break;
			}

58
59
60
61
			// doc indicator?
			if(info.onDocIndicator == BREAK && INPUT.column == 0 && Exp::DocIndicator.Matches(INPUT))
				break;

62
			// are we done via character match?
63
			int n = info.end.Match(INPUT);
64
			if(n >= 0) {
65
				if(info.eatEnd)
66
					INPUT.Eat(n);
67
68
69
70
71
72
				break;
			}

			// ********************************
			// Phase #2: eat line ending
			n = Exp::Break.Match(INPUT);
73
			INPUT.Eat(n);
74
75
76
77
78

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

			// first the required indentation
79
			while(INPUT.peek() == ' ' && (INPUT.column < info.indent || (info.detectIndent && !foundNonEmptyLine)))
80
				INPUT.Eat(1);
81

82
83
84
85
			// update indent if we're auto-detecting
			if(info.detectIndent && !foundNonEmptyLine)
				info.indent = std::max(info.indent, INPUT.column);

86
			// and then the rest of the whitespace
87
88
89
90
91
92
93
94
95
			while(Exp::Blank.Matches(INPUT)) {
				// we check for tabs that masquerade as indentation
				if(INPUT.peek() == '\t'&& INPUT.column < info.indent && info.onTabInIndentation == THROW)
					throw IllegalTabInIndentation();

				if(!info.eatLeadingWhitespace)
					break;

				INPUT.Eat(1);
96
97
98
99
100
101
			}

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

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

104
			if(info.fold && !emptyLine && !nextEmptyLine && !moreIndented && !nextMoreIndented)
105
106
				scalar += " ";
			else
107
				scalar += "\n";
108
109
110
111
112

			emptyLine = nextEmptyLine;
			moreIndented = nextMoreIndented;

			// are we done via indentation?
113
114
			if(!emptyLine && INPUT.column < info.indent) {
				info.leadingSpaces = true;
115
				break;
116
			}
117
118
119
		}

		// post-processing
120
		if(info.trimTrailingSpaces) {
121
122
123
124
125
			unsigned pos = scalar.find_last_not_of(' ');
			if(pos < scalar.size())
				scalar.erase(pos + 1);
		}

126
		if(info.chomp <= 0) {
127
			unsigned pos = scalar.find_last_not_of('\n');
128
			if(info.chomp == 0 && pos + 1 < scalar.size())
129
				scalar.erase(pos + 2);
130
			else if(info.chomp == -1 && pos < scalar.size())
131
132
133
134
135
				scalar.erase(pos + 1);
		}

		return scalar;
	}
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254

	// PlainScalarToken
	template <> PlainScalarToken *Scanner::ScanToken(PlainScalarToken *pToken)
	{
		// set up the scanning parameters
		ScanScalarInfo info;
		info.end = (m_flowLevel > 0 ? Exp::EndScalarInFlow : Exp::EndScalar) || (RegEx(' ') + Exp::Comment);
		info.eatEnd = false;
		info.indent = (m_flowLevel > 0 ? 0 : m_indents.top() + 1);
		info.fold = true;
		info.eatLeadingWhitespace = true;
		info.trimTrailingSpaces = true;
		info.chomp = CLIP;
		info.onDocIndicator = BREAK;
		info.onTabInIndentation = THROW;

		// insert a potential simple key
		if(m_simpleKeyAllowed)
			InsertSimpleKey();

		pToken->value = ScanScalar(INPUT, info);

		// can have a simple key only if we ended the scalar by starting a new line
		m_simpleKeyAllowed = info.leadingSpaces;

		// finally, we can't have any colons in a scalar, so if we ended on a colon, there
		// had better be a break after it
		if(Exp::IllegalColonInScalar.Matches(INPUT))
			throw IllegalScalar();

		return pToken;
	}

	// QuotedScalarToken
	template <> QuotedScalarToken *Scanner::ScanToken(QuotedScalarToken *pToken)
	{
		// eat single or double quote
		char quote = INPUT.GetChar();
		pToken->single = (quote == '\'');

		// setup the scanning parameters
		ScanScalarInfo info;
		info.end = (pToken->single ? RegEx(quote) && !Exp::EscSingleQuote : RegEx(quote));
		info.eatEnd = true;
		info.escape = (pToken->single ? '\'' : '\\');
		info.indent = 0;
		info.fold = true;
		info.eatLeadingWhitespace = true;
		info.trimTrailingSpaces = false;
		info.chomp = CLIP;
		info.onDocIndicator = THROW;

		// insert a potential simple key
		if(m_simpleKeyAllowed)
			InsertSimpleKey();

		pToken->value = ScanScalar(INPUT, info);
		m_simpleKeyAllowed = false;

		return pToken;
	}

	// BlockScalarToken
	// . These need a little extra processing beforehand.
	// . We need to scan the line where the indicator is (this doesn't count as part of the scalar),
	//   and then we need to figure out what level of indentation we'll be using.
	template <> BlockScalarToken *Scanner::ScanToken(BlockScalarToken *pToken)
	{
		ScanScalarInfo info;
		info.indent = 1;
		info.detectIndent = true;

		// eat block indicator ('|' or '>')
		char indicator = INPUT.GetChar();
		info.fold = (indicator == Keys::FoldedScalar);

		// eat chomping/indentation indicators
		int n = Exp::Chomp.Match(INPUT);
		for(int i=0;i<n;i++) {
			char ch = INPUT.GetChar();
			if(ch == '+')
				info.chomp = KEEP;
			else if(ch == '-')
				info.chomp = STRIP;
			else if(Exp::Digit.Matches(ch)) {
				info.indent = ch - '0';
				info.detectIndent = false;
				if(info.indent == 0)
					throw ZeroIndentationInBlockScalar();
			}
		}

		// now eat whitespace
		while(Exp::Blank.Matches(INPUT))
			INPUT.Eat(1);

		// and comments to the end of the line
		if(Exp::Comment.Matches(INPUT))
			while(INPUT && !Exp::Break.Matches(INPUT))
				INPUT.Eat(1);

		// if it's not a line break, then we ran into a bad character inline
		if(INPUT && !Exp::Break.Matches(INPUT))
			throw UnexpectedCharacterInBlockScalar();

		// set the initial indentation
		if(m_indents.top() >= 0)
			info.indent += m_indents.top();

		info.eatLeadingWhitespace = false;
		info.trimTrailingSpaces = false;
		info.onTabInIndentation = THROW;

		pToken->value = ScanScalar(INPUT, info);

		// simple keys always ok after block scalars (since we're gonna start a new line anyways)
		m_simpleKeyAllowed = true;
		return pToken;
	}
255
}