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.

Turning what I had into a recursive parser is trivial and probably more robust:

#!/usr/bin/perl use strict; use warnings; my $error; sub parse { my $fh = shift; my $content = ''; while( <$fh> ) { unless( s[^#include (.+)$][] ) { chomp; warn "Non-include line '$_' untouched\n"; $content .= $_ . "\n"; } else{ local $_ = $1; if( m[<math] ) { $content .= qq[import java.lang.Math;\n]; } elsif( m["stdafx.h"] ) { $content .= qq[#include "stdafx"\n]; } elsif( m["(.+)"] ) { open my $inc_handle, '<', $1 or warn "$1: $^E\n" and ++$error and next; $content .= parse( $inc_handle ); } else { warn "Unhandled include $_\n"; $error++; } } } return $content; } my $content = parse( \*DATA ); 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"
I also added some handling for #pragma once and discovered I'll have to handle some nested #ifdefs.

And I think a recursive parser is the only way to go if your going to start handling conditionals.

And you're going to have to get a lot more sophisticated. You'll need to start storing state--the current values of #defines etc.--in order that you can decide which branch of #ifdef #else to process, which may determine which includes you need to process. And at that point, logging and error and trying to continue for missing files doesn't work at all. The only thing you can do is die.

As an example of the use of continue, it doesn't really hold up for me. If all your if blocks have to next to avoid entering the continue anyway, you might as well just stick the error handling at the bottom of the while. But as your code above shows, the idea that you can handle all the possible errors in one place doesn't hold up either.

If this is a serious project, then you'd almost certainly be better off using an existing pre-processor like m4. Or cl/gcc -E.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"I'd rather go naked than blow up my ass"

In reply to Re^8: while(){}continue{}; Useful? by BrowserUk
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.