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 toml parser #4678

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft

Add toml parser #4678

wants to merge 4 commits into from

Conversation

steplica
Copy link

What's changed?

What's your motivation?

Anything in particular you'd like reviewers to focus on?

Anyone you would like to review specifically?

Have you considered any alternatives or workarounds?

Any additional context

Checklist

  • I've added unit tests to cover both positive and negative cases
  • I've read and applied the recipe conventions and best practices
  • I've used the IntelliJ IDEA auto-formatter on affected files

@shanman190
Copy link
Contributor

Dropping this here as well, since Steven and I are collaborating in parallel on the effort trying different things here and there. This PR will serve as the final source to be contributed for TOML support.

https://github.com/shanman190/rewrite-toml

@shanman190 shanman190 added the enhancement New feature or request label Nov 15, 2024
@shanman190
Copy link
Contributor

Related to #4400. A future PR will take the work done here and integrate it with the rewrite-gradle module.

Copy link
Contributor

@knutwannheden knutwannheden left a comment

Choose a reason for hiding this comment

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

Nice to see work on the TOML parser! I added a few small comments.

* <p>The default implementation returns the result of calling
* {@link #visitChildren} on {@code ctx}.</p>
*/
@Override public T visitArray_values(TomlParser.Array_valuesContext ctx) { return visitChildren(ctx); }
Copy link
Contributor

Choose a reason for hiding this comment

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

Our ANTLR grammars typically use camel case as that then also yields nicer Java identifiers out-of-the-box.

Copy link
Contributor

Choose a reason for hiding this comment

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

That's definitely fair. This was just pulled directly from https://github.com/antlr/grammars-v4/tree/master/toml.

Do you recall if future syncs with the upstream just usually remake those particular kinds of adjustments for other language bindings? This was the thought running through my head as to why I hadn't mentioned the same thing to Steve.

Copy link
Contributor

Choose a reason for hiding this comment

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

In my experience these grammars are hardly ever updated and quite often also contain small bugs, so I wouldn't worry too much. If we ever need to fix a bug we can still look at the history of the upstream grammar.

Copy link
Author

Choose a reason for hiding this comment

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

@knutwannheden Do you just want the methods in TomlParserBaseVisitor to be camel case? Or do you also want all the types/methods in TomlParser (also generated by ANTLR)

Copy link
Contributor

Choose a reason for hiding this comment

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

@steplica, Knut is meaning to update the ANTLR grammar to remove the underscores there which then translates throughout the rest of the code as well.

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Some suggestions could not be made:

  • rewrite-toml/src/main/java/org/openrewrite/toml/tree/TomlContainer.java
    • lines 123-124
  • rewrite-toml/src/main/java/org/openrewrite/toml/tree/TomlLeftPadded.java
    • lines 38-39
  • rewrite-toml/src/main/java/org/openrewrite/toml/tree/TomlRightPadded.java
    • lines 100-101

Space after = visitSpace(right.getAfter(), p);
return (after == right.getAfter() && t == right.getElement()) ? right : new TomlRightPadded<>(t, after, right.getMarkers());
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
}
}

_decisionToDFA[i] = new DFA(_ATN.getDecisionState(i), i);
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
}
}

_decisionToDFA[i] = new DFA(_ATN.getDecisionState(i), i);
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
}
}

* <p>The default implementation does nothing.</p>
*/
@Override public void visitErrorNode(ErrorNode node) { }
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
}
}

* {@link #visitChildren} on {@code ctx}.</p>
*/
@Override public T visitArrayTable(TomlParser.Array_tableContext ctx) { return visitChildren(ctx); }
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
}
}


@Test
void Table_With_SuperTable_Declared_After_Dotted_Table() {
rewriteRun(toml("""
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
rewriteRun(toml("""
rewriteRun(toml(
"""


@Test
void Table_With_SubTable_Added_Later() {
rewriteRun(toml("""
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
rewriteRun(toml("""
rewriteRun(toml(
"""


@Test
void InlineTable() {
rewriteRun(toml("""
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
rewriteRun(toml("""
rewriteRun(toml(
"""


@Test
void ArrayOfTables() {
rewriteRun(toml("""
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
rewriteRun(toml("""
rewriteRun(toml(
"""

Comment on lines +439 to +440
rewriteRun(toml("""
[[fruits]]
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
rewriteRun(toml("""
[[fruits]]
rewriteRun(toml(
"""

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request parser-toml
Projects
Status: In Progress
Development

Successfully merging this pull request may close these issues.

4 participants