Error when compiling a string with a quote

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

Error when compiling a string with a quote

HilaireFernandes
Hello,

I get an error (see screenshot) when I compile at runtime a method where
a quote is found in its body.

I expect this compiled method:

description
^ 'n''a pas sa force'


The user inputs part of this method as: n'a pas sa force
In its string representation, it is displayed as: n''a pas sa force
with the quote correctly escaped. But compiling fail.


I use this code to compile at runtime:

stream := WriteStream on: String new.
stream << 'description' << Character cr
 << Character tab << $^ <<$' << dialog description string << $'.
scriptClass class compile: stream contents classified: 'public'.



--
Dr. Geo - http://drgeo.eu
iStoa - http://istoa.drgeo.eu

Syntax Error: End of statement list encountered.png (22K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Error when compiling a string with a quote

Sven Van Caekenberghe-2
I think you need to send #storeString to the result of 'dialog description string'. The single quote has to be doubled.

> On 13 Nov 2014, at 17:50, Hilaire <[hidden email]> wrote:
>
> Hello,
>
> I get an error (see screenshot) when I compile at runtime a method where
> a quote is found in its body.
>
> I expect this compiled method:
>
> description
> ^ 'n''a pas sa force'
>
>
> The user inputs part of this method as: n'a pas sa force
> In its string representation, it is displayed as: n''a pas sa force
> with the quote correctly escaped. But compiling fail.
>
>
> I use this code to compile at runtime:
>
> stream := WriteStream on: String new.
> stream << 'description' << Character cr
> << Character tab << $^ <<$' << dialog description string << $'.
> scriptClass class compile: stream contents classified: 'public'.
>
>
>
> --
> Dr. Geo - http://drgeo.eu
> iStoa - http://istoa.drgeo.eu
> <Syntax Error: End of statement list encountered.png>


Reply | Threaded
Open this post in threaded view
|

Re: Error when compiling a string with a quote

HilaireFernandes
Le 13/11/2014 18:05, Sven Van Caekenberghe a écrit :
> I think you need to send #storeString to the result of 'dialog description string'. The single quote has to be doubled.

Yep, I realized it is not double quoted.
I used this message copyWithRegex: '''' matchesReplacedWith:  ''''''

However both #storeString or #printString messages can't do the job.

'n''a pas l''e'  storeString
-> '''n''''a pas l''''e'''

('n''a pas l''e'copyWithRegex: '''' matchesReplacedWith:  '''''')
-> 'n''''a pas l''''e'


#storeString seems to mess up with the quote, adding superfluous one.
Strange?

--
Dr. Geo - http://drgeo.eu
iStoa - http://istoa.drgeo.eu


Reply | Threaded
Open this post in threaded view
|

Re: Error when compiling a string with a quote

Sven Van Caekenberghe-2
I was too quick to respond, the #storeString conversion is OK:

(#( $f $o $o $' $b $a $r) as: String) storeString collect: [ :each | each ] as: Array.

=> #($' $f $o $o $' $' $b $a $r $')

The contract of #storeString is, given a String (well any object), what should it look like when written as a literal constant in source code. That is precisely what you want, no ?

but you have to drop adding your own quotes, like

stream << 'description' << Character cr
<< Character tab << $^ << dialog description string storeString.

or even (as #printOn: is the same as #storeOn: for String)

stream << 'description' << Character cr
<< Character tab << $^ print: dialog description string.

And I would turn this into a cascade, but that is more coding style.

stream
  << 'description'; cr;
  tab; nextPut: $^; print: dialog description string.

> On 13 Nov 2014, at 18:26, Hilaire <[hidden email]> wrote:
>
> Le 13/11/2014 18:05, Sven Van Caekenberghe a écrit :
>> I think you need to send #storeString to the result of 'dialog description string'. The single quote has to be doubled.
>
> Yep, I realized it is not double quoted.
> I used this message copyWithRegex: '''' matchesReplacedWith:  ''''''
>
> However both #storeString or #printString messages can't do the job.
>
> 'n''a pas l''e'  storeString
> -> '''n''''a pas l''''e'''
>
> ('n''a pas l''e'copyWithRegex: '''' matchesReplacedWith:  '''''')
> -> 'n''''a pas l''''e'
>
>
> #storeString seems to mess up with the quote, adding superfluous one.
> Strange?
>
> --
> Dr. Geo - http://drgeo.eu
> iStoa - http://istoa.drgeo.eu
>
>


Reply | Threaded
Open this post in threaded view
|

Re: Error when compiling a string with a quote

HilaireFernandes
Thanks for the tips!

Hilaire

Le 13/11/2014 19:04, Sven Van Caekenberghe a écrit :
> I was too quick to respond, the #storeString conversion is OK:


--
Dr. Geo - http://drgeo.eu
iStoa - http://istoa.drgeo.eu