From 3a60c88a3b3dbd334e4769a6283fb90c0495c25b Mon Sep 17 00:00:00 2001 From: Seba Gamboa Date: Fri, 25 Sep 2015 18:01:14 -0300 Subject: Sorting tons of stuff around --- lib/yard/mruby.rb | 2 + lib/yard/mruby/code_objects/header_object.rb | 6 --- lib/yard/mruby/handlers.rb | 1 - lib/yard/mruby/handlers/c.rb | 8 ++-- lib/yard/mruby/handlers/c/base.rb | 52 --------------------- lib/yard/mruby/handlers/c/class_handler.rb | 35 -------------- lib/yard/mruby/handlers/c/header.rb | 2 + lib/yard/mruby/handlers/c/header/base.rb | 30 ++++++++++++ lib/yard/mruby/handlers/c/header/define_handler.rb | 0 .../mruby/handlers/c/header/function_handler.rb | 26 +++++++++++ lib/yard/mruby/handlers/c/method_handler.rb | 25 ---------- lib/yard/mruby/handlers/c/module_handler.rb | 34 -------------- lib/yard/mruby/handlers/c/source.rb | 7 +++ lib/yard/mruby/handlers/c/source/base.rb | 54 ++++++++++++++++++++++ lib/yard/mruby/handlers/c/source/class_handler.rb | 35 ++++++++++++++ lib/yard/mruby/handlers/c/source/init_handler.rb | 16 +++++++ lib/yard/mruby/handlers/c/source/method_handler.rb | 25 ++++++++++ lib/yard/mruby/handlers/c/source/module_handler.rb | 34 ++++++++++++++ lib/yard/mruby/handlers/c/source/symbol_handler.rb | 16 +++++++ lib/yard/mruby/handlers/c/symbol_handler.rb | 16 ------- lib/yard/mruby/handlers/header.rb | 2 - lib/yard/mruby/handlers/header/base.rb | 28 ----------- lib/yard/mruby/handlers/header/define_handler.rb | 0 lib/yard/mruby/handlers/header/function_handler.rb | 23 --------- lib/yard/mruby/handlers/header/header_handler.rb | 0 lib/yard/mruby/parser.rb | 2 +- lib/yard/mruby/parser/c.rb | 3 ++ lib/yard/mruby/parser/c/header_parser.rb | 8 ++++ lib/yard/mruby/parser/c/parser.rb | 10 ++++ lib/yard/mruby/parser/c/source_parser.rb | 9 ++++ lib/yard/mruby/parser/header_parser.rb | 9 ---- spec/handlers/c/class_handler_spec.rb | 41 ---------------- spec/handlers/c/header/function_handler_spec.rb | 22 +++++++++ spec/handlers/c/header/spec_helper.rb | 21 +++++++++ spec/handlers/c/method_handler_spec.rb | 48 ------------------- spec/handlers/c/module_handler_spec.rb | 30 ------------ spec/handlers/c/source/class_handler_spec.rb | 41 ++++++++++++++++ spec/handlers/c/source/method_handler_spec.rb | 48 +++++++++++++++++++ spec/handlers/c/source/module_handler_spec.rb | 30 ++++++++++++ spec/handlers/c/source/spec_helper.rb | 13 ++++++ spec/handlers/c/spec_helper.rb | 12 ----- spec/handlers/header/function_handler_spec.rb | 12 ----- spec/handlers/header/spec_helper.rb | 21 --------- 43 files changed, 456 insertions(+), 401 deletions(-) delete mode 100644 lib/yard/mruby/handlers/c/base.rb delete mode 100644 lib/yard/mruby/handlers/c/class_handler.rb create mode 100644 lib/yard/mruby/handlers/c/header.rb create mode 100644 lib/yard/mruby/handlers/c/header/base.rb create mode 100644 lib/yard/mruby/handlers/c/header/define_handler.rb create mode 100644 lib/yard/mruby/handlers/c/header/function_handler.rb delete mode 100644 lib/yard/mruby/handlers/c/method_handler.rb delete mode 100644 lib/yard/mruby/handlers/c/module_handler.rb create mode 100644 lib/yard/mruby/handlers/c/source.rb create mode 100644 lib/yard/mruby/handlers/c/source/base.rb create mode 100644 lib/yard/mruby/handlers/c/source/class_handler.rb create mode 100644 lib/yard/mruby/handlers/c/source/init_handler.rb create mode 100644 lib/yard/mruby/handlers/c/source/method_handler.rb create mode 100644 lib/yard/mruby/handlers/c/source/module_handler.rb create mode 100644 lib/yard/mruby/handlers/c/source/symbol_handler.rb delete mode 100644 lib/yard/mruby/handlers/c/symbol_handler.rb delete mode 100644 lib/yard/mruby/handlers/header.rb delete mode 100644 lib/yard/mruby/handlers/header/base.rb delete mode 100644 lib/yard/mruby/handlers/header/define_handler.rb delete mode 100644 lib/yard/mruby/handlers/header/function_handler.rb delete mode 100644 lib/yard/mruby/handlers/header/header_handler.rb create mode 100644 lib/yard/mruby/parser/c.rb create mode 100644 lib/yard/mruby/parser/c/header_parser.rb create mode 100644 lib/yard/mruby/parser/c/parser.rb create mode 100644 lib/yard/mruby/parser/c/source_parser.rb delete mode 100644 lib/yard/mruby/parser/header_parser.rb delete mode 100644 spec/handlers/c/class_handler_spec.rb create mode 100644 spec/handlers/c/header/function_handler_spec.rb create mode 100644 spec/handlers/c/header/spec_helper.rb delete mode 100644 spec/handlers/c/method_handler_spec.rb delete mode 100644 spec/handlers/c/module_handler_spec.rb create mode 100644 spec/handlers/c/source/class_handler_spec.rb create mode 100644 spec/handlers/c/source/method_handler_spec.rb create mode 100644 spec/handlers/c/source/module_handler_spec.rb create mode 100644 spec/handlers/c/source/spec_helper.rb delete mode 100644 spec/handlers/header/function_handler_spec.rb delete mode 100644 spec/handlers/header/spec_helper.rb diff --git a/lib/yard/mruby.rb b/lib/yard/mruby.rb index 194c9e7..8d78182 100644 --- a/lib/yard/mruby.rb +++ b/lib/yard/mruby.rb @@ -1,3 +1,5 @@ +require 'yard' + require_relative "mruby/version" require_relative "mruby/code_objects" require_relative "mruby/parser" diff --git a/lib/yard/mruby/code_objects/header_object.rb b/lib/yard/mruby/code_objects/header_object.rb index 59f67a3..2e70230 100644 --- a/lib/yard/mruby/code_objects/header_object.rb +++ b/lib/yard/mruby/code_objects/header_object.rb @@ -3,12 +3,6 @@ module YARD::MRuby::CodeObjects # A HeaderObject represents a MRuby header inside an include directory # It groups C Functions and define macros. class HeaderObject < YARD::CodeObjects::NamespaceObject - def initialize(*args) - super - - raise "Invalid Header" unless self.title.match(/\.h$/) - end - def functions children.find_all {|d| d.is_a?(FunctionObject) } end diff --git a/lib/yard/mruby/handlers.rb b/lib/yard/mruby/handlers.rb index bcd1a5d..1ae677a 100644 --- a/lib/yard/mruby/handlers.rb +++ b/lib/yard/mruby/handlers.rb @@ -1,2 +1 @@ require_relative 'handlers/c' -require_relative 'handlers/header' diff --git a/lib/yard/mruby/handlers/c.rb b/lib/yard/mruby/handlers/c.rb index 2f25788..766989a 100644 --- a/lib/yard/mruby/handlers/c.rb +++ b/lib/yard/mruby/handlers/c.rb @@ -1,6 +1,4 @@ -require_relative 'c/base' -require_relative 'c/symbol_handler' -require_relative 'c/class_handler' -require_relative 'c/module_handler' -require_relative 'c/method_handler' +require_relative 'c/source' +require_relative 'c/header' +YARD::Handlers::Processor.namespace_for_handler.delete(:c) diff --git a/lib/yard/mruby/handlers/c/base.rb b/lib/yard/mruby/handlers/c/base.rb deleted file mode 100644 index 6bb2436..0000000 --- a/lib/yard/mruby/handlers/c/base.rb +++ /dev/null @@ -1,52 +0,0 @@ -module YARD::MRuby::Handlers - module C - class Base < YARD::Handlers::C::Base - - DEFAULT_NAMESPACES = { - # 'mrb->top_self' => '', - 'object_class' => 'Object', - 'class_class' => 'Class', - 'module_class' => 'Module', - 'proc_class' => 'Proc', - 'string_class' => 'String', - 'array_class' => 'Array', - 'hash_class' => 'Hash', - 'float_class' => 'Float', - 'fixnum_class' => 'Fixnum', - 'true_class' => 'TrueClass', - 'false_class' => 'FalseClass', - 'nil_class' => 'NilClass', - 'symbol_class' => 'Symbol', - 'kernel_module' => 'Kernel' - } - - def namespace_for_variable(var) - DEFAULT_NAMESPACES[ var[/^\w+->(\w+)$/, 1] ] || super - end - - def handle_class(var_name, class_name, parent, stmt, in_module = nil) - object = super(var_name, class_name, parent, in_module) - - if stmt.comments - register_docstring(object, stmt.comments.source, stmt) - end - - object - end - - def handle_module(var_name, module_name, stmt, in_module = nil) - object = super(var_name, module_name, in_module) - - if stmt.comments - register_docstring(object, stmt.comments.source, stmt) - end - - object - end - - end - - end - - YARD::Handlers::Processor.register_handler_namespace :c, C -end diff --git a/lib/yard/mruby/handlers/c/class_handler.rb b/lib/yard/mruby/handlers/c/class_handler.rb deleted file mode 100644 index f0223f0..0000000 --- a/lib/yard/mruby/handlers/c/class_handler.rb +++ /dev/null @@ -1,35 +0,0 @@ -module YARD::MRuby::Handlers::C - class ClassHandler < Base - - TOP_LEVEL_CLASS = /([\w]+)\s*=\s*mrb_define_class\s* - \( - \s*\w+\s*, - \s*"(\w+)"\s*, - \s*([\w\->]+)\s* - \) - /mx - - NAMESPACED_CLASS = /([\w]+)\s*=\s*mrb_define_class_under\s* - \( - \s*\w+\s*, - \s*(\w+)\s*, - \s*"(\w+)"\s*, - \s*([\w\->]+)\s* - \) - /mx - - handles TOP_LEVEL_CLASS - handles NAMESPACED_CLASS - - statement_class BodyStatement - - process do - statement.source.scan(TOP_LEVEL_CLASS) do |var_name, class_name, parent| - handle_class(var_name, class_name, parent, statement) - end - statement.source.scan(NAMESPACED_CLASS) do |var_name, in_module, class_name, parent| - handle_class(var_name, class_name, parent, statement, in_module) - end - end - end -end diff --git a/lib/yard/mruby/handlers/c/header.rb b/lib/yard/mruby/handlers/c/header.rb new file mode 100644 index 0000000..590386f --- /dev/null +++ b/lib/yard/mruby/handlers/c/header.rb @@ -0,0 +1,2 @@ +require_relative 'header/base' +require_relative 'header/function_handler' diff --git a/lib/yard/mruby/handlers/c/header/base.rb b/lib/yard/mruby/handlers/c/header/base.rb new file mode 100644 index 0000000..f0771ca --- /dev/null +++ b/lib/yard/mruby/handlers/c/header/base.rb @@ -0,0 +1,30 @@ +module YARD::MRuby::Handlers + module C + module Header + class Base < YARD::Handlers::C::Base + include YARD::MRuby::CodeObjects + + def header(path) + # Remove include prefix + path = path.gsub(/^.*include\//,'') + + headers[path] ||= begin + header = HeaderObject.new(HEADERS_ROOT, path) + register header + header + end + + + end + + def headers + globals.mruby_headers ||= {} + end + + end + + end + + YARD::Handlers::Processor.register_handler_namespace :header, Header + end +end diff --git a/lib/yard/mruby/handlers/c/header/define_handler.rb b/lib/yard/mruby/handlers/c/header/define_handler.rb new file mode 100644 index 0000000..e69de29 diff --git a/lib/yard/mruby/handlers/c/header/function_handler.rb b/lib/yard/mruby/handlers/c/header/function_handler.rb new file mode 100644 index 0000000..6bdb8a6 --- /dev/null +++ b/lib/yard/mruby/handlers/c/header/function_handler.rb @@ -0,0 +1,26 @@ +module YARD::MRuby::Handlers::C::Header + class FunctionHandler < Base + MATCH = / + MRB_(API|INLINE)\s+((\w+\s+)+)(\w+)\s*\( + /mx + + handles MATCH + statement_class ToplevelStatement + + process do + handle_function(statement) + end + + def handle_function(statement) + header = self.header(statement.file) + + statement.source.scan(MATCH) do |type, prefix, _, name, *args| + register FunctionObject.new(header, name) do |obj| + if statement.comments + register_docstring(obj, statement.comments.source, statement) + end + end + end + end + end +end diff --git a/lib/yard/mruby/handlers/c/method_handler.rb b/lib/yard/mruby/handlers/c/method_handler.rb deleted file mode 100644 index d60b568..0000000 --- a/lib/yard/mruby/handlers/c/method_handler.rb +++ /dev/null @@ -1,25 +0,0 @@ -module YARD::MRuby::Handlers::C - class MethodHandler < Base - MATCH1 = /mrb_define_( - method | - singleton_method | - module_function - ) - \s*\( - \s*\w+\s*, - \s*(\w+)\s*, - \s*"(\w+)"\s*, - \s*(\w+)\s*, - /mx - - handles MATCH1 - statement_class BodyStatement - - process do - statement.source.scan(MATCH1) do |type,var_name, name, func_name| - handle_method(type, var_name, name, func_name) - end - end - - end -end diff --git a/lib/yard/mruby/handlers/c/module_handler.rb b/lib/yard/mruby/handlers/c/module_handler.rb deleted file mode 100644 index 6b38be9..0000000 --- a/lib/yard/mruby/handlers/c/module_handler.rb +++ /dev/null @@ -1,34 +0,0 @@ -module YARD::MRuby::Handlers::C - class ModuleHandler < Base - - TOP_LEVEL_MODULE = /([\w]+)\s*=\s*mrb_define_module\s* - \( - \s*\w+\s*, - \s*"(\w+)"\s* - \) - /mx - - NAMESPACED_MODULE = /([\w]+)\s*=\s*mrb_define_module_under\s* - \( - \s*\w+\s*, - \s*(\w+)\s*, - \s*"(\w+)"\s* - \) - /mx - - handles TOP_LEVEL_MODULE - handles NAMESPACED_MODULE - - statement_class BodyStatement - - process do - statement.source.scan(TOP_LEVEL_MODULE) do |var_name, module_name| - handle_module(var_name, module_name, statement) - end - statement.source.scan(NAMESPACED_MODULE) do |var_name, in_module, module_name| - handle_module(var_name, module_name, statement, in_module) - end - end - end -end - diff --git a/lib/yard/mruby/handlers/c/source.rb b/lib/yard/mruby/handlers/c/source.rb new file mode 100644 index 0000000..8f2a68c --- /dev/null +++ b/lib/yard/mruby/handlers/c/source.rb @@ -0,0 +1,7 @@ +require_relative 'source/base' +require_relative 'source/init_handler' +require_relative 'source/symbol_handler' +require_relative 'source/class_handler' +require_relative 'source/module_handler' +require_relative 'source/method_handler' + diff --git a/lib/yard/mruby/handlers/c/source/base.rb b/lib/yard/mruby/handlers/c/source/base.rb new file mode 100644 index 0000000..268ef9f --- /dev/null +++ b/lib/yard/mruby/handlers/c/source/base.rb @@ -0,0 +1,54 @@ +module YARD::MRuby::Handlers + module C + module Source + class Base < YARD::Handlers::C::Base + + DEFAULT_NAMESPACES = { + # 'mrb->top_self' => '', + 'object_class' => 'Object', + 'class_class' => 'Class', + 'module_class' => 'Module', + 'proc_class' => 'Proc', + 'string_class' => 'String', + 'array_class' => 'Array', + 'hash_class' => 'Hash', + 'float_class' => 'Float', + 'fixnum_class' => 'Fixnum', + 'true_class' => 'TrueClass', + 'false_class' => 'FalseClass', + 'nil_class' => 'NilClass', + 'symbol_class' => 'Symbol', + 'kernel_module' => 'Kernel' + } + + def namespace_for_variable(var) + DEFAULT_NAMESPACES[ var[/^\w+->(\w+)$/, 1] ] || super + end + + def handle_class(var_name, class_name, parent, stmt, in_module = nil) + object = super(var_name, class_name, parent, in_module) + + if stmt.comments + register_docstring(object, stmt.comments.source, stmt) + end + + object + end + + def handle_module(var_name, module_name, stmt, in_module = nil) + object = super(var_name, module_name, in_module) + + if stmt.comments + register_docstring(object, stmt.comments.source, stmt) + end + + object + end + + end + + end + + YARD::Handlers::Processor.register_handler_namespace :source, Source + end +end diff --git a/lib/yard/mruby/handlers/c/source/class_handler.rb b/lib/yard/mruby/handlers/c/source/class_handler.rb new file mode 100644 index 0000000..a8f3b84 --- /dev/null +++ b/lib/yard/mruby/handlers/c/source/class_handler.rb @@ -0,0 +1,35 @@ +module YARD::MRuby::Handlers::C::Source + class ClassHandler < Base + + TOP_LEVEL_CLASS = /([\w]+)\s*=\s*mrb_define_class\s* + \( + \s*\w+\s*, + \s*"(\w+)"\s*, + \s*([\w\->]+)\s* + \) + /mx + + NAMESPACED_CLASS = /([\w]+)\s*=\s*mrb_define_class_under\s* + \( + \s*\w+\s*, + \s*(\w+)\s*, + \s*"(\w+)"\s*, + \s*([\w\->]+)\s* + \) + /mx + + handles TOP_LEVEL_CLASS + handles NAMESPACED_CLASS + + statement_class BodyStatement + + process do + statement.source.scan(TOP_LEVEL_CLASS) do |var_name, class_name, parent| + handle_class(var_name, class_name, parent, statement) + end + statement.source.scan(NAMESPACED_CLASS) do |var_name, in_module, class_name, parent| + handle_class(var_name, class_name, parent, statement, in_module) + end + end + end +end diff --git a/lib/yard/mruby/handlers/c/source/init_handler.rb b/lib/yard/mruby/handlers/c/source/init_handler.rb new file mode 100644 index 0000000..43115a0 --- /dev/null +++ b/lib/yard/mruby/handlers/c/source/init_handler.rb @@ -0,0 +1,16 @@ +module YARD::MRuby::Handlers::C::Source + class InitHandler < Base + MATCH1 = /mrb_\w+_gem_init\s*\(/mx + MATCH2 = /mrb_init_\w\s*\(/mx + + handles MATCH1 + handles MATCH2 + statement_class ToplevelStatement + + process do + parse_block + end + + end +end + diff --git a/lib/yard/mruby/handlers/c/source/method_handler.rb b/lib/yard/mruby/handlers/c/source/method_handler.rb new file mode 100644 index 0000000..b744f54 --- /dev/null +++ b/lib/yard/mruby/handlers/c/source/method_handler.rb @@ -0,0 +1,25 @@ +module YARD::MRuby::Handlers::C::Source + class MethodHandler < Base + MATCH1 = /mrb_define_( + method | + singleton_method | + module_function + ) + \s*\( + \s*\w+\s*, + \s*(\w+)\s*, + \s*"(\w+)"\s*, + \s*(\w+)\s*, + /mx + + handles MATCH1 + statement_class BodyStatement + + process do + statement.source.scan(MATCH1) do |type,var_name, name, func_name| + handle_method(type, var_name, name, func_name) + end + end + + end +end diff --git a/lib/yard/mruby/handlers/c/source/module_handler.rb b/lib/yard/mruby/handlers/c/source/module_handler.rb new file mode 100644 index 0000000..587fc3e --- /dev/null +++ b/lib/yard/mruby/handlers/c/source/module_handler.rb @@ -0,0 +1,34 @@ +module YARD::MRuby::Handlers::C::Source + class ModuleHandler < Base + + TOP_LEVEL_MODULE = /([\w]+)\s*=\s*mrb_define_module\s* + \( + \s*\w+\s*, + \s*"(\w+)"\s* + \) + /mx + + NAMESPACED_MODULE = /([\w]+)\s*=\s*mrb_define_module_under\s* + \( + \s*\w+\s*, + \s*(\w+)\s*, + \s*"(\w+)"\s* + \) + /mx + + handles TOP_LEVEL_MODULE + handles NAMESPACED_MODULE + + statement_class BodyStatement + + process do + statement.source.scan(TOP_LEVEL_MODULE) do |var_name, module_name| + handle_module(var_name, module_name, statement) + end + statement.source.scan(NAMESPACED_MODULE) do |var_name, in_module, module_name| + handle_module(var_name, module_name, statement, in_module) + end + end + end +end + diff --git a/lib/yard/mruby/handlers/c/source/symbol_handler.rb b/lib/yard/mruby/handlers/c/source/symbol_handler.rb new file mode 100644 index 0000000..a1e3caa --- /dev/null +++ b/lib/yard/mruby/handlers/c/source/symbol_handler.rb @@ -0,0 +1,16 @@ +module YARD::MRuby::Handlers::C::Source + # Keeps track of function bodies for symbol lookup during MRuby method declarations + class SymbolHandler < Base + + MATCH = / + mrb_value\s*(\w+)\s*\(\s*mrb_state\s*\*\s*\w+,\s*mrb_value\s*\w+\s*\) + /mx + + handles MATCH + statement_class ToplevelStatement + + process do + symbols[statement.source[MATCH, 1]] = statement + end + end +end diff --git a/lib/yard/mruby/handlers/c/symbol_handler.rb b/lib/yard/mruby/handlers/c/symbol_handler.rb deleted file mode 100644 index 8f24b93..0000000 --- a/lib/yard/mruby/handlers/c/symbol_handler.rb +++ /dev/null @@ -1,16 +0,0 @@ -module YARD::MRuby::Handlers::C - # Keeps track of function bodies for symbol lookup during MRuby method declarations - class SymbolHandler < Base - - MATCH = / - mrb_value\s*(\w+)\s*\(\s*mrb_state\s*\*\s*\w+,\s*mrb_value\s*\w+\s*\) - /mx - - handles MATCH - statement_class ToplevelStatement - - process do - symbols[statement.source[MATCH, 1]] = statement - end - end -end diff --git a/lib/yard/mruby/handlers/header.rb b/lib/yard/mruby/handlers/header.rb deleted file mode 100644 index 590386f..0000000 --- a/lib/yard/mruby/handlers/header.rb +++ /dev/null @@ -1,2 +0,0 @@ -require_relative 'header/base' -require_relative 'header/function_handler' diff --git a/lib/yard/mruby/handlers/header/base.rb b/lib/yard/mruby/handlers/header/base.rb deleted file mode 100644 index 1f3f16e..0000000 --- a/lib/yard/mruby/handlers/header/base.rb +++ /dev/null @@ -1,28 +0,0 @@ -module YARD::MRuby::Handlers - module Header - class Base < YARD::Handlers::C::Base - include YARD::MRuby::CodeObjects - - def header(path) - # Remove include prefix - path = path.gsub(/^.*include\//,'') - - headers[path] ||= begin - header = HeaderObject.new(HEADERS_ROOT, path) - register header - header - end - - - end - - def headers - globals.mruby_headers ||= {} - end - - end - - end - - YARD::Handlers::Processor.register_handler_namespace :header, Header -end diff --git a/lib/yard/mruby/handlers/header/define_handler.rb b/lib/yard/mruby/handlers/header/define_handler.rb deleted file mode 100644 index e69de29..0000000 diff --git a/lib/yard/mruby/handlers/header/function_handler.rb b/lib/yard/mruby/handlers/header/function_handler.rb deleted file mode 100644 index 9b98258..0000000 --- a/lib/yard/mruby/handlers/header/function_handler.rb +++ /dev/null @@ -1,23 +0,0 @@ -module YARD::MRuby::Handlers::Header - class FunctionHandler < Base - MATCH = / - MRB_(API|INLINE)\s+((\w+\s+)+)(\w+)\s*\( - /mx - - handles MATCH - statement_class ToplevelStatement - - process do - handle_function(statement) - end - - def handle_function(statement) - header = self.header(statement.file) - - statement.source.scan(MATCH) do |type, prefix, _, name, *args| - register FunctionObject.new(header, name) do |obj| - end - end - end - end -end diff --git a/lib/yard/mruby/handlers/header/header_handler.rb b/lib/yard/mruby/handlers/header/header_handler.rb deleted file mode 100644 index e69de29..0000000 diff --git a/lib/yard/mruby/parser.rb b/lib/yard/mruby/parser.rb index f8a74ac..4c91895 100644 --- a/lib/yard/mruby/parser.rb +++ b/lib/yard/mruby/parser.rb @@ -1 +1 @@ -require_relative 'parser/header_parser' +require_relative 'parser/c' diff --git a/lib/yard/mruby/parser/c.rb b/lib/yard/mruby/parser/c.rb new file mode 100644 index 0000000..4d95fa1 --- /dev/null +++ b/lib/yard/mruby/parser/c.rb @@ -0,0 +1,3 @@ +require_relative 'c/parser' +require_relative 'c/source_parser' +require_relative 'c/header_parser' diff --git a/lib/yard/mruby/parser/c/header_parser.rb b/lib/yard/mruby/parser/c/header_parser.rb new file mode 100644 index 0000000..598fe21 --- /dev/null +++ b/lib/yard/mruby/parser/c/header_parser.rb @@ -0,0 +1,8 @@ +module YARD::MRuby::Parser::C + class HeaderParser < Parser + end + + # + # Register all header files (.h) to be processed with the above HeaderParser + YARD::Parser::SourceParser.register_parser_type :header, HeaderParser, 'h' +end diff --git a/lib/yard/mruby/parser/c/parser.rb b/lib/yard/mruby/parser/c/parser.rb new file mode 100644 index 0000000..3b6414a --- /dev/null +++ b/lib/yard/mruby/parser/c/parser.rb @@ -0,0 +1,10 @@ +module YARD::MRuby::Parser + module C + class Parser < YARD::Parser::C::CParser + end + + # Disable default C Parser + YARD::Parser::SourceParser.parser_types.delete(:c) + YARD::Parser::SourceParser.parser_type_extensions.delete(:c) + end +end diff --git a/lib/yard/mruby/parser/c/source_parser.rb b/lib/yard/mruby/parser/c/source_parser.rb new file mode 100644 index 0000000..f468dea --- /dev/null +++ b/lib/yard/mruby/parser/c/source_parser.rb @@ -0,0 +1,9 @@ +module YARD::MRuby::Parser::C + class SourceParser < Parser + end + + # + # Register all header files (.h) to be processed with the above HeaderParser + YARD::Parser::SourceParser.register_parser_type :source, SourceParser, 'c' + +end diff --git a/lib/yard/mruby/parser/header_parser.rb b/lib/yard/mruby/parser/header_parser.rb deleted file mode 100644 index 6fe4df3..0000000 --- a/lib/yard/mruby/parser/header_parser.rb +++ /dev/null @@ -1,9 +0,0 @@ - -module YARD::MRuby::Parser - class HeaderParser < YARD::Parser::C::CParser - end - - # - # Register all header files (.h) to be processed with the above HeaderParser - YARD::Parser::SourceParser.register_parser_type :header, HeaderParser, 'h' -end diff --git a/spec/handlers/c/class_handler_spec.rb b/spec/handlers/c/class_handler_spec.rb deleted file mode 100644 index 9428dd6..0000000 --- a/spec/handlers/c/class_handler_spec.rb +++ /dev/null @@ -1,41 +0,0 @@ -require_relative 'spec_helper' - -describe YARD::MRuby::Handlers::C::ClassHandler do - it "should register classes" do - parse_init 'cFoo = mrb_define_class(mrb, "Foo", mrb->object_class);' - - expect(Registry.at('Foo').type).to be :class - end - - it "should register classes under namespaces" do - parse_init 'cFoo = mrb_define_class_under(mrb, cBar, "Foo", rb_cObject);' - expect(Registry.at('Bar::Foo').type).to be :class - end - - it "should remember symbol defined with class" do - parse_init(<<-eof) - cXYZ = mrb_define_class(mrb, "Foo", mrb->object_class); - mrb_define_method(mrb, cXYZ, "bar", bar, MRB_ARGS_NONE()); - eof - expect(Registry.at('Foo').type).to be :class - expect(Registry.at('Foo#bar')).not_to be_nil - end - - it "should lookup superclass symbol name" do - parse_init(<<-eof) - cXYZ = mrb_define_class(mrb,"Foo", mrb->object_class); - cBar = mrb_define_class(mrb,"Bar", cXYZ); - eof - expect(Registry.at('Bar').superclass).to eq Registry.at('Foo') - end - - it "should associate declaration comments as class docstring" do - parse_init(<<-eof) - /* Docstring! */ - cFoo = mrb_define_class(mrb, "Foo", mrb->object_class); - eof - - expect(Registry.at('Foo').docstring).not_to be_blank - end - -end diff --git a/spec/handlers/c/header/function_handler_spec.rb b/spec/handlers/c/header/function_handler_spec.rb new file mode 100644 index 0000000..2270584 --- /dev/null +++ b/spec/handlers/c/header/function_handler_spec.rb @@ -0,0 +1,22 @@ +require_relative 'spec_helper' + +describe YARD::MRuby::Handlers::C::Header::FunctionHandler do + it "should register functions" do + header_line <<-eof + MRB_API void mrb_foo( void ); + eof + expect(Registry.at('mrb_foo')).not_to be_nil + + # puts Registry.send(:thread_local_store).inspect + end + + it "should find docstrings attached to functions" do + header_line <<-eof + /* DOCSTRING */ + MRB_API void mrb_foo( void ); + eof + + foo = Registry.at('mrb_foo') + expect(foo.docstring).to eq 'DOCSTRING' + end +end diff --git a/spec/handlers/c/header/spec_helper.rb b/spec/handlers/c/header/spec_helper.rb new file mode 100644 index 0000000..da2b74f --- /dev/null +++ b/spec/handlers/c/header/spec_helper.rb @@ -0,0 +1,21 @@ + +require_relative '../spec_helper' + +def header(src, file = 'stdin.h') + YARD::Registry.clear + parser = YARD::Parser::SourceParser.new(:header) + parser.file = file + parser.parse(StringIO.new(src)) +end + +def header_line(src) + header <<-EOF + #include "mruby.h" + #ifndef HEADER_H + #define HEADER_H + MRB_BEGIN_DECL + #{src} + MRB_END_DECL + #endif + EOF +end diff --git a/spec/handlers/c/method_handler_spec.rb b/spec/handlers/c/method_handler_spec.rb deleted file mode 100644 index 31e297d..0000000 --- a/spec/handlers/c/method_handler_spec.rb +++ /dev/null @@ -1,48 +0,0 @@ -require_relative 'spec_helper' - -describe YARD::MRuby::Handlers::C::MethodHandler do - it "should register methods" do - parse_init <<-eof - mFoo = mrb_define_module(mrb, "Foo"); - mrb_define_method(mrb, mFoo, "bar", bar, MRB_ARGS_NONE()); - eof - expect(Registry.at('Foo#bar')).not_to be_nil - expect(Registry.at('Foo#bar').visibility).to be :public - end - - it "should register singleton methods" do - parse_init <<-eof - mFoo = mrb_define_module("Foo"); - mrb_define_singleton_method(mrb, mFoo, "bar", bar, MRB_ARGS_NONE()); - eof - expect(Registry.at('Foo.bar')).not_to be_nil - expect(Registry.at('Foo.bar').visibility).to be :public - end - - it "should register module functions" do - parse <<-eof - /* DOCSTRING - * @return [String] foo! - */ - static mrb_value bar(mrb_state *mrb, mrb_value self) { x(); y(); z(); } - - void mrb_foo_gem_init(mrb_state *mrb) { - mFoo = mrb_define_module(mrb, "Foo"); - mrb_define_module_function(mrb, mFoo, "bar", bar, MRB_ARGS_NONE()); - } - eof - bar_c = Registry.at('Foo.bar') - bar_i = Registry.at('Foo#bar') - - expect(bar_c).to be_module_function - expect(bar_c.visibility).to be :public - expect(bar_c.docstring).to eq "DOCSTRING" - expect(bar_c.tag(:return).object).to eq bar_c - expect(bar_c.source).to eq "static mrb_value bar(mrb_state *mrb, mrb_value self) { x(); y(); z(); }" - expect(bar_i).not_to be_module_function - expect(bar_i.visibility).to be :private - expect(bar_i.docstring).to eq "DOCSTRING" - expect(bar_i.tag(:return).object).to eq bar_i - expect(bar_i.source).to eq bar_c.source - end -end diff --git a/spec/handlers/c/module_handler_spec.rb b/spec/handlers/c/module_handler_spec.rb deleted file mode 100644 index aedf1a5..0000000 --- a/spec/handlers/c/module_handler_spec.rb +++ /dev/null @@ -1,30 +0,0 @@ -require_relative 'spec_helper' - -describe YARD::MRuby::Handlers::C::ModuleHandler do - it "should register modules" do - parse_init 'mFoo = mrb_define_module(mrb, "Foo");' - expect(Registry.at('Foo').type).to be :module - end - - it "should register modules under namespaces" do - parse_init 'mFoo = mrb_define_module_under(mrb, mBar, "Foo");' - expect(Registry.at('Bar::Foo').type).to be :module - end - - it "should remember symbol defined with module" do - parse_init(<<-eof) - cXYZ = mrb_define_module(mrb, "Foo"); - mrb_define_method(mrb, cXYZ, "bar", bar, 0); - eof - expect(Registry.at('Foo').type).to be :module - expect(Registry.at('Foo#bar')).not_to be_nil - end - - it "should associate declaration comments as module docstring" do - parse_init(<<-eof) - /* Docstring! */ - mFoo = mrb_define_module(mrb, "Foo"); - eof - expect(Registry.at('Foo').docstring).not_to be_blank - end -end diff --git a/spec/handlers/c/source/class_handler_spec.rb b/spec/handlers/c/source/class_handler_spec.rb new file mode 100644 index 0000000..bfac3b6 --- /dev/null +++ b/spec/handlers/c/source/class_handler_spec.rb @@ -0,0 +1,41 @@ +require_relative 'spec_helper' + +describe YARD::MRuby::Handlers::C::Source::ClassHandler do + it "should register classes" do + parse_init 'cFoo = mrb_define_class(mrb, "Foo", mrb->object_class);' + + expect(Registry.at('Foo').type).to be :class + end + + it "should register classes under namespaces" do + parse_init 'cFoo = mrb_define_class_under(mrb, cBar, "Foo", rb_cObject);' + expect(Registry.at('Bar::Foo').type).to be :class + end + + it "should remember symbol defined with class" do + parse_init(<<-eof) + cXYZ = mrb_define_class(mrb, "Foo", mrb->object_class); + mrb_define_method(mrb, cXYZ, "bar", bar, MRB_ARGS_NONE()); + eof + expect(Registry.at('Foo').type).to be :class + expect(Registry.at('Foo#bar')).not_to be_nil + end + + it "should lookup superclass symbol name" do + parse_init(<<-eof) + cXYZ = mrb_define_class(mrb,"Foo", mrb->object_class); + cBar = mrb_define_class(mrb,"Bar", cXYZ); + eof + expect(Registry.at('Bar').superclass).to eq Registry.at('Foo') + end + + it "should associate declaration comments as class docstring" do + parse_init(<<-eof) + /* Docstring! */ + cFoo = mrb_define_class(mrb, "Foo", mrb->object_class); + eof + + expect(Registry.at('Foo').docstring).not_to be_blank + end + +end diff --git a/spec/handlers/c/source/method_handler_spec.rb b/spec/handlers/c/source/method_handler_spec.rb new file mode 100644 index 0000000..713e390 --- /dev/null +++ b/spec/handlers/c/source/method_handler_spec.rb @@ -0,0 +1,48 @@ +require_relative 'spec_helper' + +describe YARD::MRuby::Handlers::C::Source::MethodHandler do + it "should register methods" do + parse_init <<-eof + mFoo = mrb_define_module(mrb, "Foo"); + mrb_define_method(mrb, mFoo, "bar", bar, MRB_ARGS_NONE()); + eof + expect(Registry.at('Foo#bar')).not_to be_nil + expect(Registry.at('Foo#bar').visibility).to be :public + end + + it "should register singleton methods" do + parse_init <<-eof + mFoo = mrb_define_module("Foo"); + mrb_define_singleton_method(mrb, mFoo, "bar", bar, MRB_ARGS_NONE()); + eof + expect(Registry.at('Foo.bar')).not_to be_nil + expect(Registry.at('Foo.bar').visibility).to be :public + end + + it "should register module functions" do + parse <<-eof + /* DOCSTRING + * @return [String] foo! + */ + static mrb_value bar(mrb_state *mrb, mrb_value self) { x(); y(); z(); } + + void mrb_foo_gem_init(mrb_state *mrb) { + mFoo = mrb_define_module(mrb, "Foo"); + mrb_define_module_function(mrb, mFoo, "bar", bar, MRB_ARGS_NONE()); + } + eof + bar_c = Registry.at('Foo.bar') + bar_i = Registry.at('Foo#bar') + + expect(bar_c).to be_module_function + expect(bar_c.visibility).to be :public + expect(bar_c.docstring).to eq "DOCSTRING" + expect(bar_c.tag(:return).object).to eq bar_c + expect(bar_c.source).to eq "static mrb_value bar(mrb_state *mrb, mrb_value self) { x(); y(); z(); }" + expect(bar_i).not_to be_module_function + expect(bar_i.visibility).to be :private + expect(bar_i.docstring).to eq "DOCSTRING" + expect(bar_i.tag(:return).object).to eq bar_i + expect(bar_i.source).to eq bar_c.source + end +end diff --git a/spec/handlers/c/source/module_handler_spec.rb b/spec/handlers/c/source/module_handler_spec.rb new file mode 100644 index 0000000..fd08ae6 --- /dev/null +++ b/spec/handlers/c/source/module_handler_spec.rb @@ -0,0 +1,30 @@ +require_relative 'spec_helper' + +describe YARD::MRuby::Handlers::C::Source::ModuleHandler do + it "should register modules" do + parse_init 'mFoo = mrb_define_module(mrb, "Foo");' + expect(Registry.at('Foo').type).to be :module + end + + it "should register modules under namespaces" do + parse_init 'mFoo = mrb_define_module_under(mrb, mBar, "Foo");' + expect(Registry.at('Bar::Foo').type).to be :module + end + + it "should remember symbol defined with module" do + parse_init(<<-eof) + cXYZ = mrb_define_module(mrb, "Foo"); + mrb_define_method(mrb, cXYZ, "bar", bar, 0); + eof + expect(Registry.at('Foo').type).to be :module + expect(Registry.at('Foo#bar')).not_to be_nil + end + + it "should associate declaration comments as module docstring" do + parse_init(<<-eof) + /* Docstring! */ + mFoo = mrb_define_module(mrb, "Foo"); + eof + expect(Registry.at('Foo').docstring).not_to be_blank + end +end diff --git a/spec/handlers/c/source/spec_helper.rb b/spec/handlers/c/source/spec_helper.rb new file mode 100644 index 0000000..76844a4 --- /dev/null +++ b/spec/handlers/c/source/spec_helper.rb @@ -0,0 +1,13 @@ +require_relative '../spec_helper' + +def parse(src, file = '(stdin)') + YARD::Registry.clear + parser = YARD::Parser::SourceParser.new(:source) + parser.file = file + parser.parse(StringIO.new(src)) +end + +def parse_init(src) + YARD::Registry.clear + YARD.parse_string("void mrb_foo_gem_init(mrb_state *mrb) {\n#{src}\n}", :source) +end diff --git a/spec/handlers/c/spec_helper.rb b/spec/handlers/c/spec_helper.rb index 9424e3e..935238d 100644 --- a/spec/handlers/c/spec_helper.rb +++ b/spec/handlers/c/spec_helper.rb @@ -1,13 +1 @@ require_relative '../spec_helper' - -def parse(src, file = '(stdin)') - YARD::Registry.clear - parser = YARD::Parser::SourceParser.new(:c) - parser.file = file - parser.parse(StringIO.new(src)) -end - -def parse_init(src) - YARD::Registry.clear - YARD.parse_string("void mrb_foo_gem_init(mrb_state *mrb) {\n#{src}\n}", :c) -end diff --git a/spec/handlers/header/function_handler_spec.rb b/spec/handlers/header/function_handler_spec.rb deleted file mode 100644 index b97abb6..0000000 --- a/spec/handlers/header/function_handler_spec.rb +++ /dev/null @@ -1,12 +0,0 @@ -require_relative 'spec_helper' - -describe YARD::MRuby::Handlers::Header::FunctionHandler do - it "should register functions" do - header_line <<-eof - MRB_API void mrb_foo( void ); - eof - expect(Registry.at('mrb_foo')).not_to be_nil - - # puts Registry.send(:thread_local_store).inspect - end -end diff --git a/spec/handlers/header/spec_helper.rb b/spec/handlers/header/spec_helper.rb deleted file mode 100644 index da2b74f..0000000 --- a/spec/handlers/header/spec_helper.rb +++ /dev/null @@ -1,21 +0,0 @@ - -require_relative '../spec_helper' - -def header(src, file = 'stdin.h') - YARD::Registry.clear - parser = YARD::Parser::SourceParser.new(:header) - parser.file = file - parser.parse(StringIO.new(src)) -end - -def header_line(src) - header <<-EOF - #include "mruby.h" - #ifndef HEADER_H - #define HEADER_H - MRB_BEGIN_DECL - #{src} - MRB_END_DECL - #endif - EOF -end -- cgit v1.2.3