antlr4-native 1.1.0 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/antlr4-native.gemspec +2 -0
- data/lib/antlr4-native/context.rb +24 -17
- data/lib/antlr4-native/generator.rb +65 -53
- data/lib/antlr4-native/version.rb +1 -1
- data/lib/antlr4-native/visitor_generator.rb +10 -2
- metadata +18 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f14d05febd863a51c3e111e7ef3493eb2fe1b72fe72b0b3d1c5c114a2947f7c7
|
4
|
+
data.tar.gz: e8f3fe771ae21c552a1074e2f0608f9c87da1e7bd9063f2f8e18363531c37e6b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 64cf7e6628edbaff365daf278c1e70dccd7ca083128d604efe8fcb4850c77efc621ce204fe250a7ffb91adb5d6cb4d02de9f1f68bbbb32c4c8c79d725c308a26
|
7
|
+
data.tar.gz: 1c5ba3ec3908cc1a951794d0e70f87afa0f01694ef623b38269ad5f22569fd8b587c1c6a7e820d32de273f6c87e77d15abcf604e83b0edd13f2a0636c09c5d86
|
data/antlr4-native.gemspec
CHANGED
@@ -51,16 +51,24 @@ module Antlr4Native
|
|
51
51
|
|
52
52
|
def conversions
|
53
53
|
@class_conversions ||= <<~END
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
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
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
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 ==
|
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
|
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
|
-
|
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
|
68
|
+
#include <antlr4-runtime.h>
|
69
69
|
|
70
|
-
#include "
|
71
|
-
#include "
|
72
|
-
#include "
|
70
|
+
#include "antlrgen/#{parser_ns}.h"
|
71
|
+
#include "antlrgen/#{antlr_ns}BaseVisitor.h"
|
72
|
+
#include "antlrgen/#{lexer_ns}.h"
|
73
73
|
|
74
|
-
#include
|
75
|
-
#include
|
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
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
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
|
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
|
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
|
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
|
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
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
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
|
-
|
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
|
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
|
388
|
+
return detail::To_Ruby<TerminalNodeProxy>().convert(proxy);
|
377
389
|
} else {
|
378
390
|
return Nil;
|
379
391
|
}
|
@@ -47,12 +47,20 @@ module Antlr4Native
|
|
47
47
|
|
48
48
|
Object ruby_visit(ContextProxy* proxy) {
|
49
49
|
auto result = visit(proxy -> getOriginal());
|
50
|
-
|
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
|
-
|
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:
|
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:
|
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.
|
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.
|