Class: Antlr4Native::VisitorGenerator

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

Constant Summary collapse

VISITOR_METHOD_BLACKLIST =
%w(visit visitChildren).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from StringHelpers

#capitalize, #dasherize, #underscore

Constructor Details

#initialize(visitor_methods, antlr_ns, parser_ns) ⇒ VisitorGenerator

Returns a new instance of VisitorGenerator.


9
10
11
12
13
# File 'lib/antlr4-native/visitor_generator.rb', line 9

def initialize(visitor_methods, antlr_ns, parser_ns)
  @visitor_methods = visitor_methods
  @antlr_ns = antlr_ns
  @parser_ns = parser_ns
end

Instance Attribute Details

#antlr_nsObject (readonly)

Returns the value of attribute antlr_ns.


7
8
9
# File 'lib/antlr4-native/visitor_generator.rb', line 7

def antlr_ns
  @antlr_ns
end

#parser_nsObject (readonly)

Returns the value of attribute parser_ns.


7
8
9
# File 'lib/antlr4-native/visitor_generator.rb', line 7

def parser_ns
  @parser_ns
end

#visitor_methodsObject (readonly)

Returns the value of attribute visitor_methods.


7
8
9
# File 'lib/antlr4-native/visitor_generator.rb', line 7

def visitor_methods
  @visitor_methods
end

Instance Method Details

#class_nameObject


15
16
17
# File 'lib/antlr4-native/visitor_generator.rb', line 15

def class_name
  @class_name ||= 'Visitor'
end

#cpp_class_nameObject


19
20
21
# File 'lib/antlr4-native/visitor_generator.rb', line 19

def cpp_class_name
  @cpp_class_name ||= 'VisitorProxy'
end

#each_visitor_methodObject


23
24
25
26
27
28
29
# File 'lib/antlr4-native/visitor_generator.rb', line 23

def each_visitor_method
  return to_enum(__method__) unless block_given?

  visitor_methods.each do |visitor_method|
    yield visitor_method unless VISITOR_METHOD_BLACKLIST.include?(visitor_method)
  end
end

#visitor_proxyObject


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/antlr4-native/visitor_generator.rb', line 31

def visitor_proxy
  vms = each_visitor_method.flat_map do |visitor_method|
    context = "#{capitalize(visitor_method.sub(/\Avisit/, ''))}Context"

    [
      "  virtual antlrcpp::Any #{visitor_method}(#{parser_ns}::#{context} *ctx) override {",
      "    #{context}Proxy proxy(ctx);",
      "    return getSelf().call(\"#{underscore(visitor_method)}\", &proxy);",
      "  }\n"
    ]
  end

  <<~END
    class #{cpp_class_name} : public #{antlr_ns}BaseVisitor, public Director {
    public:
      #{cpp_class_name}(Object self) : Director(self) { }

      Object ruby_visit(ContextProxy* proxy) {
        auto result = visit(proxy -> getOriginal());
        return result.as<Object>();
      }

      Object ruby_visitChildren(ContextProxy* proxy) {
        auto result = visitChildren(proxy -> getOriginal());
        return result.as<Object>();
      }

    #{vms.join("\n")}
    };
  END
end

#visitor_proxy_methods(indent) ⇒ Object


63
64
65
66
67
# File 'lib/antlr4-native/visitor_generator.rb', line 63

def visitor_proxy_methods(indent)
  @visitor_proxy_methods ||= each_visitor_method.map do |visitor_method|
    "#{indent}.define_method(\"#{underscore(visitor_method)}\", &#{cpp_class_name}::ruby_visitChildren)"
  end
end