Class: Resyma::Core::Converter

Inherits:
Object
  • Object
show all
Defined in:
lib/resyma/core/parsetree/converter.rb

Overview

Converter for Parser::AST::Node

Instance Method Summary collapse

Constructor Details

#initializeConverter

Returns a new instance of Converter.



11
12
13
14
# File 'lib/resyma/core/parsetree/converter.rb', line 11

def initialize
  @rules = {}
  @fallback = nil
end

Instance Method Details

#convert(ast, parent = nil, index = 0) ⇒ Resyma::Core::ParseTree

Convert a Parser::AST::Node to Resyma::Core::ParseTree

Parameters:

  • ast (Parser::AST::Node)

    An abstract syntax tree

Returns:



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/resyma/core/parsetree/converter.rb', line 48

def convert(ast, parent = nil, index = 0)
  converter = @rules[ast.type]
  if !converter.nil?
    converter.call(ast, parent, index)
  elsif !@fallback.nil?
    @fallback.call(ast, parent, index)
  else
    raise Resyma::Core::ConversionError,
          "Unable to convert AST whose type is #{ast.type}"
  end
end

#def_fallback(&cvt) ⇒ Object



37
38
39
# File 'lib/resyma/core/parsetree/converter.rb', line 37

def def_fallback(&cvt)
  @fallback = cvt
end

#def_rule(type_or_types) {|, , | ... } ⇒ nil

Define the conversion rule for AST with particular type(s)

Parameters:

  • type_or_types (Symbol, Array<Symbol>)

    Types

  • &cvt (Proc)

    Procedure taking a AST and returning a parse tree, i.e. Parser::AST::Node -> Resyma::Core::ParseTree

Yield Parameters:

Returns:

  • (nil)

    Nothing



28
29
30
31
32
33
34
35
# File 'lib/resyma/core/parsetree/converter.rb', line 28

def def_rule(type_or_types, &cvt)
  types = if type_or_types.is_a?(Symbol)
            [type_or_types]
          else
            type_or_types
          end
  types.each { |type| @rules[type] = cvt }
end