natalie_parser 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (142) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +22 -0
  3. data/Dockerfile +26 -0
  4. data/Gemfile +10 -0
  5. data/LICENSE +21 -0
  6. data/README.md +55 -0
  7. data/Rakefile +242 -0
  8. data/ext/natalie_parser/extconf.rb +9 -0
  9. data/ext/natalie_parser/mri_creator.hpp +139 -0
  10. data/ext/natalie_parser/natalie_parser.cpp +144 -0
  11. data/include/natalie_parser/creator/debug_creator.hpp +113 -0
  12. data/include/natalie_parser/creator.hpp +108 -0
  13. data/include/natalie_parser/lexer/interpolated_string_lexer.hpp +64 -0
  14. data/include/natalie_parser/lexer/regexp_lexer.hpp +37 -0
  15. data/include/natalie_parser/lexer/word_array_lexer.hpp +57 -0
  16. data/include/natalie_parser/lexer.hpp +135 -0
  17. data/include/natalie_parser/node/alias_node.hpp +35 -0
  18. data/include/natalie_parser/node/arg_node.hpp +74 -0
  19. data/include/natalie_parser/node/array_node.hpp +34 -0
  20. data/include/natalie_parser/node/array_pattern_node.hpp +28 -0
  21. data/include/natalie_parser/node/assignment_node.hpp +34 -0
  22. data/include/natalie_parser/node/back_ref_node.hpp +28 -0
  23. data/include/natalie_parser/node/begin_block_node.hpp +25 -0
  24. data/include/natalie_parser/node/begin_node.hpp +52 -0
  25. data/include/natalie_parser/node/begin_rescue_node.hpp +47 -0
  26. data/include/natalie_parser/node/bignum_node.hpp +37 -0
  27. data/include/natalie_parser/node/block_node.hpp +55 -0
  28. data/include/natalie_parser/node/block_pass_node.hpp +33 -0
  29. data/include/natalie_parser/node/break_node.hpp +32 -0
  30. data/include/natalie_parser/node/call_node.hpp +85 -0
  31. data/include/natalie_parser/node/case_in_node.hpp +40 -0
  32. data/include/natalie_parser/node/case_node.hpp +52 -0
  33. data/include/natalie_parser/node/case_when_node.hpp +43 -0
  34. data/include/natalie_parser/node/class_node.hpp +39 -0
  35. data/include/natalie_parser/node/colon2_node.hpp +44 -0
  36. data/include/natalie_parser/node/colon3_node.hpp +34 -0
  37. data/include/natalie_parser/node/constant_node.hpp +26 -0
  38. data/include/natalie_parser/node/def_node.hpp +55 -0
  39. data/include/natalie_parser/node/defined_node.hpp +33 -0
  40. data/include/natalie_parser/node/encoding_node.hpp +26 -0
  41. data/include/natalie_parser/node/end_block_node.hpp +25 -0
  42. data/include/natalie_parser/node/evaluate_to_string_node.hpp +37 -0
  43. data/include/natalie_parser/node/false_node.hpp +23 -0
  44. data/include/natalie_parser/node/fixnum_node.hpp +36 -0
  45. data/include/natalie_parser/node/float_node.hpp +36 -0
  46. data/include/natalie_parser/node/hash_node.hpp +34 -0
  47. data/include/natalie_parser/node/hash_pattern_node.hpp +27 -0
  48. data/include/natalie_parser/node/identifier_node.hpp +123 -0
  49. data/include/natalie_parser/node/if_node.hpp +43 -0
  50. data/include/natalie_parser/node/infix_op_node.hpp +46 -0
  51. data/include/natalie_parser/node/interpolated_node.hpp +33 -0
  52. data/include/natalie_parser/node/interpolated_regexp_node.hpp +28 -0
  53. data/include/natalie_parser/node/interpolated_shell_node.hpp +22 -0
  54. data/include/natalie_parser/node/interpolated_string_node.hpp +31 -0
  55. data/include/natalie_parser/node/interpolated_symbol_key_node.hpp +18 -0
  56. data/include/natalie_parser/node/interpolated_symbol_node.hpp +28 -0
  57. data/include/natalie_parser/node/iter_node.hpp +45 -0
  58. data/include/natalie_parser/node/keyword_arg_node.hpp +25 -0
  59. data/include/natalie_parser/node/keyword_splat_node.hpp +38 -0
  60. data/include/natalie_parser/node/logical_and_node.hpp +40 -0
  61. data/include/natalie_parser/node/logical_or_node.hpp +40 -0
  62. data/include/natalie_parser/node/match_node.hpp +38 -0
  63. data/include/natalie_parser/node/module_node.hpp +32 -0
  64. data/include/natalie_parser/node/multiple_assignment_arg_node.hpp +32 -0
  65. data/include/natalie_parser/node/multiple_assignment_node.hpp +37 -0
  66. data/include/natalie_parser/node/next_node.hpp +37 -0
  67. data/include/natalie_parser/node/nil_node.hpp +23 -0
  68. data/include/natalie_parser/node/nil_sexp_node.hpp +23 -0
  69. data/include/natalie_parser/node/node.hpp +155 -0
  70. data/include/natalie_parser/node/node_with_args.hpp +47 -0
  71. data/include/natalie_parser/node/not_match_node.hpp +35 -0
  72. data/include/natalie_parser/node/not_node.hpp +37 -0
  73. data/include/natalie_parser/node/nth_ref_node.hpp +27 -0
  74. data/include/natalie_parser/node/op_assign_accessor_node.hpp +74 -0
  75. data/include/natalie_parser/node/op_assign_and_node.hpp +34 -0
  76. data/include/natalie_parser/node/op_assign_node.hpp +47 -0
  77. data/include/natalie_parser/node/op_assign_or_node.hpp +34 -0
  78. data/include/natalie_parser/node/pin_node.hpp +33 -0
  79. data/include/natalie_parser/node/range_node.hpp +52 -0
  80. data/include/natalie_parser/node/redo_node.hpp +20 -0
  81. data/include/natalie_parser/node/regexp_node.hpp +36 -0
  82. data/include/natalie_parser/node/retry_node.hpp +20 -0
  83. data/include/natalie_parser/node/return_node.hpp +34 -0
  84. data/include/natalie_parser/node/safe_call_node.hpp +31 -0
  85. data/include/natalie_parser/node/sclass_node.hpp +37 -0
  86. data/include/natalie_parser/node/self_node.hpp +23 -0
  87. data/include/natalie_parser/node/shadow_arg_node.hpp +40 -0
  88. data/include/natalie_parser/node/shell_node.hpp +32 -0
  89. data/include/natalie_parser/node/splat_node.hpp +39 -0
  90. data/include/natalie_parser/node/splat_value_node.hpp +32 -0
  91. data/include/natalie_parser/node/stabby_proc_node.hpp +29 -0
  92. data/include/natalie_parser/node/string_node.hpp +42 -0
  93. data/include/natalie_parser/node/super_node.hpp +44 -0
  94. data/include/natalie_parser/node/symbol_key_node.hpp +19 -0
  95. data/include/natalie_parser/node/symbol_node.hpp +30 -0
  96. data/include/natalie_parser/node/to_array_node.hpp +33 -0
  97. data/include/natalie_parser/node/true_node.hpp +23 -0
  98. data/include/natalie_parser/node/unary_op_node.hpp +41 -0
  99. data/include/natalie_parser/node/undef_node.hpp +31 -0
  100. data/include/natalie_parser/node/until_node.hpp +21 -0
  101. data/include/natalie_parser/node/while_node.hpp +52 -0
  102. data/include/natalie_parser/node/yield_node.hpp +29 -0
  103. data/include/natalie_parser/node.hpp +89 -0
  104. data/include/natalie_parser/parser.hpp +218 -0
  105. data/include/natalie_parser/token.hpp +842 -0
  106. data/include/tm/defer.hpp +34 -0
  107. data/include/tm/hashmap.hpp +826 -0
  108. data/include/tm/macros.hpp +16 -0
  109. data/include/tm/optional.hpp +223 -0
  110. data/include/tm/owned_ptr.hpp +186 -0
  111. data/include/tm/recursion_guard.hpp +156 -0
  112. data/include/tm/shared_ptr.hpp +259 -0
  113. data/include/tm/string.hpp +1447 -0
  114. data/include/tm/tests.hpp +78 -0
  115. data/include/tm/vector.hpp +796 -0
  116. data/lib/natalie_parser/sexp.rb +36 -0
  117. data/lib/natalie_parser/version.rb +5 -0
  118. data/lib/natalie_parser.rb +3 -0
  119. data/natalie_parser.gemspec +23 -0
  120. data/src/lexer/interpolated_string_lexer.cpp +88 -0
  121. data/src/lexer/regexp_lexer.cpp +95 -0
  122. data/src/lexer/word_array_lexer.cpp +134 -0
  123. data/src/lexer.cpp +1703 -0
  124. data/src/node/alias_node.cpp +11 -0
  125. data/src/node/assignment_node.cpp +33 -0
  126. data/src/node/begin_node.cpp +29 -0
  127. data/src/node/begin_rescue_node.cpp +33 -0
  128. data/src/node/class_node.cpp +22 -0
  129. data/src/node/interpolated_regexp_node.cpp +19 -0
  130. data/src/node/interpolated_shell_node.cpp +25 -0
  131. data/src/node/interpolated_string_node.cpp +111 -0
  132. data/src/node/interpolated_symbol_node.cpp +25 -0
  133. data/src/node/match_node.cpp +14 -0
  134. data/src/node/module_node.cpp +21 -0
  135. data/src/node/multiple_assignment_node.cpp +37 -0
  136. data/src/node/node.cpp +10 -0
  137. data/src/node/node_with_args.cpp +35 -0
  138. data/src/node/op_assign_node.cpp +36 -0
  139. data/src/node/string_node.cpp +33 -0
  140. data/src/parser.cpp +2972 -0
  141. data/src/token.cpp +27 -0
  142. metadata +186 -0
