From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: poffice@blade.nagaokaut.ac.jp Delivered-To: poffice@blade.nagaokaut.ac.jp Received: from kankan.nagaokaut.ac.jp (kankan.nagaokaut.ac.jp [133.44.2.24]) by blade.nagaokaut.ac.jp (Postfix) with ESMTP id 3AC4D17DC6F9 for ; Thu, 21 Apr 2016 14:11:27 +0900 (JST) Received: from voscc.nagaokaut.ac.jp (voscc.nagaokaut.ac.jp [133.44.1.100]) by kankan.nagaokaut.ac.jp (Postfix) with ESMTP id C5EE2B5D87F for ; Thu, 21 Apr 2016 14:49:19 +0900 (JST) Received: from neon.ruby-lang.org (neon.ruby-lang.org [221.186.184.75]) by voscc.nagaokaut.ac.jp (Postfix) with ESMTP id 3F93A18CC7EA for ; Thu, 21 Apr 2016 14:49:20 +0900 (JST) Received: from [221.186.184.76] (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id B0A2812047B; Thu, 21 Apr 2016 14:49:18 +0900 (JST) X-Original-To: ruby-core@ruby-lang.org Delivered-To: ruby-core@ruby-lang.org Received: from o10.shared.sendgrid.net (o10.shared.sendgrid.net [173.193.132.135]) by neon.ruby-lang.org (Postfix) with ESMTPS id 83200120467 for ; Thu, 21 Apr 2016 14:49:15 +0900 (JST) DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sendgrid.me; h=from:to:references:subject:mime-version:content-type:content-transfer-encoding:list-id; s=smtpapi; bh=efQGdwe+B1Bj+sWALdVDNDcKKyI=; b=EbRIZPpyTOxUmkYUrF VEfeXUHGjnvn3dQUM7v2y6CpFDV/DN0ZqqS5Kj2atJG0wgKaSYGFZYXdXNShe6NT HUT/ECe+U+NFWb1N1a+/imppE0gm70KRL9RoYtnQGpEci5PooOA24LVqN6tkCT5r gCT7Ls1HzV9humOfVKtjG4G/8= Received: by filter0449p1mdw1.sendgrid.net with SMTP id filter0449p1mdw1.1595.571869D335 2016-04-21 05:49:07.619626909 +0000 UTC Received: from herokuapp.com (ec2-50-16-116-64.compute-1.amazonaws.com [50.16.116.64]) by ismtpd0001p1iad1.sendgrid.net (SG) with ESMTP id ZOdfdH-iTO-xc-y_l4FLyQ Thu, 21 Apr 2016 05:49:08.299 +0000 (UTC) Date: Thu, 21 Apr 2016 05:49:08 +0000 From: nobu@ruby-lang.org To: ruby-core@ruby-lang.org Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 49722 X-Redmine-Project: ruby-trunk X-Redmine-Issue-Id: 11868 X-Redmine-Issue-Author: jrafanie X-Redmine-Sender: nobu X-Mailer: Redmine X-Redmine-Host: bugs.ruby-lang.org X-Redmine-Site: Ruby Issue Tracking System X-Auto-Response-Suppress: All Auto-Submitted: auto-generated X-SG-EID: ync6xU2WACa70kv/Ymy4QrNMhiuLXJG8OTL2vJD1yS7n5piuoEsDnyxO+MaD4HClKEhvteUr0oFsu5 8DxoO8Eo4SayYssfEBoXUBSp2op7awyuNHrEXc1UBehjjcbEoWbPnyBY1WUDVFAckEYSx1n2/8mJJy gYbmVyjWG3QRmzEOrx5dyMRwYlJRT/C3aIjDJiRJq7pLDUiTbpPCIpGv3w== X-SendGrid-Contentd-ID: {"test_id":"1461217747"} X-ML-Name: ruby-core X-Mail-Count: 75068 Subject: [ruby-core:75068] [Ruby trunk Feature#11868] Proposal for RubyVM::InstructionSequence.compile to return an object containing the syntax error information currently written to STDERR X-BeenThere: ruby-core@ruby-lang.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: Ruby developers List-Id: Ruby developers List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ruby-core-bounces@ruby-lang.org Sender: "ruby-core" Issue #11868 has been updated by Nobuyoshi Nakada. Updated. ``` $ ./ruby -e 'begin eval("this.is -> not -> valid $ruby:syntax"); rescue SyntaxError => e; e.failures.each {|ex|p [ex.lineno, ex.column, ex.mesg]}; end' [1, 14, "syntax error, unexpected keyword_not, expecting keyword_do_LAMBDA or tLAMBEG\nthis.is -> not -> valid $ruby:syntax\n ^"] [1, 29, "syntax error, unexpected tGVAR, expecting keyword_do_LAMBDA or tLAMBEG\nthis.is -> not -> valid $ruby:syntax\n ^"] ``` ---------------------------------------- Feature #11868: Proposal for RubyVM::InstructionSequence.compile to return an object containing the syntax error information currently written to STDERR https://bugs.ruby-lang.org/issues/11868#change-58192 * Author: Joe Rafaniello * Status: Open * Priority: Normal * Assignee: ---------------------------------------- Currently, RubyVM::InstructionSequence.compile or RubyVM::InstructionSequence.new return a new InstructionSequence for valid ruby. For invalid syntax, a SyntaxError is raised with a message of 'compile error'. Meanwhile, the useful information, line number(s) and hint(s) to the invalid syntax location, is printed on standard error. I am proposing this information be returned as an object in the event of a SyntaxError. For example, here's good syntax: ~~~ RubyVM::InstructionSequence.new("x =1") # => @> ~~~ Here's bad syntax: ~~~ RubyVM::InstructionSequence.new("puts 'hi'\n puts 'hi2'\n\nthis.is -> not -> valid $ruby:syntax") # => SyntaxError: compile error # The useful hint and line number(s) are on standard error: :4: syntax error, unexpected keyword_not, expecting keyword_do_LAMBDA or tLAMBEG this.is -> not -> valid $ruby:syntax ^ :4: syntax error, unexpected tGVAR, expecting keyword_do_LAMBDA or tLAMBEG this.is -> not -> valid $ruby:syntax ^ ~~~ Some ideas: 1. Add methods to all SyntaxError exceptions to get all parse failures. For example: `syntax_error.parse_failures.each {|f| puts f.lineno; puts f.hint }`. In the above example, it failed on line 4 twice and we see two "hints." 2. Create a new method to RubyVM::InstructionSequence to check ruby syntax that would allow us to see if the syntax is valid and if not, the lineno and 'hint' for each parse failure. Use case: Rubocop[a] and other utilities[b] are really complicated and check for valid ruby syntax by creating a process to run ruby -wc with the script. [a] https://github.com/bbatsov/rubocop/blob/86e1acf67794bf6dd5d65812b91df475e44fa320/spec/support/mri_syntax_checker.rb#L51-L63 [b] https://github.com/ManageIQ/manageiq/blob/6725fe52222c07d576a18126d2ff825ddc6dffd0/gems/pending/util/miq-syntax-checker.rb#L8-L13 It would be nice to remove all of this complexity and use RubyVM::InstructionSequence, which already has the information we need but in a more user friendly format. Thanks! Joe Rafaniello -- https://bugs.ruby-lang.org/