Module: Ripl::MultiLine::RubyParser

Defined in:
lib/ripl/multi_line/ruby_parser.rb

Overview

# # # This multi-line implementation uses IRB’s RubyLex parser

works on:         1.8
analyze features: [:literal, :string]
                  [:literal, :hash]
                  [:statement]
                  [:forced]
notes:            statement could also be [

Constant Summary collapse

VERSION =
'0.1.0'
ERROR_MESSAGES =
[
  [[:literal, :string], /unterminated string meets end of file/],
  # [[:literal, :regexp], /unterminated regexp meets end of file/],          #-> string
  # [[:literal, :array],  /syntax error, unexpected \$end, expecting '\]'/], #-> ParseError
  [[:literal, :hash],   /syntax error, unexpected \$end, expecting '\}'/],   # {45
]

Instance Method Summary collapse

Instance Method Details

#multiline?(string) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/ripl/multi_line/ruby_parser.rb', line 25

def multiline?(string)
  return [:forced] if string =~ /;\s*\Z/ # force multi line with ;
  ::RubyParser.new.parse(string)
  false # string was parsable, no multi-line
rescue ::Racc::ParseError
  [:statement]
rescue SyntaxError => e
  ERROR_MESSAGES.each{ |type, message|
    return type if message === e.message
  }
  false # syntax error not multi-line relevant
end