Class: Grammar::Ruby::Code::Blocked

Inherits:
List show all
Defined in:
lib/grammar/ruby/code.rb

Constant Summary

Constants inherited from Grammar::Ruby::Code

False, FalseTerminator, Implicit, Nil, Self, True

Instance Method Summary collapse

Methods inherited from List

#_always, #_never, #_operator, #_operator0, #_paren

Methods inherited from Grammar::Ruby::Code

[], #_always, #_and, #_assign, #_classname, #_code_self, #_data, #_def, #_dot, #_else, #_ensure, #_equal, #_immediate, #_inspect, #_mid, #_never, #_not, #_or, #_question, #_rand, #_rcond, #_rescue, #_ror, #_rstep, #_splat, #_step, #_to_block, #_unless, #_until, #_while, #method_missing

Constructor Details

#initialize(receiver, n = nil, &block) ⇒ Blocked

:yield: *vars



779
780
781
782
783
784
785
786
787
788
789
790
791
792
# File 'lib/grammar/ruby/code.rb', line 779

def initialize(receiver, n=nil, &block) # :yield: *vars
    @receiver = receiver
    n ||= block.arity
    names = (0...n.abs).map { "_" }
    @operands = names.map { |name| Code::Primary.new(name) }
    @block = yield(*@operands)
    @locals = @block._locals(0)
    @operands[-1] = @operands[-1]._splat if n<0
    names.each { |name|
        name.concat(@locals.to_s)
        @locals += 1
    }
    @locals = @receiver._locals(@locals)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Grammar::Ruby::Code

Instance Method Details

#_codesObject



793
794
795
# File 'lib/grammar/ruby/code.rb', line 793

def _codes
    [@receiver].concat(@operands) << @block
end

#_lines(text, indent = 0, op = nil, maxlen = 72) ⇒ Object



796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
# File 'lib/grammar/ruby/code.rb', line 796

def _lines(text, indent=0, op=nil, maxlen=72)
    single = @receiver._lines(text, indent, true, maxlen)
    text.last << ?\s << ?\{
    unless @operands.empty?
        text << (indent+2) << "|"
        if super(text, indent+3, nil, maxlen) && single && 
                text[-4]+text[-3].size+text.last.size+1<=maxlen
            last = text.last
            text.pop
            text.pop
            (text.last << ?\s).concat(last)
        else
            single = nil
        end
        text.last << ?\|
    end
    text << (indent+2) << ""
    if @block._lines(text, indent+2, nil, maxlen) && single &&
            text[-4]+text[-3].size+text.last.size+1<=maxlen
        last = text.last
        text.pop
        text.pop
        (text.last << ?\s).concat(last).concat(" }")
    else
        text << indent << "}"
        nil
    end
end

#_locals(n = 0) ⇒ Object



824
825
826
# File 'lib/grammar/ruby/code.rb', line 824

def _locals(n=0)
    (@locals>n) ? @locals : n
end