webidl 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (74) hide show
  1. data/.autotest +9 -0
  2. data/.document +5 -0
  3. data/.gitignore +5 -0
  4. data/LICENSE +20 -0
  5. data/README.rdoc +31 -0
  6. data/Rakefile +65 -0
  7. data/VERSION +1 -0
  8. data/bin/webidl2ruby +13 -0
  9. data/examples/html5.rb +6 -0
  10. data/lib/webidl.rb +47 -0
  11. data/lib/webidl/ast/argument.rb +25 -0
  12. data/lib/webidl/ast/attribute.rb +29 -0
  13. data/lib/webidl/ast/const.rb +17 -0
  14. data/lib/webidl/ast/exception.rb +15 -0
  15. data/lib/webidl/ast/extended_attribute.rb +14 -0
  16. data/lib/webidl/ast/field.rb +14 -0
  17. data/lib/webidl/ast/implements_statement.rb +14 -0
  18. data/lib/webidl/ast/interface.rb +24 -0
  19. data/lib/webidl/ast/module.rb +17 -0
  20. data/lib/webidl/ast/node.rb +29 -0
  21. data/lib/webidl/ast/operation.rb +43 -0
  22. data/lib/webidl/ast/scoped_name.rb +28 -0
  23. data/lib/webidl/ast/type.rb +20 -0
  24. data/lib/webidl/ast/typedef.rb +15 -0
  25. data/lib/webidl/extensions/string.rb +22 -0
  26. data/lib/webidl/extensions/syntax_node.rb +5 -0
  27. data/lib/webidl/generator.rb +38 -0
  28. data/lib/webidl/generator/ruby_sexp_visitor.rb +118 -0
  29. data/lib/webidl/parse_tree/absolute_scoped_name.rb +11 -0
  30. data/lib/webidl/parse_tree/argument.rb +20 -0
  31. data/lib/webidl/parse_tree/argument_list.rb +14 -0
  32. data/lib/webidl/parse_tree/attribute.rb +17 -0
  33. data/lib/webidl/parse_tree/const.rb +9 -0
  34. data/lib/webidl/parse_tree/definitions.rb +25 -0
  35. data/lib/webidl/parse_tree/exception.rb +15 -0
  36. data/lib/webidl/parse_tree/exception_field.rb +11 -0
  37. data/lib/webidl/parse_tree/extended_attributes.rb +41 -0
  38. data/lib/webidl/parse_tree/implements_statement.rb +20 -0
  39. data/lib/webidl/parse_tree/interface.rb +21 -0
  40. data/lib/webidl/parse_tree/interface_inheritance.rb +11 -0
  41. data/lib/webidl/parse_tree/interface_members.rb +21 -0
  42. data/lib/webidl/parse_tree/module.rb +15 -0
  43. data/lib/webidl/parse_tree/nullable_type.rb +11 -0
  44. data/lib/webidl/parse_tree/operation.rb +30 -0
  45. data/lib/webidl/parse_tree/relative_scoped_name.rb +15 -0
  46. data/lib/webidl/parse_tree/scoped_name_list.rb +16 -0
  47. data/lib/webidl/parse_tree/specials.rb +17 -0
  48. data/lib/webidl/parse_tree/stringifier_attribute_or_operation.rb +16 -0
  49. data/lib/webidl/parse_tree/typedef.rb +11 -0
  50. data/lib/webidl/parser/debug_helper.rb +17 -0
  51. data/lib/webidl/parser/idl.treetop +369 -0
  52. data/spec/ast_spec.rb +218 -0
  53. data/spec/fixtures/empty_interface.idl +3 -0
  54. data/spec/fixtures/empty_module.idl +3 -0
  55. data/spec/fixtures/framework.idl +48 -0
  56. data/spec/fixtures/html5.idl +1440 -0
  57. data/spec/fixtures/interface_with_attribute.idl +7 -0
  58. data/spec/fixtures/interface_with_inheritance.idl +9 -0
  59. data/spec/fixtures/interface_with_members.idl +5 -0
  60. data/spec/fixtures/interface_with_stringifiers.idl +5 -0
  61. data/spec/fixtures/module_with_exception.idl +13 -0
  62. data/spec/fixtures/module_with_implements_statement.idl +6 -0
  63. data/spec/fixtures/module_with_typedef.idl +4 -0
  64. data/spec/fixtures/module_with_xattr_ident.idl +4 -0
  65. data/spec/fixtures/module_with_xattr_named_args.idl +5 -0
  66. data/spec/fixtures/module_with_xattr_no_arg.idl +5 -0
  67. data/spec/fixtures/module_with_xattr_scoped.idl +4 -0
  68. data/spec/fixtures/module_with_xattr_two_args.idl +4 -0
  69. data/spec/fixtures/nested.idl +4 -0
  70. data/spec/fixtures/websocket.idl +19 -0
  71. data/spec/generator_spec.rb +92 -0
  72. data/spec/parser_spec.rb +64 -0
  73. data/spec/spec_helper.rb +45 -0
  74. metadata +161 -0
