blob: cfc0817880e0699526e2ec8e00a74049662c0a88 [file] [log] [blame]
David Tolnay18c754c2018-08-21 23:26:58 -04001//! Parsing interface for parsing a token stream into a syntax tree node.
2
3use std::cell::Cell;
4use std::fmt::Display;
5use std::marker::PhantomData;
6use std::mem;
7use std::ops::Deref;
David Tolnayeafc8052018-08-25 16:33:53 -04008use std::rc::Rc;
9
10use proc_macro2::{Ident, Span};
David Tolnay18c754c2018-08-21 23:26:58 -040011
David Tolnay6d67c742018-08-24 20:42:39 -040012use buffer::Cursor;
David Tolnayb6254182018-08-25 08:44:54 -040013use error;
David Tolnayb77c8b62018-08-25 16:39:41 -040014use synom::PResult;
David Tolnay18c754c2018-08-21 23:26:58 -040015
David Tolnayb6254182018-08-25 08:44:54 -040016pub use error::{Error, Result};
17pub use lookahead::{Lookahead1, Peek};
David Tolnay18c754c2018-08-21 23:26:58 -040018
19/// Parsing interface implemented by all types that can be parsed in a default
20/// way from a token stream.
21pub trait Parse: Sized {
22 fn parse(input: ParseStream) -> Result<Self>;
23}
24
25/// Input to a Syn parser function.
26pub type ParseStream<'a> = &'a ParseBuffer<'a>;
27
28/// Cursor position within a buffered token stream.
David Tolnay18c754c2018-08-21 23:26:58 -040029pub struct ParseBuffer<'a> {
30 scope: Span,
31 cell: Cell<Cursor<'static>>,
32 marker: PhantomData<Cursor<'a>>,
David Tolnayeafc8052018-08-25 16:33:53 -040033 unexpected: Rc<Cell<Option<Span>>>,
34}
35
36impl<'a> Drop for ParseBuffer<'a> {
37 fn drop(&mut self) {
38 if !self.is_empty() && self.unexpected.get().is_none() {
39 self.unexpected.set(Some(self.cursor().span()));
40 }
41 }
David Tolnay18c754c2018-08-21 23:26:58 -040042}
43
David Tolnaya8e29032018-08-25 17:14:31 -040044impl<'a> Clone for ParseBuffer<'a> {
45 fn clone(&self) -> Self {
46 ParseBuffer {
47 scope: self.scope,
48 cell: self.cell.clone(),
49 marker: PhantomData,
50 // Not the parent's unexpected. Nothing cares whether the clone
51 // parses all the way.
52 unexpected: Rc::new(Cell::new(None)),
53 }
54 }
55}
56
David Tolnay18c754c2018-08-21 23:26:58 -040057// Not public API.
58#[doc(hidden)]
59#[derive(Copy, Clone)]
60pub struct StepCursor<'c, 'a> {
61 scope: Span,
62 cursor: Cursor<'c>,
63 marker: PhantomData<fn(Cursor<'c>) -> Cursor<'a>>,
64}
65
66impl<'c, 'a> Deref for StepCursor<'c, 'a> {
67 type Target = Cursor<'c>;
68
69 fn deref(&self) -> &Self::Target {
70 &self.cursor
71 }
72}
73
74impl<'c, 'a> StepCursor<'c, 'a> {
75 // Not public API.
76 #[doc(hidden)]
77 pub fn advance(self, other: Cursor<'c>) -> Cursor<'a> {
78 unsafe { mem::transmute::<Cursor<'c>, Cursor<'a>>(other) }
79 }
80
81 // Not public API.
82 #[doc(hidden)]
83 pub fn error<T: Display>(self, message: T) -> Error {
84 error::new_at(self.scope, self.cursor, message)
85 }
86}
87
88impl<'a> ParseBuffer<'a> {
89 // Not public API.
90 #[doc(hidden)]
David Tolnayeafc8052018-08-25 16:33:53 -040091 pub fn new(scope: Span, cursor: Cursor<'a>, unexpected: Rc<Cell<Option<Span>>>) -> Self {
David Tolnay18c754c2018-08-21 23:26:58 -040092 let extend = unsafe { mem::transmute::<Cursor<'a>, Cursor<'static>>(cursor) };
93 ParseBuffer {
94 scope: scope,
95 cell: Cell::new(extend),
96 marker: PhantomData,
David Tolnayeafc8052018-08-25 16:33:53 -040097 unexpected: unexpected,
David Tolnay18c754c2018-08-21 23:26:58 -040098 }
99 }
100
101 pub fn cursor(&self) -> Cursor<'a> {
102 self.cell.get()
103 }
104
105 pub fn is_empty(&self) -> bool {
106 self.cursor().eof()
107 }
108
109 pub fn lookahead1(&self) -> Lookahead1<'a> {
110 Lookahead1::new(self.scope, self.cursor())
111 }
112
113 pub fn parse<T: Parse>(&self) -> Result<T> {
David Tolnayeafc8052018-08-25 16:33:53 -0400114 self.check_unexpected()?;
David Tolnay18c754c2018-08-21 23:26:58 -0400115 T::parse(self)
116 }
117
David Tolnay3a515a02018-08-25 21:08:27 -0400118 pub fn call<T>(&self, function: fn(ParseStream) -> Result<T>) -> Result<T> {
119 function(self)
120 }
121
David Tolnayb77c8b62018-08-25 16:39:41 -0400122 pub fn peek<T: Peek>(&self, token: T) -> bool {
123 self.lookahead1().peek(token)
124 }
125
126 pub fn fork(&self) -> Self {
127 self.clone()
128 }
129
David Tolnay18c754c2018-08-21 23:26:58 -0400130 // Not public API.
131 #[doc(hidden)]
132 pub fn step_cursor<F, R>(&self, function: F) -> Result<R>
133 where
134 F: for<'c> FnOnce(StepCursor<'c, 'a>) -> Result<(R, Cursor<'c>)>,
135 {
David Tolnayeafc8052018-08-25 16:33:53 -0400136 self.check_unexpected()?;
David Tolnay18c754c2018-08-21 23:26:58 -0400137 match function(StepCursor {
138 scope: self.scope,
139 cursor: self.cell.get(),
140 marker: PhantomData,
141 }) {
142 Ok((ret, cursor)) => {
143 self.cell.set(cursor);
144 Ok(ret)
145 }
146 Err(err) => Err(err),
147 }
148 }
David Tolnayeafc8052018-08-25 16:33:53 -0400149
150 // Not public API.
151 #[doc(hidden)]
David Tolnayb77c8b62018-08-25 16:39:41 -0400152 pub fn parse_synom<T>(&self, parse: fn(Cursor) -> PResult<T>) -> Result<T> {
153 self.step_cursor(|step| parse(step.cursor))
154 }
155
156 // Not public API.
157 #[doc(hidden)]
David Tolnayeafc8052018-08-25 16:33:53 -0400158 pub fn get_unexpected(&self) -> Rc<Cell<Option<Span>>> {
159 self.unexpected.clone()
160 }
161
162 // Not public API.
163 #[doc(hidden)]
164 pub fn check_unexpected(&self) -> Result<()> {
165 match self.unexpected.get() {
166 Some(span) => Err(Error::new(span, "unexpected token")),
167 None => Ok(()),
168 }
169 }
David Tolnay18c754c2018-08-21 23:26:58 -0400170}
171
172impl Parse for Ident {
173 fn parse(input: ParseStream) -> Result<Self> {
174 input.step_cursor(|cursor| {
175 if let Some((ident, rest)) = cursor.ident() {
David Tolnayc4fdb1a2018-08-24 21:11:07 -0400176 match ident.to_string().as_str() {
177 "_"
178 // Based on https://doc.rust-lang.org/grammar.html#keywords
179 // and https://github.com/rust-lang/rfcs/blob/master/text/2421-unreservations-2018.md
180 | "abstract" | "as" | "become" | "box" | "break" | "const"
181 | "continue" | "crate" | "do" | "else" | "enum" | "extern" | "false" | "final"
182 | "fn" | "for" | "if" | "impl" | "in" | "let" | "loop" | "macro" | "match"
183 | "mod" | "move" | "mut" | "override" | "priv" | "proc" | "pub"
184 | "ref" | "return" | "Self" | "self" | "static" | "struct"
185 | "super" | "trait" | "true" | "type" | "typeof" | "unsafe" | "unsized" | "use"
186 | "virtual" | "where" | "while" | "yield" => {}
187 _ => return Ok((ident, rest)),
188 }
David Tolnay18c754c2018-08-21 23:26:58 -0400189 }
David Tolnayc4fdb1a2018-08-24 21:11:07 -0400190 Err(cursor.error("expected identifier"))
David Tolnay18c754c2018-08-21 23:26:58 -0400191 })
192 }
193}
194
195// In reality the impl would be for Punctuated.
196impl<T: Parse> Parse for Vec<T> {
197 fn parse(input: ParseStream) -> Result<Self> {
198 let mut vec = Vec::new();
199 while !input.is_empty() {
200 let t = input.parse::<T>()?;
201 vec.push(t);
202 }
203 Ok(vec)
204 }
205}