From 858e0e4424a03ce7013d57e8a5013289d08c03fa Mon Sep 17 00:00:00 2001 From: Eric Brown Date: Mon, 21 Oct 2024 07:33:22 -0700 Subject: [PATCH] Add tree-sitter-java, tree-sitter-go, tree-sitter-python (#5102) This is a follow-on to PR #5099 which added tree-sitter to the packages. This change adds tree-sitter-java, tree-sitter-go, tree-sitter-python packages and respective unit tests. Java, Go, and Python are popular languages, so its useful to add these grammars so a user can parse source code in those languages. --- .gitignore | 1 + docs/project/changelog.md | 3 ++ packages/tree-sitter-go/meta.yaml | 23 ++++++++++ .../tree-sitter-go/test_tree_sitter_go.py | 41 ++++++++++++++++++ packages/tree-sitter-java/meta.yaml | 23 ++++++++++ .../tree-sitter-java/test_tree_sitter_java.py | 42 +++++++++++++++++++ packages/tree-sitter-python/meta.yaml | 23 ++++++++++ .../test_tree_sitter_python.py | 39 +++++++++++++++++ 8 files changed, 195 insertions(+) create mode 100644 packages/tree-sitter-go/meta.yaml create mode 100644 packages/tree-sitter-go/test_tree_sitter_go.py create mode 100644 packages/tree-sitter-java/meta.yaml create mode 100644 packages/tree-sitter-java/test_tree_sitter_java.py create mode 100644 packages/tree-sitter-python/meta.yaml create mode 100644 packages/tree-sitter-python/test_tree_sitter_python.py diff --git a/.gitignore b/.gitignore index 7d8d30f90..f59bc6264 100644 --- a/.gitignore +++ b/.gitignore @@ -41,3 +41,4 @@ pytest-pyodide tools/symlinks xbuildenv/ .pyodide-xbuildenv* +DS_Store diff --git a/docs/project/changelog.md b/docs/project/changelog.md index d491c990e..857571dde 100644 --- a/docs/project/changelog.md +++ b/docs/project/changelog.md @@ -87,6 +87,9 @@ myst: - Added `iminuit` 2.29.1 {pr}`4767`, {pr}`5072` - Added `arro3-core`, `arro3-io`, and `arro3-compute` 0.3.0, 0.4.0, 0.4.1 {pr}`5020`, {pr}`5095`, {pr}`5104` - Added `tree-sitter` 0.23.0 {pr}`5099` +- Added `tree-sitter-go` 0.23.1 {pr}`5102` +- Added `tree-sitter-java` 0.23.2 {pr}`5102` +- Added `tree-sitter-python` 0.23.2 {pr}`5102` ## Version 0.26.3 diff --git a/packages/tree-sitter-go/meta.yaml b/packages/tree-sitter-go/meta.yaml new file mode 100644 index 000000000..303a1bf4f --- /dev/null +++ b/packages/tree-sitter-go/meta.yaml @@ -0,0 +1,23 @@ +package: + name: tree-sitter-go + version: 0.23.1 + top-level: + - tree_sitter_go +source: + # Have to use github source to work around broken sdist + # https://github.com/tree-sitter/tree-sitter-go/pull/151 + url: https://github.com/tree-sitter/tree-sitter-go/archive/refs/tags/v0.23.1.tar.gz + sha256: 4173bafc4c59be78642a0faf1bed8cb3854458aa59c506e8f51001a9f28da09b +requirements: + run: + - tree-sitter + host: + - tree-sitter +about: + home: https://github.com/tree-sitter/tree-sitter-go + PyPI: https://pypi.org/project/tree-sitter-go + summary: Go grammar for tree-sitter + license: MIT +extra: + recipe-maintainers: + - ericwb diff --git a/packages/tree-sitter-go/test_tree_sitter_go.py b/packages/tree-sitter-go/test_tree_sitter_go.py new file mode 100644 index 000000000..a732395fa --- /dev/null +++ b/packages/tree-sitter-go/test_tree_sitter_go.py @@ -0,0 +1,41 @@ +from pytest_pyodide import run_in_pyodide + + +@run_in_pyodide(packages=["tree-sitter-go"]) +def test_tree_sitter_go(selenium): + import textwrap + + import tree_sitter_go + from tree_sitter import Language, Parser + + GO_LANGUAGE = Language(tree_sitter_go.language()) + parser = Parser(GO_LANGUAGE) + + code = bytes( + textwrap.dedent( + """ + func foo() { + if bar { + baz() + } + } + """ + ), + "utf-8", + ) + tree = parser.parse(code) + root_node = tree.root_node + + assert str(root_node) == ( + "(source_file " + "(function_declaration " + "name: (identifier) " + "parameters: (parameter_list) " + "body: (block " + "(if_statement " + "condition: (identifier) " + "consequence: (block " + "(expression_statement (call_expression " + "function: (identifier) " + "arguments: (argument_list))))))))" + ) diff --git a/packages/tree-sitter-java/meta.yaml b/packages/tree-sitter-java/meta.yaml new file mode 100644 index 000000000..b8c479dd4 --- /dev/null +++ b/packages/tree-sitter-java/meta.yaml @@ -0,0 +1,23 @@ +package: + name: tree-sitter-java + version: 0.23.2 + top-level: + - tree_sitter_java +source: + # Have to use github url to work around broken sdist: + # https://github.com/tree-sitter/tree-sitter-java/pull/188 + url: https://github.com/tree-sitter/tree-sitter-java/archive/refs/tags/v0.23.2.tar.gz + sha256: 062fe5746deb4e362ccb987228e7b41bfc017eb2b91a0413a7447719abaa07b2 +requirements: + run: + - tree-sitter + host: + - tree-sitter +about: + home: https://github.com/tree-sitter/tree-sitter-java + PyPI: https://pypi.org/project/tree-sitter-java + summary: Java grammar for tree-sitter + license: MIT +extra: + recipe-maintainers: + - ericwb diff --git a/packages/tree-sitter-java/test_tree_sitter_java.py b/packages/tree-sitter-java/test_tree_sitter_java.py new file mode 100644 index 000000000..49726e0d4 --- /dev/null +++ b/packages/tree-sitter-java/test_tree_sitter_java.py @@ -0,0 +1,42 @@ +from pytest_pyodide import run_in_pyodide + + +@run_in_pyodide(packages=["tree-sitter-java"]) +def test_tree_sitter_java(selenium): + import textwrap + + import tree_sitter_java + from tree_sitter import Language, Parser + + JAV_LANGUAGE = Language(tree_sitter_java.language()) + parser = Parser(JAV_LANGUAGE) + + code = bytes( + textwrap.dedent( + """ + void foo() { + if (bar) { + baz(); + } + } + """ + ), + "utf-8", + ) + tree = parser.parse(code) + root_node = tree.root_node + + assert str(root_node) == ( + "(program " + "(method_declaration " + "type: (void_type) " + "name: (identifier) " + "parameters: (formal_parameters) " + "body: (block " + "(if_statement " + "condition: (parenthesized_expression (identifier)) " + "consequence: (block " + "(expression_statement (method_invocation " + "name: (identifier) " + "arguments: (argument_list))))))))" + ) diff --git a/packages/tree-sitter-python/meta.yaml b/packages/tree-sitter-python/meta.yaml new file mode 100644 index 000000000..655178f97 --- /dev/null +++ b/packages/tree-sitter-python/meta.yaml @@ -0,0 +1,23 @@ +package: + name: tree-sitter-python + version: 0.23.2 + top-level: + - tree_sitter_python +source: + # Have to use github url to work around broken sdist: + # https://github.com/tree-sitter/tree-sitter-python/pull/285 + url: https://github.com/tree-sitter/tree-sitter-python/archive/refs/tags/v0.23.2.tar.gz + sha256: b38e5b1f5237377b506109978af76422975bda5859165835e3c7b5afee987383 +requirements: + run: + - tree-sitter + host: + - tree-sitter +about: + home: https://github.com/tree-sitter/tree-sitter-python + PyPI: https://pypi.org/project/tree-sitter-python + summary: Python grammar for tree-sitter + license: MIT +extra: + recipe-maintainers: + - ericwb diff --git a/packages/tree-sitter-python/test_tree_sitter_python.py b/packages/tree-sitter-python/test_tree_sitter_python.py new file mode 100644 index 000000000..c34bc165f --- /dev/null +++ b/packages/tree-sitter-python/test_tree_sitter_python.py @@ -0,0 +1,39 @@ +from pytest_pyodide import run_in_pyodide + + +@run_in_pyodide(packages=["tree-sitter-python"]) +def test_tree_sitter_python(selenium): + import textwrap + + import tree_sitter_python + from tree_sitter import Language, Parser + + PY_LANGUAGE = Language(tree_sitter_python.language()) + parser = Parser(PY_LANGUAGE) + + code = bytes( + textwrap.dedent( + """ + def foo(): + if bar: + baz() + """ + ), + "utf-8", + ) + tree = parser.parse(code) + root_node = tree.root_node + + assert str(root_node) == ( + "(module " + "(function_definition " + "name: (identifier) " + "parameters: (parameters) " + "body: (block " + "(if_statement " + "condition: (identifier) " + "consequence: (block " + "(expression_statement (call " + "function: (identifier) " + "arguments: (argument_list))))))))" + )