@@ -40,25 +40,33 @@ SMLoc FormatParser::getLoc() {
4040 return SMLoc::getFromPointer (loc.getPointer () + pos);
4141}
4242
43+ bool FormatParser::isWhitespace (char c) {
44+ return c == ' ' || c == ' \t ' || c == ' \n ' || c == ' \r ' ;
45+ }
46+
4347void FormatParser::skipWhitespace () {
44- while (pos < input.size () && (input[pos] == ' ' || input[pos] == ' \t ' ))
48+ while (pos < input.size () && isWhitespace (input[pos]))
4549 ++pos;
4650}
4751
52+ void FormatParser::consumeAtLeastOneWhitespace () {
53+ if (pos >= input.size () || !isWhitespace (input[pos]))
54+ PrintFatalError (getLoc (), " expected whitespace" );
55+ skipWhitespace ();
56+ }
57+
4858void FormatParser::consume (StringRef str, bool skipWhitespaceFirst) {
4959 auto loc = getLoc ();
5060 if (!tryConsume (str, skipWhitespaceFirst))
5161 PrintFatalError (loc, " expected '" + str + " '" );
5262}
5363
5464bool FormatParser::tryConsume (StringRef str, bool skipWhitespaceFirst) {
55- auto savedPos = pos;
65+ assert (!str.empty () && " cannot consume empty string" );
66+ assert (llvm::none_of (str, [&](char c) { return isWhitespace (c); }) &&
67+ " string to consume must not be whitespace" );
5668
57- // In case the user tries to consume whitespace.
58- if (input.substr (pos).starts_with (str)) {
59- pos += str.size ();
60- return true ;
61- }
69+ auto savedPos = pos;
6270
6371 if (skipWhitespaceFirst)
6472 skipWhitespace ();
@@ -160,7 +168,7 @@ FormatNode *FormatParser::parseOptionalBinaryLiteral() {
160168 return nullptr ;
161169
162170 // Check for invalid characters after the binary digits
163- if (!isAtScopeEnd () && input[pos] != ' ' )
171+ if (!isAtScopeEnd () && ! isWhitespace ( input[pos]) )
164172 PrintFatalError (getLoc (), " invalid character ('" +
165173 input.substr (pos, pos + 1 ) +
166174 " ') after binary literal" );
@@ -194,10 +202,10 @@ FormatNode *FormatParser::parseOptionalSignednessSpecifier() {
194202 bool isSigned = false ;
195203 StringRef specifierName;
196204
197- if (tryConsume (" signed(" , false )) {
205+ if (tryConsume (" signed(" )) {
198206 isSigned = true ;
199207 specifierName = " 'signed'" ;
200- } else if (tryConsume (" unsigned(" , false )) {
208+ } else if (tryConsume (" unsigned(" )) {
201209 isSigned = false ;
202210 specifierName = " 'unsigned'" ;
203211 } else {
@@ -254,10 +262,9 @@ FormatNode *FormatParser::parseNextNode() {
254262 " unexpected character '" +
255263 Twine (input[std::min (pos, input.size () - 1 )]) + " '" );
256264
257- // Elements in the format are separated by a space .
265+ // Elements in the format are separated by whitespace .
258266 if (!isAtScopeEnd ())
259- consume (" " );
260- skipWhitespace ();
267+ consumeAtLeastOneWhitespace ();
261268
262269 return node;
263270}
@@ -267,7 +274,7 @@ FormatNode *FormatParser::parseOptionalIfThenElse() {
267274 if (!tryConsume (" if" ))
268275 return nullptr ;
269276
270- consume ( " " );
277+ consumeAtLeastOneWhitespace ( );
271278
272279 // Parse the condition operand (must be a 1-bit immediate)
273280 FormatNode *condNode = parseOptionalOperand ();
@@ -276,7 +283,7 @@ FormatNode *FormatParser::parseOptionalIfThenElse() {
276283
277284 auto *condition = cast<OperandNode>(condNode);
278285
279- consume ( " " );
286+ consumeAtLeastOneWhitespace ( );
280287
281288 SmallVector<FormatNode *> thenBranch;
282289 bool hasThen = tryConsume (" then" );
@@ -328,9 +335,10 @@ FormatNode *FormatParser::parseOptionalCustomDirective() {
328335 auto *operandNode = cast<OperandNode>(argNode);
329336 arguments.push_back (operandNode);
330337
331- // Arguments are separated by spaces, but the last one is followed by ')'
338+ // Arguments are separated by whitespace, but the last one is followed by
339+ // ')'
332340 if (!tryConsume (" )" )) {
333- consume ( " " );
341+ consumeAtLeastOneWhitespace ( );
334342 continue ;
335343 }
336344 break ;
0 commit comments