Comment
smokemachine
created: 2005-12-28 13:13:43
/usr/bin/comenta
#!/usr/bin/perl
our %parametros;
while($para=shift) {
		$parametros{$1}=$2 if $para =~ /^--(.+)=(.+)$/;
		$parametros{arquivo}=$para unless $para =~ /^--(.+)=(.+)$/;
}	
descobre_linguagem($arquivo) unless our $linguagem=$parametros{linguagem};
our $arquivo=$parametros{arquivo};
our $nivel=0 unless $nivel=$parametros{nivel};
comenta_codigo();

sub comenta_codigo() {
	open(ARQ, $arquivo) or die "Arquivo '$arquivo' não encontrado";
	for(){
		$linhas++;
		$teste=$_;
		chomp($teste);
		unless($teste =~ /^\s*#/) {
			open(RC, "/etc/comentarios") or die "Arquivo de configuração '/etc/comentarios' não encontrado";
			for(){
				$_ =~ //;
				$eni=$1;
				if($_ =~ /<$linguagem>/i and $eni >= $nivel) {
					$_ =~ /(.+)<\/expr>/;
					$expre=$1;
					chomp($expre);
					$_ =~ /(.+)<\/coment>/;
					$comen=$1;
					chomp($comen);
					if($teste =~ /$expre/) {
						@vetor=split(/\[|\]/, $comen);
						$i=0;
						while($vetor[$i]){
							print $vetor[$i].${$vetor[$i+1]};
							$i+=2;
						}
						print "\n";
						$comentarios++;
					}
				}
			}
		}
		close RC;
		print $teste."\n";
			open(RC, "/etc/comentarios") or die "Arquivo de configuração '/etc/comentarios' não encontrado";
			for(){
				if($_ =~ /<$linguagem>/i and $_ =~ //i) {
					$_ =~ /(.*)<\/expr>/;
					$expre=$1;
					chomp($expre);
					$_ =~ /(.*)<\/coment>/;
					$comen=$1;
					chomp($comen);
					if($teste =~ /$expre/) {
						@vetor=split(/\[|\]/, $comen);
						$i=0;
						while($vetor[$i]){
							print $vetor[$i].${$vetor[$i+1]};
							$i+=2;
						}
						print "\n";
						$comentarios++;
					}
				}
			}
		close RC;
	}
	close ARQ;
}

sub descobre_linguagem() {
	($arq)=@_;
	$arq=~/.*\.([a-z]{1,2})/;
	$ext=$1;
	open(LIN, "/etc/comenta.conf") or die "Arquivo de configuração '/etc/comenta.conf' não encontrado";
	for() {
		$a=$_;
		chomp($a);
		our $linguagem=$1 if $a =~ /^(.*)=(\w{1,2},)*$ext,?(\w{1,2},?)*$/;
		return() if $a =~ /^(.*)=(\w{1,2},)*$ext,?(\w{1,2},?)*$/;
		$linguagem="" unless $a =~ /^(.*)=(\w{1,2},)*$ext,?(\w{1,2},?)*$/;
	}
	close LIN;
}


/etc/comenta.conf
Perl=pl,pm
C=c,h,o
Bash=sh
/etc/comentarios
	# Caso a formula '[1]' seja verdadeira, executa os próximos comandos if\((.*)\)\ *\{
 #\!\/usr\/bin\/perl
	#######################################################  #\!\/usr\/bin\/perl
	#        Código comentado pelo script 'comenta'       #  #!/usr/bin/perl
	#        Feito por Fernando Corrêa de Oliveira        #  #!/usr/bin/perl
	#######################################################  #!/usr/bin/perl
 #!/usr/bin/perl
# Caso a formula '[1]' seja verdadeira, executa os próximos comandos ^\s*if\s?\((.*)\)\s*\{?
# Caso a formula '[2]' seja verdadeira, executa o comando '[1]' ^\s*(.*)\ if\ ?\(?(.*)\)?\s?;
# A não ser que a formula '[1]' seja verdadeira, executa os próximos comandos ^\s*unless\ ?\(?(.*)\)?\ *\{
# A não ser que a formula '[2]' seja verdadeira, executa o comando '[1]' ^\s*(.*)\ unless\ ?\(?(.*)\)?\ *;
# Enquanto a expressão '[1]' for verdadeira... while\s?\((.*)\)\s?\{?
# Para a expressão '[1]' for\s?\((.*)\)\s?\{
# '[1]' => o mesmo que: '[2] = [2] + [3]' ((\$\w)+\+=(\d+))
# Abre o arquivo '[2]' com o titulo '[1]' open\((.*)\ ?\,\ ?(.*)\)
# Retira o '\n' do final da variavel [1] chomp\((\$.*)\)
# Fecha o arquivo com o titulo '[1]' close\(?\ ?(.*)\)?\ ?;
	# Usuario [1] com UID [3] e home [6] (.*):(x?):(.*):(.*):(.*):(.*):(.*)
# Para a cada um dos argumentos de '[1]' faça foreach\ *\((.*)\)\ *{
# Para [1] sendo igual a cada um dos argumentos de '[2]' faça foreach\ *($.*)\ \((.*)\)\ ?{
# '[1]' => de [2] até [3] (([0-9]+)\ ?\.\.\ ?([0-9]+))
# '[1]' recebe o parametro passado na chamada da função \((\$.*)\)=\@_
# [1] => '[2]' recebe o 1º parametro passado na chamada do programa excluindo-o ((\$.*)=shift)
# Declaração da variavel '[1]' local my\s*(\$\w*);
# Declaração da variavel '[1]' global our\s*(\$\w*);
# [1] => Declaração da variavel '[2]' local contendo '[3]' (my\s*(\$\w+)=(.*));
# [1] => Declaração da variavel '[2]' global contendo '[3]' (our\s*(\$\w+)=(.*));
# Declaração do hash '[1]' local my\s*(\%\w+);
# Declaração do hash '[1]' global our\s*(\%\w+);
# [1] => Valor da posição '[3]' do hash %[2] (\$(\w+)\{(.+)\})
# return() => Sai da função return\ ?\(?\ ?\)?
# return([1]) => Sai da função com '[1]' como valor de retorno return\ ?\(?\ (.*)\ ?\)?
# Como a função já diz: Usa o módulo '[1]' ^use\s+(.+);
	############################### Função '[1]' ####################### ^\s*sub (\w*\(?\)?)\ *\{?$
# [1] => Testa a expressão regular '[3]' na variavel '[2]' \s?((\$\w+)\s?=~\s?(\/.+\/))
# [1] => Reconhece qq tipo de espaço, como branco, tabulação ou quebra de linha \s?\$\w+\s?=~\s?\/.*(\\s).*\/
# [1] => Reconhece qq letra \s?\$\w+\s?=~\s?\/.*(\\w).*\/
# [1] => Reconhece qq palavra \s?\$\w+\s?=~\s?\/.*(\\W).*\/
# [1] => Reconhece qq numero \s?\$\w+\s?=~\s?\/.*(\\d).*\/
# [1] => Reconhece qq coisa entre '[2]' e '[3]' \s?\$\w+\s?=~\s?\/.*(\[(\w|\d)\-(\w|\d)\]).*\/
# [1] => A expressão '[2]' deve aparecer entre [3] e [4] vezes \s?\$\w+\s?=~\s?\/.*(([\[|\(].+[\]|\)])\{(\d),(\d)\}).*\/
# [1] => A expressão '[2]' deve aparecer entre [3] e [4] vezes \s?\$\w+\s?=~\s?\/.*((\\.)\{(\d),(\d)\}).*\/
Re: Coment
created: 2005-12-29 12:03:37
Comment is spelled with two m's.
Re^2: Coment
created: 2005-12-30 06:48:32
Perhaps it's just short for comentário which does only have one m.
Re: Comment
mk.
created: 2006-01-06 08:39:04
as smokemachine had asked, here's the translated code (so non-portuguese speakers can understand what's being printed as well)
this code is actually for beginners who aim to understand better what's being done on each part of the code. also, soon, it'll also be available for c and shell programs (who knows, someday, for pascal too... hehe).


/usr/bin/comenta
#!/usr/bin/perl
our %parameters;
while($para=shift) {
		$parameters{$1}=$2 if $para =~ /^--(.+)=(.+)$/;
		$parameters{file}=$para unless $para =~ /^--(.+)=(.+)$/;
}	
find_language($file) unless our $language=$parameters{language};
our $file=$parameters{file};
our $level=0 unless $level=$parameters{level};
comment_code();

sub comment_code() {
	open(ARQ, $file) or die "File '$file' not found";
	for(){
		$lines++;
		$test=$_;
		chomp($test);
		unless($test =~ /^\s*#/) {
			open(RC, "/etc/comentarios") or die "Configuration file '/etc/comments' not found";
			for(){
				$_ =~ //;
				$eni=$1;
				if($_ =~ /<$language>/i and $eni >= $level) {
					$_ =~ /(.+)<\/expr>/;
					$expre=$1;
					chomp($expre);
					$_ =~ /(.+)<\/coment>/;
					$comen=$1;
					chomp($comen);
					if($test =~ /$expre/) {
						@vector=split(/\[|\]/, $comen);
						$i=0;
						while($vector[$i]){
							print $vector[$i].${$vector[$i+1]};
							$i+=2;
						}
						print "\n";
						$comments++;
					}
				}
			}
		}
		close RC;
		print $test."\n";
			open(RC, "/etc/comentarios") or die "Configuration file '/etc/comments' not found";
			for(){
				if($_ =~ /<$language>/i and $_ =~ //i) {
					$_ =~ /(.*)<\/expr>/;
					$expre=$1;
					chomp($expre);
					$_ =~ /(.*)<\/coment>/;
					$comen=$1;
					chomp($comen);
					if($test =~ /$expre/) {
						@vector=split(/\[|\]/, $comen);
						$i=0;
						while($vector[$i]){
							print $vector[$i].${$vector[$i+1]};
							$i+=2;
						}
						print "\n";
						$comments++;
					}
				}
			}
		close RC;
	}
	close ARQ;
}

