From: tom.rmadilo on
The question here is what defines a "line". In Tcl this is simple...or
at least it seems simple.

In a Tcl source file a line ends with <CR> or <LF> or <CR><LF>.

In fact, if the last non-<CR>?<LF> sequence is a \, then the following
<CR>?<LF> is removed. Of course it is only removed in the
interpretation of the source code, the next line is appended, after
removing additional whitespace to the previous line.

So in Tcl, a line is not defined as ending with <CR>, <LF> or
<CR><LF>.

Network protocols tend to support even more complex line construction
rules.

My question is: can anyone produce a program using [gets] which can
read a Tcl source file containing line continuations and produce the
same results as the Tcl parser? This is even easier than the same
exercise with nntp or an email message.

The Tcl parser includes whitespace transformation, [gets] does not. So
unless I'm missing something [gets] is not a line oriented protocol
command, but a command which defines a line in a particular Tclish
way. So you can only use [gets] when the protocol and Tcl agree on
what defines a line.

Of course [gets] also suffer because it only considers the current
contents of the input buffer. If the current buffer ends with <CR> or
<LF>, then [gets] will assume that a complete line is in the buffer.

If the next char is <LF> and the previous buffer ending is <CR>, is
the <LF> a new line? The problem is caused because of the difference
between buffer contents and stream contents..if the analysis is done
based upon new data in the input buffer, without considering the
following data, could it be correct? No.

Final analysis must wait for further data or a timeout.

Anyway, basically [gets] is a non-lookahead parser. Hardly useful as a
protocol primitive.

From: Alexandre Ferrieux on
On Jun 22, 11:03 pm, "tom.rmadilo" <tom.rmad...(a)gmail.com> wrote:
>
> My question is: can anyone produce a program using [gets] which can
> read a Tcl source file containing line continuations and produce the
> same results as the Tcl parser?

set accu ""
while {[gets stdin line]>=0} {
append accu $line
set naked $accu
append accu \n
if {![info complete $naked]} continue ;# brace/bracket/quote
imbalance
if {![info complete $accu]} { ;# line continuation
set accu [string range $accu 0 end-2]
append accu " "
continue
}
puts "FLATTENED: $accu"
set accu ""
}

Explanation:

(1) we want to fold continuations only at toplevel, not deep inside
braces
(2) a toplevel continuation has the invariant:

[info complete $x]&&![info complete $x\n]

-Alex
From: tom.rmadilo on
On Jun 22, 3:34 pm, Alexandre Ferrieux <alexandre.ferri...(a)gmail.com>
wrote:
> On Jun 22, 11:03 pm, "tom.rmadilo" <tom.rmad...(a)gmail.com> wrote:
>
>
>
> > My question is: can anyone produce a program using [gets] which can
> > read a Tcl source file containing line continuations and produce the
> > same results as the Tcl parser?
>
> set accu ""
> while {[gets stdin line]>=0} {
>   append accu $line
>   set naked $accu
>   append accu \n
>   if {![info complete $naked]} continue ;# brace/bracket/quote
> imbalance
>   if {![info complete $accu]} { ;# line continuation
>     set accu [string range $accu 0 end-2]
>     append accu " "
>     continue
>   }
>   puts "FLATTENED: $accu"
>   set accu ""
>
> }
>
> Explanation:
>
>  (1) we want to fold continuations only at toplevel, not deep inside
> braces
>  (2) a toplevel continuation has the invariant:
>
>             [info complete $x]&&![info complete $x\n]

[info complete]?

So you consider that to be line oriented? Looks like you are using the
Tcl parser to read char-by-char.

Do it without [info complete] and I'll believe you.

From: Alexandre Ferrieux on
On Jun 23, 12:55 am, "tom.rmadilo" <tom.rmad...(a)gmail.com> wrote:
> On Jun 22, 3:34 pm, Alexandre Ferrieux <alexandre.ferri...(a)gmail.com>
> wrote:
>
>
>
>
>
> > On Jun 22, 11:03 pm, "tom.rmadilo" <tom.rmad...(a)gmail.com> wrote:
>
> > > My question is: can anyone produce a program using [gets] which can
> > > read a Tcl source file containing line continuations and produce the
> > > same results as the Tcl parser?
>
> > set accu ""
> > while {[gets stdin line]>=0} {
> >   append accu $line
> >   set naked $accu
> >   append accu \n
> >   if {![info complete $naked]} continue ;# brace/bracket/quote
> > imbalance
> >   if {![info complete $accu]} { ;# line continuation
> >     set accu [string range $accu 0 end-2]
> >     append accu " "
> >     continue
> >   }
> >   puts "FLATTENED: $accu"
> >   set accu ""
>
> > }
>
> > Explanation:
>
> >  (1) we want to fold continuations only at toplevel, not deep inside
> > braces
> >  (2) a toplevel continuation has the invariant:
>
> >             [info complete $x]&&![info complete $x\n]
>
> [info complete]?
>
> So you consider that to be line oriented? Looks like you are using the
> Tcl parser to read char-by-char.

I consider nothing. You specified one task, I gave code fulfilling it.
If it doesn't fit, respecify.

> Do it without [info complete] and I'll believe you.

Emulating the Tcl parser without using its script-level API [info
complete] ? What idiot would want to do that ?

-Alex
From: tom.rmadilo on
On Jun 22, 4:10 pm, Alexandre Ferrieux <alexandre.ferri...(a)gmail.com>
wrote:
> On Jun 23, 12:55 am, "tom.rmadilo" <tom.rmad...(a)gmail.com> wrote:
>
>
>
>
>
> > On Jun 22, 3:34 pm, Alexandre Ferrieux <alexandre.ferri...(a)gmail.com>
> > wrote:
>
> > > On Jun 22, 11:03 pm, "tom.rmadilo" <tom.rmad...(a)gmail.com> wrote:
>
> > > > My question is: can anyone produce a program using [gets] which can
> > > > read a Tcl source file containing line continuations and produce the
> > > > same results as the Tcl parser?
>
> > > set accu ""
> > > while {[gets stdin line]>=0} {
> > >   append accu $line
> > >   set naked $accu
> > >   append accu \n
> > >   if {![info complete $naked]} continue ;# brace/bracket/quote
> > > imbalance
> > >   if {![info complete $accu]} { ;# line continuation
> > >     set accu [string range $accu 0 end-2]
> > >     append accu " "
> > >     continue
> > >   }
> > >   puts "FLATTENED: $accu"
> > >   set accu ""
>
> > > }
>
> > > Explanation:
>
> > >  (1) we want to fold continuations only at toplevel, not deep inside
> > > braces
> > >  (2) a toplevel continuation has the invariant:
>
> > >             [info complete $x]&&![info complete $x\n]
>
> > [info complete]?
>
> > So you consider that to be line oriented? Looks like you are using the
> > Tcl parser to read char-by-char.
>
> I consider nothing. You specified one task, I gave code fulfilling it.
> If it doesn't fit, respecify.
>
> > Do it without [info complete] and I'll believe you.
>
> Emulating the Tcl parser without using its script-level API [info
> complete] ? What idiot would want to do that ?

Then offer a similar command for parsing an nntp or http header, but
[info complete] relies on char-by-char parsing.