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
#![doc = include_str!("../README.md")]
#![doc(
    html_logo_url = "https://raw.githubusercontent.com/alloy-rs/core/main/assets/alloy.jpg",
    html_favicon_url = "https://raw.githubusercontent.com/alloy-rs/core/main/assets/favicon.ico"
)]
#![warn(
    missing_copy_implementations,
    missing_debug_implementations,
    missing_docs,
    unreachable_pub,
    clippy::missing_const_for_fn,
    rustdoc::all
)]
#![cfg_attr(not(test), warn(unused_crate_dependencies))]
#![deny(unused_must_use, rust_2018_idioms)]
#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]

#[macro_use]
extern crate alloc;

use core::{slice, str};
use winnow::{
    error::{AddContext, ParserError, StrContext, StrContextValue},
    trace::trace,
    Parser,
};

/// Errors.
mod error;
pub use error::{Error, Result};

/// Solidity ident rules.
mod ident;
pub use ident::{is_id_continue, is_id_start, is_valid_identifier, IDENT_REGEX};

/// Root type specifier.
mod root;
pub use root::RootType;

/// Type stem.
mod stem;
pub use stem::TypeStem;

/// Tuple type specifier.
mod tuple;
pub use tuple::TupleSpecifier;

/// Type specifier.
mod type_spec;
pub use type_spec::TypeSpecifier;

#[inline]
pub(crate) fn spanned<'a, O, E>(
    mut f: impl Parser<&'a str, O, E>,
) -> impl Parser<&'a str, (&'a str, O), E> {
    trace("spanned", move |input: &mut &'a str| {
        let start = input.as_ptr();

        let mut len = input.len();
        let r = f.parse_next(input)?;
        len -= input.len();

        // SAFETY: str invariant
        unsafe {
            let span = slice::from_raw_parts(start, len);
            debug_assert!(str::from_utf8(span).is_ok());
            Ok((str::from_utf8_unchecked(span), r))
        }
    })
}

#[inline]
pub(crate) fn str_parser<'a, E: ParserError<&'a str> + AddContext<&'a str, StrContext>>(
    s: &'static str,
) -> impl Parser<&'a str, &'a str, E> {
    #[cfg(feature = "debug")]
    let name = format!("str={s:?}");
    #[cfg(not(feature = "debug"))]
    let name = "str";
    trace(
        name,
        s.context(StrContext::Expected(StrContextValue::StringLiteral(s))),
    )
}