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

[feature] Add PyO3 integration for ast-grep #389

Closed
22 of 25 tasks
HerringtonDarkholme opened this issue May 17, 2023 · 2 comments
Closed
22 of 25 tasks

[feature] Add PyO3 integration for ast-grep #389

HerringtonDarkholme opened this issue May 17, 2023 · 2 comments
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@HerringtonDarkholme
Copy link
Member

HerringtonDarkholme commented May 17, 2023

We only have napi integration for nodeJS. Supporting Python community is a good idea!

https://github.com/PyO3/pyo3 looks like a good library for this.

  • Support SgNode methods on par with Napi
  • [pyo3] Implement directory based file traversal (a.k.a napi's findInFiles) #699
    • parse_files
    • find_in_files
  • Implement better runtime properties of SgNode via magic methods
    • __hash__
    • __eq__
    • __str__
    • __repr__
    • __get_item__ for access single meta-variable without None assertion
  • Implement better typing info for Python users
    • config
    • rule
    • SgNode
    • SgRoot
    • Range/Pos
  • Error Handling
@HerringtonDarkholme HerringtonDarkholme added enhancement New feature or request help wanted Extra attention is needed labels May 17, 2023
@HerringtonDarkholme
Copy link
Member Author

Implementation note:
We can use Py<T> to represent the Python managed ast-grep root.
See https://pyo3.rs/v0.12.3/types.html?highlight=Py#pyt-and-pyobject

First, we need to have a PyRoot

struct PyRoot {
  root: Root,
}

Secondly, we need to add definition of Node. The node

struct PyNode {
  node: Node<'static>,
  root_ref: Py<PyRoot>,
}

The crux here is that Node's lifetime is 'static, meaning the node is also managed by Python garbage collector.
The root_ref increments the PyRoot's ref_count so it will not be gc-ed if PyRoot is not referenced directly but still referenced by PyNode (It's ugly and expose low-level details. I prefer napi's sharedReference abstraction)

Finally we need GIL to get a reference of PyNode, this will involve some lifetime cast.

HerringtonDarkholme added a commit that referenced this issue Oct 9, 2023
github-merge-queue bot pushed a commit that referenced this issue Oct 9, 2023
HerringtonDarkholme added a commit that referenced this issue Oct 16, 2023
HerringtonDarkholme added a commit that referenced this issue Oct 16, 2023
HerringtonDarkholme added a commit that referenced this issue Oct 18, 2023
HerringtonDarkholme added a commit that referenced this issue Oct 18, 2023
HerringtonDarkholme added a commit that referenced this issue Oct 23, 2023
HerringtonDarkholme added a commit that referenced this issue Oct 23, 2023
HerringtonDarkholme added a commit that referenced this issue Oct 23, 2023
HerringtonDarkholme added a commit that referenced this issue Oct 23, 2023
HerringtonDarkholme added a commit that referenced this issue Oct 24, 2023
HerringtonDarkholme added a commit that referenced this issue Oct 24, 2023
HerringtonDarkholme added a commit that referenced this issue Oct 24, 2023
HerringtonDarkholme added a commit that referenced this issue Oct 25, 2023
HerringtonDarkholme added a commit that referenced this issue Oct 27, 2023
HerringtonDarkholme added a commit that referenced this issue Oct 31, 2023
HerringtonDarkholme added a commit that referenced this issue Nov 5, 2023
@HerringtonDarkholme
Copy link
Member Author

Closed!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
Archived in project
Development

No branches or pull requests

1 participant