cexpr/literal.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
// (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::*;
use crate::expr::EvalResult;
use crate::ToCexprResult;
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
/// Representation of a C character
pub enum 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> {
match self {
CChar::Char(c) => {
let mut s = String::with_capacity(4);
s.extend(&[c]);
s.into_bytes()
}
CChar::Raw(i) => {
let mut v = Vec::with_capacity(1);
v.push(i as u8);
v
}
}
}
}
/// ensures the child parser consumes the whole input
pub fn full<I: Clone, O, F>(
f: F,
) -> impl Fn(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,
}
}
}
// =================================
// ======== matching digits ========
// =================================
macro_rules! byte {
($($p: pat)|* ) => {{
fn parser(i: &[u8]) -> crate::nom::IResult<&[u8], u8> {
match i.split_first() {
$(Some((&c @ $p,rest)))|* => Ok((rest,c)),
Some(_) => Err(nom::Err::Error(nom::error::Error::new(i, nom::error::ErrorKind::OneOf))),
None => Err(nom::Err::Incomplete(Needed::new(1))),
}
}
parser
}}
}
fn binary(i: &[u8]) -> nom::IResult<&[u8], u8> {
byte!(b'0'..=b'1')(i)
}
fn octal(i: &[u8]) -> nom::IResult<&[u8], u8> {
byte!(b'0'..=b'7')(i)
}
fn decimal(i: &[u8]) -> nom::IResult<&[u8], u8> {
byte!(b'0'..=b'9')(i)
}
fn hexadecimal(i: &[u8]) -> nom::IResult<&[u8], u8> {
byte!(b'0' ..= b'9' | b'a' ..= b'f' | b'A' ..= b'F')(i)
}
// ========================================
// ======== characters and strings ========
// ========================================
fn escape2char(c: char) -> CChar {
CChar::Char(match c {
'a' => '\x07',
'b' => '\x08',
'f' => '\x0c',
'n' => '\n',
'r' => '\r',
't' => '\t',
'v' => '\x0b',
_ => unreachable!("invalid escape {}", c),
})
}
fn c_raw_escape(n: Vec<u8>, radix: u32) -> Option<CChar> {
str::from_utf8(&n)
.ok()
.and_then(|i| u64::from_str_radix(i, radix).ok())
.map(|i| match i {
0..=0x7f => CChar::Char(i as u8 as char),
_ => CChar::Raw(i),
})
}
fn c_unicode_escape(n: Vec<u8>) -> Option<CChar> {
str::from_utf8(&n)
.ok()
.and_then(|i| u32::from_str_radix(i, 16).ok())
.and_then(char::from_u32)
.map(CChar::Char)
}
fn escaped_char(i: &[u8]) -> nom::IResult<&[u8], CChar> {
preceded(
char('\\'),
alt((
map(one_of(r#"'"?\"#), CChar::Char),
map(one_of("abfnrtv"), escape2char),
map_opt(many_m_n(1, 3, octal), |v| c_raw_escape(v, 8)),
map_opt(preceded(char('x'), many1(hexadecimal)), |v| {
c_raw_escape(v, 16)
}),
map_opt(
preceded(char('u'), many_m_n(4, 4, hexadecimal)),
c_unicode_escape,
),
map_opt(
preceded(char('U'), many_m_n(8, 8, hexadecimal)),
c_unicode_escape,
),
)),
)(i)
}
fn c_width_prefix(i: &[u8]) -> nom::IResult<&[u8], &[u8]> {
alt((tag("u8"), tag("u"), tag("U"), tag("L")))(i)
}
fn c_char(i: &[u8]) -> nom::IResult<&[u8], CChar> {
delimited(
terminated(opt(c_width_prefix), char('\'')),
alt((
escaped_char,
map(byte!(0 ..= 91 /* \=92 */ | 93 ..= 255), CChar::from),
)),
char('\''),
)(i)
}
fn c_string(i: &[u8]) -> nom::IResult<&[u8], Vec<u8>> {
delimited(
alt((preceded(c_width_prefix, char('"')), char('"'))),
fold_many0(
alt((
map(escaped_char, |c: CChar| c.into()),
map(is_not([b'\\', b'"']), |c: &[u8]| c.into()),
)),
Vec::new,
|mut v: Vec<u8>, res: Vec<u8>| {
v.extend_from_slice(&res);
v
},
),
char('"'),
)(i)
}
// ================================
// ======== parse integers ========
// ================================
fn c_int_radix(n: Vec<u8>, radix: u32) -> Option<u64> {
str::from_utf8(&n)
.ok()
.and_then(|i| u64::from_str_radix(i, radix).ok())
}
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,
}
}
fn c_int(i: &[u8]) -> nom::IResult<&[u8], i64> {
map(
terminated(
alt((
map_opt(preceded(tag("0x"), many1(complete(hexadecimal))), |v| {
c_int_radix(v, 16)
}),
map_opt(preceded(tag("0X"), many1(complete(hexadecimal))), |v| {
c_int_radix(v, 16)
}),
map_opt(preceded(tag("0b"), many1(complete(binary))), |v| {
c_int_radix(v, 2)
}),
map_opt(preceded(tag("0B"), many1(complete(binary))), |v| {
c_int_radix(v, 2)
}),
map_opt(preceded(char('0'), many1(complete(octal))), |v| {
c_int_radix(v, 8)
}),
map_opt(many1(complete(decimal)), |v| c_int_radix(v, 10)),
|input| Err(crate::nom::Err::Error(nom::error::Error::new(input, crate::nom::ErrorKind::Fix))),
)),
opt(take_ul),
),
|i| i as i64,
)(i)
}
// ==============================
// ======== parse floats ========
// ==============================
fn float_width(i: &[u8]) -> nom::IResult<&[u8], u8> {
nom::combinator::complete(byte!(b'f' | b'l' | b'F' | b'L'))(i)
}
fn float_exp(i: &[u8]) -> nom::IResult<&[u8], (Option<u8>, Vec<u8>)> {
preceded(
byte!(b'e' | b'E'),
pair(opt(byte!(b'-' | b'+')), many1(complete(decimal))),
)(i)
}
fn c_float(i: &[u8]) -> nom::IResult<&[u8], f64> {
map_opt(
alt((
terminated(
recognize(tuple((
many1(complete(decimal)),
byte!(b'.'),
many0(complete(decimal)),
))),
opt(float_width),
),
terminated(
recognize(tuple((
many0(complete(decimal)),
byte!(b'.'),
many1(complete(decimal)),
))),
opt(float_width),
),
terminated(
recognize(tuple((
many0(complete(decimal)),
opt(byte!(b'.')),
many1(complete(decimal)),
float_exp,
))),
opt(float_width),
),
terminated(
recognize(tuple((
many1(complete(decimal)),
opt(byte!(b'.')),
many0(complete(decimal)),
float_exp,
))),
opt(float_width),
),
terminated(recognize(many1(complete(decimal))), float_width),
)),
|v| str::from_utf8(v).ok().and_then(|i| f64::from_str(i).ok()),
)(i)
}
// ================================
// ======== main interface ========
// ================================
fn one_literal(input: &[u8]) -> nom::IResult<&[u8], EvalResult, crate::Error<&[u8]>> {
alt((
map(full(c_char), EvalResult::Char),
map(full(c_int), |i| EvalResult::Int(::std::num::Wrapping(i))),
map(full(c_float), EvalResult::Float),
map(full(c_string), EvalResult::Str),
))(input)
.to_cexpr_result()
}
/// Parse a C literal.
///
/// The input must contain exactly the representation of a single literal
/// token, and in particular no whitespace or sign prefixes.
pub fn parse(input: &[u8]) -> IResult<&[u8], EvalResult, crate::Error<&[u8]>> {
crate::assert_full_parse(one_literal(input))
}