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. sub new($) {
  21. my $proto = shift;
  22. my $class = ref($proto) || $proto;
  23. my $self = {};
  24. bless ($self, $class);
  25. my $state = \%{$self->{STATE}};
  26. my $stack = \@{$self->{STACK}};
  27. my $include_found = \${$self->{INCLUDE_FOUND}};
  28. my $conditional_found = \${$self->{CONDITIONAL_FOUND}};
  29. $$include_found = shift;
  30. $$conditional_found = shift;
  31. return $self;
  32. }
  33. sub include($$) {
  34. my $self = shift;
  35. my $include_found = \${$self->{INCLUDE_FOUND}};
  36. my $argument = shift;
  37. &$$include_found($argument);
  38. }
  39. sub define($$) {
  40. my $self = shift;
  41. my $state = \%{$self->{STATE}};
  42. my $conditional_found = \${$self->{CONDITIONAL_FOUND}};
  43. my $name = shift;
  44. $$state{$name} = "def";
  45. &$$conditional_found($name);
  46. }
  47. sub undefine($$) {
  48. my $self = shift;
  49. my $state = \%{$self->{STATE}};
  50. my $conditional_found = \${$self->{CONDITIONAL_FOUND}};
  51. my $name = shift;
  52. $$state{$name} = "undef";
  53. &$$conditional_found($name);
  54. }
  55. sub begin_if($$$) {
  56. my $self = shift;
  57. my $state = \%{$self->{STATE}};
  58. my $stack = \@{$self->{STACK}};
  59. my $directive = shift;
  60. local $_ = shift;
  61. while(!/^$/) {
  62. if(/^0\s*\&\&/s) {
  63. $_ = "0";
  64. } elsif(/^1\s*\|\|/s) {
  65. $_ = "1";
  66. }
  67. if (/^(!\s*)?defined\s*\(\s*(\w+)\s*\)\s*(?:(\&\&|\|\|)\s*)?/s ||
  68. /^(!\s*)?defined\s*(\w+)\s*(?:(\&\&|\|\|)\s*)?/s)
  69. {
  70. $_ = $';
  71. my $sign = $1;
  72. my $var = $2;
  73. if (defined($sign) && $sign eq "!") {
  74. $self->undefine($var);
  75. push @$stack, $var;
  76. } else {
  77. $self->define($var);
  78. push @$stack, $var;
  79. }
  80. } elsif (/^(!\s*)?(\w+)\s*(?:(<|<=|==|!=|>=|>|\+|\-|\*\/)\s*(\w+)\s*)?(?:(\&\&|\|\|)\s*)?/s) {
  81. $_ = $';
  82. my $sign = $1;
  83. my $var = $2;
  84. if (defined($sign) && $sign eq "!") {
  85. $self->undefine($var);
  86. push @$stack, $var;
  87. } else {
  88. $self->define($var);
  89. push @$stack, $var;
  90. }
  91. } elsif(/^(!\s*)?\(/s) {
  92. $_ = "";
  93. } else {
  94. print "*** Can't parse '#$directive $_' ***\n";
  95. $_ = "";
  96. }
  97. }
  98. }
  99. sub else_if($$) {
  100. my $self = shift;
  101. my $state = \%{$self->{STATE}};
  102. my $stack = \@{$self->{STACK}};
  103. my $argument = shift;
  104. $self->end_if;
  105. if(defined($argument)) {
  106. $self->begin_if("elif", $argument);
  107. }
  108. }
  109. sub end_if($) {
  110. my $self = shift;
  111. my $state = \%{$self->{STATE}};
  112. my $stack = \@{$self->{STACK}};
  113. my $macro = pop @$stack;
  114. delete $$state{$macro} if defined($macro);
  115. }
  116. sub directive($$$) {
  117. my $self = shift;
  118. my $state = \%{$self->{STATE}};
  119. my $stack = \@{$self->{STACK}};
  120. my $directive = shift;
  121. my $argument = shift;
  122. local $_ = $directive;
  123. if(/^if$/) {
  124. $self->begin_if("if",$argument);
  125. } elsif(/^ifdef$/) {
  126. $self->begin_if("if", "defined($argument)");
  127. } elsif(/^ifndef$/) {
  128. $self->begin_if("if", "!defined($argument)");
  129. push @$stack, $argument;
  130. } elsif(/^elif$/) {
  131. $self->else_if($argument);
  132. } elsif(/^else$/) {
  133. $self->else_if;
  134. } elsif(/^endif$/) {
  135. $self->end_if;
  136. } elsif(/^include/) {
  137. $self->include($argument);
  138. }
  139. }
  140. sub is_def($$) {
  141. my $self = shift;
  142. my $state = \%{$self->{STATE}};
  143. my $name = shift;
  144. my $status = $$state{$name};
  145. return defined($status) && $status eq "def";
  146. }
  147. sub is_undef($$) {
  148. my $self = shift;
  149. my $state = \%{$self->{STATE}};
  150. my $name = shift;
  151. my $status = $$state{$name};
  152. return defined($status) && $status eq "undef";
  153. }
  154. sub is_unknown($$) {
  155. my $self = shift;
  156. my $state = \%{$self->{STATE}};
  157. my $name = shift;
  158. my $status = $$state{$name};
  159. return !defined($status);
  160. }
  161. 1;