blob: 9f63d966b9f3721000ce9af7419fe14be7dd2d01 [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 Tolnayc5ab8c62017-12-26 16:43:39 -050044
45use proc_macro2::TokenStream;
46
David Tolnay203557a2017-12-27 23:59:33 -050047pub use error::{PResult, ParseError};
David Tolnayc5ab8c62017-12-26 16:43:39 -050048
David Tolnaydfc886b2018-01-06 08:03:09 -080049use buffer::Cursor;
50
David Tolnayb34f5702018-01-06 19:39:49 -080051/// Parsing interface implemented by all types that can be parsed from a token
52/// stream.
53///
54/// Refer to the [module documentation] for details about parsing in Syn.
55///
56/// [module documentation]: index.html
David Tolnayc5ab8c62017-12-26 16:43:39 -050057pub trait Synom: Sized {
58 fn parse(input: Cursor) -> PResult<Self>;
59
60 fn description() -> Option<&'static str> {
61 None
62 }
63}
64
65impl Synom for TokenStream {
66 fn parse(input: Cursor) -> PResult<Self> {
David Tolnayf4aa6b42017-12-31 16:40:33 -050067 Ok((input.token_stream(), Cursor::empty()))
David Tolnayc5ab8c62017-12-26 16:43:39 -050068 }
Sergio Benitez5680d6a2017-12-29 11:20:29 -080069
70 fn description() -> Option<&'static str> {
71 Some("arbitrary token stream")
72 }
David Tolnayc5ab8c62017-12-26 16:43:39 -050073}