Hi,

I build a small parser to create your perl structure from your file. A part of this is a escape function, that would escape special chars in s///.

The parsing is done specially by the parse.

#!/usr/bin/perl -w use strict; use Data::Dumper; sub escape { my $str=shift; $str=~s/\\/\\\\/g; $str=~s/\[/\\[/g; $str=~s/\]/\\]/g; $str=~s/\(/\\(/g; $str=~s/\)/\\)/g; $str=~s/\{/\\{/g; $str=~s/\}/\\}/g; $str=~s/\^/\\^/g; $str=~s/\//\\\//g; $str=~s/\+/\\\+/g; $str=~s/\-/\\\-/g; $str=~s/\$/\\\$/g; $str=~s/\&/\\\&/g; $str=~s/\*/\\\*/g; $str=~s/\|/\\\|/g; $str=~s/\?/\\\?/g; return $str; }; sub parseoptions { my $options=shift; my @opts=(); $options=~s/\s+$//; for my $opt (split /\n/, $options) { $opt=~/^\s*(\w+)\s+(.*)$/; my $data={name=>$1, content=>$2}; unshift @opts, $data; } return \@opts; } sub parse { my $lookat=shift; my @struct=(); while ($$lookat=~/^\s*(\w+)\s+(\w+)?\s*{/) { my $data={type=>$1,name=>$2}; my $str=escape($&); $$lookat=~s/^$str//; if ($$lookat=~/^[^}]+{/) { $data->{content}=parse($lookat); $$lookat=~s/^\s*}//; } else { ($data->{content})=$$lookat=~/^\s*([^}]+)\s*}/ +; my $str=escape($&); $$lookat=~s/^$str//; if ($data->{type} eq 'single') { $data->{content}=parseoptions($data->{ +content}); } } unshift @struct, $data; } return \@struct; } my $quest=""; while (my $line=<>) { $line=~s/^\s+/ /g; $quest.=$line; } my $struct=parse(\$quest); print Dumper($struct);


Hope this helps.


In reply to Re: Parsing a macro language by themage
in thread Parsing a macro language by bluetrust

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.