Literals.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. #ifndef CPLUSPLUS_LITERALS_H
  21. #define CPLUSPLUS_LITERALS_H
  22. #include "CPlusPlusForwardDeclarations.h"
  23. #include "Token.h"
  24. #include "Name.h"
  25. namespace CPlusPlus {
  26. class CPLUSPLUS_EXPORT Literal
  27. {
  28. Literal(const Literal &other);
  29. void operator =(const Literal &other);
  30. public:
  31. typedef const char *iterator;
  32. typedef iterator const_iterator;
  33. public:
  34. Literal(const char *chars, unsigned size);
  35. virtual ~Literal();
  36. iterator begin() const { return _chars; }
  37. iterator end() const { return _chars + _size; }
  38. char at(unsigned index) const { return _chars[index]; }
  39. const char *chars() const { return _chars; }
  40. unsigned size() const { return _size; }
  41. unsigned hashCode() const { return _hashCode; }
  42. static unsigned hashCode(const char *chars, unsigned size);
  43. bool equalTo(const Literal *other) const;
  44. Literal *_next; // ## private
  45. private:
  46. char *_chars;
  47. unsigned _size;
  48. unsigned _hashCode;
  49. public:
  50. unsigned _index; // ### private
  51. };
  52. class CPLUSPLUS_EXPORT StringLiteral: public Literal
  53. {
  54. public:
  55. StringLiteral(const char *chars, unsigned size)
  56. : Literal(chars, size), _quotedString(false)
  57. { }
  58. // ATOMIC BEGIN
  59. bool _quotedString;
  60. // ATOMIC END
  61. };
  62. class CPLUSPLUS_EXPORT NumericLiteral: public Literal
  63. {
  64. public:
  65. NumericLiteral(const char *chars, unsigned size);
  66. enum {
  67. NumericLiteralIsInt,
  68. NumericLiteralIsFloat,
  69. NumericLiteralIsDouble,
  70. NumericLiteralIsLongDouble,
  71. NumericLiteralIsLong,
  72. NumericLiteralIsLongLong
  73. };
  74. bool isInt() const { return f._type == NumericLiteralIsInt; }
  75. bool isFloat() const { return f._type == NumericLiteralIsFloat; }
  76. bool isDouble() const { return f._type == NumericLiteralIsDouble; }
  77. bool isLongDouble() const { return f._type == NumericLiteralIsLongDouble; }
  78. bool isLong() const { return f._type == NumericLiteralIsLong; }
  79. bool isLongLong() const { return f._type == NumericLiteralIsLongLong; }
  80. bool isUnsigned() const { return f._isUnsigned; }
  81. bool isHex() const { return f._isHex; }
  82. private:
  83. struct Flags {
  84. unsigned _type : 8;
  85. unsigned _isHex : 1;
  86. unsigned _isUnsigned: 1;
  87. };
  88. union {
  89. unsigned _flags;
  90. Flags f;
  91. };
  92. };
  93. class CPLUSPLUS_EXPORT Identifier: public Literal, public Name
  94. {
  95. public:
  96. Identifier(const char *chars, unsigned size)
  97. : Literal(chars, size)
  98. { }
  99. virtual const Identifier *identifier() const { return this; }
  100. virtual const Identifier *asNameId() const
  101. { return this; }
  102. protected:
  103. virtual void accept0(NameVisitor *visitor) const;
  104. virtual bool match0(const Name *otherName, Matcher *matcher) const;
  105. };
  106. } // namespace CPlusPlus
  107. #endif // CPLUSPLUS_LITERALS_H