Home   Archive   Permalink



Parse rule question

A few years ago I came up with a scheme for putting documentation into scripts, modeled after powershell documentation. I put it in the script itself, in front of the header, and divided it into sections with section names in capital letters, as shown in the sample below. Then I wrote a program to parse a script file on the section headers and extract the four sections into four strings, as shown in the code sample below.    
    
That scheme worked, so this is not a question about how to make it work. The question came to mind recently, and is this: To accomplish what I want, do I have to parse script file four times, as I now do, or is there a way to structure the parse rule so that I can use one call of the "parse" function and divide the script into its four sections.
    
Thank you. Samples follow.
    
Sample script with embedded documentation:
TITLE
Test script to make sure the program runs.
    
SUMMARY
This is a demo script that you can run to make sure
things are working. It has the minimum code to do something.
    
DOCUMENTATION
Modify the code as needed to do something useful.
Make sure you have the interpreter installed if you want
to double-click the script to run it. Otherwise, you
could make a batch file to run the script with the
command-line switches:
    
     -i -s --script (script-name)
    
SCRIPT
R E B O L [
     Title: "COB global services module"
]
     alert "Script has run"
    
Solution as I currently do it:
LIST-TITLE: ""
LIST-SUMMARY: ""
LIST-DOCUMENTATION: ""
LIST-SCRIPT: ""
    
LIST-FILE-DATA: read LIST-FILE-PATH
;; -- Extract the four parts of the documentation.
parse/case LIST-FILE-DATA [thru "TITLE" copy LIST-TITLE to "SUMMARY"]
parse/case LIST-FILE-DATA [thru "SUMMARY" copy LIST-SUMMARY to "DOCUMENTATION"]
parse/case LIST-FILE-DATA [thru "DOCUMENTATION" copy LIST-DOCUMENTATION to "SCRIPT"]
parse/case LIST-FILE-DATA [thru "SCRIPT" copy LIST-SCRIPT to end]


posted by:   Steven White       5-Jul-2018/14:49:45-7:00



This is a good job for "any". This means match 0 or more of the following rule.
Used in combination with "|" which means "or" you get something like this (untested) code.
    
parse/case LIST-FILE-DATA [
     any [
         thru "TITLE" copy LIST-TITLE to "SUMMARY"
         |
         thru "SUMMARY" copy LIST-SUMMARY to "DOCUMENTATION"
         |
         thru "DOCUMENTATION" copy LIST-DOCUMENTATION to "SCRIPT"
         |
         thru "SCRIPT" copy LIST-SCRIPT to end
     ]
]
    
-J

posted by:   johnk       5-Jul-2018/22:08:49-7:00



The previous example will also cope with missing sections and out of order sections (assuming the SCRIPT section is always at the end).
    
Alternatively, if you are confident that all of your files are always structured correctly then you could simplify it further.
e.g.
    
parse/case LIST-FILE-DATA [
     thru "TITLE" copy LIST-TITLE to "SUMMARY"
     thru "SUMMARY" copy LIST-SUMMARY to "DOCUMENTATION"
     thru "DOCUMENTATION" copy LIST-DOCUMENTATION to "SCRIPT"
     thru "SCRIPT" copy LIST-SCRIPT to end
]
-J

posted by:   johnk       5-Jul-2018/22:13:17-7:00



Name:


Message:


Type the reverse of this captcha text: "c i p o t"



Home