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 option to Parse.java to print AST in JSON #53

Closed
StoneyJackson opened this issue Sep 20, 2023 · 10 comments · Fixed by #68
Closed

Add option to Parse.java to print AST in JSON #53

StoneyJackson opened this issue Sep 20, 2023 · 10 comments · Fixed by #68

Comments

@StoneyJackson
Copy link
Member

StoneyJackson commented Sep 20, 2023

Parent issue: #52

Design decisions that need to be made

  • What should JSON format be?
  • What should option name be?
  • Add option to ProcessFiles.java?
  • Implement as an ITrace?
@StoneyJackson
Copy link
Member Author

What should [the] JSON format be?

We should consider using Jackson (no relation to me). It serializes POJOs (plain old Java objects) into JSON format.

On the other end, we should look at Jsonic. It deserializes JSON into instances of given classes.

The question is, are the two compatible? Can the JSON generated by Jackson be read by Jsonic? If so, this could be easy. If not, we may need to programmatically massage the output of Jackson's output so that it is suitable for Jsonic.

If these work, then we don't have to invent a JSON format. They may also save us from writing a lot of code.

@StoneyJackson
Copy link
Member Author

Implement as an ITrace?

I don't think this will work (at least it won't be easy). Instead, I think we'll need to implement a new class that inherits from ProcessFiles, named something like JsonTree, that's like Parse but uses Jackson to produce JSON of the parsed tree.

What should option name be?

  1. --tree=json
  2. --json
  3. -j

The first is probably overkill at the moment since we are implementing only a single output format.

The third is a little vague.

The second is probably the best if possible.


If we want this to run as an option to Parse (i.e., parse --json)...

Options of Parse are handled by ProcessFiles, which Parse inherits. Assuming JsonTree also inherits from ProcessFiles, the Parse implementation would need to construct a JsonTree and use it instead of itself. But the JsonTree would need to ignore this option, lest it infinitely, recursively constructs JsonTree objects. The problem is that the base class ProcessFiles is where options are handled, but we need the --json option to be handled in the subclasses. So add a method toJsonTree(argv) in ProcessFiles that constructs and returns a JsonTree by default. Then override this method in JsonTree to return this. Call this method from ProcessFiles when handling the --json option.

@StoneyJackson
Copy link
Member Author

@Rarity-Belle @WilliamBowery Please explore/try-out Jackson and Jsonic and see if they will work for us.

@StoneyJackson
Copy link
Member Author

Issue: Jackson is a 3rd party Java package. To use 3rd party Java packages, it's best to use a package manager to resolve dependencies. In Java, that's Maven or Gradle. Gradle is the newer of the two. So @Rarity-Belle and @WilliamBowery are figuring out how to set up Gradle with PLCC.

@StoneyJackson
Copy link
Member Author

Gradle is a bad idea because it would add a dependency for end users (Gradle).

Instead, we're going to try to ship the jar files for Jackson with PLCC and then include them in the classpath when compiling and running plcc stuff. This would not require an extra dependency.

@jashelio
Copy link
Collaborator

jashelio commented Oct 6, 2023

The Parse program builds an AST from classes that PLCC creates. The user can only enhance those classes. If we want to display the AST visually, and the the tree already exists as a collection of instances of PLCC-created classes, why not have PLCC add code to those classes to display the tree?

Also, these notes don't say why viewing the AST in JSON is useful.

@fosler
Copy link
Collaborator

fosler commented Oct 6, 2023 via email

@StoneyJackson
Copy link
Member Author

@jashelio See #52 for rationale. Short answer: we need to export the AST from a parser in Java to a Rep-loop in Python.

@StoneyJackson
Copy link
Member Author

@fosler

The goal:

cat program | java -cp Java Parse --json | python -m plcc.rep

If we use the output of -t we'll have to write a custom parser in Python to read it. We believe it will be easier to use a Java library to dump the AST as JSON, and use a Python library to read the AST as JSON.

Once we can dump ASTs in JSON, then we can use plethora of tools and libraries that work with JSON.

@StoneyJackson StoneyJackson linked a pull request Nov 8, 2023 that will close this issue
StoneyJackson added a commit that referenced this issue Nov 29, 2023
To print an AST for a program in JSON format, do the following.

    plccmk --json_ast GRAMMAR_FILE
    parse --json_ast < PROGRAM_FILE

Adds a new dependency: the Jackson JSON library for java.
We distribute jars (in src/lib) from the following projects
under the Apache 2.0 license:

* https://github.com/FasterXML/jackson-core
* https://github.com/FasterXML/jackson-databind
* https://github.com/FasterXML/jackson-annotations

If manually compiling and running the generated Java code,
you'll need to add these jars to your classpath.

If you don't use the new --json_ast option,
these jars do not need to be in your classpath.

---

Closes #53 - Add option to print parsejava to print ast in json

Co-authored-by: Madison Mason <[email protected]>
Co-authored-by: Rarity Van Lone <[email protected]>
Co-authored-by: Stoney Jackson <[email protected]>
Copy link

🎉 This issue has been resolved in version 6.1.0 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants