Class: RubyHeaderParser::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_header_parser/parser.rb

Overview

parse ruby.h using ctags

Constant Summary collapse

DEFAULT_HEADER_FILE =
"#{RbConfig::CONFIG["rubyhdrdir"]}/ruby.h".freeze
DEFAULT_INCLUDE_PATHS =
[ RbConfig::CONFIG["rubyarchhdrdir"], RbConfig::CONFIG["rubyhdrdir"], ].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dist_preprocessed_header_file: nil, header_file: DEFAULT_HEADER_FILE, include_paths: DEFAULT_INCLUDE_PATHS, config_file: nil) ⇒ Parser

Note:

dist_preprocessed_header_file is used as the output destination for temporary files when the parser is executed

Note:

See CONFIG.md for config file details

Returns a new instance of Parser.

Parameters:

  • header_file (String) (defaults to: DEFAULT_HEADER_FILE)

    Path to ruby.h

  • include_paths (Array<String>) (defaults to: DEFAULT_INCLUDE_PATHS)
  • dist_preprocessed_header_file (String, nil) (defaults to: nil)

    Destination path to the output of preprocessed ruby.h. (default: "#{Dir.tmpdir}/ruby_preprocessed.h")

  • config_file (String, nil) (defaults to: nil)

    Path to config file (default: config/default.yml.erb)

 39 40 41 42 43 44 45 46 47
# File 'lib/ruby_header_parser/parser.rb', line 39 def initialize(dist_preprocessed_header_file: nil, header_file: DEFAULT_HEADER_FILE, include_paths: DEFAULT_INCLUDE_PATHS, config_file: nil) @header_file = header_file @include_paths = include_paths @dist_preprocessed_header_file = dist_preprocessed_header_file || File.join(Dir.tmpdir, "ruby_preprocessed.h") config_file ||= File.expand_path("../../config/default.yml.erb", __dir__.to_s) @config = Config.new(config_file) end

Instance Attribute Details

#configRubyHeaderParser::Config (readonly)

 20 21 22
# File 'lib/ruby_header_parser/parser.rb', line 20 def config @config end

#dist_preprocessed_header_fileString (readonly)

Returns:

  • (String)
 16 17 18
# File 'lib/ruby_header_parser/parser.rb', line 16 def dist_preprocessed_header_file @dist_preprocessed_header_file end

#header_fileString (readonly)

Returns:

  • (String)
 8 9 10
# File 'lib/ruby_header_parser/parser.rb', line 8 def header_file @header_file end

#include_pathsArray<String> (readonly)

Returns:

  • (Array<String>)
 12 13 14
# File 'lib/ruby_header_parser/parser.rb', line 12 def include_paths @include_paths end

Instance Method Details

#extract_enum_definitionsArray<RubyHeaderParser::EnumDefinition>

 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
# File 'lib/ruby_header_parser/parser.rb', line 93 def extract_enum_definitions stdout = execute_ctags("--c-kinds=e --fields=+n") # Workaround for Ruby::UnannotatedEmptyCollection on steep 1.9.0+  name_to_definition = {} #: Hash[String, RubyHeaderParser::EnumDefinition]  name_to_definitions = stdout.each_line.with_object(name_to_definition) do |line, hash| parts = line.split("\t") enum_name = Util.find_field(parts, "enum") next unless enum_name value = parts[0] next unless config.should_generate_enum?(enum_name) hash[enum_name] ||= EnumDefinition.new(name: enum_name) hash[enum_name].values << value end name_to_definitions.values end

#extract_function_definitionsArray<RubyHeaderParser::FunctionDefinition>

 50 51 52
# File 'lib/ruby_header_parser/parser.rb', line 50 def extract_function_definitions __extract_function_definitions(c_kinds: "p", kind: "p", is_parse_multiline_definition: true) end

#extract_static_inline_function_definitionsArray<RubyHeaderParser::FunctionDefinition>

 55 56 57
# File 'lib/ruby_header_parser/parser.rb', line 55 def extract_static_inline_function_definitions __extract_function_definitions(c_kinds: "+p-d", kind: "f", is_parse_multiline_definition: false) end

#extract_struct_definitionsArray<RubyHeaderParser::StructDefinition>

 60 61 62 63 64 65 66 67 68 69 70 71 72 73
# File 'lib/ruby_header_parser/parser.rb', line 60 def extract_struct_definitions stdout = execute_ctags("--c-kinds=s --fields=+n") stdout.each_line.with_object([]) do |line, definitions| parts = line.split("\t") struct_name = parts[0] next unless config.should_generate_struct?(struct_name) definitions << StructDefinition.new( name: struct_name, ) end end

#extract_type_definitionsArray<RubyHeaderParser::TyperefDefinition>

 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
# File 'lib/ruby_header_parser/parser.rb', line 76 def extract_type_definitions stdout = execute_ctags("--c-kinds=t --fields=+n") stdout.each_line.with_object([]) do |line, definitions| parts = line.split("\t") type_name = parts[0] next unless config.should_generate_type?(type_name) definitions << TypeDefinition.new( name: type_name, ) end.uniq(&:name) end