From 6fef4d6ffcab0fec8518adcb05458cba5dbeac25 Mon Sep 17 00:00:00 2001 From: Arthur Cohen Date: Tue, 23 Apr 2024 13:38:58 +0200 Subject: gccrs: libgrust: Add format_parser library Compile libformat_parser and link to it. gcc/rust/ChangeLog: * Make-lang.in: Compile libformat_parser. * ast/rust-fmt.cc: New FFI definitions. * ast/rust-fmt.h: Likewise. * expand/rust-macro-builtins.cc (MacroBuiltin::format_args_handler): Call into libformat_parser. * expand/rust-macro-builtins.h: Define format_args!() handler proper. libgrust/ChangeLog: * libformat_parser/Cargo.lock: New file. * libformat_parser/Cargo.toml: New file. * libformat_parser/generic_format_parser/Cargo.toml: New file. * libformat_parser/generic_format_parser/src/lib.rs: New file. * libformat_parser/src/bin.rs: New file. * libformat_parser/src/lib.rs: New file. --- libgrust/libformat_parser/src/bin.rs | 7 ++++++ libgrust/libformat_parser/src/lib.rs | 41 ++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 libgrust/libformat_parser/src/bin.rs create mode 100644 libgrust/libformat_parser/src/lib.rs (limited to 'libgrust/libformat_parser/src') diff --git a/libgrust/libformat_parser/src/bin.rs b/libgrust/libformat_parser/src/bin.rs new file mode 100644 index 00000000000..4b1f903ad5f --- /dev/null +++ b/libgrust/libformat_parser/src/bin.rs @@ -0,0 +1,7 @@ +use libformat_parser::rust; + +fn main() { + dbg!(rust::collect_pieces( + std::env::args().nth(1).unwrap().as_str() + )); +} diff --git a/libgrust/libformat_parser/src/lib.rs b/libgrust/libformat_parser/src/lib.rs new file mode 100644 index 00000000000..e6dc16eeb49 --- /dev/null +++ b/libgrust/libformat_parser/src/lib.rs @@ -0,0 +1,41 @@ +//! FFI interface for `rustc_format_parser` + +// what's the plan? Have a function return something that can be constructed into a vector? +// or an iterator? + +use std::ffi::CStr; + +// TODO: Use rustc's version here #3 +use generic_format_parser::Piece; + +// FIXME: Rename? +pub mod rust { + use generic_format_parser::{ParseMode, Parser, Piece}; + + pub fn collect_pieces(input: &str) -> Vec> { + // let parser = Parser::new(); + let parser = Parser::new(input, None, None, true, ParseMode::Format); + + parser.into_iter().collect() + } +} + +#[repr(C)] +pub struct PieceSlice { + base_ptr: *const Piece<'static /* FIXME: That's wrong */>, + len: usize, +} + +#[no_mangle] +pub extern "C" fn collect_pieces(input: *const libc::c_char) -> PieceSlice { + // FIXME: Add comment + let str = unsafe { CStr::from_ptr(input) }; + + // FIXME: No unwrap + let pieces = rust::collect_pieces(str.to_str().unwrap()); + + PieceSlice { + base_ptr: pieces.as_ptr(), + len: pieces.len(), + } +} -- cgit v1.2.3