lib/webidl/generator.rb in webidl-0.0.3 vs lib/webidl/generator.rb in webidl-0.0.4

- old
+ new

@@ -5,22 +5,42 @@ def initialize(visitor = nil) @visitor = visitor end - def generate(str) - parse_tree = parser.parse(str) + 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 - if parse_tree.nil? - raise ParseError, parser.failure_reason - end + input + when WebIDL::Ast::Node + [input] + else + raise TypeError, "unexpected input #{input.class}" + end - ast_defs = parse_tree.build 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 \ No newline at end of file