9
9
fortran_subroutine , fortran_function , fortran_block , fortran_select , \
10
10
fortran_type , fortran_enum , fortran_int , fortran_var , fortran_meth , \
11
11
fortran_associate , fortran_do , fortran_where , fortran_if , \
12
- INTERFACE_TYPE_ID , SELECT_TYPE_ID , SUBMODULE_TYPE_ID
12
+ INTERFACE_TYPE_ID , SELECT_TYPE_ID , SUBMODULE_TYPE_ID , DO_TYPE_ID
13
13
PY3K = sys .version_info >= (3 , 0 )
14
14
if not PY3K :
15
15
import io
21
21
IMPLICIT_REGEX = re .compile (r'[ ]*IMPLICIT[ ]+([a-z]*)' , re .I )
22
22
SUB_MOD_REGEX = re .compile (r'[ ]*(PURE|IMPURE|ELEMENTAL|RECURSIVE)+' , re .I )
23
23
SUB_REGEX = re .compile (r'[ ]*SUBROUTINE[ ]+([a-z0-9_]+)' , re .I )
24
- END_SUB_WORD = r'SUBROUTINE'
24
+ END_SUB_REGEX = re . compile ( r'SUBROUTINE' , re . I )
25
25
FUN_REGEX = re .compile (r'[ ]*FUNCTION[ ]+([a-z0-9_]+)' , re .I )
26
26
RESULT_REGEX = re .compile (r'RESULT[ ]*\(([a-z0-9_]*)\)' , re .I )
27
- END_FUN_WORD = r'FUNCTION'
27
+ END_FUN_REGEX = re . compile ( r'FUNCTION' , re . I )
28
28
MOD_REGEX = re .compile (r'[ ]*MODULE[ ]+([a-z0-9_]+)' , re .I )
29
- END_MOD_WORD = r'MODULE'
29
+ END_MOD_REGEX = re . compile ( r'MODULE' , re . I )
30
30
SUBMOD_REGEX = re .compile (r'[ ]*SUBMODULE[ ]*\(' , re .I )
31
- END_SMOD_WORD = r'SUBMODULE'
32
- END_PRO_WORD = r' PROCEDURE'
31
+ END_SMOD_REGEX = re . compile ( r'SUBMODULE' , re . I )
32
+ END_PRO_REGEX = re . compile ( r'(MODULE)?[ ]* PROCEDURE', re . I )
33
33
BLOCK_REGEX = re .compile (r'[ ]*([a-z_][a-z0-9_]*[ ]*:[ ]*)?BLOCK(?![a-z0-9_])' , re .I )
34
- END_BLOCK_WORD = r'BLOCK'
34
+ END_BLOCK_REGEX = re . compile ( r'BLOCK' , re . I )
35
35
DO_REGEX = re .compile (r'[ ]*(?:[a-z_][a-z0-9_]*[ ]*:[ ]*)?DO([ ]+[0-9]*|$)' , re .I )
36
- END_DO_WORD = r'DO'
36
+ END_DO_REGEX = re . compile ( r'DO' , re . I )
37
37
WHERE_REGEX = re .compile (r'[ ]*WHERE[ ]*\(' , re .I )
38
- END_WHERE_WORD = r'WHERE'
38
+ END_WHERE_REGEX = re . compile ( r'WHERE' , re . I )
39
39
IF_REGEX = re .compile (r'[ ]*(?:[a-z_][a-z0-9_]*[ ]*:[ ]*)?IF[ ]*\(' , re .I )
40
40
THEN_REGEX = re .compile (r'\)[ ]*THEN$' , re .I )
41
- END_IF_WORD = r'IF'
41
+ END_IF_REGEX = re . compile ( r'IF' , re . I )
42
42
ASSOCIATE_REGEX = re .compile (r'[ ]*ASSOCIATE[ ]*\(' , re .I )
43
- END_ASSOCIATE_WORD = r'ASSOCIATE'
43
+ END_ASSOCIATE_REGEX = re . compile ( r'ASSOCIATE' , re . I )
44
44
END_FIXED_REGEX = re .compile (r'[ ]*([0-9]*)[ ]*CONTINUE' , re .I )
45
45
SELECT_REGEX = re .compile (r'[ ]*(?:[a-z_][a-z0-9_]*[ ]*:[ ]*)?SELECT[ ]*'
46
46
r'(CASE|TYPE)[ ]*\(([a-z0-9_=> ]*)' , re .I )
47
47
SELECT_TYPE_REGEX = re .compile (r'[ ]*(TYPE|CLASS)[ ]+IS[ ]*\(([a-z0-9_ ]*)' , re .I )
48
48
SELECT_DEFAULT_REGEX = re .compile (r'[ ]*CLASS[ ]+DEFAULT' , re .I )
49
- END_SELECT_WORD = r'SELECT'
49
+ END_SELECT_REGEX = re . compile ( r'SELECT' , re . I )
50
50
PROG_REGEX = re .compile (r'[ ]*PROGRAM[ ]+([a-z0-9_]+)' , re .I )
51
- END_PROG_WORD = r'PROGRAM'
51
+ END_PROG_REGEX = re . compile ( r'PROGRAM' , re . I )
52
52
INT_REGEX = re .compile (r'[ ]*(ABSTRACT)?[ ]*INTERFACE[ ]*([a-z0-9_]*)' , re .I )
53
- END_INT_WORD = r'INTERFACE'
53
+ END_INT_REGEX = re . compile ( r'INTERFACE' , re . I )
54
54
END_WORD_REGEX = re .compile (r'[ ]*END[ ]*(DO|WHERE|IF|BLOCK|ASSOCIATE|SELECT'
55
55
r'|TYPE|ENUM|MODULE|SUBMODULE|PROGRAM|INTERFACE'
56
56
r'|SUBROUTINE|FUNCTION|PROCEDURE)?([ ]+|$)' , re .I )
57
57
TYPE_DEF_REGEX = re .compile (r'[ ]*(TYPE)[, :]+' , re .I )
58
58
EXTENDS_REGEX = re .compile (r'EXTENDS[ ]*\(([a-z0-9_]*)\)' , re .I )
59
59
GENERIC_PRO_REGEX = re .compile (r'[ ]*(GENERIC)[ ]*::[ ]*[a-z]' , re .I )
60
60
GEN_ASSIGN_REGEX = re .compile (r'(ASSIGNMENT|OPERATOR)\(' , re .I )
61
- END_TYPED_WORD = r'TYPE'
61
+ END_TYPED_REGEX = re . compile ( r'TYPE' , re . I )
62
62
ENUM_DEF_REGEX = re .compile (r'[ ]*ENUM[, ]+' , re .I )
63
- END_ENUMD_WORD = r'ENUM'
63
+ END_ENUMD_REGEX = re . compile ( r'ENUM' , re . I )
64
64
NAT_VAR_REGEX = re .compile (r'[ ]*(INTEGER|REAL|DOUBLE[ ]*PRECISION|COMPLEX'
65
65
r'|DOUBLE[ ]*COMPLEX|CHARACTER|LOGICAL|PROCEDURE'
66
66
r'|EXTERNAL|CLASS|TYPE)' , re .I )
@@ -1387,18 +1387,21 @@ def process_file(file_obj, close_open_scopes, debug=False, pp_defs={}, include_d
1387
1387
line_no_comment = line
1388
1388
line_post_comment = None
1389
1389
# Test for scope end
1390
- if file_ast .END_SCOPE_WORD is not None :
1390
+ if file_ast .END_SCOPE_REGEX is not None :
1391
1391
match = END_WORD_REGEX .match (line_no_comment )
1392
1392
# Handle end statement
1393
1393
if match is not None :
1394
- end_scope_word = match .group (1 )
1395
- if (end_scope_word is not None ) or (match .group (2 ) == "" ):
1396
- if end_scope_word is not None :
1397
- end_scope_word = end_scope_word .strip ().upper ()
1398
- if ((end_scope_word != file_ast .END_SCOPE_WORD )
1399
- and (file_ast .current_scope .req_named_end () or (end_scope_word is not None ))
1394
+ end_scope_word = None
1395
+ if match .group (1 ) is None :
1396
+ end_scope_word = ""
1397
+ if (file_ast .current_scope .req_named_end ()
1400
1398
and (file_ast .current_scope is not file_ast .none_scope )):
1401
1399
file_ast .end_errors .append ([line_number , file_ast .current_scope .sline ])
1400
+ else :
1401
+ scope_match = file_ast .END_SCOPE_REGEX .match (line_no_comment [match .start (1 ):])
1402
+ if (scope_match is not None ):
1403
+ end_scope_word = scope_match .group (0 )
1404
+ if end_scope_word is not None :
1402
1405
if (file_ast .current_scope .get_type () == SELECT_TYPE_ID ) \
1403
1406
and (file_ast .current_scope .is_type_region ()):
1404
1407
file_ast .end_scope (line_number )
@@ -1407,7 +1410,7 @@ def process_file(file_obj, close_open_scopes, debug=False, pp_defs={}, include_d
1407
1410
print ('{1} !!! END "{2}" scope({0})' .format (line_number , line .strip (), end_scope_word ))
1408
1411
continue
1409
1412
# Look for old-style end of DO loops with line labels
1410
- if (file_ast .END_SCOPE_WORD == 'DO' ) and (line_label is not None ):
1413
+ if (file_ast .current_scope . get_type () == DO_TYPE_ID ) and (line_label is not None ):
1411
1414
did_close = False
1412
1415
while (len (block_id_stack ) > 0 ) and (line_label == block_id_stack [- 1 ]):
1413
1416
file_ast .end_scope (line_number )
@@ -1531,32 +1534,32 @@ def process_file(file_obj, close_open_scopes, debug=False, pp_defs={}, include_d
1531
1534
print ('{1} !!! VARIABLE statement({0})' .format (line_number , line .strip ()))
1532
1535
elif obj_type == 'mod' :
1533
1536
new_mod = fortran_module (file_ast , line_number , obj_info )
1534
- file_ast .add_scope (new_mod , END_MOD_WORD )
1537
+ file_ast .add_scope (new_mod , END_MOD_REGEX )
1535
1538
if (debug ):
1536
1539
print ('{1} !!! MODULE statement({0})' .format (line_number , line .strip ()))
1537
1540
elif obj_type == 'smod' :
1538
1541
new_smod = fortran_submodule (file_ast , line_number , obj_info .name , ancestor_name = obj_info .parent )
1539
- file_ast .add_scope (new_smod , END_SMOD_WORD )
1542
+ file_ast .add_scope (new_smod , END_SMOD_REGEX )
1540
1543
if (debug ):
1541
1544
print ('{1} !!! SUBMODULE statement({0})' .format (line_number , line .strip ()))
1542
1545
elif obj_type == 'prog' :
1543
1546
new_prog = fortran_program (file_ast , line_number , obj_info )
1544
- file_ast .add_scope (new_prog , END_PROG_WORD )
1547
+ file_ast .add_scope (new_prog , END_PROG_REGEX )
1545
1548
if (debug ):
1546
1549
print ('{1} !!! PROGRAM statement({0})' .format (line_number , line .strip ()))
1547
1550
elif obj_type == 'sub' :
1548
1551
keywords , _ = map_keywords (obj_info .keywords )
1549
1552
new_sub = fortran_subroutine (file_ast , line_number , obj_info .name , args = obj_info .args ,
1550
1553
mod_flag = obj_info .mod_flag , keywords = keywords )
1551
- file_ast .add_scope (new_sub , END_SUB_WORD )
1554
+ file_ast .add_scope (new_sub , END_SUB_REGEX )
1552
1555
if (debug ):
1553
1556
print ('{1} !!! SUBROUTINE statement({0})' .format (line_number , line .strip ()))
1554
1557
elif obj_type == 'fun' :
1555
1558
keywords , _ = map_keywords (obj_info .keywords )
1556
1559
new_fun = fortran_function (file_ast , line_number , obj_info .name , args = obj_info .args ,
1557
1560
mod_flag = obj_info .mod_flag , keywords = keywords ,
1558
1561
return_type = obj_info .return_type , result_var = obj_info .return_var )
1559
- file_ast .add_scope (new_fun , END_FUN_WORD )
1562
+ file_ast .add_scope (new_fun , END_FUN_REGEX )
1560
1563
if obj_info .return_type is not None :
1561
1564
keywords , keyword_info = map_keywords (obj_info .return_type [1 ])
1562
1565
new_obj = fortran_var (file_ast , line_number , obj_info .name ,
@@ -1570,7 +1573,7 @@ def process_file(file_obj, close_open_scopes, debug=False, pp_defs={}, include_d
1570
1573
block_counter += 1
1571
1574
name = '#BLOCK{0}' .format (block_counter )
1572
1575
new_block = fortran_block (file_ast , line_number , name )
1573
- file_ast .add_scope (new_block , END_BLOCK_WORD , req_container = True )
1576
+ file_ast .add_scope (new_block , END_BLOCK_REGEX , req_container = True )
1574
1577
if (debug ):
1575
1578
print ('{1} !!! BLOCK statement({0})' .format (line_number , line .strip ()))
1576
1579
elif obj_type == 'do' :
@@ -1579,7 +1582,7 @@ def process_file(file_obj, close_open_scopes, debug=False, pp_defs={}, include_d
1579
1582
if obj_info != '' :
1580
1583
block_id_stack .append (obj_info )
1581
1584
new_do = fortran_do (file_ast , line_number , name )
1582
- file_ast .add_scope (new_do , END_DO_WORD , req_container = True )
1585
+ file_ast .add_scope (new_do , END_DO_REGEX , req_container = True )
1583
1586
if (debug ):
1584
1587
print ('{1} !!! DO statement({0})' .format (line_number , line .strip ()))
1585
1588
elif obj_type == 'where' :
@@ -1588,14 +1591,14 @@ def process_file(file_obj, close_open_scopes, debug=False, pp_defs={}, include_d
1588
1591
do_counter += 1
1589
1592
name = '#WHERE{0}' .format (do_counter )
1590
1593
new_do = fortran_where (file_ast , line_number , name )
1591
- file_ast .add_scope (new_do , END_WHERE_WORD , req_container = True )
1594
+ file_ast .add_scope (new_do , END_WHERE_REGEX , req_container = True )
1592
1595
if (debug ):
1593
1596
print ('{1} !!! WHERE statement({0})' .format (line_number , line .strip ()))
1594
1597
elif obj_type == 'assoc' :
1595
1598
block_counter += 1
1596
1599
name = '#ASSOC{0}' .format (block_counter )
1597
1600
new_assoc = fortran_associate (file_ast , line_number , name )
1598
- file_ast .add_scope (new_assoc , END_ASSOCIATE_WORD , req_container = True )
1601
+ file_ast .add_scope (new_assoc , END_ASSOCIATE_REGEX , req_container = True )
1599
1602
for bound_var in obj_info :
1600
1603
binding_split = bound_var .split ('=>' )
1601
1604
if len (binding_split ) == 2 :
@@ -1610,14 +1613,14 @@ def process_file(file_obj, close_open_scopes, debug=False, pp_defs={}, include_d
1610
1613
if_counter += 1
1611
1614
name = '#IF{0}' .format (if_counter )
1612
1615
new_if = fortran_if (file_ast , line_number , name )
1613
- file_ast .add_scope (new_if , END_IF_WORD , req_container = True )
1616
+ file_ast .add_scope (new_if , END_IF_REGEX , req_container = True )
1614
1617
if (debug ):
1615
1618
print ('{1} !!! IF statement({0})' .format (line_number , line .strip ()))
1616
1619
elif obj_type == 'select' :
1617
1620
select_counter += 1
1618
1621
name = '#SELECT{0}' .format (select_counter )
1619
1622
new_select = fortran_select (file_ast , line_number , name , obj_info )
1620
- file_ast .add_scope (new_select , END_SELECT_WORD , req_container = True )
1623
+ file_ast .add_scope (new_select , END_SELECT_REGEX , req_container = True )
1621
1624
new_var = new_select .create_binding_variable (
1622
1625
file_ast , line_number , '{0}({1})' .format (obj_info .desc , obj_info .binding ), obj_info .type
1623
1626
)
@@ -1630,14 +1633,14 @@ def process_file(file_obj, close_open_scopes, debug=False, pp_defs={}, include_d
1630
1633
new_type = fortran_type (file_ast , line_number , obj_info .name , keywords )
1631
1634
if obj_info .parent is not None :
1632
1635
new_type .set_inherit (obj_info .parent )
1633
- file_ast .add_scope (new_type , END_TYPED_WORD , req_container = True )
1636
+ file_ast .add_scope (new_type , END_TYPED_REGEX , req_container = True )
1634
1637
if (debug ):
1635
1638
print ('{1} !!! TYPE statement({0})' .format (line_number , line .strip ()))
1636
1639
elif obj_type == 'enum' :
1637
1640
block_counter += 1
1638
1641
name = '#ENUM{0}' .format (block_counter )
1639
1642
new_enum = fortran_enum (file_ast , line_number , name )
1640
- file_ast .add_scope (new_enum , END_ENUMD_WORD , req_container = True )
1643
+ file_ast .add_scope (new_enum , END_ENUMD_REGEX , req_container = True )
1641
1644
if (debug ):
1642
1645
print ('{1} !!! ENUM statement({0})' .format (line_number , line .strip ()))
1643
1646
elif obj_type == 'int' :
@@ -1646,12 +1649,12 @@ def process_file(file_obj, close_open_scopes, debug=False, pp_defs={}, include_d
1646
1649
int_counter += 1
1647
1650
name = '#GEN_INT{0}' .format (int_counter )
1648
1651
new_int = fortran_int (file_ast , line_number , name , abstract = obj_info .abstract )
1649
- file_ast .add_scope (new_int , END_INT_WORD , req_container = True )
1652
+ file_ast .add_scope (new_int , END_INT_REGEX , req_container = True )
1650
1653
if (debug ):
1651
1654
print ('{1} !!! INTERFACE statement({0})' .format (line_number , line .strip ()))
1652
1655
elif obj_type == 'gen' :
1653
1656
new_int = fortran_int (file_ast , line_number , obj_info .bound_name , abstract = False )
1654
- file_ast .add_scope (new_int , END_INT_WORD , req_container = True )
1657
+ file_ast .add_scope (new_int , END_INT_REGEX , req_container = True )
1655
1658
for pro_link in obj_info .pro_links :
1656
1659
file_ast .add_int_member (pro_link )
1657
1660
file_ast .end_scope (line_number )
@@ -1666,7 +1669,7 @@ def process_file(file_obj, close_open_scopes, debug=False, pp_defs={}, include_d
1666
1669
print ('{1} !!! INTERFACE-PRO statement({0})' .format (line_number , line .strip ()))
1667
1670
elif file_ast .current_scope .get_type () == SUBMODULE_TYPE_ID :
1668
1671
new_impl = fortran_scope (file_ast , line_number , obj_info [0 ])
1669
- file_ast .add_scope (new_impl , END_PRO_WORD )
1672
+ file_ast .add_scope (new_impl , END_PRO_REGEX )
1670
1673
if (debug ):
1671
1674
print ('{1} !!! PROCEDURE-IMPL statement({0})' .format (line_number , line .strip ()))
1672
1675
elif obj_type == 'use' :
0 commit comments