简体   繁体   中英

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!

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM