blob: aa3043dd930fc922e77bebdfe38357e206b057cb [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 Tolnay577d0332018-08-25 21:45:24 -040014use punctuated::Punctuated;
David Tolnayb77c8b62018-08-25 16:39:41 -040015use synom::PResult;
David Tolnay18c754c2018-08-21 23:26:58 -040016
David Tolnayb6254182018-08-25 08:44:54 -040017pub use error::{Error, Result};
18pub use lookahead::{Lookahead1, Peek};
David Tolnay18c754c2018-08-21 23:26:58 -040019
20/// Parsing interface implemented by all types that can be parsed in a default
21/// way from a token stream.
22pub trait Parse: Sized {
23 fn parse(input: ParseStream) -> Result<Self>;
24}
25
26/// Input to a Syn parser function.
27pub type ParseStream<'a> = &'a ParseBuffer<'a>;
28
29/// Cursor position within a buffered token stream.
David Tolnay18c754c2018-08-21 23:26:58 -040030pub struct ParseBuffer<'a> {
31 scope: Span,
32 cell: Cell<Cursor<'static>>,
33 marker: PhantomData<Cursor<'a>>,
David Tolnayeafc8052018-08-25 16:33:53 -040034 unexpected: Rc<Cell<Option<Span>>>,
35}
36
37impl<'a> Drop for ParseBuffer<'a> {
38 fn drop(&mut self) {
39 if !self.is_empty() && self.unexpected.get().is_none() {
40 self.unexpected.set(Some(self.cursor().span()));
41 }
42 }
David Tolnay18c754c2018-08-21 23:26:58 -040043}
44
David Tolnaya8e29032018-08-25 17:14:31 -040045impl<'a> Clone for ParseBuffer<'a> {
46 fn clone(&self) -> Self {
47 ParseBuffer {
48 scope: self.scope,
49 cell: self.cell.clone(),
50 marker: PhantomData,
51 // Not the parent's unexpected. Nothing cares whether the clone
52 // parses all the way.
53 unexpected: Rc::new(Cell::new(None)),
54 }
55 }
56}
57
David Tolnay18c754c2018-08-21 23:26:58 -040058// Not public API.
59#[doc(hidden)]
60#[derive(Copy, Clone)]
61pub struct StepCursor<'c, 'a> {
62 scope: Span,
63 cursor: Cursor<'c>,
64 marker: PhantomData<fn(Cursor<'c>) -> Cursor<'a>>,
65}
66
67impl<'c, 'a> Deref for StepCursor<'c, 'a> {
68 type Target = Cursor<'c>;
69
70 fn deref(&self) -> &Self::Target {
71 &self.cursor
72 }
73}
74
75impl<'c, 'a> StepCursor<'c, 'a> {
76 // Not public API.
77 #[doc(hidden)]
78 pub fn advance(self, other: Cursor<'c>) -> Cursor<'a> {
79 unsafe { mem::transmute::<Cursor<'c>, Cursor<'a>>(other) }
80 }
81
82 // Not public API.
83 #[doc(hidden)]
84 pub fn error<T: Display>(self, message: T) -> Error {
85 error::new_at(self.scope, self.cursor, message)
86 }
87}
88
89impl<'a> ParseBuffer<'a> {
90 // Not public API.
91 #[doc(hidden)]
David Tolnayeafc8052018-08-25 16:33:53 -040092 pub fn new(scope: Span, cursor: Cursor<'a>, unexpected: Rc<Cell<Option<Span>>>) -> Self {
David Tolnay18c754c2018-08-21 23:26:58 -040093 let extend = unsafe { mem::transmute::<Cursor<'a>, Cursor<'static>>(cursor) };
94 ParseBuffer {
95 scope: scope,
96 cell: Cell::new(extend),
97 marker: PhantomData,
David Tolnayeafc8052018-08-25 16:33:53 -040098 unexpected: unexpected,
David Tolnay18c754c2018-08-21 23:26:58 -040099 }
100 }
101
102 pub fn cursor(&self) -> Cursor<'a> {
103 self.cell.get()
104 }
105
106 pub fn is_empty(&self) -> bool {
107 self.cursor().eof()
108 }
109
110 pub fn lookahead1(&self) -> Lookahead1<'a> {
111 Lookahead1::new(self.scope, self.cursor())
112 }
113
114 pub fn parse<T: Parse>(&self) -> Result<T> {
David Tolnayeafc8052018-08-25 16:33:53 -0400115 self.check_unexpected()?;
David Tolnay18c754c2018-08-21 23:26:58 -0400116 T::parse(self)
117 }
118
David Tolnay3a515a02018-08-25 21:08:27 -0400119 pub fn call<T>(&self, function: fn(ParseStream) -> Result<T>) -> Result<T> {
120 function(self)
121 }
122
David Tolnayb77c8b62018-08-25 16:39:41 -0400123 pub fn peek<T: Peek>(&self, token: T) -> bool {
124 self.lookahead1().peek(token)
125 }
126
David Tolnay577d0332018-08-25 21:45:24 -0400127 pub fn parse_terminated<T, P: Parse>(
128 &self,
129 parser: fn(ParseStream) -> Result<T>,
130 ) -> Result<Punctuated<T, P>> {
131 Punctuated::parse_terminated2(self, parser)
132 }
133
David Tolnayb77c8b62018-08-25 16:39:41 -0400134 pub fn fork(&self) -> Self {
135 self.clone()
136 }
137
David Tolnay18c754c2018-08-21 23:26:58 -0400138 // Not public API.
139 #[doc(hidden)]
140 pub fn step_cursor<F, R>(&self, function: F) -> Result<R>
141 where
142 F: for<'c> FnOnce(StepCursor<'c, 'a>) -> Result<(R, Cursor<'c>)>,
143 {
David Tolnayeafc8052018-08-25 16:33:53 -0400144 self.check_unexpected()?;
David Tolnay18c754c2018-08-21 23:26:58 -0400145 match function(StepCursor {
146 scope: self.scope,
147 cursor: self.cell.get(),
148 marker: PhantomData,
149 }) {
150 Ok((ret, cursor)) => {
151 self.cell.set(cursor);
152 Ok(ret)
153 }
154 Err(err) => Err(err),
155 }
156 }
David Tolnayeafc8052018-08-25 16:33:53 -0400157
158 // Not public API.
159 #[doc(hidden)]
David Tolnayb77c8b62018-08-25 16:39:41 -0400160 pub fn parse_synom<T>(&self, parse: fn(Cursor) -> PResult<T>) -> Result<T> {
161 self.step_cursor(|step| parse(step.cursor))
162 }
163
164 // Not public API.
165 #[doc(hidden)]
David Tolnayeafc8052018-08-25 16:33:53 -0400166 pub fn get_unexpected(&self) -> Rc<Cell<Option<Span>>> {
167 self.unexpected.clone()
168 }
169
170 // Not public API.
171 #[doc(hidden)]
172 pub fn check_unexpected(&self) -> Result<()> {
173 match self.unexpected.get() {
174 Some(span) => Err(Error::new(span, "unexpected token")),
175 None => Ok(()),
176 }
177 }
David Tolnay18c754c2018-08-21 23:26:58 -0400178}
179
180impl Parse for Ident {
181 fn parse(input: ParseStream) -> Result<Self> {
182 input.step_cursor(|cursor| {
183 if let Some((ident, rest)) = cursor.ident() {
David Tolnayc4fdb1a2018-08-24 21:11:07 -0400184 match ident.to_string().as_str() {
185 "_"
186 // Based on https://doc.rust-lang.org/grammar.html#keywords
187 // and https://github.com/rust-lang/rfcs/blob/master/text/2421-unreservations-2018.md
188 | "abstract" | "as" | "become" | "box" | "break" | "const"
189 | "continue" | "crate" | "do" | "else" | "enum" | "extern" | "false" | "final"
190 | "fn" | "for" | "if" | "impl" | "in" | "let" | "loop" | "macro" | "match"
191 | "mod" | "move" | "mut" | "override" | "priv" | "proc" | "pub"
192 | "ref" | "return" | "Self" | "self" | "static" | "struct"
193 | "super" | "trait" | "true" | "type" | "typeof" | "unsafe" | "unsized" | "use"
194 | "virtual" | "where" | "while" | "yield" => {}
195 _ => return Ok((ident, rest)),
196 }
David Tolnay18c754c2018-08-21 23:26:58 -0400197 }
David Tolnayc4fdb1a2018-08-24 21:11:07 -0400198 Err(cursor.error("expected identifier"))
David Tolnay18c754c2018-08-21 23:26:58 -0400199 })
200 }
201}
202
203// In reality the impl would be for Punctuated.
204impl<T: Parse> Parse for Vec<T> {
205 fn parse(input: ParseStream) -> Result<Self> {
206 let mut vec = Vec::new();
207 while !input.is_empty() {
208 let t = input.parse::<T>()?;
209 vec.push(t);
210 }
211 Ok(vec)
212 }
213}