I would like to be able to parse some Tcl code where arguments are not surrounded by strings.
Consider this tcl code:
proc foo {name} {
puts "Foo --> $name"
}
foo bar
For those unfamiliar with Tcl, foo is the method name and bar is the argument (quotes are optional in Tcl).
The previous code will output:
Foo --> bar
Is it possible to parse exactly the same input using ruby (bar remains unquoted)?
The equivalent ruby code is:
def foo(name)
puts "Foo --> #{name}"
end
tcl = <<-TCL.gsub(/^\s+/, "").chop
foo bar
TCL
instance_eval(tcl)
Of course that fails when it reaches bar since it’s expected it to be quoted.
I’ve tried tinkering with method_missing
def method_missing(meth, *args)
puts meth.to_s + " --> args.to_s
end
but it parses in reverse order:
to_hash --> []
bar --> []
foo --> [nil]
Does anyone have a clean solution to this type of problem. I’d like to avoid tokenizing the strings since reading the data in by calling a method requires minimal work compared to lexical analysis. But if I’m trying to do something that’s not possible, I’d like to know. Thanks.
It’s doesn’t work for you because
.putsmethod returnsnilinstead of string:I really don’t know why
to_hashappears in thismethod_missingbut it works: