Sha256: fdeebf9444a77e3aef8094b0a309bc238b97d9ce4c95227f80bdba701c2a1c85

Contents?: true

Size: 921 Bytes

Versions: 2

Compression:

Stored size: 921 Bytes

Contents

module Gisele
  module Language
    class Transformer
      include AST::Helpers

      def call(node)
        node = pre_call(node)
        meth = :"on_#{node.rule_name}"
        meth = :"on_missing" unless respond_to?(meth)
        post_call send(meth, node)
      end

      def on_missing(node)
        raise UnexpectedNodeError, "Unexpected node: #{node.rule_name}"
      end

      protected

      def pre_call(node)
        unless looks_a_node?(node)
          raise ArgumentError, "AST node expected, got #{node.inspect}", caller
        end
        node(node)
      end

      def post_call(transformed)
        looks_a_node?(transformed) ? node(transformed) : transformed
      end

      def copy_and_applyall(node)
        node.copy do |memo,child|
          memo << (child.is_a?(AST::Node) ? call(child) : child)
        end
      end

    end # class Transformer
  end # module Language
end # module Gisele

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
gisele-0.2.0 lib/gisele/language/transformer.rb
gisele-0.1.0 lib/gisele/language/transformer.rb