1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
|
!># Parsing of package source files
!>
!> This module exposes two functions, `[[parse_f_source]]` and `[[parse_c_source]]`,
!> which perform a rudimentary parsing of fortran and c source files
!> in order to extract information required for module dependency tracking.
!>
!> Both functions additionally calculate and store a file digest (hash) which
!> is used by the backend ([[fpm_backend]]) to skip compilation of unmodified sources.
!>
!> Both functions return an instance of the [[srcfile_t]] type.
!>
!> For more information, please read the documentation for each function:
!>
!> - `[[parse_f_source]]`
!> - `[[parse_c_source]]`
!>
module fpm_source_parsing
use fpm_error, only: error_t, file_parse_error, fatal_error
use fpm_strings, only: string_t, string_cat, len_trim, split, lower, str_ends_with, fnv_1a, is_fortran_name
use fpm_model, only: srcfile_t, &
FPM_UNIT_UNKNOWN, FPM_UNIT_PROGRAM, FPM_UNIT_MODULE, &
FPM_UNIT_SUBMODULE, FPM_UNIT_SUBPROGRAM, &
FPM_UNIT_CSOURCE, FPM_UNIT_CHEADER, FPM_SCOPE_UNKNOWN, &
FPM_SCOPE_LIB, FPM_SCOPE_DEP, FPM_SCOPE_APP, FPM_SCOPE_TEST
use fpm_filesystem, only: read_lines, read_lines_expanded
implicit none
private
public :: parse_f_source, parse_c_source
character(15), parameter :: INTRINSIC_MODULE_NAMES(*) = &
['iso_c_binding ', &
'iso_fortran_env', &
'ieee_arithmetic', &
'ieee_exceptions', &
'ieee_features ', &
'omp_lib ']
contains
!> Parsing of free-form fortran source files
!>
!> The following statements are recognised and parsed:
!>
!> - `Module`/`submodule`/`program` declaration
!> - Module `use` statement
!> - `include` statement
!>
!> @note Intrinsic modules used by sources are not listed in
!> the `modules_used` field of source objects.
!>
!> @note Submodules are treated as normal modules which `use` their
!> corresponding parent modules.
!>
!>### Parsing limitations
!>
!> __Statements must not continued onto another line
!> except for an `only:` list in the `use` statement.__
!>
!> This is supported:
!>
!>```fortran
!> use my_module, only: &
!> my_var, my_function, my_subroutine
!>```
!>
!> This is __NOT supported:__
!>
!>```fortran
!> use &
!> my_module
!>```
!>
function parse_f_source(f_filename,error) result(f_source)
character(*), intent(in) :: f_filename
type(srcfile_t) :: f_source
type(error_t), allocatable, intent(out) :: error
integer :: stat
integer :: fh, n_use, n_include, n_mod, i, j, ic, pass
type(string_t), allocatable :: file_lines(:), file_lines_lower(:)
character(:), allocatable :: temp_string, mod_name, string_parts(:)
f_source%file_name = f_filename
open(newunit=fh,file=f_filename,status='old')
file_lines = read_lines_expanded(fh)
close(fh)
! for efficiency in parsing make a lowercase left-adjusted copy of the file
! Need a copy because INCLUDE (and #include) file arguments are case-sensitive
file_lines_lower=file_lines
do i=1,size(file_lines_lower)
file_lines_lower(i)%s=adjustl(lower(file_lines_lower(i)%s))
enddo
! Ignore empty files, returned as FPM_UNIT_UNKNOWN
if (len_trim(file_lines_lower) < 1) return
f_source%digest = fnv_1a(file_lines)
do pass = 1,2
n_use = 0
n_include = 0
n_mod = 0
file_loop: do i=1,size(file_lines_lower)
! Skip lines that are continued: not statements
if (i > 1) then
ic = index(file_lines_lower(i-1)%s,'!')
if (ic < 1) then
ic = len(file_lines_lower(i-1)%s)
end if
temp_string = trim(file_lines_lower(i-1)%s(1:ic))
if (len(temp_string) > 0 .and. index(temp_string,'&') == len(temp_string)) then
cycle
end if
end if
! Process 'USE' statements
if (index(file_lines_lower(i)%s,'use ') == 1 .or. &
index(file_lines_lower(i)%s,'use::') == 1) then
if (index(file_lines_lower(i)%s,'::') > 0) then
temp_string = split_n(file_lines_lower(i)%s,delims=':',n=2,stat=stat)
if (stat /= 0) then
call file_parse_error(error,f_filename, &
'unable to find used module name',i, &
file_lines_lower(i)%s,index(file_lines_lower(i)%s,'::'))
return
end if
mod_name = split_n(temp_string,delims=' ,',n=1,stat=stat)
if (stat /= 0) then
call file_parse_error(error,f_filename, &
'unable to find used module name',i, &
file_lines_lower(i)%s)
return
end if
else
mod_name = split_n(file_lines_lower(i)%s,n=2,delims=' ,',stat=stat)
if (stat /= 0) then
call file_parse_error(error,f_filename, &
'unable to find used module name',i, &
file_lines_lower(i)%s)
return
end if
end if
if (.not.is_fortran_name(mod_name)) then
cycle
end if
if (any([(index(mod_name,trim(INTRINSIC_MODULE_NAMES(j)))>0, &
j=1,size(INTRINSIC_MODULE_NAMES))])) then
cycle
end if
n_use = n_use + 1
if (pass == 2) then
f_source%modules_used(n_use)%s = mod_name
end if
end if
! Process 'INCLUDE' statements
ic = index(file_lines_lower(i)%s,'include')
if ( ic == 1 ) then
ic = index(lower(file_lines(i)%s),'include')
if (index(adjustl(file_lines(i)%s(ic+7:)),'"') == 1 .or. &
index(adjustl(file_lines(i)%s(ic+7:)),"'") == 1 ) then
n_include = n_include + 1
if (pass == 2) then
f_source%include_dependencies(n_include)%s = &
& split_n(file_lines(i)%s,n=2,delims="'"//'"',stat=stat)
if (stat /= 0) then
call file_parse_error(error,f_filename, &
'unable to find include file name',i, &
file_lines(i)%s)
return
end if
end if
end if
end if
! Extract name of module if is module
if (index(file_lines_lower(i)%s,'module ') == 1) then
! Remove any trailing comments
ic = index(file_lines_lower(i)%s,'!')-1
if (ic < 1) then
ic = len(file_lines_lower(i)%s)
end if
temp_string = trim(file_lines_lower(i)%s(1:ic))
! R1405 module-stmt := "MODULE" module-name
! module-stmt has two space-delimited parts only
! (no line continuations)
call split(temp_string,string_parts,' ')
if (size(string_parts) /= 2) then
cycle
end if
mod_name = trim(adjustl(string_parts(2)))
if (scan(mod_name,'=(&')>0 ) then
! Ignore these cases:
! module <something>&
! module =*
! module (i)
cycle
end if
if (.not.is_fortran_name(mod_name)) then
call file_parse_error(error,f_filename, &
'empty or invalid name for module',i, &
file_lines_lower(i)%s, index(file_lines_lower(i)%s,mod_name))
return
end if
n_mod = n_mod + 1
if (pass == 2) then
f_source%modules_provided(n_mod) = string_t(mod_name)
end if
f_source%unit_type = FPM_UNIT_MODULE
end if
! Extract name of submodule if is submodule
if (index(file_lines_lower(i)%s,'submodule') == 1) then
mod_name = split_n(file_lines_lower(i)%s,n=3,delims='()',stat=stat)
if (stat /= 0) then
call file_parse_error(error,f_filename, &
'unable to get submodule name',i, &
file_lines_lower(i)%s)
return
end if
if (.not.is_fortran_name(mod_name)) then
call file_parse_error(error,f_filename, &
'empty or invalid name for submodule',i, &
file_lines_lower(i)%s, index(file_lines_lower(i)%s,mod_name))
return
end if
n_mod = n_mod + 1
temp_string = split_n(file_lines_lower(i)%s,n=2,delims='()',stat=stat)
if (stat /= 0) then
call file_parse_error(error,f_filename, &
'unable to get submodule ancestry',i, &
file_lines_lower(i)%s)
return
end if
f_source%unit_type = FPM_UNIT_SUBMODULE
n_use = n_use + 1
if (pass == 2) then
if (index(temp_string,':') > 0) then
temp_string = temp_string(index(temp_string,':')+1:)
end if
if (.not.is_fortran_name(temp_string)) then
call file_parse_error(error,f_filename, &
'empty or invalid name for submodule parent',i, &
file_lines_lower(i)%s, index(file_lines_lower(i)%s,temp_string))
return
end if
f_source%modules_used(n_use)%s = temp_string
f_source%modules_provided(n_mod)%s = mod_name
end if
end if
! Detect if contains a program
! (no modules allowed after program def)
if (index(file_lines_lower(i)%s,'program ') == 1) then
temp_string = split_n(file_lines_lower(i)%s,n=2,delims=' ',stat=stat)
if (stat == 0) then
if (scan(temp_string,'=(')>0 ) then
! Ignore:
! program =*
! program (i) =*
cycle
end if
end if
f_source%unit_type = FPM_UNIT_PROGRAM
end if
end do file_loop
! Default to subprogram unit type
if (f_source%unit_type == FPM_UNIT_UNKNOWN) then
f_source%unit_type = FPM_UNIT_SUBPROGRAM
end if
if (pass == 1) then
allocate(f_source%modules_used(n_use))
allocate(f_source%include_dependencies(n_include))
allocate(f_source%modules_provided(n_mod))
end if
end do
end function parse_f_source
!> Parsing of c source files
!>
!> The following statements are recognised and parsed:
!>
!> - `#include` preprocessor statement
!>
function parse_c_source(c_filename,error) result(c_source)
character(*), intent(in) :: c_filename
type(srcfile_t) :: c_source
type(error_t), allocatable, intent(out) :: error
integer :: fh, n_include, i, pass, stat
type(string_t), allocatable :: file_lines(:)
c_source%file_name = c_filename
if (str_ends_with(lower(c_filename), ".c")) then
c_source%unit_type = FPM_UNIT_CSOURCE
elseif (str_ends_with(lower(c_filename), ".h")) then
c_source%unit_type = FPM_UNIT_CHEADER
end if
allocate(c_source%modules_used(0))
allocate(c_source%modules_provided(0))
open(newunit=fh,file=c_filename,status='old')
file_lines = read_lines(fh)
close(fh)
! Ignore empty files, returned as FPM_UNIT_UNKNOWN
if (len_trim(file_lines) < 1) then
c_source%unit_type = FPM_UNIT_UNKNOWN
return
end if
c_source%digest = fnv_1a(file_lines)
do pass = 1,2
n_include = 0
file_loop: do i=1,size(file_lines)
! Process 'INCLUDE' statements
if (index(adjustl(lower(file_lines(i)%s)),'#include') == 1 .and. &
index(file_lines(i)%s,'"') > 0) then
n_include = n_include + 1
if (pass == 2) then
c_source%include_dependencies(n_include)%s = &
& split_n(file_lines(i)%s,n=2,delims='"',stat=stat)
if (stat /= 0) then
call file_parse_error(error,c_filename, &
'unable to get c include file',i, &
file_lines(i)%s,index(file_lines(i)%s,'"'))
return
end if
end if
end if
end do file_loop
if (pass == 1) then
allocate(c_source%include_dependencies(n_include))
end if
end do
end function parse_c_source
!> Split a string on one or more delimeters
!> and return the nth substring if it exists
!>
!> n=0 will return the last item
!> n=-1 will return the penultimate item etc.
!>
!> stat = 1 on return if the index
!> is not found
!>
function split_n(string,delims,n,stat) result(substring)
character(*), intent(in) :: string
character(*), intent(in) :: delims
integer, intent(in) :: n
integer, intent(out) :: stat
character(:), allocatable :: substring
integer :: i
character(:), allocatable :: string_parts(:)
call split(string,string_parts,delims)
if (n<1) then
i = size(string_parts) + n
if (i < 1) then
stat = 1
return
end if
else
i = n
end if
if (i>size(string_parts)) then
stat = 1
return
end if
substring = trim(adjustl(string_parts(i)))
stat = 0
end function split_n
end module fpm_source_parsing
|