preprocessor.pm 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. #
  2. # Copyright 1999, 2000, 2001 Patrik Stridvall
  3. #
  4. # This library is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU Lesser General Public
  6. # License as published by the Free Software Foundation; either
  7. # version 2.1 of the License, or (at your option) any later version.
  8. #
  9. # This library is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. # Lesser General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU Lesser General Public
  15. # License along with this library; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  17. #
  18. package preprocessor;
  19. use strict;
  20. use warnings 'all';
  21. sub new($) {
  22. my $proto = shift;
  23. my $class = ref($proto) || $proto;
  24. my $self = {};
  25. bless ($self, $class);
  26. my $state = \%{$self->{STATE}};
  27. my $stack = \@{$self->{STACK}};
  28. my $include_found = \${$self->{INCLUDE_FOUND}};
  29. my $conditional_found = \${$self->{CONDITIONAL_FOUND}};
  30. $$include_found = shift;
  31. $$conditional_found = shift;
  32. return $self;
  33. }
  34. sub include($$) {
  35. my $self = shift;
  36. my $include_found = \${$self->{INCLUDE_FOUND}};
  37. my $argument = shift;
  38. &$$include_found($argument);
  39. }
  40. sub define($$) {
  41. my $self = shift;
  42. my $state = \%{$self->{STATE}};
  43. my $conditional_found = \${$self->{CONDITIONAL_FOUND}};
  44. my $name = shift;
  45. $$state{$name} = "def";
  46. &$$conditional_found($name);
  47. }
  48. sub undefine($$) {
  49. my $self = shift;
  50. my $state = \%{$self->{STATE}};
  51. my $conditional_found = \${$self->{CONDITIONAL_FOUND}};
  52. my $name = shift;
  53. $$state{$name} = "undef";
  54. &$$conditional_found($name);
  55. }
  56. sub begin_if($$$) {
  57. my $self = shift;
  58. my $state = \%{$self->{STATE}};
  59. my $stack = \@{$self->{STACK}};
  60. my $directive = shift;
  61. local $_ = shift;
  62. while(!/^$/) {
  63. if(/^0\s*\&\&/s) {
  64. $_ = "0";
  65. } elsif(/^1\s*\|\|/s) {
  66. $_ = "1";
  67. }
  68. if (/^(!\s*)?defined\s*\(\s*(\w+)\s*\)\s*(?:(\&\&|\|\|)\s*)?/s ||
  69. /^(!\s*)?defined\s*(\w+)\s*(?:(\&\&|\|\|)\s*)?/s)
  70. {
  71. $_ = $';
  72. my $sign = $1;
  73. my $var = $2;
  74. if (defined($sign) && $sign eq "!") {
  75. $self->undefine($var);
  76. push @$stack, $var;
  77. } else {
  78. $self->define($var);
  79. push @$stack, $var;
  80. }
  81. } elsif (/^(!\s*)?(\w+)\s*(?:(<|<=|==|!=|>=|>|\+|\-|\*\/)\s*(\w+)\s*)?(?:(\&\&|\|\|)\s*)?/s) {
  82. $_ = $';
  83. my $sign = $1;
  84. my $var = $2;
  85. if (defined($sign) && $sign eq "!") {
  86. $self->undefine($var);
  87. push @$stack, $var;
  88. } else {
  89. $self->define($var);
  90. push @$stack, $var;
  91. }
  92. } elsif(/^(!\s*)?\(/s) {
  93. $_ = "";
  94. } else {
  95. print "*** Can't parse '#$directive $_' ***\n";
  96. $_ = "";
  97. }
  98. }
  99. }
  100. sub else_if($$) {
  101. my $self = shift;
  102. my $state = \%{$self->{STATE}};
  103. my $stack = \@{$self->{STACK}};
  104. my $argument = shift;
  105. $self->end_if;
  106. if(defined($argument)) {
  107. $self->begin_if("elif", $argument);
  108. }
  109. }
  110. sub end_if($) {
  111. my $self = shift;
  112. my $state = \%{$self->{STATE}};
  113. my $stack = \@{$self->{STACK}};
  114. my $macro = pop @$stack;
  115. delete $$state{$macro} if defined($macro);
  116. }
  117. sub directive($$$) {
  118. my $self = shift;
  119. my $state = \%{$self->{STATE}};
  120. my $stack = \@{$self->{STACK}};
  121. my $directive = shift;
  122. my $argument = shift;
  123. local $_ = $directive;
  124. if(/^if$/) {
  125. $self->begin_if("if",$argument);
  126. } elsif(/^ifdef$/) {
  127. $self->begin_if("if", "defined($argument)");
  128. } elsif(/^ifndef$/) {
  129. $self->begin_if("if", "!defined($argument)");
  130. push @$stack, $argument;
  131. } elsif(/^elif$/) {
  132. $self->else_if($argument);
  133. } elsif(/^else$/) {
  134. $self->else_if;
  135. } elsif(/^endif$/) {
  136. $self->end_if;
  137. } elsif(/^include/) {
  138. $self->include($argument);
  139. }
  140. }
  141. sub is_def($$) {
  142. my $self = shift;
  143. my $state = \%{$self->{STATE}};
  144. my $name = shift;
  145. my $status = $$state{$name};
  146. return defined($status) && $status eq "def";
  147. }
  148. sub is_undef($$) {
  149. my $self = shift;
  150. my $state = \%{$self->{STATE}};
  151. my $name = shift;
  152. my $status = $$state{$name};
  153. return defined($status) && $status eq "undef";
  154. }
  155. sub is_unknown($$) {
  156. my $self = shift;
  157. my $state = \%{$self->{STATE}};
  158. my $name = shift;
  159. my $status = $$state{$name};
  160. return !defined($status);
  161. }
  162. 1;