class Flor::Parser::Nod

Attributes

children[R]
indent[RW]
parent[RW]
type[R]

Public Class Methods

new(tree, outdent) click to toggle source
# File lib/flor/parser.rb, line 503
def initialize(tree, outdent)

  @parent = nil
  @indent = -1
  @head = 'sequence'
  @children = []
  @line = 0

  @outdent = outdent ? outdent.strip : nil
  @outdent = nil if @outdent && @outdent.size < 1

  read(tree) if tree
end

Public Instance Methods

append(node) click to toggle source
# File lib/flor/parser.rb, line 517
def append(node)

  if @outdent
    if @outdent.index('\\')
      node.indent = self.indent + 2
    elsif @outdent.index('|') || @outdent.index(';')
      node.indent = self.indent
    end
    @outdent = nil
  end

  if node.indent > self.indent
    @children << node
    node.parent = self
  else
    @parent.append(node)
  end
end
to_a() click to toggle source
# File lib/flor/parser.rb, line 536
def to_a

  return [ @head, @children, @line ] unless @children.is_a?(Array)
  return @head if @head.is_a?(Array) && @children.empty?

  cn = @children.collect(&:to_a)

  as, non_atts = cn.partition { |c| c[0] == '_att' }
  atts, suff = [], nil

  as.each do |c|

    if %w[ if unless ].include?(c[1])
      suff = []
    elsif suff && c[1].length > 1
      iou = suff.shift; iou[1] = [ [ iou[1], [], iou[2] ] ]
      atts << iou
      atts.concat(suff)
      suff = nil
    end

    (suff || atts) << c
  end

  atts, non_atts = ta_rework_arr_or_obj(atts, non_atts)

  core = [ @head, atts + non_atts, @line ]
  core = core[0] if core[0].is_a?(Array) && core[1].empty?
  core = ta_rework_core(core) if core[0].is_a?(Array)

  return core unless suff

  ta_rework_suff(core, suff)
end

Protected Instance Methods

read(tree) click to toggle source
# File lib/flor/parser.rb, line 606
      def read(tree)

        @tree = tree
#puts "-" * 80
#Raabro.pp(tree, colours: true)

        @indent = tree.lookup(:indent).string.length

        ht = tree.lookup(:head)
        @line = Flor::Parser.line_number(ht)

        @head = Flor::Parser.rewrite(ht.c0)
        @head = @head[0] if @head[0].is_a?(String) && @head[1] == []

        atts = tree.children[2..-1]
          .inject([]) { |as, ct|

            ct = ct.lookup(nil)

            if ct.name == :iou

              as.push([ '_att', ct.string, @line ])

            else

              kt = ct.children.size == 3 ? ct.children[1].lookup(:key) : nil
              v = Flor::Parser.rewrite(ct.clast)

              if kt
                k = Flor::Parser.rewrite(kt.c0)
                as.push([ '_att', [ k, v ], k[2] ])
              else
                as.push([ '_att', [ v ], v[2] ])
              end
            end }

        @children.concat(atts)

        rework_subtraction if @head == '-'
        rework_addition if @head == '+' || @head == '-'
      end
rework_addition() click to toggle source
# File lib/flor/parser.rb, line 667
def rework_addition

  katts, atts, children = @children
    .inject([ [], [], [] ]) { |cn, ct|
      if ct[0] == '_att'
        cn[ct[1].size == 2 ? 0 : 1] << ct
      else
        cn[2] << ct
      end
      cn }

  @children =
    katts + atts.collect { |ct| ct[1].first } + children
end
rework_subtraction() click to toggle source
# File lib/flor/parser.rb, line 648
def rework_subtraction

  return unless @children.size == 1

  c = @children.first
  return unless c[0] == '_att' && c[1].size == 1

  c = c[1].first

  if c[0] == '_num'
    @head = '_num'
    @children = - c[1]
  elsif %w[ - + ].include?(c[0])
    @head = c[0]
    @children = c[1]
    @children[0] = Flor::Parser.invert('+', @children[0])
  end
end
ta_rework_arr_or_obj(atts, non_atts) click to toggle source
# File lib/flor/parser.rb, line 581
def ta_rework_arr_or_obj(atts, non_atts)

  return [ atts, non_atts ] unless (
    @head.is_a?(Array) &&
    non_atts.empty? &&
    %w[ _arr _obj ].include?(@head[0]))

  cn = @head[1] + atts + non_atts
  @head = @head[0]

  cn.partition { |c| c[0] == '_att' }
end
ta_rework_core(core) click to toggle source
# File lib/flor/parser.rb, line 594
def ta_rework_core(core)

  hd, cn, ln = core
  s = @tree.lookup(:head).string.strip

  [ '_head', [
    [ '_sqs', s, ln ],
    hd,
    [ '__head', cn, ln ]
  ], ln ]
end
ta_rework_suff(core, suff) click to toggle source
# File lib/flor/parser.rb, line 573
def ta_rework_suff(core, suff)

  cond = suff[1][1][0]
  suff[2..-1].each { |ct| cond[1] << ct }

  [ '_' + suff.shift[1], [ cond, core ], @line ]
end