Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion Q: How to split a string into elements exactly as eval would do
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Donald G Porter  
View profile  
 More options Sep 9 1998, 3:00 am
Newsgroups: comp.lang.tcl
From: d...@sunbow.cam.nist.gov (Donald G Porter)
Date: 1998/09/09
Subject: Re: Q: How to split a string into elements exactly as eval would do
In article <KIR.98Sep7144...@neptun.iitb.fhg.de>,

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 |
|______________________________________________________________________|

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.