class Imagen::AST::Parser

Public Class Methods

new(parser_version = Imagen.parser_version) click to toggle source

@param parser_version [String] ruby syntax version

# File lib/imagen/ast/parser.rb, line 34
def initialize(parser_version = Imagen.parser_version)
  validate_version(parser_version)

  require "parser/#{parser_version}"

  required_const = if parser_version == 'current'
                     'CurrentRuby'
                   else
                     parser_version.capitalize
                   end
  @parser_klass = ::Parser.const_get(required_const)
end
parse(input, file = '(string)') click to toggle source
# File lib/imagen/ast/parser.rb, line 29
def self.parse(input, file = '(string)')
  new.parse(input, file)
end
parse_file(filename) click to toggle source
# File lib/imagen/ast/parser.rb, line 25
def self.parse_file(filename)
  new.parse_file(filename)
end

Public Instance Methods

parse(input, file = '(string)') click to toggle source
# File lib/imagen/ast/parser.rb, line 51
def parse(input, file = '(string)')
  buffer = ::Parser::Source::Buffer.new(file)
  buffer.source = input
  parser.parse(buffer)
end
parse_file(filename) click to toggle source
# File lib/imagen/ast/parser.rb, line 47
def parse_file(filename)
  parse(File.read(filename), filename)
end
parser() click to toggle source
# File lib/imagen/ast/parser.rb, line 57
def parser
  @parser_klass.new(AST::Builder.new).tap do |parser|
    diagnostics = parser.diagnostics
    diagnostics.all_errors_are_fatal = true
    diagnostics.ignore_warnings = true
  end
end

Private Instance Methods

validate_version(parser_version) click to toggle source
# File lib/imagen/ast/parser.rb, line 67
def validate_version(parser_version)
  return if AVAILABLE_RUBY_VERSIONS.include?(parser_version)

  raise ArgumentError, "#{parser_version} is not supported by imagen"
end