Acme-Comment

 view release on metacpan or  search on metacpan

Makefile.PL  view on Meta::CPAN

use strict;
use Cwd;
use File::Spec;

### Comment configuration ###
my $Conf = {
        C   => {
            start       => '/*',
            end         => '*/',
        },
        HTML    => {
            start       => '<!--',
            end         => '-->',
        },
        RUBY    => {
            start       => '=begin',
            end         => '=end',
            single      => '#',
        },
        JAVA    => {
            start       => '/*',
            end         => '*/',
            single      => '//',
        },
        PASCAL  => {
            start       => '(*',
            end         => '*)',
        },

        ALGOL  => {
            start       => "'comment'",
            end         => ';',
        },

        HUGO    => {
            start       => '!\\',
            end         => '\!',
        },

        BASIC   =>  {
            single      =>  q['],
        },
        PILOT   =>  {
            single      => '\/\/',
        },
        #BLUE   =>  {

Makefile.PL  view on Meta::CPAN

            print $fh <<HERE;
BEGIN { unshift \@INC, '$dir'; }

$u strict;
$u Test::More q[no_plan];

$u Acme::Comment 1.01 type => "$type", one_line => $test->{one_line}, own_line => $test->{own_line};


HERE
            my $start   = $Conf->{$type}->{start};
            my $end     = $Conf->{$type}->{end};
            my $single  = $Conf->{$type}->{single};


            START_END: {
            if( $start && $end ) {

                OWNLINE: {
                if ( !$test->{own_line} ) {

                    ### ALGOL has ; to end it's comments
                    ### so you can't use it with own_line
                    if( $type eq 'ALGOL' ) {
                        print $fh "SKIP:{skip(q[You cannot use 'own_line' with ALGOL],3)}";
                        last START_END;
                    }

                    print $fh <<HERE;
SKIP: {
    ### Test 1 ###
    my \$one = 1;

    $start \$one = 2;
    $end

    ### Check Test 1 ###
    is(\$one, 1, "$type => $what: Standard Multiline");



    ### Test 2 ###
    my \$two = 2;

    $start \$two = 3; $start \$two = 4;
    $end \$two = 5; $end

    ### Check Test 2 ###
    is(\$two, 2, "$type => $what: Nested Multiline");



    ### Test 3 ###
    eval {
        $start this should break
        $end
        1;
    };

    ### Check 3 ###
    ok(!\$@, "$type => $what: Broken Syntax Ignored Multiline");
}
HERE

                } }

                ONELINE: {
                if( $test->{one_line} ) {

                    print $fh <<HERE;
SKIP: {
    ### Test 4 ###
    my \$four = 4;

    $start \$four= 5; $end

    ### Check Test 1 ###
    is(\$four, 4, "$type => $what: Standard Multiline");



    ### Test 5 ###
    my \$five = 5;

    $start \$five = 7; $start \$five = 8; $end \$five = 9; $end

    ### Check Test 5 ###
    is(\$five, 5, "$type => $what: Nested Multiline");


    ### Test 6 ###
    eval {
        $start this should break $end
        1;
    };

    ### Check 6 ###
    ok(!\$@, "$type => $what: Broken Syntax Ignored Multiline");
}
HERE
                } }

                STANDARD: {
                print $fh <<HERE;
### Test 7 ###
my \$seven = 7;

$start
    \$seven = 8;
$end

### Check Test 7 ###
is(\$seven, 7, "$type => $what: Standard Multiline");



### Test 8 ###
my \$eight = 8;

$start
    \$eight = 9;
    $start
        \$eight = 10;
    $end
    \$eight = 11;
$end

### Check Test 8 ###
is(\$eight, 8, "$type => $what: Nested Multiline");

### Test 9 ###
eval {
    $start
        this should break
    $end
    1;
};

### Check 9 ###
ok(!\$@, "$type => $what: Broken Syntax Ignored Multiline");

HERE
                }

lib/Acme/Comment.pm  view on Meta::CPAN

}

my $TypeCount = 0;
my $Type = 'C';
my $Conf;

{   no warnings;
    $Conf = {
        C   => {
            own_line    => 1,
            start       => quotemeta '/*',
            end         => quotemeta '*/',
            one_line    => 0,
        },
        HTML    => {
            own_line    => 1,
            start       => quotemeta '<!--',
            end         => quotemeta '-->',
            one_line    => 0,
        },
        RUBY    => {
            own_line    => 1,
            start       => quotemeta '=begin',
            end         => quotemeta '=end',
            one_line    => 0,
            single      => '#',
        },
        JAVA    => {
            own_line    => 1,
            start       => quotemeta '/*',
            end         => quotemeta '*/',
            one_line    => 0,
            single      => quotemeta '//',
        },
        PASCAL  => {
            own_line    => 1,
            start       => quotemeta '(*',
            end         => quotemeta '*)',
            one_line    => 0,
        },

        ALGOL  => {
            own_line    => 1,
            start       => quotemeta "'comment'",
            end         => quotemeta ';',
            one_line    => 0,
        },

        HUGO    => {
            own_line    => 1,
            start       => quotemeta '!\\',
            end         => quotemeta '\!',
            one_line    => 0,
            single      => '!(?!\\\\)',
        },

        BASIC   =>  {
            single      =>  q['],
        },
        PILOT   =>  {
            single      => quotemeta '\/\/',

lib/Acme/Comment.pm  view on Meta::CPAN


            ### otherwise die with an error ###
            } else {
                die "Requested an unsupported type $args{type} for Acme::Comment\n";
            }

        ### otherwise, define a new type for the user ###
        } else {
            $Type = ++$TypeCount;

            unless( (defined $args{start} and defined $args{end}) or defined $args{single} ) {
                die "You need to specify both start and end tags OR a single line comment!\n";
            } else {
                if( defined $args{start} and defined $args{end} and $args{start} eq $args{end} ) {
                    die "Start and end tags must be different!\n";
                }

                $Conf->{$TypeCount}->{start}    = quotemeta($args{start})  if defined $args{start};
                $Conf->{$TypeCount}->{end}      = quotemeta($args{end})    if defined $args{end};
                $Conf->{$TypeCount}->{single}   = quotemeta($args{single}) if defined $args{single}
            }

            $Conf->{$TypeCount}->{own_line} = defined $args{own_line}
                                                ? $args{own_line}
                                                : 1;

            $Conf->{$TypeCount}->{one_line} = defined $args{one_line}
                                                ? $args{one_line}

lib/Acme/Comment.pm  view on Meta::CPAN

}

sub parse {

    #use Data::Dumper;
    #print scalar @_;
    #die Dumper \@_;

    my $str = shift;

    my $start   = $Conf->{$Type}->{start}     if $Conf->{$Type}->{start};
    my $end     = $Conf->{$Type}->{end}       if $Conf->{$Type}->{end};
    my $single  = $Conf->{$Type}->{single}    if $Conf->{$Type}->{single};

    my ($rdel,$ldel);
    my ($roneline, $loneline);

    if( $start && $end ) {
        ### having the comments on their own line is recommended
        ### to avoid ambiguity -kane
        $roneline = '\s*' . $end . '\s*$';
        $loneline = '^\s*' . $start . '\s*';

        if( $Conf->{$Type}->{own_line} ){
            $rdel = '^' . $roneline;
            $ldel = $loneline . '$';
        } else {
            $rdel = $roneline;
            $ldel = $loneline;
        }
    }

lib/Acme/Comment.pm  view on Meta::CPAN

=head1 DESCRIPTION

Acme::Comment allows multi-line comments which are filtered out.
Unlike the pseudo multi-line comment C<if (0) {}>, the code being
commented out need not be syntactically valid.

=head1 USE

Acme::Comment contains several different commenting styles.

Styles may be specified by the C<types> argument, or by C<start> and
C<end> and manipulated with C<own_line> and C<one_line>.

Styles may contain multi-line comments and single-line comments.
Perl, for example, has single-line comments in the form of C<#>.

C, on the other hand, has multi-line comments which begin with
C</*> and end with C<*/>.

With multi-line comments, leaving out a begin or an end comment
will cause an error.

Both types of comments may only be preceded on a line by whitespace.

=head2 own_line

By default, C<own_line> is true, which means that multi-line comments may not
be followed by any characters other than whitespace on the same line.
This is the safest option if you think your code may contain the
comment characters (perhaps in a regex).  If you disable it, other
characters are allowed on the line after the starting delimiter, but these
characters will be ignored.  The closing delimiter cannot be followed by
any other characters.

Thus, in the following example, C<$foo> would be set to 1.

    /* This is my real comment.
    */
    $foo = 1;

If you wish to change this option, you must specify either a C<type> or
C<start> and C<end>.

=head2 one_line

By default, this is set to false, which means that multi-line comments
may not end on the same line in which they begin.  Turning this on
allows the following syntax:

    /* comment */

If you wish to change this option, you must specify either a C<type> or
C<start> and C<end>.

=head2 C<start> and C<end>

The C<start> and C<end> arguments allow you to supply your own commenting
pattern instead of one of the ones available with C<type>.  It is not
valid to provide the same pattern for both C<start> and C<end>.

You cannot specify both C<type> and C<start> and C<end>, and C<start>
and C<end> must both be provided if you provide one of them.

=head2 types

The C<types> argument specifies what language style should be used.
Only one language style may be specified.

=over 4

=item * Ada

Single-line comments begin with C<'>.

=item * Advsys

Advsys single-line comments begin with C<;>.

=item * Alan

Single-line comments start with C<-->.

=item * Algol

Multi-line comments begin with C<'comment'> and end with C<;>.

NOTE: You should not use Algol with C<own_line> set to 0:
The source filter will take a C<;> to be an ending tag for your
comments, regardless of where it is.

=item * AWK

lib/Acme/Comment.pm  view on Meta::CPAN

=item * Clean

Clean multi-line style uses C</*> and C<*/>.  Single-line uses C<//>.

=item * E

Single-line comments use C<#>.

=item * Eiffel

Single-line comments start with C<-->.

=item * Elastic

Elastic multi-line style uses C</*> and C<*/>.  Single-line uses C<//>.

=item * Focal

Single-line comments start with C<comment>.

=item * Fortran

Single-line comments use C<!>.

=item * Guile

Guile multi-line style uses C</*> and C<*/>.  Single-line uses C<//>.

=item * Haskell

Single-line comments start with C<-->.

=item * HTML

HTML style has multi-line commenting in the form of C<E<lt>!--> and
C<--E<gt>>.

=item * Hugo

Multi-line comments begin with C<!\> and end with C<\!>.  Single-line
comments are not implemented due to their similarity with multi-line



( run in 0.402 second using v1.01-cache-2.11-cpan-0d8aa00de5b )