Check syntactically valid code

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

Check syntactically valid code

Ian Trudel-2
Hello!

I would like to determine whether some text (both single and multiple liners) sent over network is syntactically valid. Using Parser contains the relevant information but would need a true/false answer. Also knowing what the code is (literal, block, temporaries, method, etc) would be a nice bonus. Maybe get some error message too.

How would you accomplish this?

Best regards,
Ian


Reply | Threaded
Open this post in threaded view
|

Re: Check syntactically valid code

Levente Uzonyi
Hi Ian,

The following snippet should get you started:

| error sourceCode ast |
error := nil.
sourceCode := '3 + 4. 5'.
ast := [
  Parser new
  parse: sourceCode
  class: UndefinedObject
  noPattern: true
  notifying: nil
  ifFail: nil ]
  on: SyntaxErrorNotification
  do: [ :e | error := e ].
error
  ifNil: [ ast explore ]
  ifNotNil: [ error explore ]

Levente

On Tue, 25 Apr 2017, Ian Trudel wrote:

> Hello!
> I would like to determine whether some text (both single and multiple liners) sent over network is syntactically valid. Using Parser contains the relevant information but would need a
> true/false answer. Also knowing what the code is (literal, block, temporaries, method, etc) would be a nice bonus. Maybe get some error message too.
>
> How would you accomplish this?
>
> Best regards,
> Ian
>
>

Reply | Threaded
Open this post in threaded view
|

Re: Check syntactically valid code

Ian Trudel-2
This is excellent, thanks!

Best regards,
Ian


Reply | Threaded
Open this post in threaded view
|

Re: Check syntactically valid code

Ian Trudel-2
Hello Levente,

I implemented a small syntax server based on your recommendation. The code commented in SyntaxServer>>process: is working but requires the client to reconnect each time it wants to send source code to be validated, which is time consuming. Reusing the connection would be faster but the code here is not working. Any hint?

Best regards,
Ian



SyntaxServer.st (3K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Check syntactically valid code

Levente Uzonyi
Hi Ian,

I see a few issues with the code you attached:
- #receiveData will only read at most 2000 bytes and will get the content
of one or maybe more TCP packets. But if you have larger packets enabled
on your network, it'll only read a partial packet. I suggest you use
SocketStream, because you don't want to process packets but a stream of
data.
- if you want to handle multiple requests through a single TCP connection,
you should come up with a protocol to separate the different calls, so
that you know where a request starts and ends. For example, you could just
write 2 bytes, the length of the source string and then the source string
itself. Or you could use Smalltalk's chunk format. Or whatever you come up
with that fits your needs.
- locking is unnecessary for a single variable. As long as there's no real
message send (non-failing primitives are not real sends) or backwards jump
(loop) in your code, it'll be thread safe.

Levente

On Thu, 27 Apr 2017, Ian Trudel wrote:

> Hello Levente,
>
> I implemented a small syntax server based on your recommendation. The code commented in SyntaxServer>>process: is working but requires the client to reconnect each time it wants to send source code to be validated, which is time consuming. Reusing the connection would
> be faster but the code here is not working. Any hint?
>
> Best regards,
> Ian
>
>

Reply | Threaded
Open this post in threaded view
|

Re: Check syntactically valid code

Ian Trudel-2


2017-04-27 13:26 GMT-04:00 Levente Uzonyi <[hidden email]>:
Hi Ian,

Hi again,
 
I see a few issues with the code you attached:
- #receiveData will only read at most 2000 bytes and will get the content of one or maybe more TCP packets. But if you have larger packets enabled on your network, it'll only read a partial packet. I suggest you use SocketStream, because you don't want to process packets but a stream of data.

SocketStream seems like a good idea.
 
- if you want to handle multiple requests through a single TCP connection, you should come up with a protocol to separate the different calls, so that you know where a request starts and ends. For example, you could just write 2 bytes, the length of the source string and then the source string itself. Or you could use Smalltalk's chunk format. Or whatever you come up with that fits your needs.

Yes, I was thinking about sending length of the source code first. That would certainly allow SyntaxServer to know how many bytes to expect.

- locking is unnecessary for a single variable. As long as there's no real message send (non-failing primitives are not real sends) or backwards jump (loop) in your code, it'll be thread safe.

It was actually in a Pharo Sockets tutorial by Noury Bouraqadi and Luc Fabresse.

Thanks for your feedback!

Ian