hsmyers has asked for the wisdom of the Perl Monks concerning the following question:
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:
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Raw Text and Parse::RecDescent
by kvale (Monsignor) on Jun 28, 2002 at 17:31 UTC | |
|
Re: Raw Text and Parse::RecDescent
by hsmyers (Canon) on Jun 28, 2002 at 21:11 UTC |