Hi fellow monks,

I'm currently working on a ColdFusion-like template parser (refer to this node in Meditations for more detail) and am nearly done with the main parser routine. As I'm looking through my code (and debugging ;-), I thought to seek your wisdom on this piece:

This code snippen is supposed to find outer-most closing tag for given opening tag. Refer to inline perl comments for extra details.
# have to search a little differently for nest +ed tags # making sure that an ending tag belonging to # a nested opening tag is not processed as the + ending # tag for the current opening tag. # In case i sounded awkward, here's a little d +iagram: # # <cfif> <--- this tag # <cfif> <--- nested open tag # </cfif> <--- end tag for the nested open +tag # </cfif> <--- end tag for this tag (the on +e that has # to be picked up) # # Actual example: # @chunks = # . . . . . . # 5 "<cfif bool eq 1>\cJ\cI " <--- +chk_i, found_i # 6 "<cfif foo = bar>\cJ\cI\cI " # 7 "<cfif bar = foo>\cJ\cI\cI " # 8 "</cfif>\cJ\cI " # 9 "</cfif>\cJ\cI\cI \cJ\cI BOOL is true!\c +J" # 10 "<cfelse>\cJ\cIBOOL is false!\cJ" # 11 '</cfif> <--- +after: found_i my $opening_tag = $rules->{tag_start}[0] . $ta +g_name; my $nested = 0; # count of nested open tags fo +und. while ((($chunks[++$found_i] =~ m/^$closing_ta +g/) ? ($nested > 0 ? $nested-- : 0) : ($chunks[$found_i] =~ m/^$opening_ta +g/ ? ++$nested : 1)) && $found_i < @chunks) {}
My question is do you see any problem with the code? I'm sure some of you came across similar problems and might have some knowledge of possible pitfalls/hidden bugs. Please feel free to add/subtract from this code.. all suggestions are much appreciated ;-).

Cheers,

"There is no system but GNU, and Linux is one of its kernels." -- Confession of Faith

In reply to Template Parsing - Finding tag pairs. by vladb

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.