Sunder
Sunder

Reputation: 1513

How do I parse this in Rebol?

How would I go about parsing this string

"a:foo[and it's cousin bar[are here]]"

into this

"a:" "foo[" "and" "it's" "cousin" "bar[" "are" "here" "]" "]"

In essence I'm looking to accomplish three things, extract an assignment "a:", extract sections "foo[" (including nested sections) and the closing section "]". I could evenly space them and just do a simple parse but I don't want to do that.

Hope it makes sense. Any help will be greatly appreciated!

Thanks!

Upvotes: 2

Views: 209

Answers (2)

rgchris
rgchris

Reputation: 3718

Define the elements of your language, then collect them as you match them:

parse-my-language: use [word assignment section section-end space][

    word: use [letters][
        letters: charset [#"a" - #"z" ".'"]
        [some letters]
    ]
    assignment: [word ":"]
    section: [word "["]
    section-end: "]"

    space: charset " "

    func [statement /local out element][
        out: copy []
        if parse/all statement [
            any [
                copy element [
                    assignment | section | section-end | word
                ] (append out element)
                | some space
            ]
        ][out]
    ]
]

probe parse-my-language "a:foo[and it's cousin bar[are here]]"

Note: I use 'use to isolate words used solely for this purpose.

Upvotes: 3

John
John

Reputation: 21

Some more context around the example may help as there are often many options you can try in rebol.

One simple approach would be to "fix" your string to make it more like normal rebol data.

source-string: "a:foo[and it's cousin bar[are here]]"
replace/all source-string "[" " [ "
replace/all source-string "]" " ] "
replace/all source-string ":" ": "
output: load source-string

It is rare to use strings in this way in rebol. Blocks are generally more flexible and simple to parse.

Upvotes: 2

Related Questions