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

24 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2023-11-23 18:07 +0000

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

2from typing import ClassVar, List, Optional, Tuple 

3 

4from castep_linter.fortran.argument_parser import ArgParser, ArgType 

5from castep_linter.fortran.fortran_node import Fortran, FortranNode 

6from castep_linter.fortran.fortran_statement import FortranStatementParser 

7from castep_linter.fortran.identifier import Identifier 

8 

9 

10class 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 

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

38 try: 

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

40 except KeyError: 

41 return Identifier("")