Loading...
Searching...
No Matches
Parser

Definition

Fortran Preprocessor (fpx) – core parsing and preprocessing module

This module implements a full-featured, modern Fortran preprocessor supporting:

The preprocessor is designed to be standards-conforming where possible while adding useful extensions (variadic macros, better diagnostics, include path handling).

Examples

  1. Preprocess a file to stdout:
    call preprocess('input.F90')
  2. Preprocess a file and write to another file:
    call preprocess('src/main.F90', 'preprocessed/main.F90')
  3. Use in a build system with unit numbers:
    integer :: iu, ou
    open(newunit=iu, file='input.F90')
    open(newunit=ou, file='output.F90')
    call preprocess(iu, ou)
    close(iu); close(ou)
    ...
  4. Interactive mode (stdin to stdout):
    $ ./fpx
    [in] #define PI 3.1415926535
    [out]
    [in] real :: x = pi*2
    [out] real :: x = 3.1415926535*2
    [in] (empty line or 'quit' to exit)

Data Types

interface  preprocess
 Generic interface to start preprocessing from various sources/sinks. More...
 

Methods

◆ preprocess_file()

subroutine preprocess_file ( character(*), intent(in) filepath,
character(*), intent(in), optional outputfile )
private

Preprocess a file and write result to an optional output file (default: stdout) Opens the input file, determines the base filename for error messages, opens the output file if requested, and delegates to the unit-to-unit routine.

Parameters
[in]filepathPath to the input source file
[in]outputfileOptional path to the output file; if absent output goes to stdout

Remarks

Definition at line 111 of file parser.f90.

◆ preprocess_file_to_unit()

subroutine preprocess_file_to_unit ( character(*), intent(in) ifile,
integer, intent(in) ounit )
private

Preprocess a file and write to an already-open output unit.

Parameters
[in]ifileInput filename
[in]ounitOutput unit (already open for writing)

Remarks

Definition at line 179 of file parser.f90.

◆ preprocess_unit()

subroutine preprocess_unit ( integer, intent(in) iunit,
integer, intent(in) ounit,
type(macro), dimension(:), intent(inout), allocatable macros,
logical, intent(in) from_include )
private

Worker routine that reads lines, handles continuations, comments and directives This is the main loop that:

  • reads lines with interactive prompt when iunit==stdin
  • handles both \ and & continuations
  • strips or preserves comments appropriately
  • calls process_line() for directive processing and macro expansion
  • stitches lines when Fortran continuation (&) is active
    Parameters
    [in]iunitInput unit
    [in]ounitOutput unit
    [in,out]macros(:)Current macro table (passed by value between include levels)
    [in]from_includeTrue if called recursively from include
    Remarks

Definition at line 256 of file parser.f90.

◆ preprocess_unit_to_file()

subroutine preprocess_unit_to_file ( integer, intent(in) iunit,
character(*), intent(in) ofile )
private

Preprocess from an already-open input unit and write to a file.

Parameters
[in]iunitInput unit (must already be open for reading)
[in]ofileOutput filename

Remarks

Definition at line 151 of file parser.f90.

◆ preprocess_unit_to_unit()

subroutine preprocess_unit_to_unit ( integer, intent(in) iunit,
integer, intent(in) ounit )
private

Core preprocessing routine: read from iunit, write to ounit Sets up a clean macro environment for the top-level file, resets conditional compilation state, and calls the worker routine.

Parameters
[in]iunitInput unit
[in]ounitOutput unit

Remarks

Definition at line 210 of file parser.f90.

◆ process_line()

recursive character(:) function, allocatable process_line ( character(*), intent(in) current_line,
integer, intent(in) ounit,
character(*), intent(in) filepath,
integer, intent(in) linenum,
type(macro), dimension(:), intent(inout), allocatable macros,
logical, intent(out) stch )
private

Process a single (possibly continued) line – handles directives and macro expansion Responsibilities:

  • Strip or terminate C-style block comments (/* ... */)
  • Detect and delegate preprocessor directives (#define, #include, conditionals, etc.)
  • Perform macro expansion when the line is in an active conditional block
  • Return whether the next line should be stitched (for Fortran & continuation inside macros)
    Parameters
    [in]current_lineInput line (already continued and trimmed)
    [in]ounitOutput unit (used only for diagnostics inside called routines)
    [in]filepathCurrent file name (for error messages)
    [in]linenumCurrent line number (for error messages)
    [in,out]macros(:)Macro table
    [out]stchSet to .true. if the expanded line ends with & (stitch next line)
    Returns
    Processed line (directives removed, macros expanded)
    Remarks

Definition at line 368 of file parser.f90.