blob: d14c3f802225d636cf2e304291240ccde2188002 [file] [log] [blame]
David Tolnay55535012018-01-05 16:39:23 -08001// Copyright 2018 Syn Developers
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
David Tolnayb34f5702018-01-06 19:39:49 -08009//! Parsing interface for parsing a token stream into a syntax tree node.
David Tolnayc5ab8c62017-12-26 16:43:39 -050010//!
David Tolnayb34f5702018-01-06 19:39:49 -080011//! All syntax tree nodes that can be parsed from a token stream need to
12//! implement the [`Synom`] trait. This trait is usually implemented using the
13//! [`nom`]-style parser combinator macros provided by Syn, but may also be
14//! implemented without macros be using the low-level [`Cursor`] API directly.
David Tolnayc5ab8c62017-12-26 16:43:39 -050015//!
David Tolnayb34f5702018-01-06 19:39:49 -080016//! [`Synom`]: trait.Synom.html
17//! [`nom`]: https://github.com/Geal/nom
18//! [`Cursor`]: ../buffer/index.html
David Tolnayc5ab8c62017-12-26 16:43:39 -050019//!
David Tolnayb34f5702018-01-06 19:39:49 -080020//! The following parser combinator macros are available and a `Synom` parsing
21//! example is provided for each one.
David Tolnayc5ab8c62017-12-26 16:43:39 -050022//!
David Tolnayb34f5702018-01-06 19:39:49 -080023//! - [`alt!`](../macro.alt.html)
24//! - [`braces!`](../macro.braces.html)
25//! - [`brackets!`](../macro.brackets.html)
26//! - [`call!`](../macro.call.html)
27//! - [`cond!`](../macro.cond.html)
28//! - [`cond_reduce!`](../macro.cond_reduce.html)
29//! - [`do_parse!`](../macro.do_parse.html)
30//! - [`epsilon!`](../macro.epsilon.html)
31//! - [`input_end!`](../macro.input_end.html)
32//! - [`keyword!`](../macro.keyword.html)
33//! - [`many0!`](../macro.many0.html)
34//! - [`map!`](../macro.map.html)
35//! - [`not!`](../macro.not.html)
36//! - [`option!`](../macro.option.html)
37//! - [`parens!`](../macro.parens.html)
38//! - [`punct!`](../macro.punct.html)
39//! - [`reject!`](../macro.reject.html)
40//! - [`switch!`](../macro.switch.html)
41//! - [`syn!`](../macro.syn.html)
42//! - [`tuple!`](../macro.tuple.html)
43//! - [`value!`](../macro.value.html)
David Tolnay461d98e2018-01-07 11:07:19 -080044//!
45//! *This module is available if Syn is built with the `"parsing"` feature.*
David Tolnayc5ab8c62017-12-26 16:43:39 -050046
47use proc_macro2::TokenStream;
48
David Tolnay203557a2017-12-27 23:59:33 -050049pub use error::{PResult, ParseError};
David Tolnayc5ab8c62017-12-26 16:43:39 -050050
David Tolnaydfc886b2018-01-06 08:03:09 -080051use buffer::Cursor;
52
David Tolnayb34f5702018-01-06 19:39:49 -080053/// Parsing interface implemented by all types that can be parsed from a token
54/// stream.
55///
56/// Refer to the [module documentation] for details about parsing in Syn.
57///
58/// [module documentation]: index.html
David Tolnay461d98e2018-01-07 11:07:19 -080059///
60/// *This trait is available if Syn is built with the `"parsing"` feature.*
David Tolnayc5ab8c62017-12-26 16:43:39 -050061pub trait Synom: Sized {
62 fn parse(input: Cursor) -> PResult<Self>;
63
64 fn description() -> Option<&'static str> {
65 None
66 }
67}
68
69impl Synom for TokenStream {
70 fn parse(input: Cursor) -> PResult<Self> {
David Tolnayf4aa6b42017-12-31 16:40:33 -050071 Ok((input.token_stream(), Cursor::empty()))
David Tolnayc5ab8c62017-12-26 16:43:39 -050072 }
Sergio Benitez5680d6a2017-12-29 11:20:29 -080073
74 fn description() -> Option<&'static str> {
75 Some("arbitrary token stream")
76 }
David Tolnayc5ab8c62017-12-26 16:43:39 -050077}