Method for checking syntax

Is there any method to do syntax checking of a specific block but not
whole file?

If the code allows it, you could isolate the block into a separate file
while checking.

Apart from that, I think you’d have to rely on options in your IDE.

On Wed, Dec 4, 2013 at 6:30 AM, Asmita C. [email protected]
wrote:

Is there any method to do syntax checking of a specific block but not
whole file?

Could you perhaps explain a little of why you want to do this? If you
want
to check syntax programmatically, for example, you could eval the code
as a
string, rescuing the result and checking for any syntax errors, etc.
This
is what irb and pry are doing, effectively, at a very very basic level.

On Dec 4, 2013, at 16:25, tamouse pontiki [email protected]
wrote:

On Wed, Dec 4, 2013 at 6:30 AM, Asmita C. [email protected] wrote:
Is there any method to do syntax checking of a specific block but not
whole file?

Could you perhaps explain a little of why you want to do this? If you want to
check syntax programmatically, for example, you could eval the code as a string,
rescuing the result and checking for any syntax errors, etc. This is what irb and
pry are doing, effectively, at a very very basic level.

That’s a great idea. Especially if the user wants to verify system calls
or FileUtils.rm_rf and the like. :stuck_out_tongue:

On Wed, Dec 4, 2013 at 7:18 PM, Ryan D. [email protected]
wrote:

as a string, rescuing the result and checking for any syntax errors, etc.
This is what irb and pry are doing, effectively, at a very very basic level.

That’s a great idea. Especially if the user wants to verify system calls
or FileUtils.rm_rf and the like. :stuck_out_tongue:

Sure, just like you can do with irb and pry. :stuck_out_tongue:

Actually, I want to separate out parsing(for syntax checking) and
execution.
In our Application, we are using eval, instace_eval, class_eval etc.
what i want to do is that parse code block for syntax checking at early
stage and defer the execution at later stage.

Thanks Robert K. :slight_smile:

I would try this in my application. Looks like this would be helpful.

On Thu, Dec 5, 2013 at 7:09 AM, Asmita C. [email protected]
wrote:

Actually, I want to separate out parsing(for syntax checking) and
execution.
In our Application, we are using eval, instace_eval, class_eval etc.
what i want to do is that parse code block for syntax checking at early
stage and defer the execution at later stage.

Just wrap the piece of code:

