How to check syntax without executing
-
Once again, it's me with more questions...
Lets say I have this code:
<span class="syntaxdefault">script </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> </span><span class="syntaxstring">"<br />hello = 'yo bro'<br />Sketchup.active_model.entities[0].material = 'red' # Assume entities[0] is an existing group<br />fgsdjhkjdf<br />"<br /><br /></span><span class="syntaxdefault">begin<br /> eval</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">script</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault">rescue<br /> puts </span><span class="syntaxstring">'you got some errors there'<br /></span><span class="syntaxdefault">end</span>
What happened:
local var hello created
entities[0] material changed to red
syntax errorWhat I want to do is try to check for syntax error without executing it, meaning
not creating a local variable 'hello', and avoiding coloring the group.One potential solution is here: http://troessner.wordpress.com/2011/03/15/check-ruby-code-syntax-without-executing-it/
I could call it this way: catch(:x) { eval(script) }
This will avoid creating the local variable, but the group will be colored anyways.Is there a way to just check for syntax without executing anything?
-
No sure about checking syntax without executing... but I don't understand why you are using eval()?
What is the core of the question here? You want to revert any changes your script does if it runs into errors?
<span class="syntaxdefault"><br />begin<br /> Skethup</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">active_model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">start_operation</span><span class="syntaxkeyword">(</span><span class="syntaxstring">'FooBar'</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">true</span><span class="syntaxkeyword">)<br /> </span><span class="syntaxcomment"># ... do magic stuff here ...<br /> # Any errors will be cached and aborted, reverting the<br /> # model changes.<br /> </span><span class="syntaxdefault">Skethup</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">active_model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">commit_operation<br /></span><span class="syntaxkeyword">catch<br /> </span><span class="syntaxdefault">Skethup</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">active_model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">abort_operation<br /> raise </span><span class="syntaxcomment"># Re-raise the error.<br /></span><span class="syntaxdefault">end<br /></span>
-
@thomthom said:
No sure about checking syntax without executing... but I don't understand why you are using eval()?
I'm trying to modify SP3.2, so that it handles errors well, yet I do know eval() is slow.
My question wasn't clear, I just didn't know what I was actually trying to do there. What I was asking was to check Syntax errors, which are like an if statement without an end or odd number of brackets... The eval already raises immediately, if there is a syntax error. So, anyways, I got it figured out
Thanks
-
You know you can catch
SyntaxError
specifically, right?begin eval('blah!') catch SyntaxError puts "eeek!" end
-
@thomthom said:
> begin > eval('blah!') > catch SyntaxError > puts "eeek!" > end >
Doesn't work!
catch is not like rescue or ensure. You can't write it that way.
See here what actually catch is: http://www.ruby-doc.org/core-2.0/Kernel.html#method-i-catchBut I did got it figured out.
The eval checks for the syntax errors first:begin eval('Sketchup.active_model.entities[0].material = 'green'; if true then') rescue puts 'Error' end
In the result it will puts 'Error', without setting group's material green because eval checks for the correct syntax first, and then executes the script.
-
@anton_s said:
But I did got it figured out.
The eval checks for the syntax errors first:begin > eval('Sketchup.active_model.entities[0].material = 'green'; if true then') > rescue > puts 'Error' > end
In the result it will puts 'Error', without setting group's material green because eval checks for the correct syntax first, and then executes the script.
That is probably going the be the best available approach (an AST would be nicer, but there isn't one available in SketchUps Ruby).
def self.get_syntax_errors(code) code = "begin\n#{code}\nend if false" begin eval(code) rescue SyntaxError => e return e end return nil end
Note that depending how you use this, you could introduce a security flaw. Imagine if I sent the following code to
get_syntax_errors
require 'fileutils' FileUtils.rm_rf('/') end begin puts "This will not run!
And then put the file up for download somewhere
-
Thanks Danial, that's some nice script, you have
Adding begin #{code} end if false does the trick -
catch was supposed to be rescue in my snippet. I was language confused. :s
Advertisement