Parse a whole function definition. function ::= * "function" name signature "{" preamble function-body "}"
(&mut self)
| 1217 | // function ::= * "function" name signature "{" preamble function-body "}" |
| 1218 | // |
| 1219 | fn parse_function(&mut self) -> ParseResult<(Function, Details<'a>)> { |
| 1220 | // Begin gathering comments. |
| 1221 | // Make sure we don't include any comments before the `function` keyword. |
| 1222 | self.token(); |
| 1223 | debug_assert!(self.comments.is_empty()); |
| 1224 | self.start_gathering_comments(); |
| 1225 | |
| 1226 | self.match_identifier("function", "expected 'function'")?; |
| 1227 | |
| 1228 | let location = self.loc; |
| 1229 | |
| 1230 | // function ::= "function" * name signature "{" preamble function-body "}" |
| 1231 | let name = self.parse_user_func_name()?; |
| 1232 | |
| 1233 | // function ::= "function" name * signature "{" preamble function-body "}" |
| 1234 | let sig = self.parse_signature()?; |
| 1235 | |
| 1236 | let mut ctx = Context::new(Function::with_name_signature(name, sig)); |
| 1237 | |
| 1238 | // function ::= "function" name signature * "{" preamble function-body "}" |
| 1239 | self.match_token(Token::LBrace, "expected '{' before function body")?; |
| 1240 | |
| 1241 | self.token(); |
| 1242 | self.claim_gathered_comments(AnyEntity::Function); |
| 1243 | |
| 1244 | // function ::= "function" name signature "{" * preamble function-body "}" |
| 1245 | self.parse_preamble(&mut ctx)?; |
| 1246 | // function ::= "function" name signature "{" preamble * function-body "}" |
| 1247 | self.parse_function_body(&mut ctx)?; |
| 1248 | // function ::= "function" name signature "{" preamble function-body * "}" |
| 1249 | self.match_token(Token::RBrace, "expected '}' after function body")?; |
| 1250 | |
| 1251 | // Collect any comments following the end of the function, then stop gathering comments. |
| 1252 | self.start_gathering_comments(); |
| 1253 | self.token(); |
| 1254 | self.claim_gathered_comments(AnyEntity::Function); |
| 1255 | |
| 1256 | // Claim all the declared user-defined function names. |
| 1257 | for (user_func_ref, user_external_name) in |
| 1258 | std::mem::take(&mut self.predeclared_external_names) |
| 1259 | { |
| 1260 | let actual_ref = ctx |
| 1261 | .function |
| 1262 | .declare_imported_user_function(user_external_name); |
| 1263 | assert_eq!(user_func_ref, actual_ref); |
| 1264 | } |
| 1265 | |
| 1266 | let details = Details { |
| 1267 | location, |
| 1268 | comments: self.take_comments(), |
| 1269 | map: ctx.map, |
| 1270 | }; |
| 1271 | |
| 1272 | Ok((ctx.function, details)) |
| 1273 | } |
| 1274 | |
| 1275 | // Parse a user-defined function name |
| 1276 | // |