Class: Resyma::Core::Converter
- Inherits:
-
Object
- Object
- Resyma::Core::Converter
- Defined in:
- lib/resyma/core/parsetree/converter.rb
Overview
Converter for Parser::AST::Node
Instance Method Summary collapse
-
#convert(ast, parent = nil, index = 0) ⇒ Resyma::Core::ParseTree
Convert a Parser::AST::Node to Resyma::Core::ParseTree.
- #def_fallback(&cvt) ⇒ Object
-
#def_rule(type_or_types) {|, , | ... } ⇒ nil
Define the conversion rule for AST with particular type(s).
-
#initialize ⇒ Converter
constructor
A new instance of Converter.
Constructor Details
#initialize ⇒ Converter
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
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)
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 |