Skip to content
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

Merged
merged 7 commits into from
Feb 3, 2025

Conversation

ydah
Copy link
Member

@ydah ydah commented Feb 1, 2025

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.

Syntax Diagrams

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.

@ydah ydah requested a review from yui-knk February 1, 2025 10:46
@ydah ydah self-assigned this Feb 1, 2025
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
Copy link
Member Author

@ydah ydah Feb 1, 2025

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

Copy link
Collaborator

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?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I add comment: ffd3115

Comment on lines 4 to 8
begin
require "railroad_diagrams"
rescue LoadError
warn "railroad_diagrams is not installed. Please run `bundle install`."
end
Copy link
Member Author

@ydah ydah Feb 1, 2025

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.

Copy link
Collaborator

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:

  1. extract require logic (L. 4 - 8) to an instance method
  2. 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.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

def self.require_stackprof
is an example.

Copy link
Member Author

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
Copy link
Collaborator

@yui-knk yui-knk Feb 3, 2025

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.

Copy link
Member Author

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

ydah added 3 commits February 3, 2025 15:28
call the instance method when it's necessary, it might be needed when `Diagram#initialize` is called
@ydah ydah requested a review from yui-knk February 3, 2025 08:47
@ydah ydah merged commit e156993 into ruby:master Feb 3, 2025
22 checks passed
@ydah ydah deleted the diagram branch February 3, 2025 11:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants