Feature #11868


Proposal for RubyVM::InstructionSequence.compile to return an object containing the syntax error information currently written to STDERR

Added by jrafanie (Joe Rafaniello) almost 6 years ago. Updated over 5 years ago.

Target version:


Currently, RubyVM::InstructionSequence.compile or 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:"x =1")
# => <RubyVM::InstructionSequence:<compiled>@<compiled>>

Here's bad syntax:"puts 'hi'\n puts 'hi2'\n\ -> 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 -> not -> valid $ruby:syntax
<compiled>:4: syntax error, unexpected tGVAR, expecting keyword_do_LAMBDA or tLAMBEG -> 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.

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.

Joe Rafaniello

Related issues

Related to Ruby master - Feature #11951: `RubyVM::InstructionSequence.compile` should return the error message within the raised errorClosedActions

Also available in: Atom PDF