Class: Antlr4Native::Context

Inherits:
Object
  • Object
show all
Includes:
StringHelpers
Defined in:
lib/antlr4-native/context.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from StringHelpers

#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_sourceObject (readonly)

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

#nameObject (readonly)

Returns the value of attribute name.


5
6
7
# File 'lib/antlr4-native/context.rb', line 5

def name
  @name
end

#parser_nsObject (readonly)

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


164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/antlr4-native/context.rb', line 164

def class_wrapper(module_var)
  @class_wrapper ||= begin
    lines = [
      "#{proxy_class_variable} = #{module_var}",
      ".define_class<#{name}Proxy, ContextProxy>(\"#{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

#conversionsObject


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/antlr4-native/context.rb', line 52

def conversions
  @class_conversions ||= <<~END
    template <>
    Object to_ruby<#{parser_ns}::#{name}*>(#{parser_ns}::#{name}* const &x) {
      if (!x) return Nil;
      return Data_Object<#{parser_ns}::#{name}>(x, #{proxy_class_variable}, nullptr, nullptr);
    }

    template <>
    Object to_ruby<#{name}Proxy*>(#{name}Proxy* const &x) {
      if (!x) return Nil;
      return Data_Object<#{name}Proxy>(x, #{proxy_class_variable}, nullptr, nullptr);
    }
  END
end

#each_context_methodObject


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_methodObject


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_methodsObject


72
73
74
75
76
77
78
79
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
# File 'lib/antlr4-native/context.rb', line 72

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 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 == from_ruby<ContextProxy>(child).getOriginal()) {
              return child;
            }
          }

          return Nil;
        }
      END
    end
  end
end

#proxy_class_headerObject


33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/antlr4-native/context.rb', line 33

def proxy_class_header
  @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_methodsObject


68
69
70
# File 'lib/antlr4-native/context.rb', line 68

def proxy_class_methods
  proxy_class_context_methods + proxy_class_token_methods
end

#proxy_class_token_methodsObject


120
121
122
123
124
125
126
127
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
# File 'lib/antlr4-native/context.rb', line 120

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 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(proxy);
          }

          return 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 to_ruby(proxy);
        }
      END
    end
  end
end

#proxy_class_variableObject


29
30
31
# File 'lib/antlr4-native/context.rb', line 29

def proxy_class_variable
  @proxy_class_variable ||= "rb_c#{name}"
end