# Here are some PERL regular expressions to aid preprocessor-phase # parsing of C source code. # ** QUALITY IS PRE-ALPHA, AS-IS, DEVELOPMENT CODE **. # Comments welcome. Perl5 or later required. # # Version 1999.Mar.23 # http://www.vendian.org/mncharity/ccode/ # # 1999.Mar.23 # This is from a C code understanding project of mine, at the moment # on indefinite hold. Originally intended to evolve into a real package, # I make it available now, as is, rather than waiting indefinitely for # further maturation. Just in case someone else finds it of use. # # Please let me know if you do find it useful. # I've several other fragments lying about which might be added. # # Mitchell Charity # Distributable/modifiable under the same terms as perl itself. package C_Regexps; # phase linewraps comments # p0 yes yes # p1 no yes s/$linewrap//g # p2 no no s/$comment/ /g; (not really!) # # no_foo_start does not treat positions in linewrap as part of any # following foo. $nevermatch = qr/(?=A)(?!A)/; $linewrap_p2 = $nevermatch; $linewrap_p1 = $nevermatch; $linewrap_p0 = qr/\\\n/; my $linewrap = $linewrap_p0; # beware, not idempotent $adjacency_p2 = qr//; $adjacency_p1 = qr//; $adjacency_p0 = qr/|$linewrap_p0/; my $o = $adjacency_p0; $comment2_p2 = $nevermatch; $comment2_p1 = qr!\/\/(?>.*)!; $comment2_p0 = qr!\/$o\/(?>(?:$linewrap_p0|.)*)!; $comment1_p2 = $nevermatch; $comment1_p1 = qr!\/\*(?>(?:[^*]+|\*[^\/])*)\*\/!; $comment1_p0 = qr!\/$o\*(?>(?:$linewrap_p0|[^*]|\*$o[^\/])*)\*$o\/!; $comment_p2 = qr/$comment1_p2|$comment2_p2/; $comment_p1 = qr/$comment1_p1|$comment2_p1/; $comment_p0 = qr/$comment1_p0|$comment2_p0/; my $comment = $comment_p0; $not_comment_start_p2 = qr//; $not_comment_start_p1 = qr/(?![^\/]|\/[^\/\*])/; $not_comment_start_p2 = qr/(?![^\/]|\/$o[^\/\*])/; $dot_p2 = qr/./; $dot_p1 = qr/$comment_p1|(?!$comment_p1)./; $dot_p0 = qr/(?>$linewrap*)(?:$comment_p0|(?!$comment_p0).)/; $s_p2 = qr/\s/; $s_p1 = qr/\s|$comment_p1/; $s_p0 = qr/(?>$linewrap*)(?:\s|$comment_p0)/; $sh_p2 = qr/[ \t]/; $sh_p1 = qr/[ \t]|$comment_ph0/; $sh_p0 = qr/(?>$linewrap*)(?:[ \t]|$comment_ph0)/; $string = qr/\"(?>(?:[^\\\"]+|\\.)*)\"/; $quote = qr/\'(?>(?:[^\\\']+|\\.)*)\'/; $container_p2 = qr/$comment_p2|$string|$quote/; $container_p1 = qr/$comment_p1|$string|$quote/; $container_p0 = qr/$comment_p0|$string|$quote/; $not_container_start_p2 = qr/[^\"\'\/]|\/[^\/\*]/; $not_container_start_p1 = qr/[^\"\'\/]|\/[^\/\*]/; $not_container_start_p0 = qr/[^\"\'\/]|\/$o[^\/\*]/; # $csq_or_rest_p2 $stuff_p2 = qr/$comment_p2|$string|$quote|$not_container_start_p2+/; $stuff_p1 = qr/$comment_p1|$string|$quote|$not_container_start_p1+/; $stuff_p0 = qr/$comment_p0|$string|$quote|$not_container_start_p0+/; $stuff_p1_unpacked = qr/($comment_p1)|($string)|($quote)|($not_container_start_p1+)/; $bol_p2 = qr/(?m)^/; $bol_p1 = qr/(?m)^/; $bol_p0 = qr/(?m)^(?$dot_p2*)/; $cpp_line_p1 = qr/$bol_p1$sh_p1*\#(?>$dot_p1*)/; $cpp_line_p0 = qr/$bol_p0$sh_p0*\#(?>$dot_p0*)/; $cpp_line_p2_command = qr/$bol_p2$sh_p2*\#$sh_p2*([a-z_][a-z_0-9]*)(?>$dot_p2*)/i; # End of file