Sha256: ae51a206fbccdcd4c568b88f7c1e1656ff32a70ee312def280a4de484861f573

Contents?: true

Size: 1.09 KB

Versions: 3

Compression:

Stored size: 1.09 KB

Contents

module Gisele
  module Language
    module AST
      module Helpers

        def ast(arg, markers = {})
          return node(arg, markers) if looks_a_node?(arg)
          ast(Syntax.ast(arg), markers)
        end

        def node(arg, markers = {})
          return arg if arg.is_a?(Node)
          unless looks_a_node?(arg)
            raise ArgumentError, "Array expected, #{arg.inspect} found."
          end
          extend_node(arg, markers).tap do |node|
            node.children.each{|c| node(c) if looks_a_node?(c)}
          end
        end

        private

        def looks_a_node?(arg)
          arg.is_a?(Node) or (arg.is_a?(Array) and arg.first.is_a?(Symbol))
        end

        def extend_node(arg, markers)
          mod = ast_module(arg)
          arg.extend(mod).tap do |node|
            node.markers = markers
          end
        end

        def ast_module(node)
          modname = Language.rule2mod(node.first)
          AST.const_get(modname) rescue Node
        end

        extend(self)
      end # module Helpers
    end # module AST
  end # module Language
end # module Gisele

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
gisele-0.4.0 lib/gisele/language/ast/helpers.rb
gisele-0.3.0 lib/gisele/language/ast/helpers.rb
gisele-0.2.0 lib/gisele/language/ast/helpers.rb