Class: TRuby::DeclarationParser
- Inherits:
-
Object
- Object
- TRuby::DeclarationParser
- Defined in:
- lib/t_ruby/declaration_generator.rb
Overview
Parser for .d.trb declaration files
Instance Attribute Summary collapse
-
#functions ⇒ Object
readonly
Returns the value of attribute functions.
-
#interfaces ⇒ Object
readonly
Returns the value of attribute interfaces.
-
#type_aliases ⇒ Object
readonly
Returns the value of attribute type_aliases.
Instance Method Summary collapse
-
#get_function(name) ⇒ Object
Get a function signature.
-
#get_interface(name) ⇒ Object
Get an interface definition.
-
#initialize ⇒ DeclarationParser
constructor
A new instance of DeclarationParser.
-
#load_directory(dir_path, recursive: false) ⇒ Object
Load multiple declaration files from a directory.
-
#merge(other_parser) ⇒ Object
Merge another parser’s declarations into this one.
-
#parse(content) ⇒ Object
Parse a declaration file content (resets existing data).
-
#parse_and_merge(content) ⇒ Object
Parse content and merge with existing data.
-
#parse_file(file_path) ⇒ Object
Parse a declaration file from path.
-
#resolve_type(name) ⇒ Object
Resolve a type alias.
-
#to_h ⇒ Object
Get all declarations as a hash.
-
#type_defined?(name) ⇒ Boolean
Check if a type is defined.
Constructor Details
#initialize ⇒ DeclarationParser
Returns a new instance of DeclarationParser.
121 122 123 124 125 |
# File 'lib/t_ruby/declaration_generator.rb', line 121 def initialize @type_aliases = {} @interfaces = {} @functions = {} end |
Instance Attribute Details
#functions ⇒ Object (readonly)
Returns the value of attribute functions.
119 120 121 |
# File 'lib/t_ruby/declaration_generator.rb', line 119 def functions @functions end |
#interfaces ⇒ Object (readonly)
Returns the value of attribute interfaces.
119 120 121 |
# File 'lib/t_ruby/declaration_generator.rb', line 119 def interfaces @interfaces end |
#type_aliases ⇒ Object (readonly)
Returns the value of attribute type_aliases.
119 120 121 |
# File 'lib/t_ruby/declaration_generator.rb', line 119 def type_aliases @type_aliases end |
Instance Method Details
#get_function(name) ⇒ Object
Get a function signature
206 207 208 |
# File 'lib/t_ruby/declaration_generator.rb', line 206 def get_function(name) @functions[name] end |
#get_interface(name) ⇒ Object
Get an interface definition
201 202 203 |
# File 'lib/t_ruby/declaration_generator.rb', line 201 def get_interface(name) @interfaces[name] end |
#load_directory(dir_path, recursive: false) ⇒ Object
Load multiple declaration files from a directory
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/t_ruby/declaration_generator.rb', line 174 def load_directory(dir_path, recursive: false) unless Dir.exist?(dir_path) raise ArgumentError, "Directory not found: #{dir_path}" end pattern = recursive ? "**/*#{DeclarationGenerator::DECLARATION_EXTENSION}" : "*#{DeclarationGenerator::DECLARATION_EXTENSION}" files = Dir.glob(File.join(dir_path, pattern)) files.each do |file| content = File.read(file) parse_and_merge(content) end self end |
#merge(other_parser) ⇒ Object
Merge another parser’s declarations into this one
220 221 222 223 224 225 |
# File 'lib/t_ruby/declaration_generator.rb', line 220 def merge(other_parser) @type_aliases.merge!(other_parser.type_aliases) @interfaces.merge!(other_parser.interfaces) @functions.merge!(other_parser.functions) self end |
#parse(content) ⇒ Object
Parse a declaration file content (resets existing data)
128 129 130 131 132 133 134 |
# File 'lib/t_ruby/declaration_generator.rb', line 128 def parse(content) @type_aliases = {} @interfaces = {} @functions = {} parse_and_merge(content) end |
#parse_and_merge(content) ⇒ Object
Parse content and merge with existing data
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/t_ruby/declaration_generator.rb', line 137 def parse_and_merge(content) parser = Parser.new(content) result = parser.parse # Process type aliases (result[:type_aliases] || []).each do |type_alias| @type_aliases[type_alias[:name]] = type_alias[:definition] end # Process interfaces (result[:interfaces] || []).each do |interface| @interfaces[interface[:name]] = interface end # Process functions (result[:functions] || []).each do |function| @functions[function[:name]] = function end self end |
#parse_file(file_path) ⇒ Object
Parse a declaration file from path
160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/t_ruby/declaration_generator.rb', line 160 def parse_file(file_path) unless File.exist?(file_path) raise ArgumentError, "Declaration file not found: #{file_path}" end unless file_path.end_with?(DeclarationGenerator::DECLARATION_EXTENSION) raise ArgumentError, "Expected #{DeclarationGenerator::DECLARATION_EXTENSION} file, got: #{file_path}" end content = File.read(file_path) parse(content) end |
#resolve_type(name) ⇒ Object
Resolve a type alias
196 197 198 |
# File 'lib/t_ruby/declaration_generator.rb', line 196 def resolve_type(name) @type_aliases[name] end |
#to_h ⇒ Object
Get all declarations as a hash
211 212 213 214 215 216 217 |
# File 'lib/t_ruby/declaration_generator.rb', line 211 def to_h { type_aliases: @type_aliases, interfaces: @interfaces, functions: @functions, } end |
#type_defined?(name) ⇒ Boolean
Check if a type is defined
191 192 193 |
# File 'lib/t_ruby/declaration_generator.rb', line 191 def type_defined?(name) @type_aliases.key?(name) || @interfaces.key?(name) end |