summaryrefslogtreecommitdiff
path: root/lib/Automake/Parser/Lexer.pm
blob: 61255d0ad7b4173bc3ad9057cce458b5bdbb8544 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package Lexer;

use Exporter;

our @ISA = qw(Exporter);
our @EXPORT = qw(lex);

# Store end token.
my @end_tok = [ "end" ];

# lex(string,multiline)
# Takes as input a string of line and multiline variable deciding whether 
# current line is related to the previous line. Divides it into tokens as 
# specified by Regex and outputs an array of Tokens. Every Token is an 
# array having two values: token name and its value. If its an operator, 
# it has only one value.
sub lex($)
{
	my ( $multiline ) = @_;
	my @tokens;
	my $rhs = 0;
    $_ = scalar <>;
	
	# Send an end token when EOF is reached.
	return ( \@end_tok, $multiline ) unless $_;
	
	while( $_ )
	{
		if( $multiline )
		{
			if( $multiline eq 'comment' )
			{
				die 'comment following trailing backslash' if m/^#/o;
				die 'blank line following trailing backslash' if m/^\s*$/;
				chomp;
				$multiline = undef unless s/\\//o;
				push @tokens, [ "comment" , $_ ];
				push @tokens, [ "newline" ] unless $multiline;
				$_ = undef;
			}
			else
			{
				if( m/^##/ )
				{
					$_ = undef;
					last;
				}
				elsif( m/^#/ )
				{
					die 'comment following trailing backslash';
				}
				else
				{
					$multiline = undef;
					$rhs = 1;
				}
			}
		}
		elsif( $rhs  )
		{
		    my @vals = split;
			my $comment;
			foreach my $val ( @vals )
			{
				if( $val =~ m/^#(.*)/ )
				{
					$multiline = 'comment' if $vals[ -1 ] eq '\\';
					$comment .= " ".$1;
				}
				elsif( $val =~ m/\\/ )
				{
					$multiline = 'rhsval' unless$multiline;
				}
				elsif( $comment )
				{
					$comment .= " ".$val;
				}
				else
				{
					push @tokens, [ "rhsval" , $val];
				}
			}
			push @tokens, [ "comment" , $comment] if $comment;
			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 )
		{
		}
		elsif( s/^#(.*)\n$//o )
		{
			my $val = $1;
			if( $val =~ m/(.*?)\\/o )
			{
				push @tokens, [ "comment" , substr( $1 , 0 , -1 )];
				$multiline = 'comment';
			}
			else
			{
				push @tokens, [ "comment" , $1];
				push @tokens, [ "newline" ];
			}
		}
		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 )
		{
			push @tokens, ["value",$1];
		}
		elsif( s/^(\+=)//o )
		{
			push @tokens,['+'];
			push @tokens,['='];
			$rhs = 1;
		}
		elsif( s/^(=)//o )
		{
			push @tokens, [$1];
			$rhs = 1;
		}
		elsif( s/^(:|_|!)//o )
		{
			push @tokens, [$1];
		}
		elsif( s/^\n//o )
		{
			push @tokens, ["newline"] if $#tokens > -1;
			$multiline = undef;
		}
		elsif( s/^(\r|\s+)//o )
		{
		}
		else
		{
			die "Incorrect input $_";
		}
	}
	
	# Returns undef when no tokens.
	return ( undef, $multiline ) if !@tokens;
	
	return ( \@tokens , $multiline );
}