sub find_language() {
	($arq)=@_;
	$arq=~/.*\.([a-z]{1,2})/;
	$ext=$1;
	open(LIN, "/etc/comenta.conf") or die "Configuration file '/etc/comenta.conf' not found";
	for() {
		$a=$_;
		chomp($a);
		our $language=$1 if $a =~ /^(.*)=(\w{1,2},)*$ext,?(\w{1,2},?)*$/;
		return() if $a =~ /^(.*)=(\w{1,2},)*$ext,?(\w{1,2},?)*$/;
		$language="" unless $a =~ /^(.*)=(\w{1,2},)*$ext,?(\w{1,2},?)*$/;
	}
	close LIN;
}

/etc/comentarios
	# If formula '[1]' is true, it executes the next commands if\((.*)\)\ *\{
 #\!\/usr\/bin\/perl
	#######################################################  #\!\/usr\/bin\/perl
	#        Code commented by the script 'comenta'       #  #!/usr/bin/perl
	#            by Fernando Correa de Oliveira           #  #!/usr/bin/perl
	#######################################################  #!/usr/bin/perl
 #!/usr/bin/perl
# If formula '[1]' is true, it executes the next commands ^\s*if\s?\((.*)\)\s*\{?
# If formula '[2]' is true, it executes command '[1]' ^\s*(.*)\ if\ ?\(?(.*)\)?\s?;
# Unless formula '[1]' is true, it executes the next commands ^\s*unless\ ?\(?(.*)\)?\ *\{
# Unless formula '[2]' is true, it executes command '[1]' ^\s*(.*)\ unless\ ?\(?(.*)\)?\ *;
# While expression '[1]' is true... while\s?\((.*)\)\s?\{?
# For expression '[1]' for\s?\((.*)\)\s?\{
# '[1]' => the same as: '[2] = [2] + [3]' ((\$\w)+\+=(\d+))
# Opens file '[2]' with the handle '[1]' open\((.*)\ ?\,\ ?(.*)\)
# Removes '\n' from the end of variable [1] chomp\((\$.*)\)
# Closes file with the handle '[1]' close\(?\ ?(.*)\)?\ ?;
	# User [1] with UID [3] and home [6] (.*):(x?):(.*):(.*):(.*):(.*):(.*)
# For each of the arguments of '[1]' do foreach\ *\((.*)\)\ *{
# For [1] being the same as each one of the arguments in '[2]' do foreach\ *($.*)\ \((.*)\)\ ?{
# '[1]' => from [2] to [3] (([0-9]+)\ ?\.\.\ ?([0-9]+))
# '[1]' receives parameter passed on function call \((\$.*)\)=\@_
# [1] => '[2]' receives the first parameter passed on program call, deleting it. ((\$.*)=shift)
# Declaration of local variable '[1]' my\s*(\$\w*);
# Declaration of global variable '[1]' our\s*(\$\w*);
# [1] => Declaration of local variable '[2]' containing '[3]' (my\s*(\$\w+)=(.*));
# [1] => Declaration of global variable '[2]' containing '[3]' (our\s*(\$\w+)=(.*));
# Declaration of local hash '[1]'  my\s*(\%\w+);
# Declaration of global hash '[1]' our\s*(\%\w+);
# [1] => Value of position '[3]' in hash %[2] (\$(\w+)\{(.+)\})
# return() => Leaves the function return\ ?\(?\ ?\)?
# return([1]) => Leaves the function with '[1]' as return value return\ ?\(?\ (.*)\ ?\)?
# As the function already says: Uses module '[1]' ^use\s+(.+);
	############################### Function '[1]' ####################### ^\s*sub (\w*\(?\)?)\ *\{?$
# [1] => Tests regular expression '[3]' in variable '[2]' \s?((\$\w+)\s?=~\s?(\/.+\/))
# [1] => Recognises any type of space, as whitespace, tab or line break \s?\$\w+\s?=~\s?\/.*(\\s).*\/
# [1] => Recognises any letter \s?\$\w+\s?=~\s?\/.*(\\w).*\/
# [1] => Recognises any word \s?\$\w+\s?=~\s?\/.*(\\W).*\/
# [1] => Recognises any number \s?\$\w+\s?=~\s?\/.*(\\d).*\/
# [1] => Recognises anything between '[2]' and '[3]' \s?\$\w+\s?=~\s?\/.*(\[(\w|\d)\-(\w|\d)\]).*\/
# [1] => The expression '[2]' must appear between [3] and [4] times \s?\$\w+\s?=~\s?\/.*(([\[|\(].+[\]|\)])\{(\d),(\d)\}).*\/
# [1] => The expression '[2]' must appear between [3] and [4] times \s?\$\w+\s?=~\s?\/.*((\\.)\{(\d),(\d)\}).*\/
(/etc/comenta.conf remains the same)
=)


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
grrr.... argh!

perlmonks.org content © perlmonks.org and mk., smokemachine, TedPride, ysth

prlmnks.org © 2006 edmund von der burg (eccles & toad)

v 0.03