1 // (C) Copyright 2016 Jethro G. Beekman
2 //
3 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6 // option. This file may not be copied, modified, or distributed
7 // except according to those terms.
8
9 #[macro_use]
10 extern crate nom as nom_crate;
11
12 pub mod nom {
13 //! nom's result types, re-exported.
14 pub use nom_crate::{IResult,Needed,Err,ErrorKind};
15 }
16 pub mod literal;
17 pub mod expr;
18 pub mod token;
19
20 use nom::*;
21
22 #[derive(Debug)]
23 /// Parsing errors specific to C parsing
24 pub enum Error {
25 /// Expected the specified token
26 ExactToken(token::Kind,&'static [u8]),
27 /// Expected one of the specified tokens
28 ExactTokens(token::Kind,&'static [&'static str]),
29 /// Expected a token of the specified kind
30 TypedToken(token::Kind),
31 /// An unknown identifier was encountered
32 UnknownIdentifier,
33 /// An invalid literal was encountered.
34 ///
35 /// When encountered, this generally means a bug exists in the data that
36 /// was passed in or the parsing logic.
37 InvalidLiteral,
38 /// A full parse was requested, but data was left over after parsing finished.
39 Partial,
40 }
41
42 impl From<u32> for Error {
from(_: u32) -> Self43 fn from(_: u32) -> Self {
44 Error::InvalidLiteral
45 }
46 }
47
48 macro_rules! identity (
49 ($i:expr,$e:expr) => ($e);
50 );
51
52 /// If the input result indicates a succesful parse, but there is data left,
53 /// return an `Error::Partial` instead.
assert_full_parse<I,O,E>(result: IResult<&[I],O,E>) -> IResult<&[I],O,::Error> where Error: From<E>54 pub fn assert_full_parse<I,O,E>(result: IResult<&[I],O,E>) -> IResult<&[I],O,::Error>
55 where Error: From<E> {
56 match fix_error!((),::Error,identity!(result)) {
57 Ok((rem,output)) => if rem.len()==0 {
58 Ok((rem, output))
59 } else {
60 Err(Err::Error(error_position!(rem, ErrorKind::Custom(::Error::Partial))))
61 },
62 r => r,
63 }
64 }
65