From: jrafanie@gmail.com
To: ruby-core@ruby-lang.org
Subject: [ruby-core:72472] [Ruby trunk - Feature #11868] Proposal for RubyVM::InstructionSequence.compile to return an object containing the syntax error information currently written to STDERR
Date: Thu, 24 Dec 2015 13:05:59 +0000 [thread overview]
Message-ID: <redmine.journal-55759.20151224130559.26c1f629f793ea39@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-11868.20151223201208@ruby-lang.org
Issue #11868 has been updated by Joe Rafaniello.
Nobuyoshi Nakada wrote:
> What's `hint`?
>
> https://github.com/ruby/ruby/compare/trunk...nobu:feature/11868-SyntaxError-location
Thank you for starting on this already. I appreciate it.
The hint/location suggestion is what ruby prints indicating where the syntax error occurred.
Sorry, hint is probably the wrong word.
In the example below:
~~~~
<compiled>:4: syntax error, unexpected keyword_not, expecting keyword_do_LAMBDA or tLAMBEG
this.is -> not -> valid $ruby:syntax
^
<compiled>:4: syntax error, unexpected tGVAR, expecting keyword_do_LAMBDA or tLAMBEG
this.is -> not -> valid $ruby:syntax
^
~~~~
There are two syntax errors.
Error 1
line number: 4
Error:
unexpected keyword_not, expecting keyword_do_LAMBDA or tLAMBEG
The "hint" or location is:
~~~~
this.is -> not -> valid $ruby:syntax
^
~~~~
Error 2
line number: 4
Error:
unexpected keyword_not, expecting keyword_do_LAMBDA or tLAMBEG
The "hint" or location is:
~~~~
this.is -> not -> valid $ruby:syntax
^
~~~~
Thank you!
----------------------------------------
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-55759
* 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")
# => <RubyVM::InstructionSequence:<compiled>@<compiled>>
~~~
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:
<compiled>:4: syntax error, unexpected keyword_not, expecting keyword_do_LAMBDA or tLAMBEG
this.is -> not -> valid $ruby:syntax
^
<compiled>: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/
next prev parent reply other threads:[~2015-12-24 12:33 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <redmine.issue-11868.20151223201208@ruby-lang.org>
2015-12-23 20:12 ` [ruby-core:72458] [Ruby trunk - Feature #11868] [Open] Proposal for RubyVM::InstructionSequence.compile to return an object containing the syntax error information currently written to STDERR jrafanie
2015-12-23 20:24 ` [ruby-core:72459] [Ruby trunk - Feature #11868] " jrafanie
2015-12-24 0:38 ` [ruby-core:72461] " nobu
2015-12-24 3:06 ` [ruby-core:72464] " jrafanie
2015-12-24 9:52 ` [ruby-core:72470] " nobu
2015-12-24 13:05 ` jrafanie [this message]
2016-02-01 21:28 ` [ruby-core:73636] [Ruby trunk Feature#11868]Proposal " jrafanie
2016-02-02 0:06 ` [ruby-core:73640] " sawadatsuyoshi
2016-02-02 14:56 ` [ruby-core:73654] " jrafanie
2016-04-21 5:49 ` [ruby-core:75068] [Ruby trunk Feature#11868] Proposal " nobu
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-list from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.ruby-lang.org/en/community/mailing-lists/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=redmine.journal-55759.20151224130559.26c1f629f793ea39@ruby-lang.org \
--to=ruby-core@ruby-lang.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).