Forum: JRuby Question regarding JRubyParser.parse and org.jrubyparser.lexer.SyntaxException

Posted by Roger Gilliar (Guest)
on 2012-07-25 20:20
(Received via mailing list)
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
Posted by Thomas E Enebo (Guest)
on 2012-07-26 19:07
(Received via mailing list)
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 Gilliar <roger@gilliar.de> wrote:
>
> rescue NativeException => e
>     http://xircles.codehaus.org/manage_email
>
>



--
blog: http://blog.enebo.com       twitter: tom_enebo
mail: tom.enebo@gmail.com
Posted by Thomas E Enebo (Guest)
on 2012-07-26 19:08
(Received via mailing list)
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 <tom.enebo@gmail.com> 
wrote:
>                 begin
>
>>
>>                 end
>>
> blog: http://blog.enebo.com       twitter: tom_enebo
> mail: tom.enebo@gmail.com



--
blog: http://blog.enebo.com       twitter: tom_enebo
mail: tom.enebo@gmail.com
Posted by Roger Gilliar (Guest)
on 2012-07-26 21:05
(Received via mailing list)
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 <tom.enebo@gmail.com>:
Posted by Paul O'Keeffe (paul_o)
on 2012-08-21 11:40
Roger Gilliar 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 <tom.enebo@gmail.com>:

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.
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.