Global logging, ANSI-colored diagnostics, and pretty error/warning reporting for fpx.
This module is the central place for all human-readable output in the fpx preprocessor. It provides:
- Full ANSI color and style support (bold, underline, colors, etc.)
- Structured diagnostic messages with source context, line numbers, and caret markers
- Pretty-printed multi-line error/warning/help/note/info reports
- Label-based highlighting of specific code ranges (like rustc-style diagnostics)
- Recursive sub-diagnostic support for nested explanations
Designed to produce modern, readable, IDE-friendly output similar to rustc, clang, or cargo. When nocolor = .true. (or terminal does not support ANSI), falls back to plain text.
Examples
- Simple error message
print '(A)', render(diagnostic_report( &
level_error, &
message='unexpected token', &
label=label_type('expected expression', 8, 3)), &
'a = +')
- Colored message (used internally for verbose logging):
use fpx_logging
print
'(A)',
render(
'Macro expanded: PI = 3.14159')
..
logical, public verbose
Master switch for verbose diagnostic output Default value is .false. (quiet mode)....
Generic renderer for diagnostics and source excerpts.
Full diagnostic report (like a compiler error):
character(*), parameter :: input = &
'# This is a TOML document.' // nl // &
'title = ' // nl // &
'[owner]' // nl // &
'name = ' // nl // &
'dob = 1979-05-27T07:32:00-08:00 # First class dates' // nl // &
'[database]' // nl // &
'server = ' // nl // &
'ports = [ 8001, 8001, 8002 ]' // nl // &
'connection_max = 5000' // nl // &
'enabled = true' // nl // &
'[servers]' // nl // &
' # Indentation (tabs and/or spaces) is allowed but not required' // nl // &
' [servers.alpha]' // nl // &
' ip = ' // nl // &
' dc = ' // nl // &
' [servers.beta]' // nl // &
' ip = ' // nl // &
' dc = ' // nl // &
'[title]' // nl // &
'data = [ [, ], [1, 2] ]' // nl // &
'# Line breaks are OK when inside arrays' // nl // &
'hosts = [' // nl // &
' ,' // nl // &
' ' // nl // &
message=, &
source=, &
input)
end
Structured compiler diagnostic.
Diagnostic label identifying a region of source text.
Output might look like (colored in terminal):
error: duplicated key 'title' found
--> example.toml:19:2-6
|
1 | # This is a TOML document.
2 | title =
| ----- first defined here
3 | [owner]
:
18 | dc =
19 | [title]
| ^^^^^ table 'title' redefined here
20 | data = [ [, ], [1, 2] ]
|
- ANSI style & color reference (used internally)
- Styles: BOLD_ON, UNDERLINE_ON, INVERSE_ON, STRIKETHROUGH_ON, ...
- Foreground: RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE, ...
- Background: same as foreground but prefixed with BG_
- Note
- This code is adapted from pretty-diagnostics. The visual presentation is inspired by modern compiler diagnostics such as rustc and clang, while being adapted for Fortran workflows.