-
-
Notifications
You must be signed in to change notification settings - Fork 3
Add support to SyntaxTree 6 and Mermaid.js #5
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
Changes from 6 commits
4dc5dbe
2275ef1
8c4d948
dfa8d3d
ea904ff
1ce254c
43fbf65
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,70 +16,22 @@ <h1>Syntax Tree</h1> | |
<span><button type="button" id="format" disabled>Format</button></span> | ||
|
||
<div class="toggles"> | ||
<span><button type="button" value="prettyPrint" disabled>AST</button></span> | ||
<span><button type="button" value="disasm" disabled>ISEQ</button></span> | ||
<select> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's start the |
||
<option value="prettyPrint">AST</option> | ||
<option value="disasm">ISEQ</option> | ||
<option value="mermaid">GRAPH</option> | ||
</select> | ||
</div> | ||
</nav> | ||
<textarea id="editor"># frozen_string_literal: true | ||
|
||
require "json" | ||
require "pp" | ||
require "prettyprint" | ||
require "ripper" | ||
require "stringio" | ||
|
||
require_relative "syntax_tree/formatter" | ||
require_relative "syntax_tree/node" | ||
require_relative "syntax_tree/parser" | ||
require_relative "syntax_tree/prettyprint" | ||
require_relative "syntax_tree/version" | ||
require_relative "syntax_tree/visitor" | ||
require_relative "syntax_tree/visitor/json_visitor" | ||
require_relative "syntax_tree/visitor/pretty_print_visitor" | ||
|
||
module SyntaxTree | ||
# This holds references to objects that respond to both #parse and #format | ||
# so that we can use them in the CLI. | ||
HANDLERS = {} | ||
HANDLERS.default = SyntaxTree | ||
|
||
# This is a hook provided so that plugins can register themselves as the | ||
# handler for a particular file type. | ||
def self.register_handler(extension, handler) | ||
HANDLERS[extension] = handler | ||
end | ||
|
||
# Parses the given source and returns the syntax tree. | ||
def self.parse(source) | ||
parser = Parser.new(source) | ||
response = parser.parse | ||
response unless parser.error? | ||
end | ||
|
||
# Parses the given source and returns the formatted source. | ||
def self.format(source) | ||
formatter = Formatter.new(source, []) | ||
parse(source).format(formatter) | ||
|
||
formatter.flush | ||
formatter.output.join | ||
end | ||
|
||
# Returns the source from the given filepath taking into account any potential | ||
# magic encoding comments. | ||
def self.read(filepath) | ||
encoding = | ||
File.open(filepath, "r") do |file| | ||
header = file.readline | ||
header += file.readline if header.start_with?("#!") | ||
Ripper.new(header).tap(&:parse).encoding | ||
end | ||
|
||
File.read(filepath, encoding: encoding) | ||
end | ||
end | ||
</textarea> | ||
<textarea id="editor"> | ||
SyntaxTree::Binary[ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The reason I'm changing the default example here is the fact that mermaid.js has a text limit it is able to turn into a Graph. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's fine, but let's do one that is less confusing. Even just |
||
left: SyntaxTree::Int[value: "1"], | ||
operator: :+, | ||
right: SyntaxTree::Int[value: "1"] | ||
] | ||
</textarea> | ||
<textarea id="output" disabled readonly>Loading...</textarea> | ||
<div id="graph-container" class="graph-container"></div> | ||
</main> | ||
<script type="module" src="index.js"></script> | ||
</body> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -66,10 +66,13 @@ export default async function createRuby() { | |
// files to make it work. I'm not sure why I need to explicitly require | ||
// did_you_mean here, but it doesn't work without it. | ||
ruby.eval(` | ||
require "rubygems" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this require and the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unfortunately, they are. Without them, we get |
||
require "did_you_mean" | ||
require "json" | ||
require "pp" | ||
$:.unshift("/lib") | ||
require_relative "/lib/syntax_tree" | ||
require_relative "/lib/prettier_print" | ||
`); | ||
|
||
return { | ||
|
@@ -80,6 +83,15 @@ export default async function createRuby() { | |
|
||
return ruby.eval(rubySource).toString(); | ||
}, | ||
mermaid(source: string) { | ||
const jsonSource = JSON.stringify(JSON.stringify(source)); | ||
const rubySource = ` | ||
source = JSON.parse(${jsonSource}) | ||
SyntaxTree.parse(source).to_mermaid | ||
`; | ||
|
||
return ruby.eval(rubySource).toString(); | ||
}, | ||
// A function that calls through to the SyntaxTree.format function to get | ||
// the pretty-printed version of the source. | ||
format(source: string) { | ||
|
@@ -92,12 +104,7 @@ export default async function createRuby() { | |
// the syntax tree. | ||
prettyPrint(source: string) { | ||
const jsonSource = JSON.stringify(JSON.stringify(source)); | ||
const rubySource = ` | ||
PP.format([], 80) do |q| | ||
source = JSON.parse(${jsonSource}) | ||
SyntaxTree.parse(source).pretty_print(q) | ||
end.join | ||
`; | ||
const rubySource = `PP.pp(SyntaxTree.parse(JSON.parse(${jsonSource})), +"", 80)`; | ||
|
||
return ruby.eval(rubySource).toString(); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import mermaidjs from "mermaid"; | ||
|
||
const getCleanContainer = () => { | ||
const div = document.querySelector("#graph-container"); | ||
|
||
div.innerHTML = ''; | ||
|
||
return div; | ||
} | ||
|
||
const render = (fn: Function) => { | ||
let container = getCleanContainer(); | ||
|
||
container.setAttribute("style", "display: block;"); | ||
|
||
mermaidjs.initialize({ startOnLoad: false }); | ||
mermaidjs.render('preparedScheme', fn(), (svg) => { | ||
container.innerHTML = svg; | ||
}, container); | ||
} | ||
|
||
const reset = () => getCleanContainer().setAttribute("style", "display: none;"); | ||
|
||
export { render, reset }; |
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 think this line is equivalent and can be reverted
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.
You're right, It was just a missing test.