-
Notifications
You must be signed in to change notification settings - Fork 216
feat(common): replace solidity-parser with Slang
#3811
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
Open
ggiraldez
wants to merge
15
commits into
latticexyz:main
Choose a base branch
from
NomicFoundation:slang-integration
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 13 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
94a9822
Add unit tests
ggiraldez fc9bf7a
Add slang
ggiraldez b9e3fe3
migrated `parseSystem()`
OmarTawfik b4f2804
Revert change in `parseSystem` return type and fix finding the system…
ggiraldez da8b98b
Migrate `findSymbolImport` and finish `parseSystem`
ggiraldez cae94d7
Test custom error definition in `contractToInterface`
ggiraldez cbf156a
Migrate `contractToInterface`
ggiraldez d0f0433
Migrate `resolveInheritedSymbols`
ggiraldez 0866a4d
Add `payable` as a state mutability function attribute
ggiraldez a45e8ae
Remove solidity-parser, rename utility functions
ggiraldez 5ad0f31
Deduplicate symbol imports at the end of `contractToInterface`
ggiraldez 4509177
Apply changes from code review
ggiraldez eb92fa1
Apply suggestion from code review
ggiraldez b4a80d8
Use `LanguageFacts.inferLanguageVersions` instead of hard-coding 0.8.24
ggiraldez 8365cb9
Default to latest version for parsing if inference fails
ggiraldez File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
packages/common/src/codegen/utils/contractToInterface.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
| import { contractToInterface } from "./contractToInterface"; | ||
|
|
||
| const source = ` | ||
| import { Data } from "./lib.sol"; | ||
|
|
||
| contract World { | ||
| constructor() {} | ||
| fallback(bytes calldata input) external payable returns (bytes memory) {} | ||
| receive() external payable {} | ||
| function update(Data.Entity[] memory entities, uint delta) public returns (bool) {} | ||
| function visible() view external {} | ||
| function invisible() internal {} | ||
| error UpdateError(uint entityId); | ||
| } | ||
| `; | ||
|
|
||
| describe("contractToInterface", () => { | ||
| it("extracts public functions and errors from contract", () => { | ||
| const { functions, errors, symbolImports } = contractToInterface(source, "World"); | ||
| expect(functions).toStrictEqual([ | ||
| { | ||
| name: "update", | ||
| parameters: ["Data.Entity[] memory entities", "uint delta"], | ||
| returnParameters: ["bool"], | ||
| stateMutability: "", | ||
| }, | ||
| { | ||
| name: "visible", | ||
| parameters: [], | ||
| returnParameters: [], | ||
| stateMutability: "view", | ||
| }, | ||
| ]); | ||
| expect(errors).toStrictEqual([ | ||
| { | ||
| name: "UpdateError", | ||
| parameters: ["uint entityId"], | ||
| }, | ||
| ]); | ||
| expect(symbolImports).toStrictEqual([ | ||
| { | ||
| path: "./lib.sol", | ||
| symbol: "Data", | ||
| }, | ||
| ]); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
18 changes: 18 additions & 0 deletions
18
packages/common/src/codegen/utils/findContractOrInterfaceNode.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| import { Cursor, Query } from "@nomicfoundation/slang/cst"; | ||
|
|
||
| export function findContractOrInterfaceNode(root: Cursor, contractOrInterfaceName: string): Cursor | undefined { | ||
| for (const result of root.query([ | ||
| Query.create(` | ||
| [ContractDefinition | ||
| name: ["${contractOrInterfaceName}"] | ||
| ] | ||
| `), | ||
| Query.create(` | ||
| [InterfaceDefinition | ||
| name: ["${contractOrInterfaceName}"] | ||
| ] | ||
| `), | ||
| ])) { | ||
| return result.root; | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Is there a way to avoid hardcoding the version here?
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 believe we already parse the foundry config and pass that through in a few places in MUD tools, so we could pass that through here to avoid hardcoding it
Uh oh!
There was an error while loading. Please reload this page.
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.
actually, why wouldn't the parser use the pragma to determine which version to use? 🤔
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 wasn't sure what the version policy wrt Solidity was for MUD. Is it bound to the MUD release, or is completely user defined? Otherwise, Slang can infer the version from the source code, or we can extract it as a constant, or pass it through as a parameter. Happy to implement whatever solution works best for you.
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 went with the version inference path. It should be the safest and most durable.
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.
@alvrs @frolic I noticed that the currently used solc version in this repository is updated manually every now and then. Example: aabd307
In that commit, while projects/existing code specifically use
0.8.24, the Solidity files contain a more permissive pragmapragma solidity >=0.8.24;. When using the Slang parser, the version can be hard-coded/updated manually along the rest of the codebase like the commit above, or inferred automatically (as suggested by @ggiraldez's comment here). But with inference, a higher version could be inferred, since it would match>=0.8.24. Up to you on which solution you prefer.Please let us know if you have any other feedback.