antlr4-native 1.1.0 → 2.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1abe502ed412ef8d9fda78dd371448d290c10bd757c34044c881c110f11c84df
4
- data.tar.gz: 7093d010f55d0dac4b9b4e3d9a8f0da004ed40d4595caab9623bd717b879c812
3
+ metadata.gz: f14d05febd863a51c3e111e7ef3493eb2fe1b72fe72b0b3d1c5c114a2947f7c7
4
+ data.tar.gz: e8f3fe771ae21c552a1074e2f0608f9c87da1e7bd9063f2f8e18363531c37e6b
5
5
  SHA512:
6
- metadata.gz: 0d2498b8804a2ba13e8fbc6e90ff0c3b209962712ec7005142d59a3ebe656123d28396a0a278a634bc228304bd9160beff49dd11985db6d5249ebed7857127b5
7
- data.tar.gz: 738c3abb9486de9819faaab462a9f65e0823803f382180f3b32347e51d9d330c1072bfe9c1df239e48fc5c82c4f99f8db1e9bb3c6584752b6607b78ae4173c4a
6
+ metadata.gz: 64cf7e6628edbaff365daf278c1e70dccd7ca083128d604efe8fcb4850c77efc621ce204fe250a7ffb91adb5d6cb4d02de9f1f68bbbb32c4c8c79d725c308a26
7
+ data.tar.gz: 1c5ba3ec3908cc1a951794d0e70f87afa0f01694ef623b38269ad5f22569fd8b587c1c6a7e820d32de273f6c87e77d15abcf604e83b0edd13f2a0636c09c5d86
@@ -14,4 +14,6 @@ Gem::Specification.new do |s|
14
14
 
15
15
  s.require_path = 'lib'
16
16
  s.files = Dir['{lib,spec,vendor}/**/*', 'Gemfile', 'README.md', 'Rakefile', 'antlr4-native.gemspec']
17
+
18
+ s.add_runtime_dependency "rice", "~> 4.0"
17
19
  end
@@ -51,16 +51,24 @@ module Antlr4Native
51
51
 
52
52
  def conversions
53
53
  @class_conversions ||= <<~END
54
- template <>
55
- Object to_ruby<#{parser_ns}::#{name}*>(#{parser_ns}::#{name}* const &x) {
56
- if (!x) return Nil;
57
- return Data_Object<#{parser_ns}::#{name}>(x, #{proxy_class_variable}, nullptr, nullptr);
58
- }
54
+ namespace Rice::detail {
55
+ template <>
56
+ class To_Ruby<#{parser_ns}::#{name}*> {
57
+ public:
58
+ VALUE convert(#{parser_ns}::#{name}* const &x) {
59
+ if (!x) return Nil;
60
+ return Data_Object<#{parser_ns}::#{name}>(x, false, #{proxy_class_variable});
61
+ }
62
+ };
59
63
 
60
- template <>
61
- Object to_ruby<#{name}Proxy*>(#{name}Proxy* const &x) {
62
- if (!x) return Nil;
63
- return Data_Object<#{name}Proxy>(x, #{proxy_class_variable}, nullptr, nullptr);
64
+ template <>
65
+ class To_Ruby<#{name}Proxy*> {
66
+ public:
67
+ VALUE convert(#{name}Proxy* const &x) {
68
+ if (!x) return Nil;
69
+ return Data_Object<#{name}Proxy>(x, false, #{proxy_class_variable});
70
+ }
71
+ };
64
72
  }
65
73
  END
66
74
  end
@@ -88,7 +96,7 @@ module Antlr4Native
88
96
  }
89
97
  }
90
98
 
91
- return a;
99
+ return std::move(a);
92
100
  }
93
101
  END
94
102
  else
@@ -105,7 +113,7 @@ module Antlr4Native
105
113
  }
106
114
 
107
115
  for (auto child : getChildren()) {
108
- if (ctx == from_ruby<ContextProxy>(child).getOriginal()) {
116
+ if (ctx == detail::From_Ruby<ContextProxy>().convert(child.value()).getOriginal()) {
109
117
  return child;
110
118
  }
111
119
  }
@@ -127,17 +135,17 @@ module Antlr4Native
127
135
  Array a;
128
136
 
129
137
  if (orig == nullptr) {
130
- return a;
138
+ return std::move(a);
131
139
  }
132
140
 
133
141
  auto vec = ((#{parser_ns}::#{name}*)orig) -> #{token_mtd.name}(#{params});
134
142
 
135
143
  for (auto it = vec.begin(); it != vec.end(); it ++) {
136
144
  TerminalNodeProxy proxy(*it);
137
- a.push(proxy);
145
+ a.push(detail::To_Ruby<TerminalNodeProxy>().convert(proxy));
138
146
  }
139
147
 
140
- return a;
148
+ return std::move(a);
141
149
  }
142
150
  END
143
151
  else
@@ -154,7 +162,7 @@ module Antlr4Native
154
162
  }
155
163
 
156
164
  TerminalNodeProxy proxy(token);
157
- return to_ruby(proxy);
165
+ return detail::To_Ruby<TerminalNodeProxy>().convert(proxy);
158
166
  }
159
167
  END
160
168
  end
@@ -164,8 +172,7 @@ module Antlr4Native
164
172
  def class_wrapper(module_var)
165
173
  @class_wrapper ||= begin
166
174
  lines = [
167
- "#{proxy_class_variable} = #{module_var}",
168
- ".define_class<#{name}Proxy, ContextProxy>(\"#{name}\")"
175
+ %(#{proxy_class_variable} = define_class_under<#{name}Proxy, ContextProxy>(#{module_var}, "#{name}"))
169
176
  ]
170
177
 
171
178
  each_context_method do |ctx_method|
@@ -65,47 +65,59 @@ module Antlr4Native
65
65
  <<~END
66
66
  #include <iostream>
67
67
 
68
- #include "antlr4-runtime.h"
68
+ #include <antlr4-runtime.h>
69
69
 
70
- #include "#{parser_ns}.h"
71
- #include "#{antlr_ns}BaseVisitor.h"
72
- #include "#{lexer_ns}.h"
70
+ #include "antlrgen/#{parser_ns}.h"
71
+ #include "antlrgen/#{antlr_ns}BaseVisitor.h"
72
+ #include "antlrgen/#{lexer_ns}.h"
73
73
 
74
- #include "rice/Array.hpp"
75
- #include "rice/Class.hpp"
76
- #include "rice/Constructor.hpp"
77
- #include "rice/Director.hpp"
74
+ #include <rice/rice.hpp>
75
+ #include <rice/stl.hpp>
78
76
 
79
77
  #ifdef _WIN32
80
- #undef FALSE
81
- #undef TRUE
82
78
  #undef OPTIONAL
83
79
  #undef IN
84
80
  #undef OUT
85
81
  #endif
86
82
 
83
+ #undef FALSE
84
+ #undef TRUE
85
+
86
+ #undef TYPE
87
+
87
88
  using namespace std;
88
89
  using namespace Rice;
89
90
  using namespace antlr4;
90
91
 
91
92
  #{proxy_class_declarations}
92
93
 
93
- template <>
94
- Object to_ruby<Token*>(Token* const &x) {
95
- if (!x) return Nil;
96
- return Data_Object<Token>(x, rb_cToken, nullptr, nullptr);
97
- }
98
-
99
- template <>
100
- Object to_ruby<tree::ParseTree*>(tree::ParseTree* const &x) {
101
- if (!x) return Nil;
102
- return Data_Object<tree::ParseTree>(x, rb_cParseTree, nullptr, nullptr);
103
- }
104
-
105
- template <>
106
- Object to_ruby<tree::TerminalNode*>(tree::TerminalNode* const &x) {
107
- if (!x) return Nil;
108
- return Data_Object<tree::TerminalNode>(x, rb_cTerminalNode, nullptr, nullptr);
94
+ namespace Rice::detail {
95
+ template <>
96
+ class To_Ruby<Token*> {
97
+ public:
98
+ VALUE convert(Token* const &x) {
99
+ if (!x) return Nil;
100
+ return Data_Object<Token>(x, false, rb_cToken);
101
+ }
102
+ };
103
+
104
+ template <>
105
+ class To_Ruby<tree::ParseTree*> {
106
+ public:
107
+ VALUE convert(tree::ParseTree* const &x) {
108
+ if (!x) return Nil;
109
+ return Data_Object<tree::ParseTree>(x, false, rb_cParseTree);
110
+ }
111
+ };
112
+
113
+ template <>
114
+ class To_Ruby<tree::TerminalNode*> {
115
+ public:
116
+ VALUE convert(tree::TerminalNode* const &x) {
117
+ if (!x) return Nil;
118
+ return Data_Object<tree::TerminalNode>(x, false, rb_cTerminalNode);
119
+ }
120
+ };
109
121
  }
110
122
 
111
123
  class ContextProxy {
@@ -125,13 +137,13 @@ module Antlr4Native
125
137
  Object getStart() {
126
138
  auto token = ((ParserRuleContext*) orig) -> getStart();
127
139
 
128
- return to_ruby(token);
140
+ return detail::To_Ruby<Token*>().convert(token);
129
141
  }
130
142
 
131
143
  Object getStop() {
132
144
  auto token = ((ParserRuleContext*) orig) -> getStop();
133
145
 
134
- return to_ruby(token);
146
+ return detail::To_Ruby<Token*>().convert(token);
135
147
  }
136
148
 
137
149
  Array getChildren() {
@@ -172,7 +184,7 @@ module Antlr4Native
172
184
 
173
185
  bool doubleEquals(Object other) {
174
186
  if (other.is_a(rb_cContextProxy)) {
175
- return from_ruby<ContextProxy*>(other) -> getOriginal() == getOriginal();
187
+ return detail::From_Ruby<ContextProxy*>().convert(other) -> getOriginal() == getOriginal();
176
188
  } else {
177
189
  return false;
178
190
  }
@@ -193,6 +205,7 @@ module Antlr4Native
193
205
  TerminalNodeProxy(tree::ParseTree* tree) : ContextProxy(tree) { }
194
206
  };
195
207
 
208
+
196
209
  #{proxy_class_headers}
197
210
 
198
211
  #{conversions}
@@ -261,7 +274,7 @@ module Antlr4Native
261
274
  auto ctx = this -> parser -> #{parser_root_method}();
262
275
 
263
276
  #{capitalize(parser_root_method)}ContextProxy proxy((#{parser_ns}::#{capitalize(parser_root_method)}Context*) ctx);
264
- return to_ruby(proxy);
277
+ return detail::To_Ruby<#{capitalize(parser_root_method)}ContextProxy>().convert(proxy);
265
278
  }
266
279
 
267
280
  Object visit(VisitorProxy* visitor) {
@@ -301,10 +314,15 @@ module Antlr4Native
301
314
  #{parser_ns}* parser;
302
315
  };
303
316
 
304
- template <>
305
- Object to_ruby<ParserProxy*>(ParserProxy* const &x) {
306
- if (!x) return Nil;
307
- return Data_Object<ParserProxy>(x, rb_cParser, nullptr, nullptr);
317
+ namespace Rice::detail {
318
+ template <>
319
+ class To_Ruby<ParserProxy*> {
320
+ public:
321
+ VALUE convert(ParserProxy* const &x) {
322
+ if (!x) return Nil;
323
+ return Data_Object<ParserProxy>(x, false, rb_cParser);
324
+ }
325
+ };
308
326
  }
309
327
  END
310
328
  end
@@ -315,24 +333,14 @@ module Antlr4Native
315
333
  void Init_#{ext_name}() {
316
334
  Module rb_m#{parser_ns} = define_module("#{parser_ns}");
317
335
 
318
- rb_cToken = rb_m#{parser_ns}
319
- .define_class<Token>("Token")
336
+ rb_cToken = define_class_under<Token>(rb_m#{parser_ns}, "Token")
320
337
  .define_method("text", &Token::getText)
321
338
  .define_method("channel", &Token::getChannel)
322
339
  .define_method("token_index", &Token::getTokenIndex);
323
340
 
324
- rb_cParser = rb_m#{parser_ns}
325
- .define_class<ParserProxy>("Parser")
326
- .define_singleton_method("parse", &ParserProxy::parse)
327
- .define_singleton_method("parse_file", &ParserProxy::parseFile)
328
- .define_method("#{parser_root_method}", &ParserProxy::#{parser_root_method})
329
- .define_method("visit", &ParserProxy::visit);
330
-
331
- rb_cParseTree = rb_m#{parser_ns}
332
- .define_class<tree::ParseTree>("ParseTree");
341
+ rb_cParseTree = define_class_under<tree::ParseTree>(rb_m#{parser_ns}, "ParseTree");
333
342
 
334
- rb_cContextProxy = rb_m#{parser_ns}
335
- .define_class<ContextProxy>("Context")
343
+ rb_cContextProxy = define_class_under<ContextProxy>(rb_m#{parser_ns}, "Context")
336
344
  .define_method("children", &ContextProxy::getChildren)
337
345
  .define_method("child_count", &ContextProxy::childCount)
338
346
  .define_method("text", &ContextProxy::getText)
@@ -341,17 +349,21 @@ module Antlr4Native
341
349
  .define_method("parent", &ContextProxy::getParent)
342
350
  .define_method("==", &ContextProxy::doubleEquals);
343
351
 
344
- rb_cTerminalNode = rb_m#{parser_ns}
345
- .define_class<TerminalNodeProxy, ContextProxy>("TerminalNodeImpl");
352
+ rb_cTerminalNode = define_class_under<TerminalNodeProxy, ContextProxy>(rb_m#{parser_ns}, "TerminalNodeImpl");
346
353
 
347
- rb_m#{parser_ns}
348
- .define_class<#{visitor_generator.cpp_class_name}>("#{visitor_generator.class_name}")
354
+ define_class_under<#{antlr_ns}BaseVisitor>(rb_m#{parser_ns}, "#{visitor_generator.class_name}")
349
355
  .define_director<#{visitor_generator.cpp_class_name}>()
350
356
  .define_constructor(Constructor<#{visitor_generator.cpp_class_name}, Object>())
351
357
  .define_method("visit", &#{visitor_generator.cpp_class_name}::ruby_visit)
352
358
  .define_method("visit_children", &#{visitor_generator.cpp_class_name}::ruby_visitChildren)
353
359
  #{visitor_generator.visitor_proxy_methods(' ').join("\n")};
354
360
 
361
+ rb_cParser = define_class_under<ParserProxy>(rb_m#{parser_ns}, "Parser")
362
+ .define_singleton_function("parse", &ParserProxy::parse)
363
+ .define_singleton_function("parse_file", &ParserProxy::parseFile)
364
+ .define_method("#{parser_root_method}", &ParserProxy::#{parser_root_method})
365
+ .define_method("visit", &ParserProxy::visit);
366
+
355
367
  #{class_wrappers_str(' ')}
356
368
  }
357
369
  END
@@ -363,7 +375,7 @@ module Antlr4Native
363
375
  [
364
376
  " #{idx == 0 ? 'if' : 'else if'} (antlrcpp::is<#{parser_ns}::#{context.name}*>(node)) {",
365
377
  " #{context.name}Proxy proxy((#{parser_ns}::#{context.name}*)node);",
366
- " return to_ruby(proxy);",
378
+ " return detail::To_Ruby<#{context.name}Proxy>().convert(proxy);",
367
379
  " }"
368
380
  ]
369
381
  end
@@ -373,7 +385,7 @@ module Antlr4Native
373
385
  #{wrapper_branches.join("\n")}
374
386
  else if (antlrcpp::is<tree::TerminalNodeImpl*>(node)) {
375
387
  TerminalNodeProxy proxy(node);
376
- return to_ruby(proxy);
388
+ return detail::To_Ruby<TerminalNodeProxy>().convert(proxy);
377
389
  } else {
378
390
  return Nil;
379
391
  }
@@ -1,3 +1,3 @@
1
1
  module Antlr4Native
2
- VERSION = '1.1.0'
2
+ VERSION = '2.0.0'
3
3
  end
@@ -47,12 +47,20 @@ module Antlr4Native
47
47
 
48
48
  Object ruby_visit(ContextProxy* proxy) {
49
49
  auto result = visit(proxy -> getOriginal());
50
- return result.as<Object>();
50
+ try {
51
+ return result.as<Object>();
52
+ } catch(std::bad_cast) {
53
+ return Qnil;
54
+ }
51
55
  }
52
56
 
53
57
  Object ruby_visitChildren(ContextProxy* proxy) {
54
58
  auto result = visitChildren(proxy -> getOriginal());
55
- return result.as<Object>();
59
+ try {
60
+ return result.as<Object>();
61
+ } catch(std::bad_cast) {
62
+ return Qnil;
63
+ }
56
64
  }
57
65
 
58
66
  #{vms.join("\n")}
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: antlr4-native
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cameron Dutro
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-12-23 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2022-01-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rice
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4.0'
13
27
  description: Create a Ruby native extension from any ANTLR4 grammar.
14
28
  email:
15
29
  - camertron@gmail.com
@@ -48,7 +62,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
48
62
  - !ruby/object:Gem::Version
49
63
  version: '0'
50
64
  requirements: []
51
- rubygems_version: 3.1.2
65
+ rubygems_version: 3.2.22
52
66
  signing_key:
53
67
  specification_version: 4
54
68
  summary: Create a Ruby native extension from any ANTLR4 grammar.