1
2
3
4
5
6 #ifndef SNGCPP_AST_LITERAL_INCLUDED
7 #define SNGCPP_AST_LITERAL_INCLUDED
8 #include <sngcpp/ast/Node.hpp>
9 #include <string>
10
11 namespace sngcpp { namespace ast {
12
13 enum class Suffix : uint8_t
14 {
15 none= 0,
16 f= 1 << 0,
17 l= 1 << 1,
18 ll= 1 << 2,
19 u= 1 << 3
20 };
21
22 inline Suffix operator|(Suffix left, Suffix right)
23 {
24 return Suffix(uint8_t(left) | uint8_t(right));
25 }
26
27 inline Suffix operator&(Suffix left, Suffix right)
28 {
29 return Suffix(uint8_t(left) & uint8_t(right));
30 }
31
32 enum class Base : uint8_t
33 {
34 decimal, hex, octal
35 };
36
37 std::u32string ToString(Suffix suffix);
38
39 class LiteralNode : public Node
40 {
41 public:
42 LiteralNode(NodeType nodeType_);
43 LiteralNode(NodeType nodeType_, const Span& span_, const std::u32string& rep_);
44 void Write(Writer& writer) override;
45 void Read(Reader& reader) override;
46 const std::u32string& Rep() const { return rep; }
47 private:
48 std::u32string rep;
49 };
50
51 class FloatingLiteralNode : public LiteralNode
52 {
53 public:
54 FloatingLiteralNode();
55 FloatingLiteralNode(const Span& span_, double value_, Suffix suffix_, const std::u32string& rep_);
56 void Accept(Visitor& visitor) override;
57 void Write(Writer& writer) override;
58 void Read(Reader& reader) override;
59 double Value() const { return value; }
60 Suffix GetSuffix() const { return suffix; }
61 private:
62 double value;
63 Suffix suffix;
64 };
65
66 class IntegerLiteralNode : public LiteralNode
67 {
68 public:
69 IntegerLiteralNode();
70 IntegerLiteralNode(const Span& span_, uint64_t value_, Suffix suffix_, Base base_, const std::u32string& rep_);
71 void Accept(Visitor& visitor) override;
72 void Write(Writer& writer) override;
73 void Read(Reader& reader) override;
74 uint64_t Value() const { return value; }
75 Suffix GetSuffix() const { return suffix; }
76 Base GetBase() const { return base; }
77 private:
78 uint64_t value;
79 Suffix suffix;
80 Base base;
81 };
82
83 class CharacterLiteralNode : public LiteralNode
84 {
85 public:
86 CharacterLiteralNode();
87 CharacterLiteralNode(const Span& span_, char32_t prefix_, char32_t chr_, const std::u32string& rep_);
88 void Accept(Visitor& visitor) override;
89 void Write(Writer& writer) override;
90 void Read(Reader& reader) override;
91 char32_t Prefix() const { return prefix; }
92 char32_t Chr() const { return chr; }
93 private:
94 char32_t prefix;
95 char32_t chr;
96 };
97
98 class StringLiteralNode : public LiteralNode
99 {
100 public:
101 StringLiteralNode();
102 StringLiteralNode(const Span& span_, const std::u32string& encodingPrefix_, const std::u32string& chars_, const std::u32string& rep_);
103 void Accept(Visitor& visitor) override;
104 void Write(Writer& writer) override;
105 void Read(Reader& reader) override;
106 const std::u32string& EncodigPrefix() const { return encodingPrefix; }
107 const std::u32string& Chars() const { return chars; }
108 private:
109 std::u32string encodingPrefix;
110 std::u32string chars;
111 };
112
113 class BooleanLiteralNode : public LiteralNode
114 {
115 public:
116 BooleanLiteralNode();
117 BooleanLiteralNode(const Span& span_, bool value_, const std::u32string& rep_);
118 void Accept(Visitor& visitor) override;
119 void Write(Writer& writer) override;
120 void Read(Reader& reader) override;
121 bool Value() const { return value; }
122 private:
123 bool value;
124 };
125
126 class NullPtrLiteralNode : public LiteralNode
127 {
128 public:
129 NullPtrLiteralNode();
130 NullPtrLiteralNode(const Span& span_, const std::u32string& rep_);
131 void Accept(Visitor& visitor) override;
132 };
133
134 } }
135
136 #endif // SNGCPP_AST_LITERAL_INCLUDED