scgrep 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #!/usr/bin/perl
  2. # Daniel "Trizen" Șuteu
  3. # License: GPLv3
  4. # Date: 04 February 2013
  5. # Latest edit on: 16 July 2015
  6. # https://github.com/trizen
  7. # Perl source code extractor.
  8. use utf8;
  9. use 5.018;
  10. use strict;
  11. use warnings;
  12. use open IO => ':utf8', ':std';
  13. #use lib qw(../lib);
  14. use Perl::Tokenizer qw(perl_tokens);
  15. use List::Util qw(any);
  16. use Getopt::Std qw(getopts);
  17. use Term::ANSIColor qw(color);
  18. my %opts;
  19. getopts('hnlpcNb:a:t', \%opts);
  20. sub usage {
  21. my ($code) = @_;
  22. print <<"HELP";
  23. usage: $0 [options] [types] [files]
  24. options:
  25. -b [i] : before characters
  26. -a [i] : after characters
  27. -l : print the full line
  28. -c : highlight the token (with -l)
  29. -p : print the name and position
  30. -n : print non-matching tokens
  31. -t : print the token names only
  32. -N : don't print a newline after the token
  33. types:
  34. Types are regular expressions.
  35. Example: ^operator
  36. ^keyword
  37. ^heredoc
  38. ^comment
  39. ^format
  40. ^backtick
  41. usage example: $0 -l -c regex /perl/script.pl
  42. $0 -l -c -n -p /perl/script.pl
  43. uncomment and unpod a perl script:
  44. $0 -N -n '^(?:pod|comment)\$' script.pl > clean_script.pl
  45. HELP
  46. exit $code;
  47. }
  48. usage(0) if $opts{h};
  49. my @types;
  50. while (@ARGV and not -f $ARGV[0]) {
  51. push @types, map { qr{$_} } shift @ARGV;
  52. }
  53. my $code = (
  54. do { local $/; <> }
  55. // die "usage: $0 [file]\n"
  56. );
  57. my $reset_color = color('reset');
  58. my $color = color('bold red on_black');
  59. perl_tokens {
  60. my ($token, $from, $to) = @_;
  61. if ($opts{t}) {
  62. say $token;
  63. return;
  64. }
  65. my $matches = any { $token =~ $_ } @types;
  66. if ($opts{n} ? !$matches : $matches) {
  67. if ($opts{p}) {
  68. print "[$token] pos($from, $to): ";
  69. }
  70. if ($opts{l} and not $token eq 'vertical_space') {
  71. my $beg = rindex($code, "\n", $from) + 1;
  72. my $end = index($code, "\n", $to);
  73. my $line = substr($code, $beg, ($end - $beg));
  74. if ($opts{c}) {
  75. substr($line, ($from - $beg), 0, $color);
  76. substr($line, ($from - $beg) + ($to - $from) + length($color), 0, $reset_color);
  77. }
  78. print $line;
  79. }
  80. else {
  81. if ($opts{b}) {
  82. print substr($code, $from - $opts{b}, $opts{b});
  83. }
  84. print substr($code, $from, ($to - $from));
  85. if ($opts{a}) {
  86. print substr($code, $to, $opts{a});
  87. }
  88. }
  89. print "\n" unless $opts{N};
  90. }
  91. }
  92. $code;
  93. =encoding utf8
  94. =head1 NAME
  95. pfilter - a simple token extractor.
  96. =head1 SYNOPSIS
  97. pfilter [options] [types] < [script.pl]
  98. Options:
  99. -b [i] : before characters
  100. -a [i] : after characters
  101. -l : print the full line
  102. -c : highlight the token (with -l)
  103. -p : print the name and position
  104. -n : print non-matching tokens
  105. -t : print the token names only
  106. -N : don't print a newline after the token
  107. Types:
  108. Types are regular expressions.
  109. Example: ^operator
  110. ^keyword
  111. ^heredoc
  112. ^comment
  113. ^format
  114. ^backtick
  115. For more types, see: C<perldoc Perl::Tokenizer>
  116. Example:
  117. # uncomment and unpod a Perl script:
  118. pfilter -N -n '^(?:pod|comment)\z' script.pl > clean_script.pl
  119. =head1 DESCRIPTION
  120. pfilter extracts tokens from a Perl script that match a given regular expression.
  121. =head1 AUTHOR
  122. Daniel "Trizen" Șuteu, E<lt>trizen@protonmail.comE<gt>
  123. =head1 COPYRIGHT AND LICENSE
  124. Copyright (C) 2015
  125. This library is free software; you can redistribute it and/or modify
  126. it under the same terms as Perl itself, either Perl version 5.22.0 or,
  127. at your option, any later version of Perl 5 you may have available.
  128. =cut