[cfe-commits] r38894 - in /cfe/cfe/trunk: Parse/ParseExpr.cpp Parse/Parser.cpp include/clang/Parse/Parser.h
sabre at cs.uiuc.edu
sabre at cs.uiuc.edu
Wed Jul 11 09:25:33 PDT 2007
Author: sabre
Date: Wed Jul 11 11:25:33 2007
New Revision: 38894
URL: http://llvm.org/viewvc/llvm-project?rev=38894&view=rev
Log:
Add a new ExpectAndConsume method to make parsing easier, and add a new
ConsumeAnyToken method.
Modified:
cfe/cfe/trunk/Parse/ParseExpr.cpp
cfe/cfe/trunk/Parse/Parser.cpp
cfe/cfe/trunk/include/clang/Parse/Parser.h
Modified: cfe/cfe/trunk/Parse/ParseExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/Parse/ParseExpr.cpp?rev=38894&r1=38893&r2=38894&view=diff
==============================================================================
--- cfe/cfe/trunk/Parse/ParseExpr.cpp (original)
+++ cfe/cfe/trunk/Parse/ParseExpr.cpp Wed Jul 11 11:25:33 2007
@@ -639,32 +639,22 @@
case tok::kw___builtin_offsetof:
ParseTypeName();
- if (Tok.getKind() != tok::comma) {
- Diag(Tok, diag::err_expected_comma);
- SkipUntil(tok::r_paren);
+ if (ExpectAndConsume(tok::comma, diag::err_expected_comma, tok::r_paren))
return ExprResult(true);
- }
- ConsumeToken();
// We must have at least one identifier here.
- if (Tok.getKind() != tok::identifier) {
- Diag(Tok, diag::err_expected_ident);
- SkipUntil(tok::r_paren);
+ if (ExpectAndConsume(tok::identifier, diag::err_expected_ident,
+ tok::r_paren))
return ExprResult(true);
- }
- ConsumeToken();
-
+
while (1) {
if (Tok.getKind() == tok::period) {
// offsetof-member-designator: offsetof-member-designator '.' identifier
ConsumeToken();
- if (Tok.getKind() != tok::identifier) {
- Diag(Tok, diag::err_expected_ident);
- SkipUntil(tok::r_paren);
+ if (ExpectAndConsume(tok::identifier, diag::err_expected_ident,
+ tok::r_paren))
return ExprResult(true);
- }
- ConsumeToken();
} else if (Tok.getKind() == tok::l_square) {
// offsetof-member-designator: offsetof-member-design '[' expression ']'
SourceLocation LSquareLoc = Tok.getLocation();
@@ -685,33 +675,21 @@
case tok::kw___builtin_choose_expr:
Res = ParseAssignmentExpression();
- if (Tok.getKind() != tok::comma) {
- Diag(Tok, diag::err_expected_comma);
- SkipUntil(tok::r_paren);
+ if (ExpectAndConsume(tok::comma, diag::err_expected_comma, tok::r_paren))
return ExprResult(true);
- }
- ConsumeToken();
Res = ParseAssignmentExpression();
- if (Tok.getKind() != tok::comma) {
- Diag(Tok, diag::err_expected_comma);
- SkipUntil(tok::r_paren);
+ if (ExpectAndConsume(tok::comma, diag::err_expected_comma, tok::r_paren))
return ExprResult(true);
- }
- ConsumeToken();
Res = ParseAssignmentExpression();
break;
case tok::kw___builtin_types_compatible_p:
ParseTypeName();
- if (Tok.getKind() != tok::comma) {
- Diag(Tok, diag::err_expected_comma);
- SkipUntil(tok::r_paren);
+ if (ExpectAndConsume(tok::comma, diag::err_expected_comma, tok::r_paren))
return ExprResult(true);
- }
- ConsumeToken();
ParseTypeName();
break;
Modified: cfe/cfe/trunk/Parse/Parser.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/Parse/Parser.cpp?rev=38894&r1=38893&r2=38894&view=diff
==============================================================================
--- cfe/cfe/trunk/Parse/Parser.cpp (original)
+++ cfe/cfe/trunk/Parse/Parser.cpp Wed Jul 11 11:25:33 2007
@@ -45,14 +45,7 @@
const char *LHSName, unsigned DiagID) {
if (Tok.getKind() == RHSTok) {
- if (isTokenParen())
- ConsumeParen();
- else if (isTokenBracket())
- ConsumeBracket();
- else if (isTokenBrace())
- ConsumeBrace();
- else
- ConsumeParen();
+ ConsumeAnyToken();
} else {
Diag(Tok, DiagID);
Diag(LHSLoc, diag::err_matching, LHSName);
@@ -60,6 +53,25 @@
}
}
+/// ExpectAndConsume - The parser expects that 'ExpectedTok' is next in the
+/// input. If so, it is consumed and false is returned.
+///
+/// If the input is malformed, this emits the specified diagnostic. Next, if
+/// SkipToTok is specified, it calls SkipUntil(SkipToTok). Finally, true is
+/// returned.
+bool Parser::ExpectAndConsume(tok::TokenKind ExpectedTok, unsigned DiagID,
+ tok::TokenKind SkipToTok) {
+ if (Tok.getKind() == ExpectedTok) {
+ ConsumeToken();
+ return false;
+ }
+
+ Diag(Tok, DiagID);
+ if (SkipToTok != tok::unknown)
+ SkipUntil(SkipToTok);
+ return true;
+}
+
//===----------------------------------------------------------------------===//
// Error recovery.
//===----------------------------------------------------------------------===//
@@ -81,16 +93,8 @@
if (Tok.getKind() == T) {
if (DontConsume) {
// Noop, don't consume the token.
- } else if (isTokenParen()) {
- ConsumeParen();
- } else if (isTokenBracket()) {
- ConsumeBracket();
- } else if (isTokenBrace()) {
- ConsumeBrace();
- } else if (isTokenStringLiteral()) {
- ConsumeStringToken();
} else {
- ConsumeToken();
+ ConsumeAnyToken();
}
return true;
}
Modified: cfe/cfe/trunk/include/clang/Parse/Parser.h
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/include/clang/Parse/Parser.h?rev=38894&r1=38893&r2=38894&view=diff
==============================================================================
--- cfe/cfe/trunk/include/clang/Parse/Parser.h (original)
+++ cfe/cfe/trunk/include/clang/Parse/Parser.h Wed Jul 11 11:25:33 2007
@@ -93,13 +93,26 @@
/// This does not work will all kinds of tokens: strings and specific other
/// tokens must be consumed with custom methods below.
void ConsumeToken() {
- // Note: update Parser::SkipUntil if any other special tokens are added.
assert(!isTokenStringLiteral() && !isTokenParen() && !isTokenBracket() &&
!isTokenBrace() &&
"Should consume special tokens with Consume*Token");
PP.Lex(Tok);
}
+ /// ConsumeAnyToken - Dispatch to the right Consume* method based on the
+ /// current token type. This should only be used in cases where the type of
+ /// the token really isn't known, e.g. in error recovery.
+ void ConsumeAnyToken() {
+ if (isTokenParen())
+ ConsumeParen();
+ else if (isTokenBracket())
+ ConsumeBracket();
+ else if (isTokenBrace())
+ ConsumeBrace();
+ else
+ ConsumeToken();
+ }
+
/// ConsumeParen - This consume method keeps the paren count up-to-date.
///
void ConsumeParen() {
@@ -154,6 +167,14 @@
void MatchRHSPunctuation(tok::TokenKind RHSTok, SourceLocation LHSLoc,
const char *LHSName, unsigned Diag);
+ /// ExpectAndConsume - The parser expects that 'ExpectedTok' is next in the
+ /// input. If so, it is consumed and false is returned.
+ ///
+ /// If the input is malformed, this emits the specified diagnostic. Next, if
+ /// SkipToTok is specified, it calls SkipUntil(SkipToTok). Finally, true is
+ /// returned.
+ bool ExpectAndConsume(tok::TokenKind ExpectedTok, unsigned Diag,
+ tok::TokenKind SkipToTok = tok::unknown);
private:
//===--------------------------------------------------------------------===//
// Error recovery.
More information about the cfe-commits
mailing list