Harald Kirsch <k...@iitb.fhg.de> wrote: > Is there a way to split a string into parts the same way as eval would > do?
Here's a routine I wrote to support parsing of class bodies in an itcl-like, pure Tcl, OO framework into Tcl commands. I've modified the interface to match yours. It handles the "semicolon in a comment" problem.
proc cmdSplit {body} { set commands {} set chunk "" foreach line [split $body "\n"] { append chunk $line if {[info complete "$chunk\n"]} { # $chunk ends in a complete Tcl command, and none of the # newlines within it end a complete Tcl command. If there # are multiple Tcl commands in $chunk, they must be # separated by semi-colons. set cmd "" foreach part [split $chunk ";"] { append cmd $part if {[info complete "$cmd\n"]} { set cmd [string trimleft $cmd] # Drop empty commands and comments if {![string match {} $cmd] \ && ![string match #* $cmd]} { lappend commands $cmd } if {[string match #* $cmd]} { set cmd "#;" } else { set cmd "" } } else { # No complete command yet. # Replace semicolon and continue append cmd ";" } } set chunk "" } else { # No end of command yet. Put the newline back and continue append chunk "\n" } } if {![string match {} [string trimright $chunk]]} { return -code error "Can't parse body into a\ sequence of commands.\n\tIncomplete\ command:\n-----\n$chunk\n-----" } return $commands
}
-- | Don Porter, D.Sc. Mathematical and Computational Sciences Division | | donald.por...@nist.gov Information Technology Laboratory | | http://math.nist.gov/mcsd/Staff/DPorter/ NIST | |______________________________________________________________________|