]> git.ipfire.org Git - thirdparty/automake.git/commitdiff
Updated include directive and added test cases. experimental/gsoc/ast
authorVishal Gupta <vishalgupta7972@gmail.com>
Sat, 4 Aug 2018 12:26:37 +0000 (17:56 +0530)
committerVishal Gupta <vishalgupta7972@gmail.com>
Sat, 4 Aug 2018 12:26:37 +0000 (17:56 +0530)
After parsing the file given in the include directive, the tree generated
is printed to standard input using Data::Dumper and this is evaled in the
program having include directive. As a result the tree gets appended to
the parent AST.

lib/Automake/Parser/Lexer.pm
lib/Automake/Parser/Tree.pm
lib/Automake/Parser/input.txt [deleted file]
lib/Automake/Parser/parser.pl
lib/Automake/Parser/t/app/Makefile.am
lib/Automake/Parser/t/app/app/Makefile.am [moved from lib/Automake/Parser/t/app/app3/Makefile.am with 100% similarity]
lib/Automake/Parser/t/app1/Makefile.am
lib/Automake/Parser/t/app2/Makefile.am
lib/Automake/Parser/t/app3/Makefile.am [new file with mode: 0644]
lib/Automake/Parser/t/app3/app/Makefile.am [new file with mode: 0644]
lib/Automake/Parser/t/fragment.txt [new file with mode: 0644]

index 5a206769ee080893472b67f7842d1b5d1e320928..61255d0ad7b4173bc3ad9057cce458b5bdbb8544 100644 (file)
@@ -6,7 +6,7 @@ our @ISA = qw(Exporter);
 our @EXPORT = qw(lex);
 
 # Store end token.
-my @end_tok = ["end"];
+my @end_tok = [ "end" ];
 
 # lex(string,multiline)
 # Takes as input a string of line and multiline variable deciding whether 
@@ -84,6 +84,13 @@ sub lex($)
                        push @tokens, [ "newline" ] unless $multiline;
                        $_ = undef;
                }
