{"id":13788042,"url":"https://github.com/whitequark/parser","last_synced_at":"2025-07-20T19:33:06.313Z","repository":{"id":7752377,"uuid":"9119875","full_name":"whitequark/parser","owner":"whitequark","description":"A Ruby parser.","archived":false,"fork":false,"pushed_at":"2025-04-14T01:35:59.000Z","size":4194,"stargazers_count":1618,"open_issues_count":48,"forks_count":203,"subscribers_count":43,"default_branch":"master","last_synced_at":"2025-07-19T20:33:34.787Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Yacc","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/whitequark.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2013-03-30T18:14:41.000Z","updated_at":"2025-07-15T06:42:49.000Z","dependencies_parsed_at":"2024-02-26T01:47:32.301Z","dependency_job_id":"42df4c2a-eab7-4709-8316-6c9ca52d2d42","html_url":"https://github.com/whitequark/parser","commit_stats":{"total_commits":1627,"total_committers":82,"mean_commits":"19.841463414634145","dds":0.4652735095267363,"last_synced_commit":"d2309892eff3b6c22e1f7a655f407f18b54872ca"},"previous_names":[],"tags_count":163,"template":false,"template_full_name":null,"purl":"pkg:github/whitequark/parser","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/whitequark%2Fparser","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/whitequark%2Fparser/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/whitequark%2Fparser/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/whitequark%2Fparser/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/whitequark","download_url":"https://codeload.github.com/whitequark/parser/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/whitequark%2Fparser/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266180411,"owners_count":23888742,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-03T21:00:35.156Z","updated_at":"2025-07-20T19:33:06.306Z","avatar_url":"https://github.com/whitequark.png","language":"Yacc","funding_links":[],"categories":["\u003e 1k ★","Yacc","Ruby Parser / Unparser","Gems"],"sub_categories":[],"readme":"# Parser\n\n[![Gem Version](https://badge.fury.io/rb/parser.svg)](https://badge.fury.io/rb/parser)\n[![Tests](https://github.com/whitequark/parser/workflows/Tests/badge.svg?branch=master)](https://github.com/whitequark/parser/actions?query=workflow%3ATests+branch%3Amaster)\n\n_Parser_ is a production-ready Ruby parser written in pure Ruby. It recognizes as\nmuch or more code than Ripper, Melbourne, JRubyParser or ruby\\_parser, and\nis vastly more convenient to use.\n\nYou can also use [unparser](https://github.com/mbj/unparser) to produce\nequivalent source code from Parser's ASTs.\n\nSponsored by [Evil Martians](http://evilmartians.com).\nMacRuby and RubyMotion support sponsored by [CodeClimate](http://codeclimate.com).\n\n\u003e [!WARNING]\n\u003e The `parser` gem is only compatible with the syntax of Ruby 3.3 and lower. For Ruby 3.4 and later, please use the [`Prism::Translation::Parser`](https://github.com/ruby/prism/blob/main/docs/parser_translation.md) instead.\n\u003e Starting in Ruby 3.4, Prism is the parser used in Ruby itself and can produce AST that is identical to the output of the `parser` gem. If you only need to parse Ruby 3.3 (or greater) and don't require compatibility with the `parser` gem AST, also consider using the native Prism AST.\n\u003e See this [GitHub issue](https://github.com/whitequark/parser/issues/1046) for more details.\n\u003e For a guide on how to use `parser` for older versions and `prism` for newer ones, please see [this guide](./doc/PRISM_TRANSLATION.md).\n\n## Installation\n\n    $ gem install parser\n\n## Usage\n\nLoad Parser (see the [backwards compatibility](#backwards-compatibility) section\nbelow for explanation of `emit_*` calls):\n\n```ruby\nrequire 'parser/current'\n# opt-in to most recent AST format:\nParser::Builders::Default.emit_lambda              = true\nParser::Builders::Default.emit_procarg0            = true\nParser::Builders::Default.emit_encoding            = true\nParser::Builders::Default.emit_index               = true\nParser::Builders::Default.emit_arg_inside_procarg0 = true\nParser::Builders::Default.emit_forward_arg         = true\nParser::Builders::Default.emit_kwargs              = true\nParser::Builders::Default.emit_match_pattern       = true\n```\n\nParse a chunk of code:\n\n```ruby\np Parser::CurrentRuby.parse(\"2 + 2\")\n# (send\n#   (int 2) :+\n#   (int 2))\n```\n\nAccess the AST's source map:\n\n```ruby\np Parser::CurrentRuby.parse(\"2 + 2\").loc\n# #\u003cParser::Source::Map::Send:0x007fe5a1ac2388\n#   @dot=nil,\n#   @begin=nil,\n#   @end=nil,\n#   @selector=#\u003cSource::Range (string) 2...3\u003e,\n#   @expression=#\u003cSource::Range (string) 0...5\u003e\u003e\n\np Parser::CurrentRuby.parse(\"2 + 2\").loc.selector.source\n# \"+\"\n```\n\nTraverse the AST: see the documentation for [gem ast](https://whitequark.github.io/ast/).\n\nParse a chunk of code and display all diagnostics:\n\n```ruby\nparser = Parser::CurrentRuby.new\nparser.diagnostics.consumer = lambda do |diag|\n  puts diag.render\nend\n\nbuffer = Parser::Source::Buffer.new('(string)', source: \"foo *bar\")\n\np parser.parse(buffer)\n# (string):1:5: warning: `*' interpreted as argument prefix\n# foo *bar\n#     ^\n# (send nil :foo\n#   (splat\n#     (send nil :bar)))\n```\n\nIf you reuse the same parser object for multiple `#parse` runs, you need to\n`#reset` it.\n\nYou can also use the `ruby-parse` utility (it's bundled with the gem) to play\nwith Parser:\n\n    $ ruby-parse -L -e \"2+2\"\n    (send\n      (int 2) :+\n      (int 2))\n    2+2\n     ~ selector\n    ~~~ expression\n    (int 2)\n    2+2\n    ~ expression\n    (int 2)\n    2+2\n\n    $ ruby-parse -E -e \"2+2\"\n    2+2\n    ^ tINTEGER 2                                    expr_end     [0 \u003c= cond] [0 \u003c= cmdarg]\n    2+2\n     ^ tPLUS \"+\"                                    expr_beg     [0 \u003c= cond] [0 \u003c= cmdarg]\n    2+2\n      ^ tINTEGER 2                                  expr_end     [0 \u003c= cond] [0 \u003c= cmdarg]\n    2+2\n      ^ false \"$eof\"                                expr_end     [0 \u003c= cond] [0 \u003c= cmdarg]\n    (send\n      (int 2) :+\n      (int 2))\n\n## Features\n\n* Precise source location reporting.\n* [Documented](doc/AST_FORMAT.md) AST format which is convenient to work with.\n* A simple interface and a powerful, tweakable one.\n* Parses 1.8, 1.9, 2.0, 2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 3.0, 3.1, 3.2, and 3.3 syntax with backwards-compatible\n  AST formats.\n* Parses MacRuby and RubyMotion syntax extensions.\n* [Rewriting][rewriting] support.\n* Parsing error recovery.\n* Improved [clang-like][] diagnostic messages with location information.\n* Written in pure Ruby, runs on MRI \u003e=2.0.0, JRuby and Rubinius (and historically, all versions of Ruby since 1.8)\n* Only one runtime dependency: the [ast][] gem.\n* [Insane][insane-lexer] Ruby lexer rewritten from scratch in Ragel.\n* 100% test coverage for Bison grammars (except error recovery).\n* Readable, commented source code.\n\n[clang-like]: http://clang.llvm.org/diagnostics.html\n[ast]: https://rubygems.org/gems/ast\n[insane-lexer]: http://web.archive.org/web/20210621201915/http://whitequark.org/blog/2013/04/01/ruby-hacking-guide-ch-11-finite-state-lexer/\n[rewriting]: http://web.archive.org/web/20220123050223/http://whitequark.org/blog/2013/04/26/lets-play-with-ruby-code/\n\n## Documentation\n\nDocumentation for Parser is available [online](https://whitequark.github.io/parser/).\n\n### Node names\n\nSeveral Parser nodes seem to be confusing enough to warrant a dedicated README section.\n\n#### (block)\n\nThe `(block)` node passes a Ruby block, that is, a closure, to a method call represented by its first child, a `(send)`, `(super)` or `(zsuper)` node. To demonstrate:\n\n```bash\n$ ruby-parse -e 'foo { |x| x + 2 }'\n(block\n  (send nil :foo)\n  (args\n    (arg :x))\n  (send\n    (lvar :x) :+\n    (int 2)))\n```\n\n#### (begin) and (kwbegin)\n\n**TL;DR: Unless you perform rewriting, treat `(begin)` and `(kwbegin)` as the same node type.**\n\nBoth `(begin)` and `(kwbegin)` nodes represent compound statements, that is, several expressions which are executed sequentally and the value of the last one is the value of entire compound statement. They may take several forms in the source code:\n\n  * `foo; bar`: without delimiters\n  * `(foo; bar)`: parenthesized\n  * `begin foo; bar; end`: grouped with `begin` keyword\n  * `def x; foo; bar; end`: grouped inside a method definition\n\nand so on.\n\n```bash\n$ ruby-parse -e '(foo; bar)'\n(begin\n  (send nil :foo)\n  (send nil :bar))\n$ ruby-parse -e 'def x; foo; bar end'\n(def :x\n  (args)\n  (begin\n    (send nil :foo)\n    (send nil :bar)))\n```\n\nNote that, despite its name, `kwbegin` node only has tangential relation to the `begin` keyword. Normally, Parser AST is semantic, that is, if two constructs look differently but behave identically, they get parsed to the same node. However, there exists a peculiar construct called post-loop in Ruby:\n\n```\nbegin\n  body\nend while condition\n```\n\nThis specific syntactic construct, that is, keyword `begin..end` block followed by a postfix `while`, [behaves][postloop] very unlike other similar constructs, e.g. `(body) while condition`. While the body itself is wrapped into a `while-post` node, Parser also supports rewriting, and in that context it is important to not accidentally convert one kind of loop into another.\n\n  [postloop]: http://rosettacode.org/wiki/Loops/Do-while#Ruby\n\n```\n$ ruby-parse -e 'begin foo end while cond'\n(while-post\n  (send nil :cond)\n  (kwbegin\n    (send nil :foo)))\n$ ruby-parse -e 'foo while cond'\n(while\n  (send nil :cond)\n  (send nil :foo))\n$ ruby-parse -e '(foo) while cond'\n(while\n  (send nil :cond)\n  (begin\n    (send nil :foo)))\n```\n\n(Parser also needs the `(kwbegin)` node type internally, and it is highly problematic to map it back to `(begin)`.)\n\n## Backwards compatibility\n\nParser does _not_ use semantic versioning. Parser versions are structured as `x.y.z.t`,\nwhere `x.y.z` indicates the most recent supported Ruby release (support for every\nRuby release that is chronologically earlier is implied), and `t` is a monotonically\nincreasing number.\n\nThe public API of Parser as well as the AST format (as listed in the documentation)\nare considered stable forever, although support for old Ruby versions may be removed\nat some point.\n\nSometimes it is necessary to modify the format of AST nodes that are already being emitted\nin a way that would break existing applications. To avoid such breakage, applications\nmust opt-in to these modifications; without explicit opt-in, Parser will continue to emit\nthe old AST node format. The most recent set of opt-ins is specified in\nthe [usage section](#usage) of this README.\n\n## Compatibility with Ruby MRI\n\nUnfortunately, Ruby MRI often changes syntax in patchlevel versions. This has happened, at least, for every release since 1.9; for example, commits [c5013452](https://github.com/ruby/ruby/commit/c501345218dc5fb0fae90d56a0c6fd19d38df5bb) and [04bb9d6b](https://github.com/ruby/ruby/commit/04bb9d6b75a55d4000700769eead5a5cb942c25b) were backported all the way from HEAD to 1.9. Moreover, there is no simple way to track these changes.\n\nThis policy makes it all but impossible to make Parser precisely compatible with the Ruby MRI parser. Indeed, at September 2014, it would be necessary to maintain and update ten different parsers together with their lexer quirks in order to be able to emulate any given released Ruby MRI version.\n\nAs a result, Parser chooses a different path: the `parser/rubyXY` parsers recognize the syntax of the latest minor version of Ruby MRI X.Y at the time of the gem release.\n\n## Compatibility with MacRuby and RubyMotion\n\nParser implements the MacRuby 0.12 and RubyMotion mid-2015 parsers precisely. However, the lexers of these have been forked off Ruby MRI and independently maintained for some time, and because of that, Parser may accept some code that these upstream implementations are unable to parse.\n\n## Known issues\n\nAdding support for the following Ruby MRI features in Parser would needlessly complicate it, and as they all are very specific and rarely occurring corner cases, this is not done.\n\nParser has been extensively tested; in particular, it parses almost entire [Rubygems][rg] corpus. For every issue, a breakdown of affected gems is offered.\n\n [rg]: https://rubygems.org\n\n### Void value expressions\n\nRuby MRI prohibits so-called \"void value expressions\". For a description\nof what a void value expression is, see [this\ngist](https://gist.github.com/JoshCheek/5625007) and [this Parser\nissue](https://github.com/whitequark/parser/issues/72).\n\nIt is unknown whether any gems are affected by this issue.\n\n### Syntax check of block exits\n\nSimilar to \"void value expression\" checks Ruby MRI also checks for correct\nusage of `break`, `next` and `redo`, if it's used outside of a {break,next,redo}-able\ncontext Ruby returns a syntax error starting from 3.3.0. `parser` gem simply doesn't\nrun this type of checks.\n\nIt is unknown whether any gems are affected by this issue.\n\n### Invalid characters inside comments and literals\n\nRuby MRI permits arbitrary non-7-bit byte sequences to appear in comments, as well as in string or symbol literals in form of escape sequences, regardless of source encoding. Parser requires all source code, including the expanded escape sequences, to consist of valid byte sequences in the source encoding that are convertible to UTF-8.\n\nAs of 2013-07-25, there are about 180 affected gems.\n\n### \\u escape in 1.8 mode\n\nRuby MRI 1.8 permits to specify a bare `\\u` escape sequence in a string; it treats it like `u`. Ruby MRI 1.9 and later treat `\\u` as a prefix for Unicode escape sequence and do not allow it to appear bare. Parser follows 1.9+ behavior.\n\nAs of 2013-07-25, affected gems are: activerdf, activerdf_net7, fastreader, gkellog-reddy.\n\n### Dollar-dash\n\n(This one is so obscure I couldn't even think of a saner name for this issue.) Pre-2.1 Ruby allows\nto specify a global variable named `$-`. Ruby 2.1 and later treat it as a syntax error. Parser\nfollows 2.1 behavior.\n\nNo known code is affected by this issue.\n\n### EOF characters after embedded documents before 2.7\n\nCode like `\"=begin\\n\"\"=end\\0\"` is invalid for all versions of Ruby before 2.7. Ruby 2.7 and later parses it\nnormally. Parser follows 2.7 behavior.\n\nIt is unknown whether any gems are affected by this issue.\n\n## Contributors\n\n* Catherine [whitequark][]\n* Markus Schirp ([mbj][])\n* Yorick Peterse ([yorickpeterse][])\n* Magnus Holm ([judofyr][])\n* Bozhidar Batsov ([bbatsov][])\n\n[whitequark]:     https://github.com/whitequark\n[mbj]:            https://github.com/mbj\n[yorickpeterse]:  https://github.com/yorickpeterse\n[judofyr]:        https://github.com/judofyr\n[bbatsov]:        https://github.com/bbatsov\n\n## Acknowledgements\n\nThe lexer testsuite is derived from\n[ruby\\_parser](https://github.com/seattlerb/ruby_parser).\n\nThe Bison parser rules are derived from [Ruby MRI](https://github.com/ruby/ruby)\nparse.y.\n\n## Contributing\n\n1. Make sure you have [Ragel ~\u003e 6.7](http://www.colm.net/open-source/ragel/) installed\n2. Fork it\n3. Create your feature branch (`git checkout -b my-new-feature`)\n4. Commit your changes (`git commit -am 'Add some feature'`)\n5. Push to the branch (`git push origin my-new-feature`)\n6. Create new Pull Request\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwhitequark%2Fparser","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwhitequark%2Fparser","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwhitequark%2Fparser/lists"}