Coverage for src/castep_linter/fortran/parser.py: 100%

1"""Tests for Fortran code in CASTEP"""

2 from importlib import resources as impresources

3 from typing import Generator

4

5 from tree_sitter import Language, Parser, Tree

6

7 from castep_linter.fortran.fortran_node import FortranNode

8

9

10 def traverse_tree(tree: Tree) -> Generator[FortranNode, None, None]:

11 """Traverse a tree-sitter tree in a depth first search"""

12 cursor = tree.walk()

13

14 reached_root = False

15 while not reached_root:

16 yield FortranNode(cursor.node)

17

18 if cursor.goto_first_child():

19 continue

20

21 if cursor.goto_next_sibling():

22 continue

23

24 retracing = True

25 while retracing:

26 if not cursor.goto_parent():

27 retracing = False

28 reached_root = True

29

30 if cursor.goto_next_sibling():

31 retracing = False

32

33

34 def get_fortran_parser():

35 """Get a tree-sitter-fortran parser from src"""

36

37 tree_sitter_src_ref = impresources.files("castep_linter") / "tree_sitter_fortran"

38 with impresources.as_file(tree_sitter_src_ref) as tree_sitter_src:

39 fortran_language = Language(tree_sitter_src / "fortran.so", "fortran")

40

41 parser = Parser()

42 parser.set_language(fortran_language)

43 return parser