Class: Antlr4Native::Context
- Inherits:
-
Object
- Object
- Antlr4Native::Context
show all
- Includes:
- StringHelpers
- Defined in:
- lib/antlr4-native/context.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
#capitalize, #dasherize, #underscore
Constructor Details
#initialize(name, parser_ns, cpp_parser_source) ⇒ Context
Returns a new instance of Context.
7
8
9
10
11
|
# File 'lib/antlr4-native/context.rb', line 7
def initialize(name, parser_ns, cpp_parser_source)
@name = name
@parser_ns = parser_ns
@cpp_parser_source = cpp_parser_source
end
|
Instance Attribute Details
#cpp_parser_source ⇒ Object
Returns the value of attribute cpp_parser_source.
5
6
7
|
# File 'lib/antlr4-native/context.rb', line 5
def cpp_parser_source
@cpp_parser_source
end
|
#name ⇒ Object
Returns the value of attribute name.
5
6
7
|
# File 'lib/antlr4-native/context.rb', line 5
def name
@name
end
|
#parser_ns ⇒ Object
Returns the value of attribute parser_ns.
5
6
7
|
# File 'lib/antlr4-native/context.rb', line 5
def parser_ns
@parser_ns
end
|
Instance Method Details
#class_wrapper(module_var) ⇒ Object
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
|
# File 'lib/antlr4-native/context.rb', line 172
def class_wrapper(module_var)
@class_wrapper ||= begin
lines = [
%(#{proxy_class_variable} = define_class_under<#{name}Proxy, ContextProxy>(#{module_var}, "#{name}"))
]
each_context_method do |ctx_method|
lines << ".define_method(\"#{underscore(ctx_method.cpp_name)}\", &#{name}Proxy::#{ctx_method.cpp_name})"
end
each_token_method do |token_method|
lines << ".define_method(\"#{token_method.cpp_name}\", &#{name}Proxy::#{token_method.name})"
end
lines[-1] << ';'
lines
end
end
|
#conversions ⇒ Object
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
# File 'lib/antlr4-native/context.rb', line 52
def conversions
@class_conversions ||= <<~END
namespace Rice::detail {
template <>
class To_Ruby<#{parser_ns}::#{name}*> {
public:
VALUE convert(#{parser_ns}::#{name}* const &x) {
if (!x) return Nil;
return Data_Object<#{parser_ns}::#{name}>(x, false, #{proxy_class_variable});
}
};
template <>
class To_Ruby<#{name}Proxy*> {
public:
VALUE convert(#{name}Proxy* const &x) {
if (!x) return Nil;
return Data_Object<#{name}Proxy>(x, false, #{proxy_class_variable});
}
};
}
END
end
|
#each_context_method ⇒ Object
13
14
15
16
17
18
19
|
# File 'lib/antlr4-native/context.rb', line 13
def each_context_method
return to_enum(__method__) unless block_given?
mtds.each do |mtd|
yield mtd if mtd.context_method?
end
end
|
#each_token_method ⇒ Object
21
22
23
24
25
26
27
|
# File 'lib/antlr4-native/context.rb', line 21
def each_token_method
return to_enum(__method__) unless block_given?
mtds.each do |mtd|
yield mtd if mtd.token_method?
end
end
|
#method_signatures_for(mtds) ⇒ Object
46
47
48
49
50
|
# File 'lib/antlr4-native/context.rb', line 46
def method_signatures_for(mtds)
mtds
.map { |mtd| " Object #{mtd.cpp_name}(#{mtd.raw_args});" }
.join("\n")
end
|
#proxy_class_context_methods ⇒ Object
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
# File 'lib/antlr4-native/context.rb', line 80
def proxy_class_context_methods
each_context_method.map do |ctx_method|
return_type = "#{capitalize(ctx_method.name)}Context"
return_proxy_type = "#{return_type}Proxy"
params = ctx_method.args.map(&:name).join(', ')
if ctx_method.returns_vector?
<<~END
Object #{name}Proxy::#{ctx_method.cpp_name}(#{ctx_method.raw_args}) {
Array a;
if (orig != nullptr) {
size_t count = ((#{parser_ns}::#{name}*)orig) -> #{ctx_method.name}(#{params}).size();
for (size_t i = 0; i < count; i ++) {
a.push(#{ctx_method.name}At(i));
}
}
return std::move(a);
}
END
else
<<~END
Object #{name}Proxy::#{ctx_method.cpp_name}(#{ctx_method.raw_args}) {
if (orig == nullptr) {
return Qnil;
}
auto ctx = ((#{parser_ns}::#{name}*)orig) -> #{ctx_method.name}(#{params});
if (ctx == nullptr) {
return Qnil;
}
for (auto child : getChildren()) {
if (ctx == detail::From_Ruby<ContextProxy>().convert(child.value()).getOriginal()) {
return child;
}
}
return Nil;
}
END
end
end
end
|
33
34
35
36
37
38
39
40
41
42
43
44
|
# File 'lib/antlr4-native/context.rb', line 33
def
@proxy_class_header ||= begin
<<~END
class #{name}Proxy : public ContextProxy {
public:
#{name}Proxy(tree::ParseTree* ctx) : ContextProxy(ctx) {};
#{method_signatures_for(each_context_method)}
#{method_signatures_for(each_token_method)}
};
END
end
end
|
#proxy_class_methods ⇒ Object
76
77
78
|
# File 'lib/antlr4-native/context.rb', line 76
def proxy_class_methods
proxy_class_context_methods + proxy_class_token_methods
end
|
#proxy_class_token_methods ⇒ Object
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
# File 'lib/antlr4-native/context.rb', line 128
def proxy_class_token_methods
each_token_method.map do |token_mtd|
params = token_mtd.args.map(&:name).join(', ')
if token_mtd.returns_vector?
<<~END
Object #{name}Proxy::#{token_mtd.cpp_name}(#{token_mtd.raw_args}) {
Array a;
if (orig == nullptr) {
return std::move(a);
}
auto vec = ((#{parser_ns}::#{name}*)orig) -> #{token_mtd.name}(#{params});
for (auto it = vec.begin(); it != vec.end(); it ++) {
TerminalNodeProxy proxy(*it);
a.push(detail::To_Ruby<TerminalNodeProxy>().convert(proxy));
}
return std::move(a);
}
END
else
<<~END
Object #{name}Proxy::#{token_mtd.cpp_name}(#{token_mtd.raw_args}) {
if (orig == nullptr) {
return Qnil;
}
auto token = ((#{parser_ns}::#{name}*)orig) -> #{token_mtd.name}(#{params});
if (token == nullptr) {
return Qnil;
}
TerminalNodeProxy proxy(token);
return detail::To_Ruby<TerminalNodeProxy>().convert(proxy);
}
END
end
end
end
|
#proxy_class_variable ⇒ Object
29
30
31
|
# File 'lib/antlr4-native/context.rb', line 29
def proxy_class_variable
@proxy_class_variable ||= "rb_c#{name}"
end
|