| 1 | <h1 align="center">Prism Ruby parser</h1> |
| 2 | <div align="center"> |
| 3 | <img alt="Prism Ruby parser" height="256px" src="https://github.com/ruby/prism/blob/main/doc/images/prism.png?raw=true"> |
| 4 | </div> |
| 5 | |
| 6 | This is a parser for the Ruby programming language. It is designed to be portable, error tolerant, and maintainable. It is written in C99 and has no dependencies. |
| 7 | |
| 8 | ## Overview |
| 9 | |
| 10 | The repository contains the infrastructure for both a shared library (libprism) and a native CRuby extension. The shared library has no bindings to CRuby itself, and so can be used by other projects. The native CRuby extension links against `ruby.h`, and so is suitable in the context of CRuby. |
| 11 | |
| 12 | ``` |
| 13 | . |
| 14 | ├── Makefile configuration to compile the shared library and native tests |
| 15 | ├── Rakefile configuration to compile the native extension and run the Ruby tests |
| 16 | ├── bin |
| 17 | │ ├── lex runs the lexer on a file or string, prints the tokens, and compares to ripper |
| 18 | │ ├── parse runs the parse on a file or string and prints the AST |
| 19 | │ └── prism a CLI for development and debugging |
| 20 | ├── config.yml specification for tokens and nodes in the tree |
| 21 | ├── doc documentation website |
| 22 | ├── docs markdown documentation about the project |
| 23 | ├── ext |
| 24 | │ └── prism |
| 25 | │ ├── extconf.rb configuration to generate the Makefile for the native extension |
| 26 | │ └── extension.c the native extension that interacts with libprism |
| 27 | ├── fuzz files related to fuzz testing |
| 28 | ├── gemfiles gemfiles used by different Ruby versions in CI |
| 29 | ├── include |
| 30 | │ ├── prism header files for the shared library |
| 31 | │ └── prism.h main header file for the shared library |
| 32 | ├── java Java bindings for the shared library |
| 33 | ├── java-wasm Java WASM bindings for the shared library |
| 34 | ├── javascript JavaScript WASM bindings for the shared library |
| 35 | ├── lib |
| 36 | │ ├── prism Ruby library files |
| 37 | │ └── prism.rb main entrypoint for the Ruby library |
| 38 | ├── rakelib various Rake tasks for the project |
| 39 | ├── rbi RBI type signatures for the Ruby library |
| 40 | ├── rust |
| 41 | │ ├── ruby-prism Rustified crate for the shared library |
| 42 | │ └── ruby-prism-sys FFI binding for Rust |
| 43 | ├── sample |
| 44 | │ └── prism Sample code that uses the Ruby API for documentation purposes |
| 45 | ├── sig RBS type signatures for the Ruby library |
| 46 | ├── src |
| 47 | │ ├── util various utility files |
| 48 | │ └── prism.c main entrypoint for the shared library |
| 49 | ├── templates contains ERB templates generated by templates/template.rb |
| 50 | │ └── template.rb generates code from the nodes and tokens configured by config.yml |
| 51 | └── test |
| 52 | └── prism |
| 53 | ├── fixtures Ruby code used for testing |
| 54 | └── snapshots snapshots of generated syntax trees corresponding to fixtures |
| 55 | ``` |
| 56 | |
| 57 | ## Getting started |
| 58 | |
| 59 | To compile the shared library, you will need: |
| 60 | |
| 61 | * C99 compiler |
| 62 | * GNU make |
| 63 | * Ruby 2.7.0 or later |
| 64 | |
| 65 | Once you have these dependencies, run: |
| 66 | |
| 67 | ``` |
| 68 | bundle install |
| 69 | ``` |
| 70 | |
| 71 | to fetch the Ruby dependencies. Finally, run: |
| 72 | |
| 73 | ``` |
| 74 | bundle exec rake compile |
| 75 | ``` |
| 76 | |
| 77 | to compile the shared library. It will be built in the `build` directory. To test that everything is working, run: |
| 78 | |
| 79 | ``` |
| 80 | bin/parse -e "1 + 2" |
| 81 | ``` |
| 82 | |
| 83 | to see the syntax tree for the expression `1 + 2`. |
| 84 | |
| 85 | ## Contributing |
| 86 | |
| 87 | See the [CONTRIBUTING.md](CONTRIBUTING.md) file for more information. We additionally have documentation about the overall design of the project as well as various subtopics. |
| 88 | |
| 89 | * [Build system](docs/build_system.md) |
| 90 | * [Configuration](docs/configuration.md) |
| 91 | * [CRuby compilation](docs/cruby_compilation.md) |
| 92 | * [Design](docs/design.md) |
| 93 | * [Encoding](docs/encoding.md) |
| 94 | * [Fuzzing](docs/fuzzing.md) |
| 95 | * [Heredocs](docs/heredocs.md) |
| 96 | * [JavaScript](docs/javascript.md) |
| 97 | * [Local variable depth](docs/local_variable_depth.md) |
| 98 | * [Mapping](docs/mapping.md) |
| 99 | * [Parser translation](docs/parser_translation.md) |
| 100 | * [Parsing rules](docs/parsing_rules.md) |
| 101 | * [Releasing](docs/releasing.md) |
| 102 | * [Ripper translation](docs/ripper_translation.md) |
| 103 | * [Ruby API](docs/ruby_api.md) |
| 104 | * [RubyParser translation](docs/ruby_parser_translation.md) |
| 105 | * [Serialization](docs/serialization.md) |
| 106 | * [Testing](docs/testing.md) |
| 107 | |
| 108 | ## Examples |
| 109 | |
| 110 | Prism has been integrated into the majority of Ruby runtimes, many libraries, and some applications. Below is a list of some of the projects that use Prism: |
| 111 | |
| 112 | ### Runtimes |
| 113 | |
| 114 | * [CRuby](https://github.com/ruby/ruby/pull/7964) (via C) |
| 115 | * [Garnet](https://github.com/camertron/garnet-js) (via WASM) |
| 116 | * [JRuby](https://github.com/jruby/jruby/pull/8103) (via Java) |
| 117 | * [Natalie](https://github.com/natalie-lang/natalie/pull/1213) (via C++ and Ruby) |
| 118 | * [Opal](https://github.com/opal/opal/pull/2642) (via Ruby and WASM) |
| 119 | * [TruffleRuby](https://github.com/truffleruby/truffleruby/issues/3117) (via Java) |
| 120 | |
| 121 | ### Libraries |
| 122 | |
| 123 | * [dispersion](https://github.com/joeldrapper/dispersion) |
| 124 | * [minifyrb](https://github.com/koic/minifyrb) |
| 125 | * [packwerk](https://github.com/Shopify/packwerk/pull/388) (via parser translator) |
| 126 | * [rbi](https://github.com/Shopify/rbi) |
| 127 | * [rails](https://github.com/rails/rails) |
| 128 | * [parsing renders](https://github.com/rails/rails/pull/49438) |
| 129 | * [parsing rdoc](https://github.com/rails/rails/pull/50870) |
| 130 | * [parsing tests](https://github.com/rails/rails/pull/51006) |
| 131 | * [repl_type_completor](https://github.com/ruby/repl_type_completor) |
| 132 | * [rubocop](https://docs.rubocop.org/rubocop/configuration.html#setting-the-parser-engine) (via parser translator) |
| 133 | * [ruby-lsp](https://github.com/Shopify/ruby-lsp) |
| 134 | * [smart_todo](https://github.com/Shopify/smart_todo/pull/69) |
| 135 | * [sorbet-eraser](https://github.com/kddnewton/sorbet-eraser/pull/25) |
| 136 | * [synvert](https://github.com/xinminlabs/synvert-core-ruby) |
| 137 | * [typeprof](https://github.com/ruby/typeprof) |
| 138 | * [unparser](https://github.com/mbj/unparser) (via parser translator) |
| 139 | |
| 140 | ### Applications |
| 141 | |
| 142 | * [gem.sh](https://github.com/marcoroth/gem.sh/pull/96) |
| 143 | * [Sorbet](https://github.com/sorbet/sorbet) |