+               elsif( $include )
+               {
+                       push @tokens, [ "value" , $1] if m/^\s+(.+)/;
+                       push @tokens, [ "newline" ];
+                       $include = 0;
+                       last;
+               }
                elsif( s/^##.*\n$//o )
                {
                }
@@ -104,6 +111,7 @@ sub lex($)
                elsif( s/^(PROGRAMS|LIBRARIES|LTLIBRARIES|LISP|PYTHON|JAVA|SCRIPTS|DATA|HEADERS|MASN|TEXINFOS|if|else|endif|include)//o)
                {
                        push @tokens, [$1];
+                       $include = 1 if ( $1 eq 'include' );                    
                }
                elsif( s/^([a-zA-Z0-9_]+)//o )
                {
index e6e9c9836261d2485b34c9aca6384351caf7f8b2..69153ff5b0631dee44d5f74aef19e9fb75d54d4a 100644 (file)
@@ -3,19 +3,24 @@ package Tree;
 use Exporter;
 
 our @ISA = qw(Exporter);
-our @EXPORT = qw(input stmts stmt automakerule makerule conditional ifblock 
-optionalelse optionalcond optionalrhs optionalcomments lhs rhs commentlist primaries 
+our @EXPORT = qw(basedir input stmts stmt automakerule makerule conditional ifblock 
+optionalelse optionalcond optionalrhs optionalcomments includerule lhs rhs commentlist primaries 
 optionlist traverse printgraph recursesubdirs);
 
+# Stores whether subdir directive is used or not. If used array
+# consist of subdirectories.
 my $isSubdir = 0 , @subdirnodes = ();
 
+# Stores the value recieved by Parser.pl
+our $basedir;
+
 # Grammar Rule : (1) input => stmts
 # Create a node having child as stmts.
 sub input($)
 {
        my ( $val ) = @_;
        my %node = ( name => input, childs => [ $val ] );
-       push @{$node{childs}}, subdirNode() if $#subdirnodes > -1;
+       push @{ $node{ childs } }, subdirNode() if $#subdirnodes > -1;
        return \%node;
 }
 
@@ -44,7 +49,7 @@ sub stmts($$;$)
        }
        else
        {
-               push @{$val1 -> { childs }}, $val2;
+               push @{ $val1 -> { childs } }, $val2;
                return $val1;
        }
 }
@@ -70,7 +75,7 @@ sub automakerule($$$$;$)
 {
        my ( $val1, $val2, $val3, $val4, $val5 ) = @_;
        my %node = (name => automakerule, childs => [ $val1 ]);
-       if($val2->[0] eq '=')
+       if($val2 -> [0] eq '=')
        {
                push @{ $node{ childs }}, $val3;
                push @{ $node{ childs }}, $val4 if $val4;
@@ -89,7 +94,7 @@ sub automakerule($$$$;$)
 sub makerule($$$)
 {
        my ( $val1, $val2, $val3 ) = @_;
-       my %node = (name => makerule, childs => [ $val1,$val3 ]);
+       my %node = ( name => makerule, childs => [ $val1,$val3 ]);
        return \%node;
 }
 
@@ -296,11 +301,19 @@ sub optionlist($$;$)
        }
 }
 
+# Grammar Rule : (1) includerule : include value
+# Create a node having the tree after parsing the included value.
+# String recieved as standard input is evaled to generate corresponding
+# tree.
 sub includerule($$)
 {
        my ( $val1, $val2 ) = @_;
-       print STDERR $val2;
-       my %node = (name => includerule, value => $val2);
+       my $VAL1;
+       my $file = $val2 -> [1];
+       my $data = `parser.pl -include $basedir/$file`;
+       my %node = %{ eval( $data ) };
+       $node{ name } = includerule;
+       $node{ file } = $val2;
        return \%node;
 }
 
@@ -312,7 +325,7 @@ sub printgraph($)
        print "graph graphname {\n";
        my ( $ref ) = @_;
        print "0 [label=\"Root\"];";
-       traverse( $ref, 0);
+       traverse( $ref, 0 );
        print "}\n";
 }
 
@@ -324,9 +337,9 @@ my $id = 0;
 # to Standard Output. Call all its child with Parent Id equal to current Node Id.
 sub traverse($$)
 {
-       my ( $ref,$parent ) = @_;
+       my ( $ref , $parent ) = @_;
        my %node = %$ref;
-       return if $node{empty};
+       return if $node{ empty };
        $id++;
        my $curr_id = $id;
        print "$parent--$id;\n";
@@ -355,12 +368,12 @@ sub traverse($$)
        }
 }
 
-# recursesubdirs(Basedir, Reference)
+# recursesubdirs( Tree Reference)
 # Recurse into sub directories to generate AST 
-sub recursesubdirs($$)
+sub recursesubdirs($)
 {
-       my ( $basedir , $ref) = @_;
-       my %node= %$ref;
+       my ( $ref ) = @_;
+       my %node = %$ref;
        if( scalar @{ $node{childs} } == 2)
        {
                my $subdirRef = $node{childs} -> [1];
diff --git a/lib/Automake/Parser/input.txt b/lib/Automake/Parser/input.txt
deleted file mode 100644 (file)
index 618da52..0000000
+++ /dev/null
@@ -1,3 +0,0 @@
-bin_PROGRAMS = server client
-server_SOURCES = server.c
-client_SOURCES = client.c
index b2384fab9cf5aae08359e8727f563f4013d8ca79..d81de04930055c9cfed1b1b47df80df02aaf1f88 100644 (file)
@@ -12,10 +12,20 @@ use Lexer;
 use Tree;
 use ParserTable;
 use File::Basename;
+use Data::Dumper;
+
+# Checks if the parser is called on a file included using include
+# directive in parent file.
+my $isinclude = 0;
+if( $ARGV[0] eq '-include' )
+{      
+       $isinclude = 1;
+       shift @ARGV;
+}
 
 # Stores the relative path of the Makefile.am file with respect to 
 # current working directory
-my $basedir = File::Basename::dirname($ARGV[0]);
+$Tree::basedir = File::Basename::dirname($ARGV[0]);
 
 # To enable debug mode, use 1 . Prints the parser stack at each 
 # iteration
@@ -34,9 +44,17 @@ while ( @stack )
 {
        if($stack[-1] == $ParserTable::accept)
        {
-               print STDERR "Complete\n";
-               printgraph( $stack[-4] );
-               recursesubdirs( $basedir, $stack[-4] );
+               if($isinclude)
+               {
+                       # Dump the tree to standard output.
+                       print Dumper( $stack[-4] );
+               }
+               else
+               {
+                       print STDERR "Complete\n";
+                       printgraph( $stack[-4] );
+                       recursesubdirs( $stack[-4] );
+               }
                last;
        }
        while( !@tokens )
index 79468923b09b19056e9b5fd4998de3329b04a186..c6d6bd0976848b0e0c843accabb0af7c0bc60d44 100644 (file)
@@ -1,2 +1,2 @@
 bin_PROGRAMS=apple
-SUBDIRS = app3
+SUBDIRS = app
index 1566cc4be92defe1c270e3a9aac6994282c88941..3d1c248b04b3c99b4b3401f22f695db23f365192 100644 (file)
@@ -1 +1,2 @@
-bin_PROGRAMS=apple
\ No newline at end of file
+bin_PROGRAMS=apple
+apple_SOURCES=apple.c
index 1566cc4be92defe1c270e3a9aac6994282c88941..3d1c248b04b3c99b4b3401f22f695db23f365192 100644 (file)
@@ -1 +1,2 @@
-bin_PROGRAMS=apple
\ No newline at end of file
+bin_PROGRAMS=apple
+apple_SOURCES=apple.c
diff --git a/lib/Automake/Parser/t/app3/Makefile.am b/lib/Automake/Parser/t/app3/Makefile.am
new file mode 100644 (file)
index 0000000..694ae2b
--- /dev/null
@@ -0,0 +1 @@
+include app/Makefile.am
diff --git a/lib/Automake/Parser/t/app3/app/Makefile.am b/lib/Automake/Parser/t/app3/app/Makefile.am
new file mode 100644 (file)
index 0000000..faa681b
--- /dev/null
@@ -0,0 +1,2 @@
+bin_PROGRAMS = apple
+apple_SOURCES = apple.c
diff --git a/lib/Automake/Parser/t/fragment.txt b/lib/Automake/Parser/t/fragment.txt
new file mode 100644 (file)
index 0000000..43285fa
--- /dev/null
@@ -0,0 +1,3 @@
+include app1/Makefile.am
+include app2/Makefile.am
+include app3/Makefile.am