-
Notifications
You must be signed in to change notification settings - Fork 28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add diagram generation feature and integrate Railroad Diagrams #523
Conversation
Lrama provides an API for generating HTML syntax diagrams. These visual diagrams are highly useful as grammar development tools and can also serve as a form of automatic self-documentation. ![Syntax Diagrams](https://github.com/user-attachments/assets/5d9bca77-93fd-4416-bc24-9a0f70693a22) This feature was inspired by the functionality of [Chevrotain](https://chevrotain.io/docs/), a parser construction toolkit for JavaScript. The main motivation for adding this feature was to make the structure of `parse.y`, and thus the structure of Ruby's grammar, easier to understand. I thought that generating syntax diagrams in a more human-readable format would be beneficial. These diagrams are highly useful as grammar development tools and can also serve as automatic self-documentation, which I believe is a significant advancement. Chevrotain used [tabatkins/railroad-diagrams](https://github.com/tabatkins/railroad-diagrams), but this library only supported JavaScript and Python. To address this, I created a Ruby library and integrated it into Lrama: [https://github.com/ydah/railroad_diagrams](https://github.com/ydah/railroad_diagrams) This makes collaboration with Lrama easier and allows improvements to the library based on Lrama's needs. For these reasons, I decided to release it as a Ruby gem.
@@ -26,7 +26,6 @@ jobs: | |||
- uses: ruby/setup-ruby@v1 | |||
with: | |||
ruby-version: ${{ matrix.ruby }} | |||
bundler-cache: true |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
memo) If this cache is present, the following will fail at v2.5 If I could delete the cache, that might solve the problem, but I don't have the authority to delete that cache.
see: https://github.com/ruby/lrama/actions/runs/13088401502/job/36522284488
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could comment out the line instead of deletion and add the link to failed CI job as comments?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I add comment: ffd3115
lib/lrama/diagram.rb
Outdated
begin | ||
require "railroad_diagrams" | ||
rescue LoadError | ||
warn "railroad_diagrams is not installed. Please run `bundle install`." | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
memo) If we're using it in ruby/ruby
, the library may not be installed by default. In such cases, you need to handle it like this:
This ensures that the script doesn't break if the gem isn't available, while also notifying the user about the missing dependency.
But if there is a better way, I would like to know.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, we should not enforce any installation of gems for ruby/ruby build.
However current implementation causes warnings on ruby/ruby build (see: https://github.com/ruby/lrama/actions/runs/13088787136/job/36523059170?pr=523#step:14:76), we also need to avoid such situation.
When I implemented stackprof integration, I postponed require
until entry point method is called.
Then my suggestions are:
- extract
require
logic (L. 4 - 8) to an instance method - call the instance method when it's necessary, it might be needed when
Diagram#initialize
is called
By the way, we can assume "erb" is bundled to ruby then please keep require "erb"
as is.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
def self.require_stackprof |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you! I updated 9113e0b
@@ -1,5 +1,13 @@ | |||
# NEWS for Lrama | |||
|
|||
## Lrama 0.7.1 (2025-xx-xx) | |||
|
|||
### Syntax Diagrams |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might be good to add how to specify the option.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed it is. I updated: 4280ca2
call the instance method when it's necessary, it might be needed when `Diagram#initialize` is called
Overview
Lrama provides an API for generating HTML syntax diagrams. These visual diagrams are highly useful as grammar development tools and can also serve as a form of automatic self-documentation.
This feature was inspired by the functionality of Chevrotain, a parser construction toolkit for JavaScript.
Motivation
The main motivation for adding this feature was to make the structure of
parse.y
, and thus the structure of Ruby's grammar, easier to understand. I thought that generating syntax diagrams in a more human-readable format would be beneficial.These diagrams are highly useful as grammar development tools and can also serve as automatic self-documentation, which I believe is a significant advancement.
Library for Rendering Railroad Diagrams
Chevrotain used tabatkins/railroad-diagrams, but this library only supported JavaScript and Python.
To address this, I created a Ruby library and integrated it into Lrama: https://github.com/ydah/railroad_diagrams
This makes collaboration with Lrama easier and allows improvements to the library based on Lrama's needs. For these reasons, I decided to release it as a Ruby gem.