@@ -0,0 +1,259 @@
1
+ #pragma once
2
+
3
+ #include <assert.h>
4
+ #include <stdio.h>
5
+
6
+ namespace TM {
7
+
8
+ class Counter {
9
+ public:
10
+ Counter(unsigned int count)
11
+ : m_count { count } { }
12
+
13
+ void increment() { m_count++; }
14
+ void decrement() { m_count--; }
15
+
16
+ bool operator==(unsigned int other) const {
17
+ return m_count == other;
18
+ }
19
+
20
+ unsigned int count() const { return m_count; }
21
+
22
+ private:
23
+ unsigned int m_count;
24
+ };
25
+
26
+ template <typename T>
27
+ class SharedPtr {
28
+ public:
29
+ /**
30
+ * Constructs a null SharedPtr.
31
+ *
32
+ * ```
33
+ * auto ptr = SharedPtr<Thing>();
34
+ * assert_not(ptr);
35
+ * ```
36
+ */
37
+ SharedPtr()
38
+ : m_ptr { nullptr }
39
+ , m_count { nullptr } { }
40
+
41
+ /**
42
+ * Constructs a SharedPtr with the given raw pointer.
43
+ *
44
+ * ```
45
+ * auto ptr = SharedPtr<Thing>(new Thing(1));
46
+ * assert(ptr);
47
+ * ```
48
+ *
49
+ * Alternative syntax:
50
+ *
51
+ * ```
52
+ * SharedPtr<Thing> ptr = new Thing(1);
53
+ * assert(ptr);
54
+ * ```
55
+ */
56
+ SharedPtr(T *ptr)
57
+ : m_ptr { ptr }
58
+ , m_count { new Counter(1) } {
59
+ assert(m_ptr);
60
+ }
61
+
62
+ SharedPtr(T *ptr, Counter *counter)
63
+ : m_ptr { ptr }
64
+ , m_count { counter } {
65
+ assert(m_ptr);
66
+ assert(m_count);
67
+ m_count->increment();
68
+ }
69
+
70
+ ~SharedPtr() {
71
+ destroy();
72
+ }
73
+
74
+ /**
75
+ * Copies the given SharedPtr, incrementing the internal
76
+ * reference counter.
77
+ *
78
+ * ```
79
+ * auto ptr1 = SharedPtr<Thing>(new Thing(1));
80
+ * auto ptr2 = SharedPtr<Thing>(ptr1);
81
+ * assert_eq(2, ptr1.count());
82
+ * assert_eq(2, ptr2.count());
83
+ * ```
84
+ */
85
+ SharedPtr(const SharedPtr &other)
86
+ : m_ptr { other.m_ptr }
87
+ , m_count { other.m_count } {
88
+ assert(valid());
89
+ if (m_count)
90
+ m_count->increment();
91
+ }
92
+
93
+ /**
94
+ * Overwrites this SharedPtr with the given one,
95
+ * decrementing the ref count for our old pointer,
96
+ * and incrementing the ref counter for our new pointer.
97
+ *
98
+ * ```
99
+ * auto ptr1 = SharedPtr<Thing>(new Thing(1));
100
+ * auto ptr2 = SharedPtr<Thing>(new Thing(2));
101
+ * auto ptr3 = SharedPtr<Thing>(ptr2);
102
+ * assert_eq(1, ptr1.count());
103
+ * assert_eq(2, ptr3.count());
104
+ *
105
+ * ptr2 = ptr1;
106
+ * assert_eq(2, ptr1.count());
107
+ * assert_eq(1, ptr3.count());
108
+ * ```
109
+ */
110
+ SharedPtr<T> &operator=(const SharedPtr<T> &other) {
111
+ destroy();
112
+ m_ptr = other.m_ptr;
113
+ m_count = other.m_count;
114
+ assert(valid());
115
+ if (m_count)
116
+ m_count->increment();
117
+ return *this;
118
+ }
119
+
120
+ /**
121
+ * Returns true if this SharedPtr is not null.
122
+ *
123
+ * ```
124
+ * auto ptr1 = SharedPtr<Thing>(new Thing(1));
125
+ * auto ptr2 = SharedPtr<Thing>();
126
+ * assert(ptr1);
127
+ * assert_not(ptr2);
128
+ * ```
129
+ */
130
+ operator bool() const {
131
+ return !!m_ptr;
132
+ }
133
+
134
+ /**
135
+ * Returns a reference to the underlying raw pointer.
136
+ *
137
+ * ```
138
+ * auto raw = new Thing(1);
139
+ * auto ptr = SharedPtr<Thing>(raw);
140
+ * assert_eq(*raw, *ptr);
141
+ * ```
142
+ *
143
+ * This method aborts if the pointer is null.
144
+ *
145
+ * ```should_abort
146
+ * auto ptr = SharedPtr<Thing>();
147
+ * *ptr;
148
+ * ```
149
+ */
150
+ T &operator*() const {
151
+ assert(m_ptr);
152
+ return *m_ptr;
153
+ }
154
+
155
+ /**
156
+ * Returns a reference to the underlying raw pointer.
157
+ *
158
+ * ```
159
+ * auto raw = new Thing(1);
160
+ * auto ptr = SharedPtr<Thing>(raw);
161
+ * assert_eq(*raw, ptr.ref());
162
+ * ```
163
+ *
164
+ * This method aborts if the pointer is null.
165
+ *
166
+ * ```should_abort
167
+ * auto ptr = SharedPtr<Thing>();
168
+ * ptr.ref();
169
+ * ```
170
+ */
171
+ T &ref() const {
172
+ assert(m_ptr);
173
+ return *m_ptr;
174
+ }
175
+
176
+ /**
177
+ * Dereferences the underlying raw pointer for
178
+ * chained member reference.
179
+ *
180
+ * ```
181
+ * auto raw = new Thing(1);
182
+ * auto ptr = SharedPtr<Thing>(raw);
183
+ * assert_eq(1, ptr->value());
184
+ * ```
185
+ *
186
+ * This method aborts if the pointer is null.
187
+ *
188
+ * ```should_abort
189
+ * auto ptr = SharedPtr<Thing>();
190
+ * ptr->value();
191
+ * ```
192
+ */
193
+ T *operator->() const {
194
+ assert(m_ptr);
195
+ return m_ptr;
196
+ }
197
+
198
+ /**
199
+ * Returns the current count of outstanding copies of this SharedPtr.
200
+ *
201
+ * ```
202
+ * auto ptr1 = SharedPtr<Thing>(new Thing(1));
203
+ * assert_eq(1, ptr1.count());
204
+ * auto ptr2 = ptr1;
205
+ * auto ptr3 = new SharedPtr<Thing>(ptr1);
206
+ * assert_eq(3, ptr1.count());
207
+ * delete ptr3;
208
+ * assert_eq(2, ptr1.count());
209
+ * ```
210
+ *
211
+ * If this is a null SharedPtr, returns 0.
212
+ *
213
+ * ```
214
+ * auto ptr = SharedPtr<Thing>();
215
+ * assert_eq(0, ptr.count());
216
+ * ```
217
+ */
218
+ unsigned int count() const {
219
+ if (!m_count)
220
+ return 0;
221
+ return m_count->count();
222
+ }
223
+
224
+ /**
225
+ * Returns a new SharedPtr with the underlying pointer
226
+ * statically cast as the templated type.
227
+ *
228
+ * TODO: example
229
+ */
230
+ template <typename To>
231
+ SharedPtr<To> static_cast_as() const {
232
+ if (!m_ptr)
233
+ return {};
234
+ return SharedPtr<To> { static_cast<To *>(m_ptr), m_count };
235
+ }
236
+
237
+ private:
238
+ bool valid() {
239
+ return (m_ptr && m_count) || (!m_ptr && !m_count);
240
+ }
241
+
242
+ void destroy() {
243
+ if (m_ptr == nullptr) {
244
+ delete m_count;
245
+ return;
246
+ }
247
+ assert(m_count->count() > 0);
248
+ m_count->decrement();
249
+ if (m_count->count() == 0) {
250
+ delete m_ptr;
251
+ delete m_count;
252
+ }
253
+ }
254
+
255
+ T *m_ptr;
256
+ Counter *m_count;
257
+ };
258
+
259
+ }