Hai有人请给我一个代码或说perl模块来解析ruby文件。 就像它必须计算有评论的行数。具有源代码的行数。 提取源代码并提取注释行。 在此先感谢
答案 0 :(得分:2)
您可以简单地处理ruby源文件并计算以\s*#
开头的任何行,或仅将空格作为注释,将所有其他行作为代码......
答案 1 :(得分:1)
你可以为Regexp::Grammars编写一个语法,然后为你解析文件。该模块有很好的文档记录。
我希望以下脚本可以帮助您理解解析背后的想法:
#!/usr/bin/env perl
use strict;
use warnings;
use Regexp::Grammars;
my $parser = qr{
<File>
<rule: File>
(
(?:
\n
| <[Comment]>
)
| <[Comment]>
| <[Source]>
)+
<rule: Comment>
\# <InlineComment>
| ^=begin
<MultilineComment> \n
^=end
<rule: InlineComment>
[^\n]+
<rule: MultilineComment>
.*
<rule: Source>
[^\n]+
}xms;
my $text = do { local $/; <DATA> };
if ( $text =~ $parser ) {
my @source;
my @comments;
if ( exists $/{'File'}->{'Source'} ) {
@source = @{ $/{'File'}->{'Source'} };
}
if ( exists $/{'File'}->{'Comment'} ) {
@comments = @{ $/{'File'}->{'Comment'} };
}
my $line = 1;
print '__SOURCE__ [', scalar @source, "]\n";
for (@source) {
print "$line: $_\n";
$line++;
}
print "\n\n";
$line = 1;
print '__COMMENTS__ [', scalar @comments, "]\n";
for my $comment (@comments) {
print "$line: ";
if ( exists $comment->{'InlineComment'} ) {
print $comment->{'InlineComment'};
}
elsif ( exists $comment->{'MultilineComment'} ) {
print $comment->{'MultilineComment'};
}
print "\n";
$line++;
}
}
else {
}
__DATA__
=begin
The following code snippet was copied from:
http://www.ruby-lang.org/en/documentation/quickstart/4/
=end
# Say hi to everybody
def say_hi
if @names.nil?
puts "..."
elsif @names.respond_to?("each")
# @names is a list of some kind, iterate!
@names.each do |name|
puts "Hello #{name}!"
end
else
puts "Hello #{@names}!"
end
end
__SOURCE__ [11]
1: def say_hi
2: if @names.nil?
3: puts "..."
4: elsif @names.respond_to?("each")
5: @names.each do |name|
6: puts "Hello #{name}!"
7: end
8: else
9: puts "Hello #{@names}!"
10: end
11: end
__COMMENTS__ [3]
1: The following code snippet was copied from:
http://www.ruby-lang.org/en/documentation/quickstart/4/
2: Say hi to everybody
3: @names is a list of some kind, iterate!