// (C) Copyright 2016 Jethro G. Beekman // // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your // option. This file may not be copied, modified, or distributed // except according to those terms. //! Parsing C literals from byte slices. //! //! This will parse a representation of a C literal into a Rust type. //! //! # characters //! Character literals are stored into the `CChar` type, which can hold values //! that are not valid Unicode code points. ASCII characters are represented as //! `char`, literal bytes with the high byte set are converted into the raw //! representation. Escape sequences are supported. If hex and octal escapes //! map to an ASCII character, that is used, otherwise, the raw encoding is //! used, including for values over 255. Unicode escapes are checked for //! validity and mapped to `char`. Character sequences are not supported. Width //! prefixes are ignored. //! //! # strings //! Strings are interpreted as byte vectors. Escape sequences are supported. If //! hex and octal escapes map onto multi-byte characters, they are truncated to //! one 8-bit character. Unicode escapes are converted into their UTF-8 //! encoding. Width prefixes are ignored. //! //! # integers //! Integers are read into `i64`. Binary, octal, decimal and hexadecimal are //! all supported. If the literal value is between `i64::MAX` and `u64::MAX`, //! it is bit-cast to `i64`. Values over `u64::MAX` cannot be parsed. Width and //! sign suffixes are ignored. Sign prefixes are not supported. //! //! # real numbers //! Reals are read into `f64`. Width suffixes are ignored. Sign prefixes are //! not supported in the significand. Hexadecimal floating points are not //! supported.
use std::char; use std::str::{self, FromStr};
use nom::branch::alt; use nom::bytes::complete::is_not; use nom::bytes::complete::tag; use nom::character::complete::{char, one_of}; use nom::combinator::{complete, map, map_opt, opt, recognize}; use nom::multi::{fold_many0, many0, many1, many_m_n}; use nom::sequence::{delimited, pair, preceded, terminated, tuple}; use nom::*;
#[derive(Debug, Copy, Clone, PartialEq, Eq)] /// Representation of a C character pubenum CChar { /// A character that can be represented as a `char`
Char(char), /// Any other character (8-bit characters, unicode surrogates, etc.)
Raw(u64),
}
impl From<u8> for CChar { fn from(i: u8) -> CChar { match i { 0..=0x7f => CChar::Char(i as u8 as char),
_ => CChar::Raw(i as u64),
}
}
}
// A non-allocating version of this would be nice... impl std::convert::Into<Vec<u8>> for CChar { fn into(self) -> Vec<u8> { matchself {
CChar::Char(c) => { letmut s = String::with_capacity(4);
s.extend(&[c]);
s.into_bytes()
}
CChar::Raw(i) => { letmut v = Vec::with_capacity(1);
v.push(i as u8);
v
}
}
}
}
/// ensures the child parser consumes the whole input pubfn full<I: Clone, O, F>(
f: F,
) -> implFn(I) -> nom::IResult<I, O> where
I: nom::InputLength,
F: Fn(I) -> nom::IResult<I, O>,
{ move |input| { let res = f(input); match res {
Ok((i, o)) => { if i.input_len() == 0 {
Ok((i, o))
} else {
Err(nom::Err::Error(nom::error::Error::new(i, nom::error::ErrorKind::Complete)))
}
}
r => r,
}
}
}
fn take_ul(input: &[u8]) -> IResult<&[u8], &[u8]> { let r = input.split_at_position(|c| c != b'u' && c != b'U' && c != b'l' && c != b'L'); match r {
Err(Err::Incomplete(_)) => Ok((&input[input.len()..], input)),
res => res,
}
}
/// Parse a C literal. /// /// The input must contain exactly the representation of a single literal /// token, and in particular no whitespace or sign prefixes. pubfn parse(input: &[u8]) -> IResult<&[u8], EvalResult, crate::Error<&[u8]>> { crate::assert_full_parse(one_literal(input))
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.12 Sekunden
(vorverarbeitet am 2026-06-19)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.