| 1 | // SPDX-License-Identifier: Apache-2.0 OR MIT |
| 2 | |
| 3 | //! Extensions to the parsing API with niche applicability. |
| 4 | |
| 5 | use crateCursor; |
| 6 | use crateResult; |
| 7 | use crate; |
| 8 | use DelimSpan; |
| 9 | use Delimiter; |
| 10 | use Cell; |
| 11 | use mem; |
| 12 | use Rc; |
| 13 | |
| 14 | /// Extensions to the `ParseStream` API to support speculative parsing. |
| 15 | |
| 16 | /// Advance this parse stream to the position of a forked parse stream. |
| 17 | /// |
| 18 | /// This is the opposite operation to [`ParseStream::fork`]. You can fork a |
| 19 | /// parse stream, perform some speculative parsing, then join the original |
| 20 | /// stream to the fork to "commit" the parsing from the fork to the main |
| 21 | /// stream. |
| 22 | /// |
| 23 | /// If you can avoid doing this, you should, as it limits the ability to |
| 24 | /// generate useful errors. That said, it is often the only way to parse |
| 25 | /// syntax of the form `A* B*` for arbitrary syntax `A` and `B`. The problem |
| 26 | /// is that when the fork fails to parse an `A`, it's impossible to tell |
| 27 | /// whether that was because of a syntax error and the user meant to provide |
| 28 | /// an `A`, or that the `A`s are finished and it's time to start parsing |
| 29 | /// `B`s. Use with care. |
| 30 | /// |
| 31 | /// Also note that if `A` is a subset of `B`, `A* B*` can be parsed by |
| 32 | /// parsing `B*` and removing the leading members of `A` from the |
| 33 | /// repetition, bypassing the need to involve the downsides associated with |
| 34 | /// speculative parsing. |
| 35 | /// |
| 36 | /// [`ParseStream::fork`]: ParseBuffer::fork |
| 37 | /// |
| 38 | /// # Example |
| 39 | /// |
| 40 | /// There has been chatter about the possibility of making the colons in the |
| 41 | /// turbofish syntax like `path::to::<T>` no longer required by accepting |
| 42 | /// `path::to<T>` in expression position. Specifically, according to [RFC |
| 43 | /// 2544], [`PathSegment`] parsing should always try to consume a following |
| 44 | /// `<` token as the start of generic arguments, and reset to the `<` if |
| 45 | /// that fails (e.g. the token is acting as a less-than operator). |
| 46 | /// |
| 47 | /// This is the exact kind of parsing behavior which requires the "fork, |
| 48 | /// try, commit" behavior that [`ParseStream::fork`] discourages. With |
| 49 | /// `advance_to`, we can avoid having to parse the speculatively parsed |
| 50 | /// content a second time. |
| 51 | /// |
| 52 | /// This change in behavior can be implemented in syn by replacing just the |
| 53 | /// `Parse` implementation for `PathSegment`: |
| 54 | /// |
| 55 | /// ``` |
| 56 | /// # use syn::ext::IdentExt; |
| 57 | /// use syn::parse::discouraged::Speculative; |
| 58 | /// # use syn::parse::{Parse, ParseStream}; |
| 59 | /// # use syn::{Ident, PathArguments, Result, Token}; |
| 60 | /// |
| 61 | /// pub struct PathSegment { |
| 62 | /// pub ident: Ident, |
| 63 | /// pub arguments: PathArguments, |
| 64 | /// } |
| 65 | /// # |
| 66 | /// # impl<T> From<T> for PathSegment |
| 67 | /// # where |
| 68 | /// # T: Into<Ident>, |
| 69 | /// # { |
| 70 | /// # fn from(ident: T) -> Self { |
| 71 | /// # PathSegment { |
| 72 | /// # ident: ident.into(), |
| 73 | /// # arguments: PathArguments::None, |
| 74 | /// # } |
| 75 | /// # } |
| 76 | /// # } |
| 77 | /// |
| 78 | /// impl Parse for PathSegment { |
| 79 | /// fn parse(input: ParseStream) -> Result<Self> { |
| 80 | /// if input.peek(Token![super]) |
| 81 | /// || input.peek(Token![self]) |
| 82 | /// || input.peek(Token![Self]) |
| 83 | /// || input.peek(Token![crate]) |
| 84 | /// { |
| 85 | /// let ident = input.call(Ident::parse_any)?; |
| 86 | /// return Ok(PathSegment::from(ident)); |
| 87 | /// } |
| 88 | /// |
| 89 | /// let ident = input.parse()?; |
| 90 | /// if input.peek(Token![::]) && input.peek3(Token![<]) { |
| 91 | /// return Ok(PathSegment { |
| 92 | /// ident, |
| 93 | /// arguments: PathArguments::AngleBracketed(input.parse()?), |
| 94 | /// }); |
| 95 | /// } |
| 96 | /// if input.peek(Token![<]) && !input.peek(Token![<=]) { |
| 97 | /// let fork = input.fork(); |
| 98 | /// if let Ok(arguments) = fork.parse() { |
| 99 | /// input.advance_to(&fork); |
| 100 | /// return Ok(PathSegment { |
| 101 | /// ident, |
| 102 | /// arguments: PathArguments::AngleBracketed(arguments), |
| 103 | /// }); |
| 104 | /// } |
| 105 | /// } |
| 106 | /// Ok(PathSegment::from(ident)) |
| 107 | /// } |
| 108 | /// } |
| 109 | /// |
| 110 | /// # syn::parse_str::<PathSegment>("a<b,c>").unwrap(); |
| 111 | /// ``` |
| 112 | /// |
| 113 | /// # Drawbacks |
| 114 | /// |
| 115 | /// The main drawback of this style of speculative parsing is in error |
| 116 | /// presentation. Even if the lookahead is the "correct" parse, the error |
| 117 | /// that is shown is that of the "fallback" parse. To use the same example |
| 118 | /// as the turbofish above, take the following unfinished "turbofish": |
| 119 | /// |
| 120 | /// ```text |
| 121 | /// let _ = f<&'a fn(), for<'a> serde::>(); |
| 122 | /// ``` |
| 123 | /// |
| 124 | /// If this is parsed as generic arguments, we can provide the error message |
| 125 | /// |
| 126 | /// ```text |
| 127 | /// error: expected identifier |
| 128 | /// --> src.rs:L:C |
| 129 | /// | |
| 130 | /// L | let _ = f<&'a fn(), for<'a> serde::>(); |
| 131 | /// | ^ |
| 132 | /// ``` |
| 133 | /// |
| 134 | /// but if parsed using the above speculative parsing, it falls back to |
| 135 | /// assuming that the `<` is a less-than when it fails to parse the generic |
| 136 | /// arguments, and tries to interpret the `&'a` as the start of a labelled |
| 137 | /// loop, resulting in the much less helpful error |
| 138 | /// |
| 139 | /// ```text |
| 140 | /// error: expected `:` |
| 141 | /// --> src.rs:L:C |
| 142 | /// | |
| 143 | /// L | let _ = f<&'a fn(), for<'a> serde::>(); |
| 144 | /// | ^^ |
| 145 | /// ``` |
| 146 | /// |
| 147 | /// This can be mitigated with various heuristics (two examples: show both |
| 148 | /// forks' parse errors, or show the one that consumed more tokens), but |
| 149 | /// when you can control the grammar, sticking to something that can be |
| 150 | /// parsed LL(3) and without the LL(*) speculative parsing this makes |
| 151 | /// possible, displaying reasonable errors becomes much more simple. |
| 152 | /// |
| 153 | /// [RFC 2544]: https://github.com/rust-lang/rfcs/pull/2544 |
| 154 | /// [`PathSegment`]: crate::PathSegment |
| 155 | /// |
| 156 | /// # Performance |
| 157 | /// |
| 158 | /// This method performs a cheap fixed amount of work that does not depend |
| 159 | /// on how far apart the two streams are positioned. |
| 160 | /// |
| 161 | /// # Panics |
| 162 | /// |
| 163 | /// The forked stream in the argument of `advance_to` must have been |
| 164 | /// obtained by forking `self`. Attempting to advance to any other stream |
| 165 | /// will cause a panic. |
| 166 | ; |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | if !cratesame_scope |
| 172 | panic!; |
| 173 | |
| 174 | |
| 175 | let = inner_unexpected; |
| 176 | let = inner_unexpected; |
| 177 | if !ptr_eq |
| 178 | match |
| 179 | // Unexpected set on the fork, but not on `self`, copy it over. |
| 180 | => |
| 181 | self_unexp.set; |
| 182 | |
| 183 | // Unexpected unset. Use chain to propagate errors from fork. |
| 184 | => |
| 185 | fork_unexp.set; |
| 186 | |
| 187 | // Ensure toplevel 'unexpected' tokens from the fork don't |
| 188 | // propagate up the chain by replacing the root `unexpected` |
| 189 | // pointer, only 'unexpected' tokens from existing group |
| 190 | // parsers should propagate. |
| 191 | fork.unexpected |
| 192 | .set; |
| 193 | |
| 194 | // Unexpected has been set on `self`. No changes needed. |
| 195 | => |
| 196 | |
| 197 | |
| 198 | |
| 199 | // See comment on `cell` in the struct definition. |
| 200 | self.cell |
| 201 | .set; |
| 202 | |
| 203 | |
| 204 | |
| 205 | /// Extensions to the `ParseStream` API to support manipulating invisible |
| 206 | /// delimiters the same as if they were visible. |
| 207 | |
| 208 | /// Returns the delimiter, the span of the delimiter token, and the nested |
| 209 | /// contents for further parsing. |
| 210 | ; |
| 211 | |
| 212 | |
| 213 | |
| 214 | |
| 215 | self.step |
| 216 | if let Some = cursor.any_group |
| 217 | let scope = span.close; |
| 218 | let nested = crateadvance_step_cursor; |
| 219 | let unexpected = crateget_unexpected; |
| 220 | let content = cratenew_parse_buffer; |
| 221 | Ok |
| 222 | else |
| 223 | Err |
| 224 | |
| 225 | |
| 226 | |
| 227 |