Coverage for src/castep_linter/fortran/call_expression.py: 79%

1"""Module holding the CallExpression type"""

2 from typing import ClassVar, List, Optional, Tuple

3

4 from castep_linter.fortran.argument_parser import ArgParser, ArgType

5 from castep_linter.fortran.fortran_node import Fortran, FortranNode

6 from castep_linter.fortran.fortran_statement import FortranStatementParser

7 from castep_linter.fortran.identifier import Identifier

8

9

10 class CallExpression(FortranStatementParser):

11 """Class representing a fortran call expression"""

12

13 ALLOWED_NODES: ClassVar[List[Fortran]] = [Fortran.CALL_EXPRESSION, Fortran.SUBROUTINE_CALL]

14

15 def __init__(self, call_expression_node: FortranNode) -> None:

16 super().__init__(call_expression_node)

17

18 self.name = _get_name(call_expression_node)

19

20 try:

21 arg_list = call_expression_node.get(Fortran.ARGUMENT_LIST)

22 except KeyError:

23 arg_list = None

24

25 self.args = ArgParser(arg_list)

26

27 def get_arg(

28 self, keyword: Identifier, position: Optional[int] = None

29 ) -> Tuple[ArgType, FortranNode]:

30 """Get an argument from the call expression"""

31 return self.args.get(keyword, position)

32

33 def __str__(self):

34 return f"{self.name=} {self.args=}"

35

36

37 def _get_name(node: FortranNode) -> Identifier:

38 try:

39 return Identifier.from_node(node.get(Fortran.IDENTIFIER))

40 except KeyError:

41 return Identifier("")