Updated 2008-09-04 07:45:39 by lars_h

For information on "rc files", see the RC file. This page describes a utility command for parsing application-specific RC files.

CMcC 2008Sep1 was looking for a way to read configuration into dicts and such.

I wanted something like subst to perform variable and functional substitution, but which guaranteed the resultant form would be list-like, if the input was. I also wanted the ability to intersperse comments in the configuration structure. What I really wanted was a tcl-like little-language for configuration.

The following proc ignores lines which are commented like tcl source, and substitutes [] and $-forms. Its output is a list of fully substituted tcl-like words. It may be nested.

Caveat: The little language isn't completely coherent and consistent, in that the characters ; and # always have special literal significance as commencing comments. I'm prepared to tolerate that inflexibility in exchange for speed and ease of parsing, but it may not suit you.
    proc rc {text} {
	set accum ""
	set result {}
	foreach line [split $text \n] {
	    set line [string trim $line]
	    if {$line eq ""} continue
	    lassign [split $line {\#;}] line
	    append accum " " [string trim $line]
	    if {$accum ne "" && [info complete $accum]} {
		set pass [uplevel 1 list $accum]
		lappend result {*}$pass
		set accum ""
	    }
	}
	return $result
    }

Lars H: Have you considered using an empty interpreter instead?

CMcC yes, and it may well be worth doing, however I want to be able to invoke a fairly wide range of functions and grab variable values from the config.

Additionally, it doesn't buy you much, as you must still feed the stuff a 'complete' line at a time, and presumably prepend a set to each line, or something.

Lars H: Defining just the unknown of the empty interpreter to be an alias to lappend result in the master goes a long way towards achieving the above, I think. Concretely it comes out as
 interp create -safe empty
 empty eval {namespace delete ::}
 empty alias unknown lappend result
 proc rc {text} {
    set result {}
    empty eval $text
    return $result
 }

but perhaps I'm missing something. It would clarify things if you gave a concrete example of the kind of data rc is supposed to parse, and what the parsed results look like.