data/.autotest ADDED
@@ -0,0 +1,9 @@
1
+ require "autotest/redgreen"
2
+
3
+ class Autotest
4
+
5
+ def tests_for_file(filename)
6
+ return Dir["spec/**/*_spec.rb"]
7
+ end
8
+
9
+ end
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Jari Bakken
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,31 @@
1
+ = webidl
2
+
3
+ This gem provides a pure-ruby parser and code generator for Web IDL, an interface description language for interfaces intended to be implemented in web browsers.
4
+ The code generation will be used to generate part of the implementation of Watir 2.0, backed by WebDriver.
5
+
6
+ = Problems
7
+
8
+ The code generation could be improved a lot - a lot of the data in the IDL is just ignored. I don't need anything more ATM, but it should be easy to complete it if anyone needs to.
9
+
10
+ The parser rules for ExtendedAttribute is not exactly like the grammar from the Web IDL spec, since I ran into infinite recursion issues and don't have the Treetop-fu to figure them out.
11
+ So far this hasn't led to any problems - the parser does parse the IDLs parts of the current HTML5 spec just fine.
12
+
13
+ = See also
14
+
15
+ http://dev.w3.org/2006/webapi/WebIDL/
16
+ http://dev.w3.org/2006/webapi/WebIDL/#idl-grammar
17
+ http://dev.w3.org/html5/spec/Overview.html
18
+
19
+ == Note on Patches/Pull Requests
20
+
21
+ * Fork the project.
22
+ * Make your feature addition or bug fix.
23
+ * Add tests for it. This is important so I don't break it in a
24
+ future version unintentionally.
25
+ * Commit, do not mess with rakefile, version, or history.
26
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
27
+ * Send me a pull request. Bonus points for topic branches.
28
+
29
+ == Copyright
30
+
31
+ Copyright (c) 2009 Jari Bakken. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,65 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "webidl"
8
+ gem.summary = %Q{WebIDL parser/generator for ruby}
9
+ gem.description = %Q{Built on Treetop, this gem will parse an interface declaration in WebIDL and generate ruby code}
10
+ gem.email = "jari.bakken@gmail.com"
11
+ gem.homepage = "http://github.com/jarib/webidl"
12
+ gem.authors = ["Jari Bakken"]
13
+ gem.bindir = "bin"
14
+
15
+ gem.add_dependency "treetop"
16
+ gem.add_dependency "ruby2ruby"
17
+ gem.add_development_dependency "rspec"
18
+ end
19
+
20
+ Jeweler::GemcutterTasks.new
21
+ rescue LoadError
22
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
23
+ end
24
+
25
+ require 'spec/rake/spectask'
26
+ Spec::Rake::SpecTask.new(:spec) do |spec|
27
+ spec.libs << 'lib' << 'spec'
28
+ spec.spec_files = FileList['spec/**/*_spec.rb']
29
+ end
30
+
31
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
32
+ spec.libs << 'lib' << 'spec'
33
+ spec.pattern = 'spec/**/*_spec.rb'
34
+ spec.rcov = true
35
+ spec.rcov_opts = %w[--exclude spec,ruby-debug,/Library/Ruby,.gem --include lib/webidl]
36
+ end
37
+
38
+ namespace :parser do
39
+ task :compile do
40
+ require 'treetop'
41
+ compiler = Treetop::Compiler::GrammarCompiler.new
42
+ treetop_dir = File.expand_path(File.join(File.dirname(__FILE__), "lib", "webidl", "parser"))
43
+ Dir[File.join(treetop_dir, "*.{tt,treetop}")].each do |treetop_file_path|
44
+ compiler.compile(treetop_file_path)
45
+ end
46
+ end
47
+ end
48
+
49
+ task :spec => :check_dependencies
50
+
51
+ task :default => :spec
52
+
53
+ require 'rake/rdoctask'
54
+ Rake::RDocTask.new do |rdoc|
55
+ if File.exist?('VERSION')
56
+ version = File.read('VERSION')
57
+ else
58
+ version = ""
59
+ end
60
+
61
+ rdoc.rdoc_dir = 'rdoc'
62
+ rdoc.title = "webidl #{version}"
63
+ rdoc.rdoc_files.include('README*')
64
+ rdoc.rdoc_files.include('lib/**/*.rb')
65
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
data/bin/webidl2ruby ADDED
@@ -0,0 +1,13 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $LOAD_PATH.unshift File.expand_path(File.join(File.dirname(__FILE__), "..", "lib"))
4
+ require "webidl"
5
+
6
+ if ARGV.empty?
7
+ $stderr.puts "no files given, reading from stdin.."
8
+ end
9
+
10
+ puts WebIDL::Generator.new.generate(ARGF.read)
11
+
12
+
13
+
data/examples/html5.rb ADDED
@@ -0,0 +1,6 @@
1
+ $LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', "lib")
2
+
3
+ require "rubygems"
4
+ require "webidl"
5
+
6
+ puts WebIDL::Generator.new.generate(File.read("#{File.dirname(__FILE__)}/../spec/fixtures/html5.idl"))
data/lib/webidl.rb ADDED
@@ -0,0 +1,47 @@
1
+ require "treetop"
2
+ require "ruby2ruby"
3
+
4
+ require "webidl/extensions/syntax_node"
5
+ require "webidl/extensions/string"
6
+
7
+ require "webidl/parse_tree/definitions"
8
+ require "webidl/parse_tree/module"
9
+ require "webidl/parse_tree/attribute"
10
+ require "webidl/parse_tree/typedef"
11
+ require "webidl/parse_tree/interface"
12
+ require "webidl/parse_tree/argument"
13
+ require "webidl/parse_tree/argument_list"
14
+ require "webidl/parse_tree/extended_attributes"
15
+ require "webidl/parse_tree/relative_scoped_name"
16
+ require "webidl/parse_tree/absolute_scoped_name"
17
+ require "webidl/parse_tree/nullable_type"
18
+ require "webidl/parse_tree/interface_members"
19
+ require "webidl/parse_tree/operation"
20
+ require "webidl/parse_tree/const"
21
+ require "webidl/parse_tree/specials"
22
+ require "webidl/parse_tree/exception"
23
+ require "webidl/parse_tree/exception_field"
24
+ require "webidl/parse_tree/implements_statement"
25
+ require "webidl/parse_tree/interface_inheritance"
26
+ require "webidl/parse_tree/scoped_name_list"
27
+ require "webidl/parse_tree/stringifier_attribute_or_operation"
28
+
29
+ require "webidl/ast/node"
30
+ require "webidl/ast/module"
31
+ require "webidl/ast/typedef"
32
+ require "webidl/ast/interface"
33
+ require "webidl/ast/extended_attribute"
34
+ require "webidl/ast/argument"
35
+ require "webidl/ast/type"
36
+ require "webidl/ast/scoped_name"
37
+ require "webidl/ast/operation"
38
+ require "webidl/ast/const"
39
+ require "webidl/ast/exception"
40
+ require "webidl/ast/attribute"
41
+ require "webidl/ast/field"
42
+ require "webidl/ast/implements_statement"
43
+
44
+ require "webidl/parser/debug_helper"
45
+ require "webidl/parser/idl"
46
+
47
+ require "webidl/generator"
@@ -0,0 +1,25 @@
1
+ module WebIDL
2
+ module Ast
3
+ class Argument < Node
4
+
5
+ attr_reader :name, :type
6
+
7
+ def initialize(name, type, opts = {})
8
+ @name = name
9
+ @type = type
10
+
11
+ @optional = !!opts[:optional]
12
+ @variadic = !!opts[:variadic]
13
+ end
14
+
15
+ def optional?
16
+ @optional
17
+ end
18
+
19
+ def variadic?
20
+ @variadic
21
+ end
22
+
23
+ end # Argument
24
+ end # Ast
25
+ end # WebIDL
@@ -0,0 +1,29 @@
1
+ module WebIDL
2
+ module Ast
3
+ class Attribute < Node
4
+
5
+ attr_reader :type, :name, :getraises, :setraises
6
+ attr_accessor :extended_attributes, :stringifier
7
+
8
+ def initialize(parent, type, name, opts = {})
9
+ super(parent)
10
+
11
+ @type = type
12
+ @name = name
13
+ @readonly = !!opts[:readonly]
14
+ @setraises = opts[:setraises] || []
15
+ @getraises = opts[:getraises] || []
16
+ @extended_attributes = opts[:extended_attributes] || []
17
+ end
18
+
19
+ def readonly?
20
+ @readonly
21
+ end
22
+
23
+ def stringifier?
24
+ !!@stringifier
25
+ end
26
+
27
+ end # Attribute
28
+ end # Ast
29
+ end # WebIDL
@@ -0,0 +1,17 @@
1
+ module WebIDL
2
+ module Ast
3
+ class Const < Node
4
+
5
+ attr_reader :type, :name, :value
6
+
7
+ def initialize(parent, type, name, value)
8
+ super(parent)
9
+
10
+ @type = type
11
+ @name = name
12
+ @value = value
13
+ end
14
+
15
+ end # Type
16
+ end # Ast
17
+ end # WebIDL
@@ -0,0 +1,15 @@
1
+ module WebIDL
2
+ module Ast
3
+ class Exception < Node
4
+ attr_reader :name
5
+ attr_accessor :members
6
+
7
+ def initialize(parent, name)
8
+ @parent = parent
9
+ @name = name
10
+ @members = []
11
+ end
12
+
13
+ end # Exception
14
+ end # Ast
15
+ end # WebIDL
@@ -0,0 +1,14 @@
1
+ module WebIDL
2
+ module Ast
3
+ class ExtendedAttribute < Node
4
+
5
+ attr_reader :name, :args
6
+
7
+ def initialize(name, args = [])
8
+ @name = name
9
+ @args = args
10
+ end
11
+
12
+ end
13
+ end # Ast
14
+ end # WebIDL
@@ -0,0 +1,14 @@
1
+ module WebIDL
2
+ module Ast
3
+ class Field < Node
4
+
5
+ attr_reader :type, :name
6
+
7
+ def initialize(parent, type, name)
8
+ super(parent)
9
+ @type, @name = type, name
10
+ end
11
+
12
+ end # Field
13
+ end # Ast
14
+ end # WebIDL
@@ -0,0 +1,14 @@
1
+ module WebIDL
2
+ module Ast
3
+ class ImplementsStatement < Node
4
+
5
+ attr_reader :implementor, :implementee
6
+
7
+ def initialize(parent, implementor_name, implementee_name)
8
+ @implementor = implementor_name
9
+ @implementee = implementee_name
10
+ end
11
+
12
+ end # ImplementsStatement
13
+ end # Ast
14
+ end # WebIDL
@@ -0,0 +1,24 @@
1
+ module WebIDL
2
+ module Ast
3
+ class Interface < Node
4
+
5
+ def self.list
6
+ @list ||= {}
7
+ end
8
+
9
+ attr_reader :name
10
+ attr_accessor :extended_attributes, :members, :inherits, :implements
11
+
12
+ def initialize(parent, name)
13
+ super(parent)
14
+
15
+ @name = name
16
+ @members = []
17
+ @inherits = []
18
+ @implements = []
19
+ @extended_attributes = []
20
+ end
21
+
22
+ end
23
+ end # Ast
24
+ end # WebIDL
@@ -0,0 +1,17 @@
1
+ module WebIDL
2
+ module Ast
3
+ class Module < Node
4
+
5
+ attr_reader :name
6
+ attr_accessor :extended_attributes, :definitions
7
+
8
+ def initialize(parent, name)
9
+ @parent = parent
10
+ @name = name
11
+ @definitions = []
12
+ @extended_attributes = []
13
+ end
14
+
15
+ end # Attribute
16
+ end # Ast
17
+ end # WebIDL
@@ -0,0 +1,29 @@
1
+ module WebIDL
2
+ module Ast
3
+ class Node
4
+
5
+ def initialize(parent = nil)
6
+ @parent = parent
7
+ end
8
+
9
+ def qualified_name
10
+ return unless @name
11
+
12
+ if @parent.respond_to?(:qualified_name)
13
+ "#{@parent.qualified_name}::#{@name}"
14
+ else
15
+ "::#{@name}"
16
+ end
17
+ end
18
+
19
+ def accept(visitor, &blk)
20
+ visitor.send("visit_#{snake_name}", self, &blk)
21
+ end
22
+
23
+ def snake_name
24
+ self.class.name.split("::").last.snake_case
25
+ end
26
+
27
+ end # Node
28
+ end # Ast
29
+ end # WebIDL
@@ -0,0 +1,43 @@
1
+ module WebIDL
2
+ module Ast
3
+ class Operation < Node
4
+
5
+ attr_reader :type, :name, :args, :specials, :raises
6
+ attr_accessor :stringifier
7
+
8
+ def initialize(parent, type, opts = {})
9
+ @parent = parent
10
+ @type = type
11
+ @name = opts[:name] || ''
12
+ @specials = opts[:specials] || []
13
+ @args = opts[:args] || []
14
+ @raises = opts[:raises] || []
15
+ end
16
+
17
+ def stringifier?
18
+ !!@stringifier
19
+ end
20
+
21
+ def getter?
22
+ @specials.include? 'getter'
23
+ end
24
+
25
+ def setter?
26
+ @specials.include? 'setter'
27
+ end
28
+
29
+ def creator?
30
+ @specials.include? 'creator'
31
+ end
32
+
33
+ def deleter?
34
+ @specials.include? 'deleter'
35
+ end
36
+
37
+ def caller?
38
+ @specials.include? 'caller'
39
+ end
40
+
41
+ end # Operation
42
+ end # Ast
43
+ end # WebIDL