Advertisement
dschneider

Tokenlist as enum and string

Aug 27th, 2012
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.84 KB | None | 0 0
  1. #include <iostream>
  2. #include <boost/preprocessor/comparison/greater.hpp>
  3. #include <boost/preprocessor/punctuation/comma_if.hpp>
  4. #include <boost/preprocessor/seq/enum.hpp>
  5. #include <boost/preprocessor/seq/for_each.hpp>
  6. #include <boost/preprocessor/stringize.hpp>
  7.  
  8. #define TOK_ENUM(ENUM_NAME, ENUM_SEQ) \
  9.   enum ENUM_NAME { BOOST_PP_SEQ_ENUM(ENUM_SEQ), BOOST_PP_CAT(NUM_, ENUM_NAME) };
  10.  
  11. #define TOK_HELPER(r, data, elem) \
  12.   BOOST_PP_COMMA_IF(BOOST_PP_GREATER(r,2)) BOOST_PP_STRINGIZE(elem)
  13.  
  14. #define TOK_NAMES(STRING_NAME, ENUM_SEQ) \
  15.   const char* const STRING_NAME[] = { BOOST_PP_SEQ_FOR_EACH(TOK_HELPER, ~, ENUM_SEQ) };
  16.  
  17.  
  18. #define TOK_SEQ (comment)(identifier)(string_literal)(char_constant)
  19.  
  20. int main()
  21. {
  22.   TOK_ENUM(TokKind, TOK_SEQ);
  23.   TOK_NAMES(tokNames, TOK_SEQ);
  24.  
  25.   std::cout << tokNames[identifier] << std::endl;
  26.  
  27.   return 0;
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement