• Stars
    star
    471
  • Rank 93,216 (Top 2 %)
  • Language
    Ruby
  • Created almost 16 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

ruby_parser is a ruby parser written in pure ruby. It outputs s-expressions which can be manipulated and converted back to ruby via the ruby2ruby gem.

ruby_parser

home

github.com/seattlerb/ruby_parser

bugs

github.com/seattlerb/ruby_parser/issues

rdoc

docs.seattlerb.org/ruby_parser

DESCRIPTION:

ruby_parser (RP) is a ruby parser written in pure ruby (utilizing racc–which does by default use a C extension). It outputs s-expressions which can be manipulated and converted back to ruby via the ruby2ruby gem.

As an example:

def conditional1 arg1
  return 1 if arg1 == 0
  return 0
end

becomes:

s(:defn, :conditional1, s(:args, :arg1),
  s(:if,
    s(:call, s(:lvar, :arg1), :==, s(:lit, 0)),
    s(:return, s(:lit, 1)),
    nil),
  s(:return, s(:lit, 0)))

Tested against 801,039 files from the latest of all rubygems (as of 2013-05):

  • 1.8 parser is at 99.9739% accuracy, 3.651 sigma

  • 1.9 parser is at 99.9940% accuracy, 4.013 sigma

  • 2.0 parser is at 99.9939% accuracy, 4.008 sigma

  • 2.6 parser is at 99.9972% accuracy, 4.191 sigma

  • 3.0 parser has a 100% parse rate.

    • Tested against 2,672,412 unique ruby files across 167k gems.

    • As do all the others now, basically.

FEATURES/PROBLEMS:

  • Pure ruby, no compiles.

  • Includes preceding comment data for defn/defs/class/module nodes!

  • Incredibly simple interface.

  • Output is 100% equivalent to ParseTree.

    • Can utilize PT’s SexpProcessor and UnifiedRuby for language processing.

  • Known Issue: Speed is now pretty good, but can always improve:

    • RP parses a corpus of 3702 files in 125s (avg 108 Kb/s)

    • MRI+PT parsed the same in 67.38s (avg 200.89 Kb/s)

  • Known Issue: Code is much better, but still has a long way to go.

  • Known Issue: Totally awesome.

  • Known Issue: line number values can be slightly off. Parsing LR sucks.

SYNOPSIS:

RubyParser.new.parse "1+1"
# => s(:call, s(:lit, 1), :+, s(:lit, 1))

You can also use Ruby19Parser, Ruby18Parser, or RubyParser.for_current_ruby:

RubyParser.for_current_ruby.parse "1+1"
# => s(:call, s(:lit, 1), :+, s(:lit, 1))

DEVELOPER NOTES:

To add a new version:

  • New parser should be generated from lib/ruby_parser.yy.

  • Extend lib/ruby_parser.yy with new class name.

  • Add new version number to V2/V3 in Rakefile for rule creation.

  • Add new ‘ruby_parse “x.y.z”` line to Rakefile for rake compare (line ~300).

  • Require generated parser in lib/ruby_parser.rb.

  • Add new V## = ::Ruby##Parser; end to ruby_parser.rb (bottom of file).

  • Add empty TestRubyParserShared##Plus module and TestRubyParserV## to test/test_ruby_parser.rb.

  • Extend Manifest.txt with generated file names.

  • Add new version number to sexp_processor’s pt_testcase.rb in all_versions

Until all of these are done, you won’t have a clean test run.

REQUIREMENTS:

  • ruby. woot.

  • sexp_processor for Sexp and SexpProcessor classes, and testing.

  • racc full package for parser development (compiling .y to .rb).

INSTALL:

  • sudo gem install ruby_parser

LICENSE:

(The MIT License)

Copyright © Ryan Davis, seattle.rb

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

More Repositories

1

flog

Flog reports the most tortured code in an easy to read pain report. The higher the score, the more pain the code is in.
Ruby
869
star
2

flay

Flay analyzes code for structural similarities. Differences in literal values, variable, class, method names, whitespace, programming style, braces vs do/end, etc are all ignored.
Ruby
709
star
3

debride

Analyze code for potentially uncalled / dead methods, now with auto-removal.
Ruby
673
star
4

vlad

Ruby
353
star
5

rubyinline

Ruby
286
star
6

hoe

Hoe is a rake/rubygems helper for project Rakefiles. It helps you manage, maintain, and release your project and includes a dynamic plug-in system allowing for easy extensibility. Hoe ships with plug-ins for all your usual project tasks including rdoc generation, testing, packaging, deployment, and announcement.
Ruby
285
star
7

heckle

Ruby
176
star
8

ruby2ruby

Ruby
162
star
9

graph

Graph is a type of hash that outputs in graphviz's dot format. It comes with a command-line interface that is easily pluggable.
Ruby
158
star
10

zentest

Ruby
145
star
11

wilson

Ruby
138
star
12

image_science

Ruby
128
star
13

omnifocus

Ruby
127
star
14

rake-remote_task

Ruby
90
star
15

sexp_processor

Ruby
80
star
16

ruby_to_c

Ruby
79
star
17

zenweb

Ruby
64
star
18

parsetree

Ruby
54
star
19

omnifocus-github

Ruby
42
star
20

autotest-rails

Ruby
40
star
21

rdoc_osx_dictionary

Ruby
39
star
22

oedipus_lex

This is not your father’s lexer.
Ruby
34
star
23

osx_keychain

Ruby
24
star
24

sysloglogger

This project is EOL'd because it was merged into ruby 2.0.
Ruby
23
star
25

upnp

Ruby
20
star
26

ar_mailer

Ruby
20
star
27

zenprofile

Ruby
19
star
28

autotest-screen

Ruby
17
star
29

png

Ruby
17
star
30

metaruby

Ruby
15
star
31

seattlerb.org

CSS
15
star
32

omnifocus-redmine

Ruby
14
star
33

event_hook

Ruby
13
star
34

ohmygems

Ruby
12
star
35

production_log_analyzer

Ruby
12
star
36

minitest-excludes

Ruby
11
star
37

upnp-mediaserver

Ruby
10
star
38

ssh

Ruby
10
star
39

graphics

C++
9
star
40

ruby2smalltalk

Ruby
9
star
41

minitest-proveit

Ruby
8
star
42

orca_card

Ruby
8
star
43

gauntlet

Ruby
7
star
44

imap_processor

Ruby
7
star
45

rails_analyzer_tools

Ruby
7
star
46

path_expander

Ruby
7
star
47

minitest-macruby

Ruby
7
star
48

rubygems-sandbox

Ruby
7
star
49

github_contribs

Ruby
7
star
50

upnp-contentdirectory

Ruby
6
star
51

sphincter

6
star
52

cocor

Ruby
6
star
53

smtp_tls

Ruby
6
star
54

minitest-stackprofit

Ruby
6
star
55

upnp-igd

Ruby
5
star
56

cached_model

Ruby
5
star
57

mem_inspect

5
star
58

makerakeworkwell

Ruby
5
star
59

minitest-trump

Ruby
5
star
60

un

Ruby
5
star
61

debride-erb

Ruby
4
star
62

action_profiler

Ruby
4
star
63

synchronizer

Perforce, meet GitHub.
Ruby
3
star
64

rubyaudit

Ruby
3
star
65

minitest-speed

Ruby
3
star
66

minitest-compare

Ruby
3
star
67

rfc_spec

Ruby
3
star
68

rubyholic

Ruby
3
star
69

rubygems-cleanroom

Ruby
3
star
70

zombies

Ruby
3
star
71

upnp-connectionmanager

Ruby
3
star
72

box_layout

Ruby
3
star
73

zenhacks

Ruby
3
star
74

worker_bee

Ruby
3
star
75

ringydingy

Ruby
3
star
76

rubygems-sing

Ruby
3
star
77

minitest_tu_shim

Ruby
3
star
78

omnifocus-rubyforge

Ruby
2
star
79

rubyinlinefortran

Ruby
2
star
80

zencallgraph

Ruby
2
star
81

rubygems-isit19

Ruby
2
star
82

supercaller

Ruby
2
star
83

minitest-unordered

Ruby
2
star
84

siren_song

Ruby
2
star
85

zengraph

Ruby
2
star
86

gmail_contacts

Ruby
2
star
87

mogilefs-client

2
star
88

imap_to_rss

Ruby
2
star
89

drawr

Ruby
2
star
90

minitest_bench

Ruby
2
star
91

change_class

Ruby
2
star
92

minitest-allow

Ruby
2
star
93

hoe-seattlerb

Ruby
2
star
94

rbayes

Ruby
1
star
95

firebrigade_api

Ruby
1
star
96

flay-persistence

Ruby
1
star
97

seattlerb_dashboard

Ruby
1
star
98

memcache-client

1
star
99

rc-rest

Ruby
1
star
100

omnifocus-bugzilla

Ruby
1
star