Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Parsing Ruby Rathesan Iyadurai 1 Goal Extract all classes and their methods without having to define the complete grammar of Ruby. 2 Previously on “Parsing Ruby” class Dog attr_accessor :age def initialize(age) raise "NOO" if age < 1 @name = "foo class bar" @age = age end def bark if age > 3 puts "wuff" else puts "wiff" end end end 4 class Dog attr_accessor :age def initialize(age) raise "NOO" if age < 1 @name = "foo class bar" @age = age end def bark if age > 3 puts "wuff" else puts "wiff" end end end 5 class Dog attr_accessor :age def initialize(age) raise "NOO" if age < 1 @name = "foo class bar" @age = age end def bark if age > 3 puts "wuff" else puts "wiff" end end end 6 class Dog attr_accessor :age def initialize(age) raise "NOO" if age < 1 @name = "foo class bar" @age = age end def bark if age > 3 puts "wuff" else puts "wiff" end end end 7 class Dog attr_accessor :age def initialize(age) raise "NOO" if age < 1 @name = "foo class bar" @age = age end def bark if age > 3 puts "wuff" else puts "wiff" end end end 8 'foo class bar' "foo class bar" %?foo class bar? %{foo class bar} %<foo class bar> %(foo class bar) %[foo class bar] %q{foo class bar} %Q{foo class bar} <<spongebob.strip foo class bar spongebob 9 Modifiers 10 class Dog attr_accessor :age def initialize(age) raise "NOO" if age < 1 @name = "foo class bar" @age = age end def bark if age > 3 puts "wuff" else puts "wiff" end end end 11 class Dog class Dog attr_accessor :age attr_accessor :age def initialize(age) raise "NOO" if age < 1 def initialize(age) raise "NOO" if age < 1 @name = "foo class bar" @age = age end @name = "foo class bar" @age = age end def bark str = if age > 3 "wuff" else "wiff" end puts str end def bark if age > 3 puts "wuff" else puts "wiff" end end end end 12 class Dog attr_accessor :age def initialize(age) raise "NOO" if age < 1 @name = "foo class bar" @age = age end def bark str = if age > 3 "wuff" else "wiff" end puts str end end 13 class Dog attr_accessor :age def initialize(age) raise "NOO" if age < 1 modifier := startOfLine , stuffToConsume , newline not , '=' not , 'if' @name = "foo class bar" @age = age end def bark str = if age > 3 "wuff" else "wiff" end puts str end end 14 class Dog attr_accessor :age def initialize(age) raise "NOO" if age < 1 modifier := startOfLine , stuffToConsume , newline not , '=' not , 'if' @name = "foo class bar" @age = age end def bark str = if age > 3 "wuff" else "wiff" end puts str end end 15 class Dog attr_accessor :age def initialize(age) raise "NOO" if age < 1 modifier := startOfLine , stuffToConsume , newline not , '=' not , 'if' @name = "foo class bar" @age = age end def bark str = if age > 3 "wuff" else "wiff" end puts str end end 16 class Dog attr_accessor :age def initialize(age) raise "NOO" if age < 1 modifier := startOfLine , stuffToConsume , newline not , '=' not , 'if' @name = "foo class bar" @age = age end def bark str = if age > 3 "wuff" else "wiff" end puts str end end 17 ~ 50 rules 10 for matching “end”s 13 for keywords 10 for strings and comments 18 Keywords def if endless bado end.class 19 Keywords def if endless bado end.class 20 String Interpolation name = "Jan" "Hi #{name}!" # => "Hi Jan!" "Hi #{class Dog; end}" "Hi #{puts ""}" "Hi #{list.each { |l| foo }}" 21 vs https://github.com/jruby/jruby-parser 22 class Dog end if foo? 23 Now what? 24 def foo a = "i'm a local variable!" b end def b "i'm a method!" end 25 def foo(arg) a = "i'm a local variable!" arg.b end 26 Overview Goal: Extract all classes and their methods without having to define the complete grammar of Ruby Problems: modifiers, keywords, string interpolation Goal achieved? 27 RubyGrammar Performance 3000 smooth milliseconds per character count 2500 Milliseconds 2000 1500 1000 500 0 0 5000 10000 15000 20000 25000 Number of Characters 30000 35000 40000 45000 RubyGrammar Performance 3000 smooth milliseconds per method count 2500 Milliseconds 2000 1500 1000 500 0 0 20 40 60 Number of Methods 80 100 120