Script Library: 1004 scripts
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 
View scriptLicenseDownload documentation as: HTML or editable
Download scriptHistoryOther scripts by: brett

Documentation for: load-parse-tree.r


Load-Parse-Tree

1. What is this all about?

It is a function that returns nested blocks and words that represent the structure of your input, based upon parse rules that you give it.

2. How is it useful?

If you have parse rules that validate some input, you can use this function to give you a REBOL representation of that input, without coding any actions in your parse rules.

2.1 How can you use the output?

  • Use it as you would normal REBOL data.
  • You could bind the output to functions and reduce the result.
  • You could try rewriting the structure with Gabriele Santilli's Rewrite function. For example, parse a pascal program say, rewriting the structure into executable REBOL code.

3. Example

For example, this code:

    ; Parse rules
    digit: charset {0123456789}
    hex-digit: charset {0123456789ABCDEF}
    letter: charset [#"a" - #"z" #"A" - #"Z"]
    word: [some letter]
    phrase: [some [word | { }]]
    number: [some digit]
    hex-literal: [#"$" some hex-digit]
    item: [phrase | hex-literal | number | { }]

    ; The information we want to appear in the structure.
    structure-terms: [phrase word hex-literal number]

    ; Define a helper function that uses functions from parse-analysis.r
    get-parse-tree: func [
        terms
        data [string!]
        /local ctx result
    ][
        ctx: hook-parse terms
        result: load-parse-tree [parse/all data [any item]] ctx
        unhook-parse ctx
        result
    ]

    data: {There were 374 brown foxes and $0001 mottley one.}
    print mold get-parse-tree structure-terms data

Produces this output:

    [
        phrase [
        word "There"
        word "were"
        ]
        number "374"
        phrase [
        word "brown"
        word "foxes"
        word "and"
        ]
        hex-literal "$0001"
        phrase [
        word "mottley"
        word "one"
        ]
    ]

4. How do you use the functions?

  • You need to run the parse-analysis.r script first. See the documentation for that script to understand out to hook parse rules.
  • Then you invoke Parse using the function.
  • Finally you clean up by restoring (unhooking) the original rules.

5. When will this function not work?

As for the parse-analysis.r functions on which this function is based:

  • Standard parse programming rules and even some dynamic parse programming should be fine. Some advanced dynamic parse programming may not work with these functions.
  • You might hit an internal recursion limit.
  • Obviously a bug could cause an infinite loop - there is a lot of work going on and a few assumptions being made.

Therefore the best use of this script is in an ad-hoc fashion by developers not as part of production programs.

5.1 What do you mean by dynamic parse programming?

Changing the input or the rules as parse executes. If you have the skills to do this you should be able to work out if you can use this function with your dynamic parse programming.

6. About the script author

Brett Handley started programming REBOL early 2000 and maintains a site of REBOL information and scripts at:

http://www.codeconscious.com/rebol/