From 0603db21d2622588ab35389d3ddcaac7410c9f11 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Mon, 19 Sep 2011 10:27:23 +0200 Subject: Recognize C++ keywords when parsing C++ expressions in pragmas --- odb/cxx-lexer.cxx | 46 ++++++++++++++++++++++------------------------ odb/cxx-lexer.hxx | 6 ++++++ odb/pragma.cxx | 13 +++++++++++-- 3 files changed, 39 insertions(+), 26 deletions(-) diff --git a/odb/cxx-lexer.cxx b/odb/cxx-lexer.cxx index 7076e8b..6124855 100644 --- a/odb/cxx-lexer.cxx +++ b/odb/cxx-lexer.cxx @@ -79,39 +79,37 @@ start (tree& token, cpp_ttype& type) cpp_ttype cxx_pragma_lexer:: next (string& token) { - *type_ = pragma_lex (token_); + next (*token_); token = translate (); return *type_; } +cpp_ttype cxx_pragma_lexer:: +next (tree& token) +{ + *type_ = pragma_lex (token_); + + // See if this is a keyword using the C++ parser machinery and + // the current C++ dialect. + // + if (*type_ == CPP_NAME && C_IS_RESERVED_WORD (*token_)) + *type_ = CPP_KEYWORD; + + if (&token != token_) + token = *token_; + + return *type_; +} + string cxx_pragma_lexer:: translate () { string r; - switch (*type_) - { - case CPP_NAME: - { - r = IDENTIFIER_POINTER (*token_); - - // See if this is a keyword using the C++ parser machinery and - // the current C++ dialect. - // - tree id (get_identifier (r.c_str ())); - - if (C_IS_RESERVED_WORD (id)) - *type_ = CPP_KEYWORD; - break; - } - case CPP_STRING: - { - r = TREE_STRING_POINTER (*token_); - break; - } - default: - break; - } + if (*type_ == CPP_NAME || *type_ == CPP_KEYWORD) + r = IDENTIFIER_POINTER (*token_); + else if (*type_ == CPP_STRING) + r = TREE_STRING_POINTER (*token_); return r; } diff --git a/odb/cxx-lexer.hxx b/odb/cxx-lexer.hxx index e5b117e..11f4070 100644 --- a/odb/cxx-lexer.hxx +++ b/odb/cxx-lexer.hxx @@ -72,6 +72,12 @@ public: virtual cpp_ttype next (std::string& token); + // This pragma-specific version of next() returns a token as a tree + // node. + // + cpp_ttype + next (tree& token); + private: std::string translate (); diff --git a/odb/pragma.cxx b/odb/pragma.cxx index 66c026a..76e2968 100644 --- a/odb/pragma.cxx +++ b/odb/pragma.cxx @@ -51,11 +51,14 @@ parse_expression (tree& t, string const& prag) { // Keep reading tokens until we see a matching ')' while keeping track - // of their balance. + // of their balance. Also switch to the pragma lexer so that we detect + // C++ keywords (this is a C++ expression). // size_t balance (0); + cxx_pragma_lexer lex; + lex.start (t, tt); - for (; tt != CPP_EOF; tt = pragma_lex (&t)) + for (; tt != CPP_EOF; tt = lex.next (t)) { bool done (false); cxx_token ct; @@ -81,6 +84,7 @@ parse_expression (tree& t, break; } case CPP_NAME: + //case CPP_KEYWORD: see default: { ct.literal = IDENTIFIER_POINTER (t); break; @@ -161,6 +165,11 @@ parse_expression (tree& t, } default: { + // CPP_KEYWORD is not in the cpp_ttype enumeration. + // + if (tt == CPP_KEYWORD) + ct.literal = IDENTIFIER_POINTER (t); + break; } } -- cgit v1.1