Method: Parser::Base.parse

Defined in:
lib/parser/base.rb

.parse(string, file = '(string)', line = 1) ⇒ Parser::AST::Node

Parses a string of Ruby code and returns the AST. If the source cannot be parsed, SyntaxError is raised and a diagnostic is printed to stderr.

Examples:

Parser::Base.parse('puts "hello"')

Parameters:

  • string (String)

    The block of code to parse.

  • file (String) (defaults to: '(string)')

    The name of the file the code originated from.

  • line (Numeric) (defaults to: 1)

    The initial line number.

Returns:



33
34
35
36
37
# File 'lib/parser/base.rb', line 33

def self.parse(string, file='(string)', line=1)
  parser = default_parser
  source_buffer = setup_source_buffer(file, line, string, parser.default_encoding)
  parser.parse(source_buffer)
end