1 unstable release
0.1.0 | Feb 11, 2025 |
---|
#1828 in Parser implementations
100 downloads per month
Used in fortran-descriptor-parser
14KB
244 lines
fortran_descriptor_parser
fortran_descriptor_parser
provides a macro descriptor_parser
to generate a parser at compile time
to parse bytes which are formatted according to a fortran format edit descriptor. The macro uses
a simplified syntax inspired by the format used in the fortran descriptors.
Syntax
nTw
n
= number of repetitions
T
= the type to parse (I: i32, S: String, F: f32, D: f64)
w
= number of bytes to take
Basic example
use fortran_descriptor_parser::descriptor_parser;
let input = " -0.31415D+01";
let f = descriptor_parser!("D13")(input.as_bytes()).unwrap();
assert_eq!(f, -3.1415);
Explained example
use fortran_descriptor_parser::descriptor_parser;
// input is a string generated by a fortran program.
// For this example a format edit descriptor like the following has been used:
// FORMAT(I10, A10, E13.5, D13.5) and values 1, "Test", -3.1415, 3.1415
let input = " 1 Test -0.31415E+01 0.31415D+01";
// The descriptor_parser macro generates four parsers in a tuple
// bytes 0..10 get parsed into an i32
// bytes 10..20 get parsed into a String
// bytes 20..33 get parsed into a f32
// bytes 33..46 get parsed into a f64
let (i, s, f, d) = descriptor_parser!("I10,S10,F13,D13")(input.as_bytes()).unwrap();
assert_eq!(i, 1);
assert_eq!(s, "Test");
assert_eq!(f, -3.1415);
assert_eq!(d, 3.1415);
Repetitions
use fortran_descriptor_parser::descriptor_parser;
// repetition of single elements can be done like this
descriptor_parser!("3I10");
// which is equivalent to
descriptor_parser!("I10,I10,I10");
// repetition of multiple elements is also supported
descriptor_parser!("2(I5,S5)");
// which is equivalent to
descriptor_parser!("I5,S5,I5,S5");
The macro can parse i32
, f32
, f64
and Strings
Dependencies
~74KB