Sha256: 1633ac50734d721e2de3c27b1e3479d76a951fc9a46659e808a403fec5ff104b

Contents?: true

Size: 1.26 KB

Versions: 20

Compression:

Stored size: 1.26 KB

Contents

module WebIDL
  class Generator

    class ParseError < StandardError; end

    def initialize(visitor = nil)
      @visitor = visitor
    end

    def generate(input)
      ast_defs = case input
                 when String
                   parse(input)
                 when Array
                   input.each do |e|
                     unless e.kind_of? WebIDL::Ast::Node
                       raise TypeError, "input Array elements must be of WebIDL::Ast::Node (got #{e.class})"
                     end
                   end

                   input
                 when WebIDL::Ast::Node
                   [input]
                 else
                   raise TypeError, "unexpected input #{input.class}"
                 end

      strings = ast_defs.map { |definition| ruby2ruby.process definition.accept(visitor) }.compact
      strings.join("\n\n")
    end

    private

    def parse(str)
      parse_tree = parser.parse(str)

      if parse_tree.nil?
        raise ParseError, parser.failure_reason
      end

      parse_tree.build
    end

    def ruby2ruby
      @ruby2ruby ||= Ruby2Ruby.new
    end

    def parser
      @parser ||= WebIDL::Parser::IDLParser.new
    end

    def visitor
      @visitor ||= RubySexpVisitor.new
    end

  end # Generator
end # WebIDL

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
webidl-0.2.2 lib/webidl/generator.rb
webidl-0.2.1 lib/webidl/generator.rb
webidl-0.1.10 lib/webidl/generator.rb
webidl-0.2.0 lib/webidl/generator.rb
webidl-0.1.9 lib/webidl/generator.rb
webidl-0.1.8 lib/webidl/generator.rb
webidl-0.1.7 lib/webidl/generator.rb
webidl-0.1.6 lib/webidl/generator.rb
webidl-0.1.5 lib/webidl/generator.rb
webidl-0.1.4 lib/webidl/generator.rb
webidl-0.1.3 lib/webidl/generator.rb
webidl-0.1.2 lib/webidl/generator.rb
webidl-0.1.1 lib/webidl/generator.rb
webidl-0.1.0 lib/webidl/generator.rb
webidl-0.0.9 lib/webidl/generator.rb
webidl-0.0.8 lib/webidl/generator.rb
webidl-0.0.7 lib/webidl/generator.rb
webidl-0.0.6 lib/webidl/generator.rb
webidl-0.0.5 lib/webidl/generator.rb
webidl-0.0.4 lib/webidl/generator.rb