{"id":25676843,"url":"https://github.com/digital-fabric/sirop","last_synced_at":"2025-04-23T14:05:10.781Z","repository":{"id":223539402,"uuid":"760649710","full_name":"digital-fabric/sirop","owner":"digital-fabric","description":"Ruby code rewriter","archived":false,"fork":false,"pushed_at":"2024-04-29T10:36:20.000Z","size":76,"stargazers_count":18,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-25T08:47:14.730Z","etag":null,"topics":["parser","prism","ruby"],"latest_commit_sha":null,"homepage":"https://www.rubydoc.info/gems/sirop","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/digital-fabric.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","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}},"created_at":"2024-02-20T17:21:40.000Z","updated_at":"2024-10-03T19:37:04.000Z","dependencies_parsed_at":"2024-02-20T19:46:39.851Z","dependency_job_id":"d10b4b57-c0cb-4ed4-90c2-c45fd4cd2e73","html_url":"https://github.com/digital-fabric/sirop","commit_stats":null,"previous_names":["digital-fabric/sirop"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/digital-fabric%2Fsirop","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/digital-fabric%2Fsirop/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/digital-fabric%2Fsirop/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/digital-fabric%2Fsirop/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/digital-fabric","download_url":"https://codeload.github.com/digital-fabric/sirop/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250447900,"owners_count":21432164,"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":["parser","prism","ruby"],"created_at":"2025-02-24T14:38:11.947Z","updated_at":"2025-04-23T14:05:10.756Z","avatar_url":"https://github.com/digital-fabric.png","language":"Ruby","readme":"# Sirop\n\nSirop is a Ruby gem for manipulating Ruby source code. Sirop is very young, so\nthe following information might be incomplete, out of date, or simply wrong!\n\n## Use Cases\n\nSome of the use cases addressed by Sirop are:\n\n- Compile DSLs into optimized Ruby code. This is especially interesting for HTML\n  templating DSLs in libraries like Phlex, Papercraft etc.\n  [Example](https://github.com/digital-fabric/sirop/blob/main/test/dsl_compiler.rb)\n- Get the source of a given block or method.\n- Rewrite parts of Ruby code, for implementing Ruby macros (and why not?).\n\n## Limitations\n\n- Sirop supports Ruby 3.2 or newer.\n- Sirop can be used only on blocks and methods defined in a file, so cannot\n  really be used on dynamically `eval`'d Ruby code, or in an IRB/Pry session.\n\n## Getting the AST/source of a Ruby proc or method\n\nTo get the AST of a proc or a method, use `Sirop.to_ast`:\n\n```ruby\n# for a proc\nmul = -\u003e(x, y) { x * y }\nSirop.to_ast(mul) #=\u003e ...\n\n# for a method\ndef foo; :bar; end\nSirop.to_ast(method(:foo)) #=\u003e ...\n```\n\nTo get the source of a proc or a method, use `Sirop.to_source`:\n\n```ruby\nmul = -\u003e(x, y) { x * y }\nSirop.to_source(mul) #=\u003e \"-\u003e(x, y) { x * y }\"\n\ndef foo; :bar; end\nSirop.to_source(method(:foo)) #=\u003e \"def foo; :bar; end\"\n```\n\n## Rewriting Ruby code\n\nYou can consult the [DSL compiler\nexample](https://github.com/digital-fabric/sirop/blob/main/test/dsl_compiler.rb). This example intercepts method calls by defining a `visit_call_node` method:\n\n```ruby\n# Annotated with some explanations\ndef visit_call_node(node)\n  # don't rewrite if the call has a receiver\n  return super if node.receiver\n\n  # set HTML location start\n  @html_location_start ||= node.location\n  # get method arguments...\n  inner_text, attrs = tag_args(node)\n  # and block\n  block = node.block\n\n  # emit HTML tag according to given arguments\n  if inner_text\n    emit_tag_open(node, attrs)\n    emit_tag_inner_text(inner_text)\n    emit_tag_close(node)\n  elsif block\n    emit_tag_open(node, attrs)\n    visit(block.body)\n    emit_tag_close(node)\n  else\n    emit_tag_open_close(node, attrs)\n  end\n  # set HTML location end\n  @html_location_end = node.location\nend\n```\n\n## Future directions\n\n- Implement a macro expander with support for `quote`/`unquote`:\n\n  ```ruby\n  trace_macro = Sirop.macro do |ast|\n    source = Sirop.to_source(ast)\n    quote do\n      result = unquote(ast)\n      puts \"The result of #{source} is: #{result}\"\n      result\n    end\n  end\n\n  def add(x, y)\n    trace(x + y)\n  end\n\n  Sirop.expand_macros(method(:add), trace: trace_macro)\n  ```\n\n- Implement a DSL compiler with hooks for easier usage in DSL libraries.\n\n## Contributing\n\nWe gladly welcome contributions from anyone! Some areas that need work currently\nare:\n\n- Documentation\n- More test cases for Ruby syntax in the Sirop tests. Look here:\n  https://github.com/digital-fabric/sirop/tree/main/test/fixtures\n\nPlease feel free to contribute PR's and issues","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdigital-fabric%2Fsirop","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdigital-fabric%2Fsirop","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdigital-fabric%2Fsirop/lists"}