% set obrace 3
3
% if {$obrace<0} {return "missing '}'"}
extra characters after close-brace
Why doest this code throw an error?
TCL-Rule [4]: If the first character of a word is double-quote (“"”)
then the word is terminated by the next double-quote character.
"missing '}'" is ONE word and the interpreter should not look "inside"
the string.
What did I oversee?
Nevertheless
return "missing '\}'"
is working as expected.
--
Gerhard Reithofer
Tech-EDV Support Forum - http://support.tech-edv.co.at
You are experiencing a common form of Tcl syntax blindness here. The
word doesn't start at the first ", it starts at the first { so the
actual word is:
{return "missing '}
Which indeed has characters after the close brace.
Regards,
Mark
But Tcl-Rule [6] says: [6] Braces.
If the first character of a word is an open brace (“{”) and rule
[5] does not apply, then the word is terminated by the matching close
brace (“}”). Braces nest within the word: for each additional open brace
there must be an additional close brace (however, if an open brace or
close brace within the word is quoted with a backslash then it is not
counted in locating the matching close brace). No substitutions are
performed on the characters between the braces except for
backslash-newline substitutions described below, nor do semi-colons,
newlines, close brackets, or white space receive any special
interpretation. The word will consist of exactly the characters between
the outer braces, not including the braces themselves.
And since in Tcl-Rule [4] there is no talk about braces being treated as
ordinary characters within a quoted string, it's obvious that Tcl-Rule
[6] takes precedence as there was a word opened with '{' first
hth
Ronnie
--
Ronnie Brunner | ronnie....@netcetera.ch
phone +41-44-247 79 79 | fax +41-44-247 70 75
Netcetera AG | 8040 Zürich | Switzerland | http://netcetera.ch
What you overlooked is rule 6: If the first character of a word is an
open brace (“{”) ... then the word is terminated by the matching close
brace (“}”). Braces nest within the word ... (however, if an open brace
or close brace within the word is quoted with a backslash then it is not
counted in locating the matching close brace). No substitutions are
performed on the characters between the braces ... *nor do semi-colons,
newlines, close brackets, or white space* receive any special
interpretation. The word will consist of *exactly the characters between
the outer braces*, not including the braces themselves.
I have elided a few pieces of the rule, but the main point is still
clear: The word begins with “{”, the space after “return” received no
special interpretation and is simply part of the word. The double-quote
is therefore not the first character of anything, and is also simply
part of the word. The word ends at the close-brace that is enclosed in
single-quotes, and is the string “return "missing '”. This is
immediately followed by “'"}” with no intervening whitespace, which is
the error message that you're getting.
Your solution, “return "missing '\}'"” is, as you can see above, the
correct way to escape the close-brace.
-EE
--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
First word: if
second word: {$obrace<0}
third word: {return "missing '}
immediately after that closing brace of the third word is the
character ', hence the error message.
Remember: when a word starts with a brace it continues to the closing
brace, and double quotes are ignored with the opening and closing
brace.
On Thu, 19 Nov 2009, Bryan Oakley wrote:
> On Nov 19, 10:22 am, Gerhard Reithofer <gerhard.reitho...@tech-
> edv.co.at> wrote:
> > Hi TCLers,
> > I've studied Tcl language syntax several times, but I don't see my
> > fault:
> >
> > % set obrace 3
> > 3
> > % if {$obrace<0} {return "missing '}'"}
> > extra characters after close-brace
...
> First word: if
> second word: {$obrace<0}
> third word: {return "missing '}
>
> immediately after that closing brace of the third word is the
> character ', hence the error message.
>
> Remember: when a word starts with a brace it continues to the closing
> brace, and double quotes are ignored with the opening and closing
> brace.
thanks, it really was the "common Tcl syntax blindness" ;)
But what about the following:
if {$o eq "{"} {puts "doesn't work"}
Word 1: if
Word 2: {$c eq "{"}
Word 3: {puts "doensn't work"}
If I interprete that correct, is any double quote ignored within
braces - therefore the "matching" closing brace is missing.
A few solutions:
if { $o eq "\{" } {puts "1 works"}
if [expr { $o eq "\{" }] {puts "2 works"}
if {[expr { $o eq "\{" }]} {puts "3 works"}
if [expr \"$o\" eq \"{\" ] {puts "4 works"}
In other words, it is necessary to quote ANY brace within a braced
block, the only exception is a single command without braces (see 4.)
puts "{ is ok"
if 1 {puts "{ is not"}
Nope:
Word 1: if
Word 2: <not complete>
Rule [6] again: "Braces nest within the word: for each additional open
brace there must be an additional close brace [...]" which is not the
case for your '{' in double quotes
if {$o eq "\{"} {puts "works"}
is what you want
> In other words, it is necessary to quote ANY brace within a braced
> block, the only exception is a single command without braces (see 4.)
Nope, as long as they are matched: no need to escape them
if {$o eq "{}"} {puts "works too"}
hth
Ronnie
--
Ronnie Brunner | ronnie....@netcetera.ch
phone +41-44-247 79 79 | fax +41-44-247 70 75
Netcetera AG | 8040 Z�rich | Switzerland | http://netcetera.ch
ACK
I thought of the typical single braces (e.g. for parsing, ...)
THX