[cfe-commits] r38860 - in /cfe/cfe/trunk: Parse/ParseExpr.cpp include/clang/Basic/DiagnosticKinds.def include/clang/Parse/Parser.h
sabre at cs.uiuc.edu
sabre at cs.uiuc.edu
Wed Jul 11 09:25:16 PDT 2007
Author: sabre
Date: Wed Jul 11 11:25:15 2007
New Revision: 38860
URL: http://llvm.org/viewvc/llvm-project?rev=38860&view=rev
Log:
Parse primary expressions, handle string concatenation
Modified:
cfe/cfe/trunk/Parse/ParseExpr.cpp
cfe/cfe/trunk/include/clang/Basic/DiagnosticKinds.def
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=38860&r1=38859&r2=38860&view=diff
==============================================================================
--- cfe/cfe/trunk/Parse/ParseExpr.cpp (original)
+++ cfe/cfe/trunk/Parse/ParseExpr.cpp Wed Jul 11 11:25:15 2007
@@ -25,12 +25,7 @@
Parser::ExprTy Parser::ParseExpression() {
- if (Tok.getKind() == tok::numeric_constant) {
- ConsumeToken();
- return 0;
- }
-
- Diag(Tok, diag::err_parse_error);
+ ParsePostfixExpression();
return 0;
}
@@ -39,11 +34,95 @@
ParseExpression();
}
-/// primary-expression:
+/// ParsePostfixExpression
+/// postfix-expression: [C99 6.5.2]
+/// primary-expression
+/// postfix-expression '[' expression ']'
+/// postfix-expression '(' argument-expression-list[opt] ')'
+/// postfix-expression '.' identifier
+/// postfix-expression '->' identifier
+/// postfix-expression '++'
+/// postfix-expression '--'
+/// '(' type-name ')' '{' initializer-list '}'
+/// '(' type-name ')' '{' initializer-list ',' '}'
+///
+/// argument-expression-list: [C99 6.5.2]
+/// argument-expression
+/// argument-expression-list ',' argument-expression
+///
+/// primary-expression: [C99 6.5.1]
/// identifier
/// constant
/// string-literal
/// '(' expression ')'
+/// '__func__' [C99 6.4.2.2]
+/// [GNU] '__FUNCTION__'
+/// [GNU] '__PRETTY_FUNCTION__'
+/// [GNU] '(' compound-statement ')'
+/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
+/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
+/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
+/// assign-expr ')'
+/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
+/// [OBC] '[' objc-receiver objc-message-args ']' [TODO]
+/// [OBC] '@selector' '(' objc-selector-arg ')' [TODO]
+/// [OBC] '@protocol' '(' identifier ')' [TODO]
+/// [OBC] '@encode' '(' type-name ')' [TODO]
+/// [OBC] objc-string-literal [TODO]
+///
+/// constant: [C99 6.4.4]
+/// integer-constant
+/// floating-constant
+/// enumeration-constant -> identifier
+/// character-constant
+///
+/// [GNU] offsetof-member-designator:
+/// [GNU] identifier
+/// [GNU] offsetof-member-designator '.' identifier
+/// [GNU] offsetof-member-designator '[' expression ']'
+///
+void Parser::ParsePostfixExpression() {
+ // First step, parse the primary expression.
+ switch (Tok.getKind()) {
+ // primary-expression
+ case tok::identifier: // primary-expression: identifier
+ // constant: enumeration-constant
+ case tok::numeric_constant: // constant: integer-constant
+ // constant: floating-constant
+ case tok::char_constant: // constant: character-constant
+ case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
+ case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
+ case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
+ ConsumeToken();
+ break;
+ case tok::string_literal: // primary-expression: string-literal
+ ParseStringLiteralExpression();
+ break;
+ case tok::l_paren: // primary-expression: '(' expression ')'
+ ParseParenExpression();
+ break;
+
+ default:
+ Diag(Tok, diag::err_expected_expression);
+ return;
+ }
+}
+
+/// ParseStringLiteralExpression - This handles the various token types that
+/// form string literals, and also handles string concatenation [C99 5.1.1.2,
+/// translation phase #6].
+///
+/// primary-expression: [C99 6.5.1]
+/// string-literal
+void Parser::ParseStringLiteralExpression() {
+ assert(isStringLiteral() && "Not a string literal!");
+ ConsumeStringToken();
+
+ // String concat. Note that keywords like __func__ and __FUNCTION__ aren't
+ // considered to be strings.
+ while (isStringLiteral())
+ ConsumeStringToken();
+}
/// ParseParenExpression - C99 c.5.1p5
Modified: cfe/cfe/trunk/include/clang/Basic/DiagnosticKinds.def
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/include/clang/Basic/DiagnosticKinds.def?rev=38860&r1=38859&r2=38860&view=diff
==============================================================================
--- cfe/cfe/trunk/include/clang/Basic/DiagnosticKinds.def (original)
+++ cfe/cfe/trunk/include/clang/Basic/DiagnosticKinds.def Wed Jul 11 11:25:15 2007
@@ -262,6 +262,8 @@
// Generic errors.
DIAG(err_parse_error, ERROR,
"parse error")
+DIAG(err_expected_expression, ERROR,
+ "expected expression")
DIAG(err_expected_ident, ERROR,
"expected identifier")
DIAG(err_expected_ident_lparen, ERROR,
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=38860&r1=38859&r2=38860&view=diff
==============================================================================
--- cfe/cfe/trunk/include/clang/Parse/Parser.h (original)
+++ cfe/cfe/trunk/include/clang/Parse/Parser.h Wed Jul 11 11:25:15 2007
@@ -75,13 +75,19 @@
return Tok.getKind() == tok::l_brace || Tok.getKind() == tok::r_brace;
}
+ /// isStringLiteral - True if this token is a string-literal.
+ ///
+ bool isStringLiteral() const {
+ return Tok.getKind() == tok::string_literal;
+ }
+
/// ConsumeToken - Consume the current 'peek token' and lex the next one.
/// 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(Tok.getKind() != tok::string_literal &&
- !isTokenParen() && !isTokenBracket() && !isTokenBrace() &&
+ assert(!isStringLiteral() && !isTokenParen() && !isTokenBracket() &&
+ !isTokenBrace() &&
"Should consume special tokens with Consume*Token");
PP.Lex(Tok);
}
@@ -127,12 +133,9 @@
/// handles string literal concatenation, as per C99 5.1.1.2, translation
/// phase #6.
void ConsumeStringToken() {
- assert(Tok.getKind() != tok::string_literal &&
- "Should consume special tokens with Consume*Token");
- // Due to string literal concatenation, all consequtive string literals are
- // a single token.
- while (Tok.getKind() == tok::string_literal)
- PP.Lex(Tok);
+ assert(isStringLiteral() &&
+ "Should only consume string literals with this method");
+ PP.Lex(Tok);
}
private:
@@ -160,7 +163,10 @@
// C99 6.5: Expressions.
//ExprTy ParseExpression(); // Above.
void ParseAssignmentExpression(); // Expr that doesn't include commas.
+
+ void ParsePostfixExpression();
void ParseParenExpression();
+ void ParseStringLiteralExpression();
void ParseInitializer(); // C99 6.7.8
More information about the cfe-commits
mailing list