String::Comments::Extract - Extract comments from C/C++/JavaScript/Java source


String-Comments-Extract documentation Contained in the String-Comments-Extract distribution.

Index


Code Index:

NAME

Top

String::Comments::Extract - Extract comments from C/C++/JavaScript/Java source

VERSION

Top

version 0.023

SYNOPSIS

Top

    use String::Comments::Extract;

    my $source = <<_END_
    /* A Hello World program

        Copyright Ty Coon
        // ...and Buckaroo Banzai
      "Yoyodyne"*/

    void main() {
        printf("Hello, World.\n");
        printf("/* This is not a real comment */");
        printf("// Neither is this */");
        // But this is
    }

    // Last comment
    _END_

    my $comments = String::Comments::Extract::C->extract($source)
    # ... returns the following result:

        /* A Hello World program

            Copyright Ty Coon
            // ...and Buckaroo Banzai
          "Yoyodyne"*/

          


            


            


            


            // But this is




        // Last comment

    my @comments = String::Comments::Extract::C->collect($source)
    # ... returns the following list:
        (
' A Hello World program

        Copyright Ty Coon
        // ...and Buckaroo Banzai
      "Yoyodyne"',
            ' But this is',
            ' Last comment',
        )

DESCRIPTION

Top

String::Comments::Extract is a tool for extracting comments from C/C++/JavaScript/Java source. The extractor is implemented using an actual tokenizer (written in C via XS [adapted from JavaScript::Minifier::XS]). By using a tokenizer, it can correctly deal with notoriously problematic cases, such as comment-like structures embedded in strings:

    std::cout << "This is not a // real C++ comment " << std::endl
    printf("/* This is not a real C comment */\n");
    # The extractor will ignore both of the above

String::Comments::Extract considers C/C++/JavaScript/Java comment structures the same, so, for now, it doesn't really matter which method you use (this means it will not complain about C++ style comments in C source).

The language agnostic interface to C/C++/JavaScript/Java comment extractor is accessible via String::Comments::Extract::SlashStar

    # Can handle slash-star (/* */) and slash-slash (//) comments
    String::Comments::Extract::SlashStar->extract
    String::Comments::Extract::SlashStar->collect

METHODS

Top

String::Comments::Extract::JavaScript->extract( <source> )

String::Comments::Extract::CPP->extract( <source> )

String::Comments::Extract::C->extract( <source> )

String::Comments::Extract::SlashStar->extract( <source> )

Returns a string representing the comments in <source>

Comment delimeters ( /* */ // ) are left in as-is

Whitespace of <source> is otherwise preserved, so you'll probably have to do some post-processing to get rid of some cruft.

String::Comments::Extract::JavaScript->collect( <source> )

String::Comments::Extract::CPP->collect( <source> )

String::Comments::Extract::C->collect( <source> )

String::Comments::Extract::SlashStar->collect( <source> )

Returns a list containing an item for each block- or line-comment in <source>

Comment delimeters ( /* */ // ) around the comment are removed

Whitespace outside of comments may not be preserved exactly

SEE ALSO

Top

File::Comments

AUTHOR

Top

Robert Krimen <robertkrimen@gmail.com>

COPYRIGHT AND LICENSE

Top


String-Comments-Extract documentation Contained in the String-Comments-Extract distribution.

package String::Comments::Extract;
BEGIN {
  $String::Comments::Extract::VERSION = '0.023';
}
# ABSTRACT: Extract comments from C/C++/JavaScript/Java source

use warnings;
use strict;

use XSLoader;

XSLoader::load(
    __PACKAGE__,
    # We need to be careful not to touch $VERSION at compile time, otherwise
    # DynaLoader will assume it's set and check against it, which will cause
    # fail when being run in the checkout without dzil having set the actual
    # $VERSION
    exists $String::Comments::Extract::{VERSION} ? ${ $String::Comments::Extract::{VERSION} } : (), 
);

use String::Comments::Extract::SlashStar;
use String::Comments::Extract::C;
use String::Comments::Extract::CPP;
use String::Comments::Extract::JavaScript;
use String::Comments::Extract::Java;


1; # End of String::Comments::Extract

__END__