Question regarding JRubyParser.parse and org.jrubyparser.lexer.SyntaxException

How would I retrieve the line number of the syntax error in the rescue
clause ?

Regards
Roger

Example

require “jruby-parser”

begin

root = JRubyParser.parse("begin
b = foo(1)
x = 'l
begin
x
resc
end
x = 12
end ", opts = {:version => JRubyParser::Compat::RUBY1_9, :filename
=> ‘name.rb’})

rescue NativeException => e

puts e

end

You can catch the SyntaxException itself.

-Tom

require “jruby-parser”

begin
root = JRubyParser.parse("begin
b = foo(1)
x = 'l
begin
x
resc
end
x = 12
end ", opts = {:version =>
JRubyParser::Compat::RUBY1_9, :filename => ‘name.rb’})

rescue org.jrubyparser.lexer.SyntaxException => e

    puts e

end

On Wed, Jul 25, 2012 at 1:18 PM, Roger G. [email protected] wrote:

rescue NativeException => e
http://xircles.codehaus.org/manage_email


blog: http://blog.enebo.com twitter: tom_enebo
mail: [email protected]

Sorry. I missed the important bit:

rescue org.jrubyparser.lexer.SyntaxException => e

    puts e.position.start_line, e.position.file

end

-Tom

On Thu, Jul 26, 2012 at 12:06 PM, Thomas E Enebo [email protected]
wrote:

            begin
            end

blog: http://blog.enebo.com twitter: tom_enebo
mail: [email protected]


blog: http://blog.enebo.com twitter: tom_enebo
mail: [email protected]

Thanks for the reply,

but now I get:

NoMethodError: undefined method `position’ for
#NativeException:0x93f95d6
(root) at jtest.rb:5

Regards
Roger

Am 26.07.2012 um 19:08 schrieb Thomas E Enebo [email protected]:

Roger G. wrote in post #1070301:

Thanks for the reply,

but now I get:

NoMethodError: undefined method `position’ for
#NativeException:0x93f95d6
(root) at jtest.rb:5

Regards
Roger

Am 26.07.2012 um 19:08 schrieb Thomas E Enebo [email protected]:

From my own experiences, I’ve found that in your rescue block, e is a
NativeException and its cause is a Ruby class which proxies for the
underlying Java org.jrubyparser.lexer.SyntaxException class. So you
need:

rescue org.jrubyparser.lexer.SyntaxException => e
puts e.cause.position.start_line, e.cause.position.file
end

Far from obvious behaviour. I had to go digging around in the JRuby
source code to figure this out.

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