how to parse thru nested parens?
[1/4] from: petr::krenzelok::trz::cz at: 10-Dec-2001 21:26
Hi,
I am building sms system and I would like to trace following occurance:
comand(text info or parameters)
however, I wold like following to be parsed correctly, so ignoring
nested parens, e.g.:
command(some params(some subparams), maybe some other params)
Thanks a lot,
Cheers,
-pekr-
[2/4] from: robert:muench:robertmuench at: 10-Dec-2001 21:43
> -----Original Message-----
> From: [rebol-bounce--rebol--com] [mailto:[rebol-bounce--rebol--com]]On Behalf Of
<<quoted lines omitted: 7>>
> nested parens, e.g.:
> command(some params(some subparams), maybe some other params)
Hi, do you want to know how to parse the ( and ) exactly or do you want to know
how nested structures are parsed?
Anyway, the second is more interesting (not tested!):
rules: "command" some params
params: ["(" [params | some subparams] ")" ]
subparams: what-ever-it-takes
Robert
[3/4] from: nitsch-lists:netcologne at: 11-Dec-2001 15:01
RE: [REBOL] how to parse thru nested parens?
Hi Pekr
[petr--krenzelok--trz--cz] wrote:
> Hi,
> I am building sms system and I would like to trace following occurance:
<<quoted lines omitted: 3>>
> command(some params(some subparams), maybe some other params)
> Thanks a lot,
see %parse-code.r in the script-library:
REBOL [
Title: "Parse REBOL Source"
Author: "Carl Sassenrath"
File: %parse-code.r
Date: 30-May-2000
Purpose: {An example of how to parse REBOL source code.}
Category: [script util text 2]
]
parse-code: func [
"Parse REBOL source code."
text /local str new
] [
parse text blk-rule: [
some [; repeat until done
str:
newline |
#";" [thru newline | to end] new: (probe copy/part str new) |
[#"[" | #"("] blk-rule |
[#"]" | #")"] |
skip (set [value new] load/next str probe :value) :new
]
]
]
i think your parsing has to replace the [skip ..]-part.
beware, this is recursive and parse has no local variables for that.
so real processing needs some workaround.
whats the best way to to this?
based on this is a pretty-printer %clean-script.r and a colorizer %color-code.r
there.
note there seems to be an bug in the parser,
triggered by long rebol-scripts in %clean-script.r .
starts doubling "[" to "[[" with the scripts rest, and nobody knows why.
i replaced the 'some with 'any, now it runs stable.
but it does not warn anymore if parents are unbalanced.
> Cheers,
> -pekr-
>
-Volker
[4/4] from: robert:muench:robertmuench at: 12-Dec-2001 9:30
> -----Original Message-----
> From: [rebol-bounce--rebol--com] [mailto:[rebol-bounce--rebol--com]]On Behalf Of
<<quoted lines omitted: 5>>
> so real processing needs some workaround.
> whats the best way to to this?
Hi, the best way to solve this is to use inline variables and put the values on
stack variables ;-) as soon as possible. I use this all the time when parsing.
Here is my stack-context I normally use:
; Stack Datastructure Object
stack!: make object! [
stack: make block! []
push: func['value][
insert head stack value
]
pop: does [
value: first stack
remove stack
return value
]
top: does [
if not empty? [return first stack]
]
empty?: does [
either (length? stack) == 0 [return true][return false]
]
ontop?: func ['value][
either value == top [return true][return false]
]
instack?: func ['value][
either result: find stack value [return index? result][return none]
]
]
--
Robert M. Münch
IT & Management Freelancer
Mobile: +49 (0)177 2452 802
Fax : +49 (0)721 8408 9112
Web : http://www.robertmuench.de
Notes
- Quoted lines have been omitted from some messages.
View the message alone to see the lines that have been omitted