Macro management and expansion core of the fpx Fortran preprocessor.
This module implements a complete, standards-inspired macro system supporting:
- Object-like and function-like macros
- Variadic macros (... and __VA_ARGS__)
- Parameter stringification (#param) and token pasting (##)
- Built-in predefined macros: __FILE__, __FILENAME__, __LINE__, __DATE__, __TIME__, __TIMESTAMP__
- Recursive expansion with circular dependency detection via digraph analysis
- Dynamic macro table of macro objects with efficient addition, lookup, removal
- Full support for nested macro calls and proper argument handling
The design allows safe, repeated expansion while preventing infinite recursion. All operations are container-agnostic using allocatable dynamic arrays.
- Define and use simple macros:
type(macro), allocatable :: macros(:)
call add(macros, macro('PI', '3.1415926535'))
call add(macros, macro('MSG(x)', 'print *, ″Hello ″, x'))
print *, expand_all(context('area = PI * r**2', 10, './circle.F90', 'circle'), macros, stitch, .false., .false.)
- Variadic macro with stringification and pasting:
call add(macros, macro('DEBUG_PRINT(...)', 'print *, ″DEBUG[″, __FILE__, ″:″, __LINE__, ″]: ″, __VA_ARGS__'))
print *, expand_all(context('DEBUG_PRINT(″value =″, x)', 42, 'test.F90', 'text'), macros, stitch, .false., false)
- Token pasting with ##:
call add(macros, macro('MAKE_VAR(name,num)', 'var_name_##num'))
print *, expand_all(context('real :: MAKE_VAR(temp,42)', 5, 'file.F90', 'file'), macros, stitch, .false., .false.)
| character(:) function, allocatable, public expand_macros |
( |
character(*), intent(in) | line, |
|
|
type(macro), dimension(:), intent(in) | macros, |
|
|
logical, intent(out) | stitch, |
|
|
logical, intent(in) | implicit_conti, |
|
|
type(context), intent(in) | ctx ) |
Core recursive macro expander (handles function-like, variadic, #, ##).
Performs actual macro replacement with full support for:
- Function-like macros with argument collection
- Stringification (#param)
- Token pasting (##)
- Variadic macros and __VA_ARGS__, __VA_OPT__
- Recursion with cycle detection via digraph
- Proper handling of nested parentheses and quoted strings
- Parameters
-
| [in] | line | Line to be expanded |
| [in] | macros | Current macro table |
| [out] | stitch | .true. if final line ends with '&' |
| [in] | implicit_conti | If .true., implicit continuation is permitted |
| [in] | ctx | Context |
- Returns
- Line with user-defined macros expanded (predefined tokens untouched)
Remarks
Definition at line 299 of file macro.f90.