Sha256: 2f623628ec95051e5c232d4d7b160cded76436f7b50229916bed4f8b9f74a964

Contents?: true

Size: 1.78 KB

Versions: 28

Compression:

Stored size: 1.78 KB

Contents

module OrigenAppGenerators
  require 'strscan'
  # Responsible for parsing something like this:
  #
  #   "ram, osc, pll, atd(2), comms[ram(2), osc](3)"
  #
  # into this:
  #
  #   {
  #     "RAM"=>{}, "Osc"=>{}, "PLL"=>{}, "ATD"=> {:instances=>2},
  #     "Comms"=>{:instances=>3, :children=>{"RAM"=>{:instances=>2}, "Osc"=>{}}}
  #   }
  #
  class SubBlockParser
    def parse(str)
      r = {}
      split(str).each do |tag|
        tag, i = extract_instances(tag)
        name, children = extract_children(tag)
        name = camelize(name)
        r[name] = {}
        r[name][:instances] = i if i
        r[name][:children] = children if children
      end
      r
    end

    private

    # Splits the given string by comma, but understands that nested
    # commas should not be split on
    def split(str)
      r = []
      str = StringScanner.new(str)
      r << next_tag(str) until str.eos?
      r
    end

    def next_tag(str)
      v = str.scan_until(/,|\[|$/)
      if v[-1] == ','
        v.chop.strip
      elsif v[-1] == '['
        open = 1
        while open > 0
          v += str.scan_until(/\[|\]/)
          if v[-1] == '['
            open += 1
          else
            open -= 1
          end
        end
        v += next_tag(str)
      # End of line
      else
        v.strip
      end
    end

    def extract_children(tag)
      # http://rubular.com/r/plGILY2e2U
      if tag.strip =~ /([^\[]*)\[(.*)\]/
        [Regexp.last_match(1), parse(Regexp.last_match(2))]
      else
        [tag.strip, nil]
      end
    end

    def extract_instances(tag)
      if tag.strip =~ /(.*)\((\d+)\)$/
        [Regexp.last_match(1), Regexp.last_match(2).to_i]
      else
        [tag.strip, nil]
      end
    end

    def camelize(val)
      val.strip.gsub(/\s+/, '_').camelize
    end
  end
end

Version data entries

28 entries across 28 versions & 2 rubygems

Version Path
origen-0.44.0 origen_app_generators/lib/origen_app_generators/sub_block_parser.rb
origen-0.43.0 origen_app_generators/lib/origen_app_generators/sub_block_parser.rb
origen-0.42.2 origen_app_generators/lib/origen_app_generators/sub_block_parser.rb
origen-0.42.1 origen_app_generators/lib/origen_app_generators/sub_block_parser.rb
origen-0.42.0 origen_app_generators/lib/origen_app_generators/sub_block_parser.rb
origen-0.41.2 origen_app_generators/lib/origen_app_generators/sub_block_parser.rb
origen-0.41.1 origen_app_generators/lib/origen_app_generators/sub_block_parser.rb
origen-0.41.0 origen_app_generators/lib/origen_app_generators/sub_block_parser.rb
origen-0.40.2 origen_app_generators/lib/origen_app_generators/sub_block_parser.rb
origen-0.40.1 origen_app_generators/lib/origen_app_generators/sub_block_parser.rb
origen-0.40.0 origen_app_generators/lib/origen_app_generators/sub_block_parser.rb
origen_app_generators-1.2.0 lib/origen_app_generators/sub_block_parser.rb
origen_app_generators-1.1.4 lib/origen_app_generators/sub_block_parser.rb
origen_app_generators-1.1.3 lib/origen_app_generators/sub_block_parser.rb
origen_app_generators-1.1.2 lib/origen_app_generators/sub_block_parser.rb
origen_app_generators-1.1.1 lib/origen_app_generators/sub_block_parser.rb
origen_app_generators-1.1.0 lib/origen_app_generators/sub_block_parser.rb
origen_app_generators-1.0.1 lib/origen_app_generators/sub_block_parser.rb
origen_app_generators-1.0.0 lib/origen_app_generators/sub_block_parser.rb
origen_app_generators-0.4.0 lib/origen_app_generators/sub_block_parser.rb