blob: 2525d8f497d069f8d25908266415caaf86d81a14 [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 Tolnayb77c8b62018-08-25 16:39:41 -0400118 pub fn peek<T: Peek>(&self, token: T) -> bool {
119 self.lookahead1().peek(token)
120 }
121
122 pub fn fork(&self) -> Self {
123 self.clone()
124 }
125
David Tolnay18c754c2018-08-21 23:26:58 -0400126 // Not public API.
127 #[doc(hidden)]
128 pub fn step_cursor<F, R>(&self, function: F) -> Result<R>
129 where
130 F: for<'c> FnOnce(StepCursor<'c, 'a>) -> Result<(R, Cursor<'c>)>,
131 {
David Tolnayeafc8052018-08-25 16:33:53 -0400132 self.check_unexpected()?;
David Tolnay18c754c2018-08-21 23:26:58 -0400133 match function(StepCursor {
134 scope: self.scope,
135 cursor: self.cell.get(),
136 marker: PhantomData,
137 }) {
138 Ok((ret, cursor)) => {
139 self.cell.set(cursor);
140 Ok(ret)
141 }
142 Err(err) => Err(err),
143 }
144 }
David Tolnayeafc8052018-08-25 16:33:53 -0400145
146 // Not public API.
147 #[doc(hidden)]
David Tolnayb77c8b62018-08-25 16:39:41 -0400148 pub fn parse_synom<T>(&self, parse: fn(Cursor) -> PResult<T>) -> Result<T> {
149 self.step_cursor(|step| parse(step.cursor))
150 }
151
152 // Not public API.
153 #[doc(hidden)]
David Tolnayeafc8052018-08-25 16:33:53 -0400154 pub fn get_unexpected(&self) -> Rc<Cell<Option<Span>>> {
155 self.unexpected.clone()
156 }
157
158 // Not public API.
159 #[doc(hidden)]
160 pub fn check_unexpected(&self) -> Result<()> {
161 match self.unexpected.get() {
162 Some(span) => Err(Error::new(span, "unexpected token")),
163 None => Ok(()),
164 }
165 }
David Tolnay18c754c2018-08-21 23:26:58 -0400166}
167
168impl Parse for Ident {
169 fn parse(input: ParseStream) -> Result<Self> {
170 input.step_cursor(|cursor| {
171 if let Some((ident, rest)) = cursor.ident() {
David Tolnayc4fdb1a2018-08-24 21:11:07 -0400172 match ident.to_string().as_str() {
173 "_"
174 // Based on https://doc.rust-lang.org/grammar.html#keywords
175 // and https://github.com/rust-lang/rfcs/blob/master/text/2421-unreservations-2018.md
176 | "abstract" | "as" | "become" | "box" | "break" | "const"
177 | "continue" | "crate" | "do" | "else" | "enum" | "extern" | "false" | "final"
178 | "fn" | "for" | "if" | "impl" | "in" | "let" | "loop" | "macro" | "match"
179 | "mod" | "move" | "mut" | "override" | "priv" | "proc" | "pub"
180 | "ref" | "return" | "Self" | "self" | "static" | "struct"
181 | "super" | "trait" | "true" | "type" | "typeof" | "unsafe" | "unsized" | "use"
182 | "virtual" | "where" | "while" | "yield" => {}
183 _ => return Ok((ident, rest)),
184 }
David Tolnay18c754c2018-08-21 23:26:58 -0400185 }
David Tolnayc4fdb1a2018-08-24 21:11:07 -0400186 Err(cursor.error("expected identifier"))
David Tolnay18c754c2018-08-21 23:26:58 -0400187 })
188 }
189}
190
191// In reality the impl would be for Punctuated.
192impl<T: Parse> Parse for Vec<T> {
193 fn parse(input: ParseStream) -> Result<Self> {
194 let mut vec = Vec::new();
195 while !input.is_empty() {
196 let t = input.parse::<T>()?;
197 vec.push(t);
198 }
199 Ok(vec)
200 }
201}