Token.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // Copyright (c) 2008 Roberto Raggi <[email protected]>
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be included in
  11. // all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. // THE SOFTWARE.
  20. #include "Token.h"
  21. #include "Literals.h"
  22. using namespace CPlusPlus;
  23. const char *token_names[] = {
  24. (""), ("<error>"),
  25. ("<C++ comment>"), ("<C++ doxy comment>"),
  26. ("<comment>"), ("<doxy comment>"),
  27. ("<identifier>"),
  28. ("<numeric literal>"),
  29. ("<char literal>"), ("<wide char literal>"), ("<utf16 char literal>"), ("<utf32 char literal>"),
  30. ("<string literal>"), ("<wide string literal>"), ("<utf8 string literal>"),
  31. ("<utf16 string literal>"), ("<utf32 string literal>"),
  32. ("<raw string literal>"), ("<raw wide string literal>"), ("<raw utf8 string literal>"),
  33. ("<raw utf16 string literal>"), ("<raw utf32 string literal>"),
  34. ("<@string literal>"), ("<angle string literal>"),
  35. ("&"), ("&&"), ("&="), ("->"), ("->*"), ("^"), ("^="), (":"), ("::"),
  36. (","), ("/"), ("/="), ("."), ("..."), (".*"), ("="), ("=="), ("!"),
  37. ("!="), (">"), (">="), (">>"), (">>="), ("{"), ("["), ("<"), ("<="),
  38. ("<<"), ("<<="), ("("), ("-"), ("-="), ("--"), ("%"), ("%="), ("|"),
  39. ("|="), ("||"), ("+"), ("+="), ("++"), ("#"), ("##"), ("?"), ("}"),
  40. ("]"), (")"), (";"), ("*"), ("*="), ("~"), ("~="),
  41. ("alignas"), ("alignof"), ("asm"), ("auto"), ("break"), ("case"), ("catch"),
  42. ("class"), ("const"), ("const_cast"), ("constexpr"), ("continue"),
  43. ("decltype"), ("default"),
  44. ("delete"), ("do"), ("dynamic_cast"), ("else"), ("enum"),
  45. ("explicit"), ("export"), ("extern"), ("false"), ("for"),
  46. ("friend"), ("goto"), ("if"), ("inline"),
  47. ("mutable"), ("namespace"), ("new"), ("noexcept"),
  48. ("nullptr"), ("operator"), ("private"),
  49. ("protected"), ("public"), ("register"), ("reinterpret_cast"),
  50. ("return"), ("sizeof"), ("static"), ("static_assert"),
  51. ("static_cast"), ("struct"), ("switch"), ("template"), ("this"), ("thread_local"),
  52. ("throw"), ("true"), ("try"), ("typedef"), ("typeid"), ("typename"),
  53. ("union"), ("using"), ("virtual"),
  54. ("volatile"), ("while"),
  55. // gnu
  56. ("__attribute__"), ("__thread"), ("__typeof__"),
  57. // objc @keywords
  58. ("@catch"), ("@class"), ("@compatibility_alias"), ("@defs"), ("@dynamic"),
  59. ("@encode"), ("@end"), ("@finally"), ("@implementation"), ("@interface"),
  60. ("@not_keyword"), ("@optional"), ("@package"), ("@private"), ("@property"),
  61. ("@protected"), ("@protocol"), ("@public"), ("@required"), ("@selector"),
  62. ("@synchronized"), ("@synthesize"), ("@throw"), ("@try"),
  63. // Primitive types
  64. ("bool"), ("char"), ("char16_t"), ("char32_t"), ("double"), ("float"), ("int"),
  65. ("long"), ("short"), ("signed"), ("unsigned"), ("void"), ("wchar_t"),
  66. // Qt keywords
  67. ("emit"), ("SIGNAL"), ("SLOT"), ("Q_SIGNAL"), ("Q_SLOT"), ("signals"), ("slots"),
  68. ("Q_FOREACH"), ("Q_D"), ("Q_Q"),
  69. ("Q_INVOKABLE"), ("Q_PROPERTY"), ("T_Q_PRIVATE_PROPERTY"),
  70. ("Q_INTERFACES"), ("Q_EMIT"), ("Q_ENUMS"), ("Q_FLAGS"),
  71. ("Q_PRIVATE_SLOT"), ("Q_DECLARE_INTERFACE"), ("Q_OBJECT"), ("Q_GADGET"),
  72. };
  73. void Token::reset()
  74. {
  75. flags = 0;
  76. byteOffset = 0;
  77. utf16charOffset = 0;
  78. ptr = 0;
  79. }
  80. const char *Token::name(int kind)
  81. { return token_names[kind]; }
  82. const char *Token::spell() const
  83. {
  84. switch (f.kind) {
  85. case T_IDENTIFIER:
  86. return identifier->chars();
  87. case T_NUMERIC_LITERAL:
  88. case T_CHAR_LITERAL:
  89. case T_WIDE_CHAR_LITERAL:
  90. case T_UTF16_CHAR_LITERAL:
  91. case T_UTF32_CHAR_LITERAL:
  92. case T_STRING_LITERAL:
  93. case T_WIDE_STRING_LITERAL:
  94. case T_UTF8_STRING_LITERAL:
  95. case T_UTF16_STRING_LITERAL:
  96. case T_UTF32_STRING_LITERAL:
  97. case T_RAW_STRING_LITERAL:
  98. case T_RAW_WIDE_STRING_LITERAL:
  99. case T_RAW_UTF8_STRING_LITERAL:
  100. case T_RAW_UTF16_STRING_LITERAL:
  101. case T_RAW_UTF32_STRING_LITERAL:
  102. case T_AT_STRING_LITERAL:
  103. case T_ANGLE_STRING_LITERAL:
  104. return literal->chars();
  105. default:
  106. return token_names[f.kind];
  107. } // switch
  108. }