The problem is a simple single pass processor (my first thought, too) doesn't handle the case of nested includes (header files in-lining other header files). As best as I can tell, you either need a recursive parser to handle the new material or need to add new material to the existing work queue. Thinking about it, I've improved the basic structure by changing the while conditional to be based off a work queue. This way I can unshift new material to the top of the stack as I hit includes rather than re-running the same regular expression - this was actually getting some false positives for failure due to multiple headers including the same libraries. I also added some handling for #pragma once and discovered I'll have to handle some nested #ifdefs. I still think the continue seems to be the cleanest way to handle parsing failures (quick exit to standard error message). I do see it's a bit contrived with all the undef $_, next; blocks, but see elsif constructs as messier.

#!/usr/bin/perl use strict; use warnings; my @lines = <DATA>; my ($error, $content); my %pragma_once; while (defined(local $_ = shift @lines)) { unless (/^#/) { $content .= $_; undef $_, next; } if (/^#include\s*/) { if (/<math/) { $content .= "import java.lang.Math;\n"; undef $_, next; } if( /"stdafx.h"/ ) { # Drop it undef $_, next; } if( m["(.+)"] ) { open my $inc_handle, '<', $1 or warn "$1: $^E\n" and next; my $include = do {local $/; <$inc_handle>;}; # slurp $include = "" if defined $pragma_once{$1}; $pragma_once{$1}++ if ($include =~ s/^#pragma once//m); unshift @lines, split /(?<=$\/)/, $include; undef $_, next; } warn "Unhandled include\n"; } if (/^#defined/) { #... } if (/^#ifdef/) { #... } if (/^#else/) { #... } if (/^#endif/) { #... } } continue { if (defined) { chomp; warn qq{Unhandled line "$_"\n}; $error++; } } print "\nContent:\n'$content'\n"; die "$error errors encountered\n" if $error; __DATA__ #include "stdafx.h" #include <math.h> #include <stdio.h> // A comment #include "AlyLee.h" #include "Common.h"

In reply to Re^7: while(){}continue{}; Useful? by kennethk
in thread while(){}continue{}; Useful? by BrowserUk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.