Class: Depager::ASTBuilderExtension

Inherits:
Extension
  • Object
show all
Defined in:
lib/depager/plugins/ast.rb,
lib/depager/plugins/ast.rb

Defined Under Namespace

Modules: PostRhs1, PreRuleList0

Constant Summary collapse

BASE_NODE_TEMPLATE =
<<-'CODE'
  class Node
    attr_accessor :lineno
    attr_accessor <%= _opt_attr.map{|i| ":#{i}" }.join(', ') %>
    <%= ini %>

    def self.[] lineno, *args
      self.new lineno, *args
    end

    def accept v
    end
  end

  class NodeList < Node
    attr_accessor :lineno
    def initialize(lineno, lst=[])
      @lineno = lineno
      @lst = lst.to_a.select{|i| ! i.is_a?(NilNode)}
    end

    <%- _opt_attr.each do |i| -%>
    def all_<%= i %>
      @lst.map{|i| i.<%= i %>}
    end
    <%- end -%>

    def size
      @lst.size
    end
    alias length size

    def push(i)
      @lst.push i unless i.is_a? NilNode
      self
    end

    def concat(i)
      @lst.concat i
      self
    end

    def to_ary()
      @lst
    end

    alias to_a to_ary
    alias list to_ary

    def accept(v)
      @lst.each{|i| i.accept(v) }
    end
  end

  class NilNode
    attr_accessor :lineno
    attr_accessor <%= _opt_attr.map{|i| ":#{i}" }.join(', ') %>

    def initialize(lineno, *args)
      @lineno = lineno
    end

    def accept v
    end
  end
CODE
NODE_TEMPLATE =
<<-'CODE'
  class Node_<%= name %> < Node
    attr_accessor <%= nodes.map{|i| ":#{i}" }.join(', ') %>
    attr_accessor <%= attrs.map{|i| ":#{i}" }.join(', ') %>

    def initialize <%= ['lineno', *nodes].join(', ') %>
      super()
      @lineno = lineno
      <%= nodes.inject(''){|r,i| r << "    @#{i} = #{i}\n"} %>
    end

    def accept v
      warn @lineno.to_s+':'+self.class.to_s if $DEBUG
      <%= accept %>
      v.visit_Node_<%= name %>(self)
      self
    end

    def attributes
      { <%= (nodes + attrs).uniq.map{|i| "#{i}: #{i}" }.join(', ') %>, lineno: lineno }
    end
  end
CODE
VISITOR_TEMPLATE =
<<-'CODE'
  class Visitor<%= name ? '_' + name : '' %>
    def visit node
      node.accept(self)
    end
    <%= body.join %>
  end # Visitor<%= name ? '_' + name : '' %>
CODE

Instance Attribute Summary

Attributes inherited from Extension

#d_parser, #g_parser

Instance Method Summary collapse

Methods inherited from Extension

#decorator_name, #init_extension, #term_extension

Methods included from Utils::CodeGeneratorMethods

#expand_inline_code, #generate_action_decorator_code, #generate_decorator_code, #parse_block

Methods included from Utils::CommonMethods

#error_exit, #error_message, #expanded_code_delimiter, #file, #full_target_name, #input_path, #inspect, #target_name, #target_namespace, #warning

Constructor Details

#initializeASTBuilderExtension

Returns a new instance of ASTBuilderExtension.



11
12
13
14
15
# File 'lib/depager/plugins/ast.rb', line 11

def initialize
  super
  @master = self
  @slaves = []
end

Instance Method Details

#extension_registered(g_parser) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/depager/plugins/ast.rb', line 17

def extension_registered g_parser
  super g_parser

  @slaves << Depager::ASTBuilderExtension::PreRuleList0::Parser.new(g_parser, self)
  g_parser.hooks[:pre_rule_list].push [@slaves.last, :do_parse]
  @slaves << Depager::ASTBuilderExtension::PostRhs1::Parser.new(g_parser, self)
  g_parser.hooks[:post_rhs].push [@slaves.last, :do_parse]
end