Sha256: b808b5b712ecf1ed13f56b3116c963194286d320c44000e4d083eebf442d05bc

Contents?: true

Size: 1.77 KB

Versions: 3

Compression:

Stored size: 1.77 KB

Contents

module Weskit::WML::Parsers
  class Simple
    attr_accessor :result

    def initialize data
      @data = data.to_s
    end

    def parse
      root    = Weskit::WML::Root.new
      stack   = []
      current = root

      lines.each do |line|
        line.strip!

        if attribute? line
          current << Weskit::WML::Attribute.new(*current_attribute)

        elsif amending_tag? line
          stack.push current
          current = Weskit::WML::Element.new(current_tag, :amendment => true)

        elsif closing_tag? line
          if current.name != current_tag
            raise Weskit::WML::Errors::ParseError
          end
          stack.last << current
          current = stack.pop

        elsif opening_tag? line
          stack.push current
          current = Weskit::WML::Element.new(current_tag)

        end
      end

      @result = root.empty? ? nil : root
    end

    private

    def lines
      @data.lines.collect &:strip
    end

    def attribute? line
      !!(@current_match = line.match attribute)
    end

    def tag? line, prefix
      !!(@current_match = line.match tag(prefix))
    end

    def amending_tag? line
      tag? line, '\+'
    end

    def closing_tag? line
      tag? line, '\/'
    end

    def opening_tag? line
      tag? line, ''
    end

    def data_rule
      '(.+)'
    end

    def id_rule
      '([a-z][0-9a-z_]*)'
    end

    def attribute
      /#{id_rule}\s*=\s*_?\s*#{data_rule}/i
    end

    def tag prefix
      /\[#{prefix}#{id_rule}\]/i
    end

    def current_attribute
      tmp = @current_match[1..2].collect &:strip

      tmp[0] = tmp.first.to_sym

      tmp.last.gsub! /^"|"$/,  ''
      tmp.last.gsub! '""', '"'
      tmp.last.strip!

      tmp
    end

    def current_tag
      @current_match[1].to_sym
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
weskit-0.3.2 lib/weskit/wml/parsers/simple.rb
weskit-0.3.1 lib/weskit/wml/parsers/simple.rb
weskit-0.3.0 lib/weskit/wml/parsers/simple.rb