Skip to content

Commit

Permalink
a better attributes query
Browse files Browse the repository at this point in the history
  • Loading branch information
apozharski committed Sep 5, 2024
1 parent 263240f commit 6449b60
Showing 1 changed file with 27 additions and 10 deletions.
37 changes: 27 additions & 10 deletions sphinxcontrib/mat_tree_sitter_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
import re

# rpath = "../../../syscop/software/nosnoc/+nosnoc/Options.m"
rpath = (
"/home/anton/tools/matlabdomain/tests/test_data/submodule/f_ellipsis_empty_output.m"
)
# rpath = "/home/anton/tools/matlabdomain/tests/test_data/submodule/f_empty_output.m"
rpath = "/home/anton/tools/matlabdomain/tests/test_data/ClassWithMethodAttributes.m"
# rpath = "/home/anton/tools/matlabdomain/tests/test_data/f_with_dummy_argument.m"

tree_sitter_ver = tuple([int(sec) for sec in version("tree_sitter").split(".")])
if tree_sitter_ver[1] == 21:
Expand Down Expand Up @@ -36,7 +34,17 @@
"""
)

q_attributes = ML_LANG.query("""(attribute (identifier) @name (_)? @value)""")
q_attributes = ML_LANG.query(
"""(attribute
(identifier) @name
[
(identifier) @value
(string) @value
(metaclass_operator) @value
(cell) @value
]?)
"""
)

q_supers = ML_LANG.query("""[(identifier) @secs "."]+ """)

Expand Down Expand Up @@ -110,15 +118,15 @@
[
(identifier) @outputs
(multioutput_variable
[(identifier) @outputs _]+
[[(identifier) (ignored_argument)] @outputs _]+
)
]
)?
_*
name: (identifier) @name
_*
(function_arguments
[(identifier) @params _]*
[(identifier) @params (ignored_argument) @params _]*
)?
_*
[(arguments_statement) @argblocks _]*
Expand Down Expand Up @@ -685,6 +693,7 @@ def _parse_attributes(self, attrs_nodes):
attrs = {}
if attrs_nodes is not None:
for attr_node in attrs_nodes:
print(attr_node.sexp())
_, attr_match = q_attributes.matches(attr_node)[0]
name = attr_match.get("name").text.decode("utf-8")
value_node = attr_match.get("value")
Expand All @@ -695,11 +704,19 @@ def _parse_attributes(self, attrs_nodes):


if __name__ == "__main__":
parser = Parser(ML_LANG)
tree_sitter_ver = tuple([int(sec) for sec in version("tree_sitter").split(".")])
if tree_sitter_ver[1] == 21:
parser = Parser()
parser.set_language(ML_LANG)
else:
parser = Parser(ML_LANG)

with open(rpath, "rb") as f:
data = f.read()

tree = parser.parse(data)
# class_parser = MatClassParser(tree.root_node)
fun_parser = MatFunctionParser(tree.root_node)
class_parser = MatClassParser(tree.root_node)
# fun_parser = MatFunctionParser(tree.root_node)
import pdb

pdb.set_trace()

0 comments on commit 6449b60

Please sign in to comment.