parsing ruby reloaded - scg.unibe.chscg.unibe.ch/...03-23-iyadurai-parsingrubyreloaded.pdf · raise...

Post on 08-Aug-2020

1 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Parsing Ruby

Rathesan Iyadurai

1

Goal

2

Extract all classes and their methods without having to define the complete

grammar of Ruby.

Previously on “Parsing Ruby”

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

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

'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

11

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

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

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

13

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

14

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

modifier := startOfLine , stuffToConsume , newline not , '=' not , 'if'

15

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

modifier := startOfLine , stuffToConsume , newline not , '=' not , 'if'

16

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

modifier := startOfLine , stuffToConsume , newline not , '=' not , 'if'

17

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

modifier := startOfLine , stuffToConsume , newline not , '=' not , 'if'

~ 50 rules

10 for matching “end”s

13 for keywords

18

10 for strings and comments

Keywords

def if endless bado end.class

19

Keywords

def if endless bado end.class

20

String Interpolationname = "Jan" "Hi #{name}!" # => "Hi Jan!"

"Hi #{class Dog; end}"

"Hi #{puts ""}"

"Hi #{list.each { |l| foo }}"

21

https://github.com/jruby/jruby-parser

vs

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

0

500

1000

1500

2000

2500

3000

0 5000 10000 15000 20000 25000 30000 35000 40000 45000

Milli

seco

nds

Number of Characters

RubyGrammar Performance

smooth milliseconds per character count

0

500

1000

1500

2000

2500

3000

0 20 40 60 80 100 120

Milli

seco

nds

Number of Methods

RubyGrammar Performance

smooth milliseconds per method count

top related