irb(main):007:0> code = ‘1 + 2’
=> “1 + 2”
irb(main):008:0> compiled = eval(“lambda { #{code} }”)
=> #<Proc:[email protected](eval):1 (lambda)>
irb(main):009:0> compiled.call
=> 3

Now with a broken piece:

irb(main):010:0> code = '1 + ’
=> "1 + "
irb(main):011:0> compiled = eval(“lambda { #{code} }”)
SyntaxError: (eval):1: syntax error, unexpected ‘}’
lambda { 1 + }
^
from (irb):11:in eval' from (irb):11 from /usr/bin/irb:12:in

Cheers

robert

On Dec 4, 2013, at 20:52, tamouse pontiki [email protected]
wrote:

Sure, just like you can do with irb and pry. :stuck_out_tongue:

Except that’s not what they do. If they evaled, then they’d delete all
your files before you got to type “if false”.

in your repl you can read in the current file and parse with regex. Pry
has
it built in nowdays. I’ve used one of my own scripts for years which
defeats the repl -> editor -> run cycle. Ruby’s ability to reopen
classes
makes it ideal for this style of computation and experimentation.

On Dec 5, 2013, at 1:14, Robert K. [email protected]
wrote:

=> “1 + 2”
irb(main):008:0> compiled = eval(“lambda { #{code} }”)
=> #<Proc:[email protected](eval):1 (lambda)>
irb(main):009:0> compiled.call
=> 3

The other one I’ve seen looks like:

def check_syntax src
catch(:good) do
eval(“throw :good; #{src}”)
end
true
rescue SyntaxError
false
end

and you can, of course, use RubyParser.new.parse(src).

On 12/06/2013 01:49 PM, Ryan D. wrote:

def check_syntax src
catch(:good) do
eval(“throw :good; #{src}”)
end
true
rescue SyntaxError
false
end

Use BEGIN to be a bit safer:

def check_syntax_unsafe src
catch(:good) do
eval(“throw :good; #{src}”)
end
true
rescue SyntaxError
false
end

def check_syntax_safe src
catch(:good) do
eval(“BEGIN {throw :good}; #{src}”)
end
true
rescue SyntaxError
false
end

puts “UNSAFE”
p check_syntax_unsafe %{
BEGIN {puts “haha”}
}

puts

puts “SAFE”
p check_syntax_safe %{
BEGIN {puts “haha”}
}

END

Output:

UNSAFE
haha
true

SAFE
true

On Fri, Dec 6, 2013 at 10:49 PM, Ryan D. [email protected]
wrote:

The other one I’ve seen looks like:

def check_syntax src
catch(:good) do
eval(“throw :good; #{src}”)
end
true
rescue SyntaxError
false
end

This is nice! Thank you for that.

Kind regards

robert

On Sun, Dec 8, 2013 at 8:57 PM, Joel VanderWerf
[email protected] wrote:

false
end

puts “UNSAFE”
p check_syntax_unsafe %{
BEGIN {puts “haha”}
}

Very good point! I’d prefer my approach with the lambda because that
does not execute the code and it also gives you a handle to the code
for later execution. That avoids a second compilation.

Cheers

robert

On Mon, Dec 9, 2013 at 9:47 PM, Joel VanderWerf
[email protected] wrote:

compiled = eval(“lambda { #{code} }”)
p compiled.call

Dang! I should think more next time.

Cheers

robert

On 12/08/2013 11:57 PM, Robert K. wrote:

Very good point! I’d prefer my approach with the lambda because that
does not execute the code and it also gives you a handle to the code
for later execution. That avoids a second compilation.

Only if you really trust the input code…

code = ‘1 + 2 }; puts “haha”; proc {’
compiled = eval(“lambda { #{code} }”)
p compiled.call

a:~ $ ruby -e ‘code = ARGV.shift; p Thread.new(code){|code| $SAFE = 12;
begin; eval(code); rescue Object; $!; end }.value; p 42’ “… blech”
#<SyntaxError: (eval):1: syntax error, unexpected tDOT3
… blech
^>
42

+1 cleverness for Joel VanderWerf!

Code injection on the go!

(I didn’t understand it at first sight. I’ve had to fire up an irb
session to realize how it broke Robert K.'s approach).

On Mon, Dec 9, 2013 at 6:53 PM, Robert K.

On 12/10/2013 02:44 PM, dojo 4. wrote:

a:~ $ ruby -e ‘code = ARGV.shift; p Thread.new(code){|code| $SAFE = 12;
begin; eval(code); rescue Object; $!; end }.value; p 42’ “… blech”
#<SyntaxError: (eval):1: syntax error, unexpected tDOT3
… blech
^>
42

Watch out for #eval; this never finishes:

ruby -e ‘code = ARGV.shift; p Thread.new(code){|code| $SAFE = 12;
begin; eval(code); rescue Object; $!; end }.value; p 42’ ‘loop {}’

I’m sorry, guys, this wrapped around my brain and I couldn’t unwrap it
so
far:

In

def check_syntax src
catch(:good) do
eval(“throw :good; #{src}”)
end
true
rescue SyntaxError
false
end

Why insert the throw before the code to be checked? If throw comes
first, shouldn’t it always jump out of the eval before checking src?
Sorry for slowing the conversation down, just seeing an opportunity to
learn =]

2013/12/11 Joel VanderWerf [email protected]

Watch out for #eval; this never finishes:

ruby -e ‘code = ARGV.shift; p Thread.new(code){|code| $SAFE = 12;
begin; eval(code); rescue Object; $!; end }.value; p 42’ ‘loop {}’


Thyago Barbosa Rodrigues [Natal-RN/Brasil]

“Who loves not wine, women and song,
remains a fool his whole life long”
(Martin Luther [Martinho Lutero], Telogo Alemo)

“Phantasie ist wichtiger als Wissen, denn Wissen ist begrenzt.” (Albert
Einstein)

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs