The code below works as given, but is seriously flawed. There are (at least) two things wrong:

The root cause of this dis-harmony is the definition of script_line. The /\s.*/ essentially eats any line that begins with white space. But since we are not talking Python here, this is too arbitrary to easily choke down. Likewise the first column requirement for the closing brace. There is a small herd of things that I've tried:

plus numerous incremental versions of this and similar ideas. Loosely put, given balanced braces give me each line of text within regardless of content. Seems simple but I suspect that like many such situations that the semblance is false!

So then-- the question I put to you my brethren is this: How do I re-write the grammar so that I can parse essentially raw text while still allowing for capacious code layout (i.e. the closing brace must be allowed to wander as it will)?

#!/perl/bin/perl # # test.pl -- use strict; use warnings; use diagnostics; use Parse::RecDescent; use Data::Dumper; $::RD_TRACE = 1 if ( $ARGV[0] ); $::RD_AUTOACTION = q { [@item[0..$#item]] }; my $parser = new Parse::RecDescent( q{ startrule: script script: 'script' '[' script_options(s /,/) ']' script_body script_options: script_option '=' qstring script_option: 'tag-prefix' | 'language' | 'implements-prefix' script_body: '{' script_line(s?) '}' script_line: <skip: ''> newline(s?) /\s.*/ newline newline: "\n" is_printable: /[a-zA-Z0-9_&\#;:\$()\'= ,!\@\/.\[\]\-]+/ qstring: '"' is_printable '"' } ); #_____________________________________________________________________ +_________ my $data; my $test; while (<DATA>) { $test .= $_; } $data = $parser->startrule($test); print Dumper $data,"\n"; __DATA__ script [tag-prefix="msxsl" ,language="VBScript", implements-prefix="us +er"] { dim fs dim FoldersOnly dim FilesOnly dim FilesAndFolders function getFolder(dirName,Mode) dim folder init set fso = CreateObject("Scripting.FileSystemObject") set folder = fso.GetFolder(dirName) set folderDoc=createObject("MSXML2.FreeThreadedDOMDocument") folderDoc.setProperty "SelectionLanguage","XPath" folderDoc.async=false folderDoc.loadXML "<folder name='"+dirName+"'/>" set folderNode=folderDoc.documentElement folderNode.setAttribute "name",folder.name folderNode.setAttribute "path",folder.path folderNode.setAttribute "dateCreated",mapDate(folder.dateCreat +ed) folderNode.setAttribute "dateLastAccessed",mapDate(folder.date +LastAccessed) folderNode.setAttribute "shortName",folder.shortName folderNode.setAttribute "shortPath",folder.shortPath folderNode.setAttribute "size",folder.size folderNode.setAttribute "type",folder.type if mode=FoldersOnly or mode=FilesAndFolders then getFolders folder,folderNode,dirName,mode end if if mode=FilesOnly or mode=FilesAndFolders then getFiles folder,folderNode,dirName end if set getFolder=folderDoc end function }

–hsm

"Never try to teach a pig to sing…it wastes your time and it annoys the pig."

edited: Fri Jun 28 23:05:16 2002 by jeffa - added readmore tag


In reply to Raw Text and Parse::RecDescent by hsmyers

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.