STON wtf parsing

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

STON wtf parsing

Peter Uhnak
How come this passes?

STON fromString: '{
"a": "b"
}wtf",
"x": "y"
'.

The result is dictionary "a" -> "b".

I would expect for it to die on parse error.

Peter
Reply | Threaded
Open this post in threaded view
|

Re: STON wtf parsing

Sven Van Caekenberghe-2
Hi Peter,

> On 05 Jul 2015, at 14:36, Peter Uhnák <[hidden email]> wrote:
>
> How come this passes?
>
> STON fromString: '{
> "a": "b"
> }wtf",
> "x": "y"
> '.
>
> The result is dictionary "a" -> "b".
>
> I would expect for it to die on parse error.
>
> Peter

The reason this does not fail is because (1) STON is a stream parser that accepts possibly multiple top-level expression from one stream (2) your input is basically valid and complete until the closing curly brace.

You could enforce the fact that the whole input should be consumed yourself.

| input reader result |
input := '{
        "a": "b"
}wtf",
        "x": "y"
' readStream.
reader := STON reader on: input.
result := reader next.
reader consumeWhitespace.
self assert: reader atEnd.
result

Sven


Reply | Threaded
Open this post in threaded view
|

Re: STON wtf parsing

Peter Uhnak
Ok but how does the parser knows whether the input is multiexpression or an error?
I mean it stops parsing in the middle of the string which seems really weird to me.

Peter

On Sun, Jul 5, 2015 at 3:28 PM, Sven Van Caekenberghe <[hidden email]> wrote:
Hi Peter,

> On 05 Jul 2015, at 14:36, Peter Uhnák <[hidden email]> wrote:
>
> How come this passes?
>
> STON fromString: '{
>       "a": "b"
> }wtf",
>       "x": "y"
> '.
>
> The result is dictionary "a" -> "b".
>
> I would expect for it to die on parse error.
>
> Peter

The reason this does not fail is because (1) STON is a stream parser that accepts possibly multiple top-level expression from one stream (2) your input is basically valid and complete until the closing curly brace.

You could enforce the fact that the whole input should be consumed yourself.

| input reader result |
input := '{
        "a": "b"
}wtf",
        "x": "y"
' readStream.
reader := STON reader on: input.
result := reader next.
reader consumeWhitespace.
self assert: reader atEnd.
result

Sven



Reply | Threaded
Open this post in threaded view
|

Re: STON wtf parsing

Sven Van Caekenberghe-2
The user/caller decides what it expects from the input and how to deal with exceptions.

For example, here is how to read multiple expression until EOF

Array streamContents: [ :out |
  [ reader atEnd ] whileFalse: [ out nextPut: reader next ] ]

I think the current design is the both simple and flexible.

> On 05 Jul 2015, at 15:47, Peter Uhnák <[hidden email]> wrote:
>
> Ok but how does the parser knows whether the input is multiexpression or an error?
> I mean it stops parsing in the middle of the string which seems really weird to me.
>
> Peter
>
> On Sun, Jul 5, 2015 at 3:28 PM, Sven Van Caekenberghe <[hidden email]> wrote:
> Hi Peter,
>
> > On 05 Jul 2015, at 14:36, Peter Uhnák <[hidden email]> wrote:
> >
> > How come this passes?
> >
> > STON fromString: '{
> >       "a": "b"
> > }wtf",
> >       "x": "y"
> > '.
> >
> > The result is dictionary "a" -> "b".
> >
> > I would expect for it to die on parse error.
> >
> > Peter
>
> The reason this does not fail is because (1) STON is a stream parser that accepts possibly multiple top-level expression from one stream (2) your input is basically valid and complete until the closing curly brace.
>
> You could enforce the fact that the whole input should be consumed yourself.
>
> | input reader result |
> input := '{
>         "a": "b"
> }wtf",
>         "x": "y"
> ' readStream.
> reader := STON reader on: input.
> result := reader next.
> reader consumeWhitespace.
> self assert: reader atEnd.
> result
>
> Sven
>
>
>