blob: d45fc263c97c805e1169a68f1dc06a224608022e [file] [log] [blame]
David Tolnay94d304f2018-08-30 23:43:53 -07001//! Extension traits that are made available within the `call!` parser.
2//!
3//! *This module is available if Syn is built with the `"parsing"` feature.*
4
5use proc_macro2::Ident;
6
7use parse::{ParseStream, Result};
8
9/// Additional parsing methods for `Ident`.
10///
11/// This trait is sealed and cannot be implemented for types outside of Syn.
12///
13/// *This trait is available if Syn is built with the `"parsing"` feature.*
14pub trait IdentExt: Sized + private::Sealed {
15 /// Parses any identifier including keywords.
16 ///
17 /// This is useful when parsing a DSL which allows Rust keywords as
18 /// identifiers.
19 ///
20 /// ```rust
21 /// #[macro_use]
22 /// extern crate syn;
23 ///
24 /// use syn::Ident;
25 ///
26 /// // Parses input that looks like `name = NAME` where `NAME` can be
27 /// // any identifier.
28 /// //
29 /// // Examples:
30 /// //
31 /// // name = anything
32 /// // name = impl
33 /// named!(parse_dsl -> Ident, do_parse!(
34 /// custom_keyword!(name) >>
35 /// punct!(=) >>
36 /// name: call!(Ident::parse_any) >>
37 /// (name)
38 /// ));
39 /// #
40 /// # fn main() {}
41 /// ```
42 fn parse_any(input: ParseStream) -> Result<Self>;
43}
44
45impl IdentExt for Ident {
46 fn parse_any(input: ParseStream) -> Result<Self> {
47 input.step(|cursor| match cursor.ident() {
48 Some((ident, rest)) => Ok((ident, rest)),
49 None => Err(cursor.error("expected ident")),
50 })
51 }
52}
53
54mod private {
55 use proc_macro2::Ident;
56
57 pub trait Sealed {}
58
59 impl Sealed for Ident {}
60}