Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Experiment with getting @!group directive working for C parser. #1238

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/yard/autoload.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ module C
autoload :AttributeHandler, __p('handlers/c/attribute_handler')
autoload :ClassHandler, __p('handlers/c/class_handler')
autoload :ConstantHandler, __p('handlers/c/constant_handler')
autoload :DirectiveParserHandler, __p('handlers/c/directive_parser_handler')
autoload :HandlerMethods, __p('handlers/c/handler_methods')
autoload :InitHandler, __p('handlers/c/init_handler')
autoload :MethodHandler, __p('handlers/c/method_handler')
Expand Down
9 changes: 9 additions & 0 deletions lib/yard/handlers/c/directive_parser_handler.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true
class YARD::Handlers::C::DirectiveParserHandler < YARD::Handlers::C::Base
handles(/./)
statement_class Comment

process do
Docstring.parser.parse(statement.source, namespace, self)
end
end
18 changes: 17 additions & 1 deletion lib/yard/parser/c/c_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,14 @@ def consume_body_statements
stmts = []
brace_level = 1
loop do
strip_non_statement_data
# Need to include the @! directive tags for them to be processed
# along with the body statement content.
# (At least the @!group directive.)
directive_tags = capture_directive_tags_hack do
strip_non_statement_data
end
stmts.concat(directive_tags)

start = @index
line = @line
consume_until(/[{};]/)
Expand All @@ -115,6 +122,15 @@ def consume_body_statements
stmts
end

def capture_directive_tags_hack
temp = @statements.dup
yield if block_given?
stmts = (@statements - temp)
stmts.select do |stmt|
stmt.is_a?(Comment) && stmt.source.start_with?('@!')
end
end

def strip_non_statement_data
start = @index
loop do
Expand Down
38 changes: 38 additions & 0 deletions spec/parser/c_parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,44 @@ def parse(contents)
end
end

describe "Group directives" do
it "groups constants" do
parse <<-eof
void Init_Mask(void)
{
rb_cExample = rb_define_class("Example", rb_cObject);

// @!group Foos

/* 1: Foobar description. */
rb_define_const(rb_cExample, "FOOBAR", INT2NUM(1));

/* 2: Foobiz description. */
rb_define_const(rb_cExample, "FOOBIZ", INT2NUM(2));

// @!endgroup

/* 3: Hello description. */
rb_define_const(rb_cExample, "HELLO", INT2NUM(3));
}
eof
constant = Registry.at('Example::FOOBAR')
expect(constant.value).to eq '1'
expect(constant.docstring).to eq "Foobar description."
expect(constant.group).to eq "Foos"

constant = Registry.at('Example::FOOBIZ')
expect(constant.value).to eq '2'
expect(constant.docstring).to eq "Foobiz description."
expect(constant.group).to eq "Foos"

constant = Registry.at('Example::HELLO')
expect(constant.value).to eq '3'
expect(constant.docstring).to eq "Hello description."
expect(constant.group).to eq nil
end
end

describe "Macros" do
it "handles param## inside of macros" do
thr = Thread.new do
Expand Down
33 changes: 33 additions & 0 deletions spec/parser/ruby/ruby_parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -504,5 +504,38 @@ def bar; end if true
it "handles compile errors" do
expect { stmt(":~$ Do not clobber") }.to raise_error(Parser::ParserSyntaxError)
end

it "groups constants" do
YARD.parse_string <<-eof
class Example
# @!group Foos

# Foobar description.
FOOBAR = 1

# Foobiz description.
FOOBIZ = 2

# @!endgroup

# Hello description.
HELLO = 3
end
eof
constant = Registry.at('Example::FOOBAR')
expect(constant.value).to eq '1'
expect(constant.docstring).to eq "Foobar description."
expect(constant.group).to eq "Foos"

constant = Registry.at('Example::FOOBIZ')
expect(constant.value).to eq '2'
expect(constant.docstring).to eq "Foobiz description."
expect(constant.group).to eq "Foos"

constant = Registry.at('Example::HELLO')
expect(constant.value).to eq '3'
expect(constant.docstring).to eq "Hello description."
expect(constant.group).to eq nil
end
end
end if HAVE_RIPPER