Struct flexi_parse::ParseBuffer
source · pub struct ParseBuffer<'a> { /* private fields */ }
Expand description
A cursor position within a token stream.
Implementations§
source§impl<'a> ParseBuffer<'a>
impl<'a> ParseBuffer<'a>
sourcepub fn parse<T: Parse>(&self) -> Result<T>
pub fn parse<T: Parse>(&self) -> Result<T>
Attempts to parse self
into the given syntax tree node, using T
’s
default parsing implementation.
Errors
Returns an error if T
’s Parse
implementation fails.
Examples found in repository?
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
fn parse(input: ParseStream) -> Result<Self> {
let mut expr = factor(input)?;
loop {
if input.peek(Punct!["+"]) {
expr = Expr::Add(Box::new(expr), input.parse()?, Box::new(factor(input)?));
} else if input.peek(Punct!["-"]) {
expr = Expr::Sub(Box::new(expr), input.parse()?, Box::new(factor(input)?));
} else {
break;
}
}
Ok(expr)
}
}
fn factor(input: ParseStream) -> Result<Expr> {
let mut expr: Expr = unary(input)?;
loop {
if input.peek(Punct!["*"]) {
expr = Expr::Mul(Box::new(expr), input.parse()?, Box::new(unary(input)?));
} else if input.peek(Punct!["/"]) {
expr = Expr::Div(Box::new(expr), input.parse()?, Box::new(unary(input)?));
} else if input.peek(Punct!["%"]) {
expr = Expr::Mod(Box::new(expr), input.parse()?, Box::new(unary(input)?));
} else {
break;
}
}
Ok(expr)
}
fn unary(input: ParseStream) -> Result<Expr> {
if input.peek(Punct!["-"]) {
Ok(Expr::Neg(input.parse()?, Box::new(unary(input)?)))
} else {
primary(input)
}
}
#[allow(clippy::cast_precision_loss)]
fn primary(input: ParseStream) -> Result<Expr> {
let lookahead = input.lookahead();
if lookahead.peek(token::LitFloat) {
Ok(Expr::Num(input.parse::<token::LitFloat>()?.value()))
} else if lookahead.peek(token::LitInt) {
Ok(Expr::Num(input.parse::<token::LitInt>()?.value() as f64))
} else if lookahead.peek(token::LeftParen) {
let group: Group<Parentheses> = input.parse()?;
parse(group.into_token_stream())
} else {
Err(lookahead.error())
}
}
More examples
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709
fn assignment(input: ParseStream) -> Result<Self> {
let expr = Expr::or(input)?;
if input.peek(Punct!["="]) {
let equals: Punct!["="] = input.parse()?;
let value = Box::new(Expr::assignment(input)?);
if let Expr::Variable { name, .. } = expr {
return Ok(Expr::Assign {
name,
value,
distance: Cell::new(None),
});
} else if let Expr::Get { object, name } = expr {
return Ok(Expr::Set {
object,
name,
value,
});
}
input.add_error(input.new_error(
"Invalid assignment target".to_string(),
&equals,
error_codes::INVALID_ASSIGN,
));
}
Ok(expr)
}
fn or(input: ParseStream) -> Result<Self> {
let mut expr = Expr::and(input)?;
while input.peek(kw::or) {
expr = Expr::Logical(Box::new(Logical::Or(
expr,
input.parse()?,
Expr::and(input)?,
)));
}
Ok(expr)
}
fn and(input: ParseStream) -> Result<Self> {
let mut expr = Expr::equality(input)?;
while input.peek(kw::and) {
expr = Expr::Logical(Box::new(Logical::And(
expr,
input.parse()?,
Expr::equality(input)?,
)));
}
Ok(expr)
}
fn equality(input: ParseStream) -> Result<Self> {
let mut expr = Expr::comparison(input)?;
loop {
if input.peek(Punct!["=="]) {
expr = Expr::binary(Binary::Equal(
expr,
input.parse()?,
Expr::comparison(input)?,
));
} else if input.peek(Punct!["!="]) {
expr = Expr::binary(Binary::NotEqual(
expr,
input.parse()?,
Expr::comparison(input)?,
));
} else {
break Ok(expr);
}
}
}
fn comparison(input: ParseStream) -> Result<Self> {
let mut expr = Expr::term(input)?;
loop {
if input.peek(Punct![">"]) {
expr = Expr::binary(Binary::Greater(expr, input.parse()?, Expr::term(input)?));
} else if input.peek(Punct![">="]) {
expr = Expr::binary(Binary::GreaterEqual(
expr,
input.parse()?,
Expr::term(input)?,
));
} else if input.peek(Punct!["<"]) {
expr = Expr::binary(Binary::Less(expr, input.parse()?, Expr::term(input)?));
} else if input.peek(Punct!["<="]) {
expr = Expr::binary(Binary::LessEqual(expr, input.parse()?, Expr::term(input)?));
} else {
break Ok(expr);
}
}
}
fn term(input: ParseStream) -> Result<Self> {
let mut expr = Expr::factor(input)?;
loop {
if input.peek(Punct!["+"]) {
expr = Expr::binary(Binary::Add(expr, input.parse()?, Expr::factor(input)?));
} else if input.peek(Punct!["-"]) {
expr = Expr::binary(Binary::Sub(expr, input.parse()?, Expr::factor(input)?));
} else {
break Ok(expr);
}
}
}
fn factor(input: ParseStream) -> Result<Self> {
let mut expr = Expr::unary(input)?;
loop {
if input.peek(Punct!["*"]) {
expr = Expr::binary(Binary::Mul(expr, input.parse()?, Expr::unary(input)?));
} else if input.peek(Punct!["/"]) {
expr = Expr::binary(Binary::Div(expr, input.parse()?, Expr::unary(input)?));
} else {
break Ok(expr);
}
}
}
fn unary(input: ParseStream) -> Result<Self> {
if input.peek(Punct!["-"]) {
Ok(Expr::Unary(Box::new(Unary::Neg(
input.parse()?,
Expr::unary(input)?,
))))
} else if input.peek(Punct!["!"]) {
Ok(Expr::Unary(Box::new(Unary::Not(
input.parse()?,
Expr::unary(input)?,
))))
} else {
Expr::call(input)
}
}
fn call(input: ParseStream) -> Result<Self> {
let mut expr = Expr::primary(input)?;
loop {
if input.peek(Punct!["("]) {
expr = Expr::finish_call(input, expr)?;
} else if input.peek(Punct!["."]) {
let _: Punct!["."] = input.parse()?;
let name: Ident = input.parse()?;
expr = Expr::Get {
object: Box::new(expr),
name,
};
} else {
break Ok(expr);
}
}
}
fn finish_call(input: ParseStream<'_>, callee: Expr) -> Result<Self> {
let content;
let paren: Parentheses = group!(content in input);
let arguments: Punctuated<Expr, Punct![","]> =
Punctuated::parse_separated_trailing(&content)?;
let arguments: Vec<_> = arguments.into_iter().collect();
if arguments.len() >= 255 {
input.add_error(input.new_error(
"Can't have more than 254 arguments".to_string(),
paren.0.clone(),
error_codes::TOO_MANY_ARGS,
));
}
Ok(Expr::Call {
callee: Box::new(callee),
paren,
arguments,
})
}
fn primary(input: ParseStream) -> Result<Self> {
let lookahead = input.lookahead();
if lookahead.peek(kw::kw_false) {
Ok(Expr::Literal(Literal::False(input.parse()?)))
} else if lookahead.peek(kw::kw_true) {
Ok(Expr::Literal(Literal::True(input.parse()?)))
} else if lookahead.peek(kw::nil) {
Ok(Expr::Literal(Literal::Nil(input.parse()?)))
} else if lookahead.peek(LitFloat) {
Ok(Expr::Literal(Literal::Float(input.parse()?)))
} else if lookahead.peek(LitInt) {
Ok(Expr::Literal(Literal::Int(input.parse()?)))
} else if lookahead.peek(LitStr) {
Ok(Expr::Literal(Literal::String(input.parse()?)))
} else if input.peek(kw::kw_super) {
Ok(Expr::Super {
keyword: input.parse()?,
distance: Cell::new(None),
dot: input.parse()?,
method: kw::ident(input)?,
})
} else if input.peek(kw::this) {
Ok(Expr::This {
keyword: input.parse()?,
distance: Cell::new(None),
})
} else if lookahead.peek(Ident) {
Ok(Expr::Variable {
name: kw::ident(input)?,
distance: Cell::new(None),
})
} else if lookahead.peek(Punct!["("]) {
let content;
let _: Parentheses = group!(content in input);
Ok(Expr::Group(Box::new(content.parse()?)))
} else {
Err(lookahead.error())
}
}
}
impl Parse for Expr {
fn parse(input: ParseStream) -> Result<Self> {
Expr::assignment(input)
}
}
#[derive(Debug, Clone, PartialEq)]
struct Function {
name: Ident,
params: Vec<Ident>,
body: Vec<Stmt>,
}
impl Parse for Function {
fn parse(input: ParseStream) -> Result<Self> {
let name: Ident = input.parse()?;
let mut contents: Group<Parentheses> = input.parse()?;
contents.remove_whitespace();
let Parentheses(span) = contents.delimiters();
let tokens = contents.into_token_stream();
let params: Vec<Ident> = if tokens.is_empty() {
vec![]
} else {
let params: Punctuated<Ident, Punct![","]> =
Punctuated::parse_separated.parse(tokens)?;
params.into_iter().collect()
};
if params.len() >= 255 {
input.add_error(input.new_error(
"Can't have more than 254 parameters".to_string(),
span,
error_codes::TOO_MANY_ARGS,
));
}
let mut contents: Group<Braces> = input.parse()?;
contents.remove_whitespace();
let body = block.parse(contents.into_token_stream())?;
Ok(Function { name, params, body })
}
}
#[derive(Debug, Clone, PartialEq)]
enum Stmt {
Block(Vec<Stmt>),
Class {
name: Ident,
superclass: Option<Ident>,
superclass_distance: Cell<Option<usize>>,
methods: Vec<Function>,
},
Expr(Expr),
Function(Function),
If {
condition: Expr,
then_branch: Box<Stmt>,
else_branch: Option<Box<Stmt>>,
},
Print(Expr),
Return {
keyword: kw::kw_return,
value: Option<Expr>,
},
Variable {
name: Ident,
initialiser: Option<Expr>,
},
While {
condition: Expr,
body: Box<Stmt>,
},
}
fn block(input: ParseStream) -> Result<Vec<Stmt>> {
let mut statements = vec![];
while !input.is_empty() {
statements.push(Stmt::declaration(input)?);
}
Ok(statements)
}
impl Stmt {
fn declaration(input: ParseStream) -> Result<Self> {
if input.peek(kw::class) {
Stmt::class_declaration(input)
} else if input.peek(kw::fun) {
let _: kw::fun = input.parse()?;
Ok(Stmt::Function(Function::parse(input)?))
} else if input.peek(kw::var) {
Stmt::var_declaration(input)
} else {
Stmt::statement(input)
}
}
fn class_declaration(input: ParseStream) -> Result<Self> {
let _: kw::class = input.parse()?;
let name: Ident = input.parse()?;
let superclass = if input.peek(Punct!["<"]) {
let _: Punct!["<"] = input.parse()?;
Some(input.parse()?)
} else {
None
};
let content;
let _: Braces = group!(content in input);
let methods = parse_repeated(&content)?;
Ok(Stmt::Class {
name,
superclass,
superclass_distance: Cell::new(None),
methods,
})
}
fn var_declaration(input: ParseStream) -> Result<Self> {
let _: kw::var = input.parse()?;
let name = kw::ident(input)?;
let initialiser = if input.peek(Punct!["="]) {
let _: Punct!["="] = input.parse()?;
Some(input.parse()?)
} else {
None
};
let _: Punct![";"] = input.parse()?;
Ok(Stmt::Variable { name, initialiser })
}
fn statement(input: ParseStream) -> Result<Self> {
if input.peek(kw::kw_if) {
Stmt::if_statement(input)
} else if input.peek(kw::kw_for) {
Stmt::for_statement(input)
} else if input.peek(kw::print) {
Stmt::print_statement(input)
} else if input.peek(kw::kw_return) {
Stmt::return_statement(input)
} else if input.peek(kw::kw_while) {
Stmt::while_statement(input)
} else if input.peek(Punct!["{"]) {
let content;
let _: Braces = group!(content in input);
Ok(Stmt::Block(block(&content)?))
} else {
Stmt::expression_statement(input)
}
}
fn for_statement(input: ParseStream) -> Result<Self> {
let _: kw::kw_for = input.parse()?;
let content;
let _: Parentheses = group!(content in input);
let initialiser = if content.peek(Punct![";"]) {
let _: Punct![";"] = content.parse()?;
None
} else if content.peek(kw::var) {
Some(Stmt::var_declaration(&content)?)
} else {
Some(Stmt::expression_statement(&content)?)
};
let condition = if content.peek(Punct![";"]) {
Expr::Literal(Literal::True(kw::kw_true::new(&content)))
} else {
content.parse()?
};
let _: Punct![";"] = content.parse()?;
let increment = if content.is_empty() {
None
} else {
Some(content.parse()?)
};
let mut body = Stmt::statement(input)?;
if let Some(increment) = increment {
body = Stmt::Block(vec![body, Stmt::Expr(increment)]);
}
body = Stmt::While {
condition,
body: Box::new(body),
};
if let Some(initialiser) = initialiser {
body = Stmt::Block(vec![initialiser, body]);
}
Ok(body)
}
fn if_statement(input: ParseStream) -> Result<Self> {
let _: kw::kw_if = input.parse()?;
let content;
let _: Parentheses = group!(content in input);
let condition = content.parse()?;
let then_branch = Box::new(Stmt::statement(input)?);
let else_branch = if input.peek(kw::kw_else) {
Some(Box::new(Stmt::statement(input)?))
} else {
None
};
Ok(Stmt::If {
condition,
then_branch,
else_branch,
})
}
fn print_statement(input: ParseStream) -> Result<Self> {
let _: kw::print = input.parse()?;
let value = input.parse()?;
let _: Punct![";"] = input.parse()?;
Ok(Self::Print(value))
}
fn return_statement(input: ParseStream) -> Result<Self> {
let keyword: kw::kw_return = input.parse()?;
let value = if input.peek(Punct![";"]) {
None
} else {
Some(input.parse()?)
};
let _: Punct![";"] = input.parse()?;
Ok(Stmt::Return { keyword, value })
}
fn while_statement(input: ParseStream) -> Result<Self> {
let _: kw::kw_while = input.parse()?;
let content;
let _: Parentheses = group!(content in input);
let condition = content.parse()?;
let body = Box::new(Stmt::statement(input)?);
Ok(Stmt::While { condition, body })
}
fn expression_statement(input: ParseStream) -> Result<Self> {
let expr = input.parse()?;
let _: Punct![";"] = input.parse()?;
Ok(Self::Expr(expr))
}
}
impl Parse for Stmt {
fn parse(input: ParseStream) -> Result<Self> {
Stmt::declaration(input)
}
}
struct Ast(Vec<Stmt>);
impl Ast {
#[allow(clippy::wildcard_imports)]
fn synchronise(input: ParseStream) {
input.synchronise(|input| {
use kw::*;
input.peek(Punct![";"]) && !input.peek2(Punct!["}"])
|| peek2_any!(input, class, kw_for, fun, kw_if, print, kw_return, var, kw_while)
});
}
}
impl Parse for Ast {
fn parse(input: ParseStream) -> Result<Self> {
let mut stmts = vec![];
while !input.is_empty() {
match input.parse() {
Ok(stmt) => stmts.push(stmt),
Err(err) => {
Ast::synchronise(input);
input.add_error(err);
}
}
}
input.get_error().map_or(Ok(Ast(stmts)), Err)
}
sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if this stream has been exhausted.
Examples found in repository?
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709
fn block(input: ParseStream) -> Result<Vec<Stmt>> {
let mut statements = vec![];
while !input.is_empty() {
statements.push(Stmt::declaration(input)?);
}
Ok(statements)
}
impl Stmt {
fn declaration(input: ParseStream) -> Result<Self> {
if input.peek(kw::class) {
Stmt::class_declaration(input)
} else if input.peek(kw::fun) {
let _: kw::fun = input.parse()?;
Ok(Stmt::Function(Function::parse(input)?))
} else if input.peek(kw::var) {
Stmt::var_declaration(input)
} else {
Stmt::statement(input)
}
}
fn class_declaration(input: ParseStream) -> Result<Self> {
let _: kw::class = input.parse()?;
let name: Ident = input.parse()?;
let superclass = if input.peek(Punct!["<"]) {
let _: Punct!["<"] = input.parse()?;
Some(input.parse()?)
} else {
None
};
let content;
let _: Braces = group!(content in input);
let methods = parse_repeated(&content)?;
Ok(Stmt::Class {
name,
superclass,
superclass_distance: Cell::new(None),
methods,
})
}
fn var_declaration(input: ParseStream) -> Result<Self> {
let _: kw::var = input.parse()?;
let name = kw::ident(input)?;
let initialiser = if input.peek(Punct!["="]) {
let _: Punct!["="] = input.parse()?;
Some(input.parse()?)
} else {
None
};
let _: Punct![";"] = input.parse()?;
Ok(Stmt::Variable { name, initialiser })
}
fn statement(input: ParseStream) -> Result<Self> {
if input.peek(kw::kw_if) {
Stmt::if_statement(input)
} else if input.peek(kw::kw_for) {
Stmt::for_statement(input)
} else if input.peek(kw::print) {
Stmt::print_statement(input)
} else if input.peek(kw::kw_return) {
Stmt::return_statement(input)
} else if input.peek(kw::kw_while) {
Stmt::while_statement(input)
} else if input.peek(Punct!["{"]) {
let content;
let _: Braces = group!(content in input);
Ok(Stmt::Block(block(&content)?))
} else {
Stmt::expression_statement(input)
}
}
fn for_statement(input: ParseStream) -> Result<Self> {
let _: kw::kw_for = input.parse()?;
let content;
let _: Parentheses = group!(content in input);
let initialiser = if content.peek(Punct![";"]) {
let _: Punct![";"] = content.parse()?;
None
} else if content.peek(kw::var) {
Some(Stmt::var_declaration(&content)?)
} else {
Some(Stmt::expression_statement(&content)?)
};
let condition = if content.peek(Punct![";"]) {
Expr::Literal(Literal::True(kw::kw_true::new(&content)))
} else {
content.parse()?
};
let _: Punct![";"] = content.parse()?;
let increment = if content.is_empty() {
None
} else {
Some(content.parse()?)
};
let mut body = Stmt::statement(input)?;
if let Some(increment) = increment {
body = Stmt::Block(vec![body, Stmt::Expr(increment)]);
}
body = Stmt::While {
condition,
body: Box::new(body),
};
if let Some(initialiser) = initialiser {
body = Stmt::Block(vec![initialiser, body]);
}
Ok(body)
}
fn if_statement(input: ParseStream) -> Result<Self> {
let _: kw::kw_if = input.parse()?;
let content;
let _: Parentheses = group!(content in input);
let condition = content.parse()?;
let then_branch = Box::new(Stmt::statement(input)?);
let else_branch = if input.peek(kw::kw_else) {
Some(Box::new(Stmt::statement(input)?))
} else {
None
};
Ok(Stmt::If {
condition,
then_branch,
else_branch,
})
}
fn print_statement(input: ParseStream) -> Result<Self> {
let _: kw::print = input.parse()?;
let value = input.parse()?;
let _: Punct![";"] = input.parse()?;
Ok(Self::Print(value))
}
fn return_statement(input: ParseStream) -> Result<Self> {
let keyword: kw::kw_return = input.parse()?;
let value = if input.peek(Punct![";"]) {
None
} else {
Some(input.parse()?)
};
let _: Punct![";"] = input.parse()?;
Ok(Stmt::Return { keyword, value })
}
fn while_statement(input: ParseStream) -> Result<Self> {
let _: kw::kw_while = input.parse()?;
let content;
let _: Parentheses = group!(content in input);
let condition = content.parse()?;
let body = Box::new(Stmt::statement(input)?);
Ok(Stmt::While { condition, body })
}
fn expression_statement(input: ParseStream) -> Result<Self> {
let expr = input.parse()?;
let _: Punct![";"] = input.parse()?;
Ok(Self::Expr(expr))
}
}
impl Parse for Stmt {
fn parse(input: ParseStream) -> Result<Self> {
Stmt::declaration(input)
}
}
struct Ast(Vec<Stmt>);
impl Ast {
#[allow(clippy::wildcard_imports)]
fn synchronise(input: ParseStream) {
input.synchronise(|input| {
use kw::*;
input.peek(Punct![";"]) && !input.peek2(Punct!["}"])
|| peek2_any!(input, class, kw_for, fun, kw_if, print, kw_return, var, kw_while)
});
}
}
impl Parse for Ast {
fn parse(input: ParseStream) -> Result<Self> {
let mut stmts = vec![];
while !input.is_empty() {
match input.parse() {
Ok(stmt) => stmts.push(stmt),
Err(err) => {
Ast::synchronise(input);
input.add_error(err);
}
}
}
input.get_error().map_or(Ok(Ast(stmts)), Err)
}
sourcepub fn new_error<T: Into<Span>>(
&self,
message: String,
location: T,
code: u16
) -> Error
pub fn new_error<T: Into<Span>>( &self, message: String, location: T, code: u16 ) -> Error
Creates a new error at the given location with the given message and code.
Examples found in repository?
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
fn assignment(input: ParseStream) -> Result<Self> {
let expr = Expr::or(input)?;
if input.peek(Punct!["="]) {
let equals: Punct!["="] = input.parse()?;
let value = Box::new(Expr::assignment(input)?);
if let Expr::Variable { name, .. } = expr {
return Ok(Expr::Assign {
name,
value,
distance: Cell::new(None),
});
} else if let Expr::Get { object, name } = expr {
return Ok(Expr::Set {
object,
name,
value,
});
}
input.add_error(input.new_error(
"Invalid assignment target".to_string(),
&equals,
error_codes::INVALID_ASSIGN,
));
}
Ok(expr)
}
fn or(input: ParseStream) -> Result<Self> {
let mut expr = Expr::and(input)?;
while input.peek(kw::or) {
expr = Expr::Logical(Box::new(Logical::Or(
expr,
input.parse()?,
Expr::and(input)?,
)));
}
Ok(expr)
}
fn and(input: ParseStream) -> Result<Self> {
let mut expr = Expr::equality(input)?;
while input.peek(kw::and) {
expr = Expr::Logical(Box::new(Logical::And(
expr,
input.parse()?,
Expr::equality(input)?,
)));
}
Ok(expr)
}
fn equality(input: ParseStream) -> Result<Self> {
let mut expr = Expr::comparison(input)?;
loop {
if input.peek(Punct!["=="]) {
expr = Expr::binary(Binary::Equal(
expr,
input.parse()?,
Expr::comparison(input)?,
));
} else if input.peek(Punct!["!="]) {
expr = Expr::binary(Binary::NotEqual(
expr,
input.parse()?,
Expr::comparison(input)?,
));
} else {
break Ok(expr);
}
}
}
fn comparison(input: ParseStream) -> Result<Self> {
let mut expr = Expr::term(input)?;
loop {
if input.peek(Punct![">"]) {
expr = Expr::binary(Binary::Greater(expr, input.parse()?, Expr::term(input)?));
} else if input.peek(Punct![">="]) {
expr = Expr::binary(Binary::GreaterEqual(
expr,
input.parse()?,
Expr::term(input)?,
));
} else if input.peek(Punct!["<"]) {
expr = Expr::binary(Binary::Less(expr, input.parse()?, Expr::term(input)?));
} else if input.peek(Punct!["<="]) {
expr = Expr::binary(Binary::LessEqual(expr, input.parse()?, Expr::term(input)?));
} else {
break Ok(expr);
}
}
}
fn term(input: ParseStream) -> Result<Self> {
let mut expr = Expr::factor(input)?;
loop {
if input.peek(Punct!["+"]) {
expr = Expr::binary(Binary::Add(expr, input.parse()?, Expr::factor(input)?));
} else if input.peek(Punct!["-"]) {
expr = Expr::binary(Binary::Sub(expr, input.parse()?, Expr::factor(input)?));
} else {
break Ok(expr);
}
}
}
fn factor(input: ParseStream) -> Result<Self> {
let mut expr = Expr::unary(input)?;
loop {
if input.peek(Punct!["*"]) {
expr = Expr::binary(Binary::Mul(expr, input.parse()?, Expr::unary(input)?));
} else if input.peek(Punct!["/"]) {
expr = Expr::binary(Binary::Div(expr, input.parse()?, Expr::unary(input)?));
} else {
break Ok(expr);
}
}
}
fn unary(input: ParseStream) -> Result<Self> {
if input.peek(Punct!["-"]) {
Ok(Expr::Unary(Box::new(Unary::Neg(
input.parse()?,
Expr::unary(input)?,
))))
} else if input.peek(Punct!["!"]) {
Ok(Expr::Unary(Box::new(Unary::Not(
input.parse()?,
Expr::unary(input)?,
))))
} else {
Expr::call(input)
}
}
fn call(input: ParseStream) -> Result<Self> {
let mut expr = Expr::primary(input)?;
loop {
if input.peek(Punct!["("]) {
expr = Expr::finish_call(input, expr)?;
} else if input.peek(Punct!["."]) {
let _: Punct!["."] = input.parse()?;
let name: Ident = input.parse()?;
expr = Expr::Get {
object: Box::new(expr),
name,
};
} else {
break Ok(expr);
}
}
}
fn finish_call(input: ParseStream<'_>, callee: Expr) -> Result<Self> {
let content;
let paren: Parentheses = group!(content in input);
let arguments: Punctuated<Expr, Punct![","]> =
Punctuated::parse_separated_trailing(&content)?;
let arguments: Vec<_> = arguments.into_iter().collect();
if arguments.len() >= 255 {
input.add_error(input.new_error(
"Can't have more than 254 arguments".to_string(),
paren.0.clone(),
error_codes::TOO_MANY_ARGS,
));
}
Ok(Expr::Call {
callee: Box::new(callee),
paren,
arguments,
})
}
fn primary(input: ParseStream) -> Result<Self> {
let lookahead = input.lookahead();
if lookahead.peek(kw::kw_false) {
Ok(Expr::Literal(Literal::False(input.parse()?)))
} else if lookahead.peek(kw::kw_true) {
Ok(Expr::Literal(Literal::True(input.parse()?)))
} else if lookahead.peek(kw::nil) {
Ok(Expr::Literal(Literal::Nil(input.parse()?)))
} else if lookahead.peek(LitFloat) {
Ok(Expr::Literal(Literal::Float(input.parse()?)))
} else if lookahead.peek(LitInt) {
Ok(Expr::Literal(Literal::Int(input.parse()?)))
} else if lookahead.peek(LitStr) {
Ok(Expr::Literal(Literal::String(input.parse()?)))
} else if input.peek(kw::kw_super) {
Ok(Expr::Super {
keyword: input.parse()?,
distance: Cell::new(None),
dot: input.parse()?,
method: kw::ident(input)?,
})
} else if input.peek(kw::this) {
Ok(Expr::This {
keyword: input.parse()?,
distance: Cell::new(None),
})
} else if lookahead.peek(Ident) {
Ok(Expr::Variable {
name: kw::ident(input)?,
distance: Cell::new(None),
})
} else if lookahead.peek(Punct!["("]) {
let content;
let _: Parentheses = group!(content in input);
Ok(Expr::Group(Box::new(content.parse()?)))
} else {
Err(lookahead.error())
}
}
}
impl Parse for Expr {
fn parse(input: ParseStream) -> Result<Self> {
Expr::assignment(input)
}
}
#[derive(Debug, Clone, PartialEq)]
struct Function {
name: Ident,
params: Vec<Ident>,
body: Vec<Stmt>,
}
impl Parse for Function {
fn parse(input: ParseStream) -> Result<Self> {
let name: Ident = input.parse()?;
let mut contents: Group<Parentheses> = input.parse()?;
contents.remove_whitespace();
let Parentheses(span) = contents.delimiters();
let tokens = contents.into_token_stream();
let params: Vec<Ident> = if tokens.is_empty() {
vec![]
} else {
let params: Punctuated<Ident, Punct![","]> =
Punctuated::parse_separated.parse(tokens)?;
params.into_iter().collect()
};
if params.len() >= 255 {
input.add_error(input.new_error(
"Can't have more than 254 parameters".to_string(),
span,
error_codes::TOO_MANY_ARGS,
));
}
let mut contents: Group<Braces> = input.parse()?;
contents.remove_whitespace();
let body = block.parse(contents.into_token_stream())?;
Ok(Function { name, params, body })
}
sourcepub fn add_error(&self, error: Error)
pub fn add_error(&self, error: Error)
Adds a new error to this buffer’s storage.
Examples found in repository?
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709
fn assignment(input: ParseStream) -> Result<Self> {
let expr = Expr::or(input)?;
if input.peek(Punct!["="]) {
let equals: Punct!["="] = input.parse()?;
let value = Box::new(Expr::assignment(input)?);
if let Expr::Variable { name, .. } = expr {
return Ok(Expr::Assign {
name,
value,
distance: Cell::new(None),
});
} else if let Expr::Get { object, name } = expr {
return Ok(Expr::Set {
object,
name,
value,
});
}
input.add_error(input.new_error(
"Invalid assignment target".to_string(),
&equals,
error_codes::INVALID_ASSIGN,
));
}
Ok(expr)
}
fn or(input: ParseStream) -> Result<Self> {
let mut expr = Expr::and(input)?;
while input.peek(kw::or) {
expr = Expr::Logical(Box::new(Logical::Or(
expr,
input.parse()?,
Expr::and(input)?,
)));
}
Ok(expr)
}
fn and(input: ParseStream) -> Result<Self> {
let mut expr = Expr::equality(input)?;
while input.peek(kw::and) {
expr = Expr::Logical(Box::new(Logical::And(
expr,
input.parse()?,
Expr::equality(input)?,
)));
}
Ok(expr)
}
fn equality(input: ParseStream) -> Result<Self> {
let mut expr = Expr::comparison(input)?;
loop {
if input.peek(Punct!["=="]) {
expr = Expr::binary(Binary::Equal(
expr,
input.parse()?,
Expr::comparison(input)?,
));
} else if input.peek(Punct!["!="]) {
expr = Expr::binary(Binary::NotEqual(
expr,
input.parse()?,
Expr::comparison(input)?,
));
} else {
break Ok(expr);
}
}
}
fn comparison(input: ParseStream) -> Result<Self> {
let mut expr = Expr::term(input)?;
loop {
if input.peek(Punct![">"]) {
expr = Expr::binary(Binary::Greater(expr, input.parse()?, Expr::term(input)?));
} else if input.peek(Punct![">="]) {
expr = Expr::binary(Binary::GreaterEqual(
expr,
input.parse()?,
Expr::term(input)?,
));
} else if input.peek(Punct!["<"]) {
expr = Expr::binary(Binary::Less(expr, input.parse()?, Expr::term(input)?));
} else if input.peek(Punct!["<="]) {
expr = Expr::binary(Binary::LessEqual(expr, input.parse()?, Expr::term(input)?));
} else {
break Ok(expr);
}
}
}
fn term(input: ParseStream) -> Result<Self> {
let mut expr = Expr::factor(input)?;
loop {
if input.peek(Punct!["+"]) {
expr = Expr::binary(Binary::Add(expr, input.parse()?, Expr::factor(input)?));
} else if input.peek(Punct!["-"]) {
expr = Expr::binary(Binary::Sub(expr, input.parse()?, Expr::factor(input)?));
} else {
break Ok(expr);
}
}
}
fn factor(input: ParseStream) -> Result<Self> {
let mut expr = Expr::unary(input)?;
loop {
if input.peek(Punct!["*"]) {
expr = Expr::binary(Binary::Mul(expr, input.parse()?, Expr::unary(input)?));
} else if input.peek(Punct!["/"]) {
expr = Expr::binary(Binary::Div(expr, input.parse()?, Expr::unary(input)?));
} else {
break Ok(expr);
}
}
}
fn unary(input: ParseStream) -> Result<Self> {
if input.peek(Punct!["-"]) {
Ok(Expr::Unary(Box::new(Unary::Neg(
input.parse()?,
Expr::unary(input)?,
))))
} else if input.peek(Punct!["!"]) {
Ok(Expr::Unary(Box::new(Unary::Not(
input.parse()?,
Expr::unary(input)?,
))))
} else {
Expr::call(input)
}
}
fn call(input: ParseStream) -> Result<Self> {
let mut expr = Expr::primary(input)?;
loop {
if input.peek(Punct!["("]) {
expr = Expr::finish_call(input, expr)?;
} else if input.peek(Punct!["."]) {
let _: Punct!["."] = input.parse()?;
let name: Ident = input.parse()?;
expr = Expr::Get {
object: Box::new(expr),
name,
};
} else {
break Ok(expr);
}
}
}
fn finish_call(input: ParseStream<'_>, callee: Expr) -> Result<Self> {
let content;
let paren: Parentheses = group!(content in input);
let arguments: Punctuated<Expr, Punct![","]> =
Punctuated::parse_separated_trailing(&content)?;
let arguments: Vec<_> = arguments.into_iter().collect();
if arguments.len() >= 255 {
input.add_error(input.new_error(
"Can't have more than 254 arguments".to_string(),
paren.0.clone(),
error_codes::TOO_MANY_ARGS,
));
}
Ok(Expr::Call {
callee: Box::new(callee),
paren,
arguments,
})
}
fn primary(input: ParseStream) -> Result<Self> {
let lookahead = input.lookahead();
if lookahead.peek(kw::kw_false) {
Ok(Expr::Literal(Literal::False(input.parse()?)))
} else if lookahead.peek(kw::kw_true) {
Ok(Expr::Literal(Literal::True(input.parse()?)))
} else if lookahead.peek(kw::nil) {
Ok(Expr::Literal(Literal::Nil(input.parse()?)))
} else if lookahead.peek(LitFloat) {
Ok(Expr::Literal(Literal::Float(input.parse()?)))
} else if lookahead.peek(LitInt) {
Ok(Expr::Literal(Literal::Int(input.parse()?)))
} else if lookahead.peek(LitStr) {
Ok(Expr::Literal(Literal::String(input.parse()?)))
} else if input.peek(kw::kw_super) {
Ok(Expr::Super {
keyword: input.parse()?,
distance: Cell::new(None),
dot: input.parse()?,
method: kw::ident(input)?,
})
} else if input.peek(kw::this) {
Ok(Expr::This {
keyword: input.parse()?,
distance: Cell::new(None),
})
} else if lookahead.peek(Ident) {
Ok(Expr::Variable {
name: kw::ident(input)?,
distance: Cell::new(None),
})
} else if lookahead.peek(Punct!["("]) {
let content;
let _: Parentheses = group!(content in input);
Ok(Expr::Group(Box::new(content.parse()?)))
} else {
Err(lookahead.error())
}
}
}
impl Parse for Expr {
fn parse(input: ParseStream) -> Result<Self> {
Expr::assignment(input)
}
}
#[derive(Debug, Clone, PartialEq)]
struct Function {
name: Ident,
params: Vec<Ident>,
body: Vec<Stmt>,
}
impl Parse for Function {
fn parse(input: ParseStream) -> Result<Self> {
let name: Ident = input.parse()?;
let mut contents: Group<Parentheses> = input.parse()?;
contents.remove_whitespace();
let Parentheses(span) = contents.delimiters();
let tokens = contents.into_token_stream();
let params: Vec<Ident> = if tokens.is_empty() {
vec![]
} else {
let params: Punctuated<Ident, Punct![","]> =
Punctuated::parse_separated.parse(tokens)?;
params.into_iter().collect()
};
if params.len() >= 255 {
input.add_error(input.new_error(
"Can't have more than 254 parameters".to_string(),
span,
error_codes::TOO_MANY_ARGS,
));
}
let mut contents: Group<Braces> = input.parse()?;
contents.remove_whitespace();
let body = block.parse(contents.into_token_stream())?;
Ok(Function { name, params, body })
}
}
#[derive(Debug, Clone, PartialEq)]
enum Stmt {
Block(Vec<Stmt>),
Class {
name: Ident,
superclass: Option<Ident>,
superclass_distance: Cell<Option<usize>>,
methods: Vec<Function>,
},
Expr(Expr),
Function(Function),
If {
condition: Expr,
then_branch: Box<Stmt>,
else_branch: Option<Box<Stmt>>,
},
Print(Expr),
Return {
keyword: kw::kw_return,
value: Option<Expr>,
},
Variable {
name: Ident,
initialiser: Option<Expr>,
},
While {
condition: Expr,
body: Box<Stmt>,
},
}
fn block(input: ParseStream) -> Result<Vec<Stmt>> {
let mut statements = vec![];
while !input.is_empty() {
statements.push(Stmt::declaration(input)?);
}
Ok(statements)
}
impl Stmt {
fn declaration(input: ParseStream) -> Result<Self> {
if input.peek(kw::class) {
Stmt::class_declaration(input)
} else if input.peek(kw::fun) {
let _: kw::fun = input.parse()?;
Ok(Stmt::Function(Function::parse(input)?))
} else if input.peek(kw::var) {
Stmt::var_declaration(input)
} else {
Stmt::statement(input)
}
}
fn class_declaration(input: ParseStream) -> Result<Self> {
let _: kw::class = input.parse()?;
let name: Ident = input.parse()?;
let superclass = if input.peek(Punct!["<"]) {
let _: Punct!["<"] = input.parse()?;
Some(input.parse()?)
} else {
None
};
let content;
let _: Braces = group!(content in input);
let methods = parse_repeated(&content)?;
Ok(Stmt::Class {
name,
superclass,
superclass_distance: Cell::new(None),
methods,
})
}
fn var_declaration(input: ParseStream) -> Result<Self> {
let _: kw::var = input.parse()?;
let name = kw::ident(input)?;
let initialiser = if input.peek(Punct!["="]) {
let _: Punct!["="] = input.parse()?;
Some(input.parse()?)
} else {
None
};
let _: Punct![";"] = input.parse()?;
Ok(Stmt::Variable { name, initialiser })
}
fn statement(input: ParseStream) -> Result<Self> {
if input.peek(kw::kw_if) {
Stmt::if_statement(input)
} else if input.peek(kw::kw_for) {
Stmt::for_statement(input)
} else if input.peek(kw::print) {
Stmt::print_statement(input)
} else if input.peek(kw::kw_return) {
Stmt::return_statement(input)
} else if input.peek(kw::kw_while) {
Stmt::while_statement(input)
} else if input.peek(Punct!["{"]) {
let content;
let _: Braces = group!(content in input);
Ok(Stmt::Block(block(&content)?))
} else {
Stmt::expression_statement(input)
}
}
fn for_statement(input: ParseStream) -> Result<Self> {
let _: kw::kw_for = input.parse()?;
let content;
let _: Parentheses = group!(content in input);
let initialiser = if content.peek(Punct![";"]) {
let _: Punct![";"] = content.parse()?;
None
} else if content.peek(kw::var) {
Some(Stmt::var_declaration(&content)?)
} else {
Some(Stmt::expression_statement(&content)?)
};
let condition = if content.peek(Punct![";"]) {
Expr::Literal(Literal::True(kw::kw_true::new(&content)))
} else {
content.parse()?
};
let _: Punct![";"] = content.parse()?;
let increment = if content.is_empty() {
None
} else {
Some(content.parse()?)
};
let mut body = Stmt::statement(input)?;
if let Some(increment) = increment {
body = Stmt::Block(vec![body, Stmt::Expr(increment)]);
}
body = Stmt::While {
condition,
body: Box::new(body),
};
if let Some(initialiser) = initialiser {
body = Stmt::Block(vec![initialiser, body]);
}
Ok(body)
}
fn if_statement(input: ParseStream) -> Result<Self> {
let _: kw::kw_if = input.parse()?;
let content;
let _: Parentheses = group!(content in input);
let condition = content.parse()?;
let then_branch = Box::new(Stmt::statement(input)?);
let else_branch = if input.peek(kw::kw_else) {
Some(Box::new(Stmt::statement(input)?))
} else {
None
};
Ok(Stmt::If {
condition,
then_branch,
else_branch,
})
}
fn print_statement(input: ParseStream) -> Result<Self> {
let _: kw::print = input.parse()?;
let value = input.parse()?;
let _: Punct![";"] = input.parse()?;
Ok(Self::Print(value))
}
fn return_statement(input: ParseStream) -> Result<Self> {
let keyword: kw::kw_return = input.parse()?;
let value = if input.peek(Punct![";"]) {
None
} else {
Some(input.parse()?)
};
let _: Punct![";"] = input.parse()?;
Ok(Stmt::Return { keyword, value })
}
fn while_statement(input: ParseStream) -> Result<Self> {
let _: kw::kw_while = input.parse()?;
let content;
let _: Parentheses = group!(content in input);
let condition = content.parse()?;
let body = Box::new(Stmt::statement(input)?);
Ok(Stmt::While { condition, body })
}
fn expression_statement(input: ParseStream) -> Result<Self> {
let expr = input.parse()?;
let _: Punct![";"] = input.parse()?;
Ok(Self::Expr(expr))
}
}
impl Parse for Stmt {
fn parse(input: ParseStream) -> Result<Self> {
Stmt::declaration(input)
}
}
struct Ast(Vec<Stmt>);
impl Ast {
#[allow(clippy::wildcard_imports)]
fn synchronise(input: ParseStream) {
input.synchronise(|input| {
use kw::*;
input.peek(Punct![";"]) && !input.peek2(Punct!["}"])
|| peek2_any!(input, class, kw_for, fun, kw_if, print, kw_return, var, kw_while)
});
}
}
impl Parse for Ast {
fn parse(input: ParseStream) -> Result<Self> {
let mut stmts = vec![];
while !input.is_empty() {
match input.parse() {
Ok(stmt) => stmts.push(stmt),
Err(err) => {
Ast::synchronise(input);
input.add_error(err);
}
}
}
input.get_error().map_or(Ok(Ast(stmts)), Err)
}
sourcepub fn get_error(&self) -> Option<Error>
pub fn get_error(&self) -> Option<Error>
Returns an error consisting of all errors from
ParseBuffer::add_error
, if it has been called.
Examples found in repository?
695 696 697 698 699 700 701 702 703 704 705 706 707 708 709
fn parse(input: ParseStream) -> Result<Self> {
let mut stmts = vec![];
while !input.is_empty() {
match input.parse() {
Ok(stmt) => stmts.push(stmt),
Err(err) => {
Ast::synchronise(input);
input.add_error(err);
}
}
}
input.get_error().map_or(Ok(Ast(stmts)), Err)
}
sourcepub fn synchronise<F: FnMut(ParseStream<'_>) -> bool>(&self, function: F)
pub fn synchronise<F: FnMut(ParseStream<'_>) -> bool>(&self, function: F)
Repeatedly skips tokens until function
returns true or self
is
empty.
sourcepub fn parse_joint<T1: Token, T2: Token>(&self) -> Result<(T1, T2)>
pub fn parse_joint<T1: Token, T2: Token>(&self) -> Result<(T1, T2)>
Parses T1
and T2
, with no whitespace allowed between them.
Errors
Returns an error if self
does not start with the required tokens.
sourcepub fn parse_repeated<T: Parse>(&self) -> Result<Vec<T>>
pub fn parse_repeated<T: Parse>(&self) -> Result<Vec<T>>
Attempts to parse self
into Vec<T>
, with no separating punctuation,
fully consuming self
.
To parse separated instances of T
, see
Punctuated.
Errors
Returns an error if self
is not a valid sequence of T
.
sourcepub fn peek<T: Peek>(&self, token: T) -> bool
pub fn peek<T: Peek>(&self, token: T) -> bool
Returns true if the next token is an instance of T
.
Examples found in repository?
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
fn parse(input: ParseStream) -> Result<Self> {
let mut expr = factor(input)?;
loop {
if input.peek(Punct!["+"]) {
expr = Expr::Add(Box::new(expr), input.parse()?, Box::new(factor(input)?));
} else if input.peek(Punct!["-"]) {
expr = Expr::Sub(Box::new(expr), input.parse()?, Box::new(factor(input)?));
} else {
break;
}
}
Ok(expr)
}
}
fn factor(input: ParseStream) -> Result<Expr> {
let mut expr: Expr = unary(input)?;
loop {
if input.peek(Punct!["*"]) {
expr = Expr::Mul(Box::new(expr), input.parse()?, Box::new(unary(input)?));
} else if input.peek(Punct!["/"]) {
expr = Expr::Div(Box::new(expr), input.parse()?, Box::new(unary(input)?));
} else if input.peek(Punct!["%"]) {
expr = Expr::Mod(Box::new(expr), input.parse()?, Box::new(unary(input)?));
} else {
break;
}
}
Ok(expr)
}
fn unary(input: ParseStream) -> Result<Expr> {
if input.peek(Punct!["-"]) {
Ok(Expr::Neg(input.parse()?, Box::new(unary(input)?)))
} else {
primary(input)
}
}
More examples
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691
fn assignment(input: ParseStream) -> Result<Self> {
let expr = Expr::or(input)?;
if input.peek(Punct!["="]) {
let equals: Punct!["="] = input.parse()?;
let value = Box::new(Expr::assignment(input)?);
if let Expr::Variable { name, .. } = expr {
return Ok(Expr::Assign {
name,
value,
distance: Cell::new(None),
});
} else if let Expr::Get { object, name } = expr {
return Ok(Expr::Set {
object,
name,
value,
});
}
input.add_error(input.new_error(
"Invalid assignment target".to_string(),
&equals,
error_codes::INVALID_ASSIGN,
));
}
Ok(expr)
}
fn or(input: ParseStream) -> Result<Self> {
let mut expr = Expr::and(input)?;
while input.peek(kw::or) {
expr = Expr::Logical(Box::new(Logical::Or(
expr,
input.parse()?,
Expr::and(input)?,
)));
}
Ok(expr)
}
fn and(input: ParseStream) -> Result<Self> {
let mut expr = Expr::equality(input)?;
while input.peek(kw::and) {
expr = Expr::Logical(Box::new(Logical::And(
expr,
input.parse()?,
Expr::equality(input)?,
)));
}
Ok(expr)
}
fn equality(input: ParseStream) -> Result<Self> {
let mut expr = Expr::comparison(input)?;
loop {
if input.peek(Punct!["=="]) {
expr = Expr::binary(Binary::Equal(
expr,
input.parse()?,
Expr::comparison(input)?,
));
} else if input.peek(Punct!["!="]) {
expr = Expr::binary(Binary::NotEqual(
expr,
input.parse()?,
Expr::comparison(input)?,
));
} else {
break Ok(expr);
}
}
}
fn comparison(input: ParseStream) -> Result<Self> {
let mut expr = Expr::term(input)?;
loop {
if input.peek(Punct![">"]) {
expr = Expr::binary(Binary::Greater(expr, input.parse()?, Expr::term(input)?));
} else if input.peek(Punct![">="]) {
expr = Expr::binary(Binary::GreaterEqual(
expr,
input.parse()?,
Expr::term(input)?,
));
} else if input.peek(Punct!["<"]) {
expr = Expr::binary(Binary::Less(expr, input.parse()?, Expr::term(input)?));
} else if input.peek(Punct!["<="]) {
expr = Expr::binary(Binary::LessEqual(expr, input.parse()?, Expr::term(input)?));
} else {
break Ok(expr);
}
}
}
fn term(input: ParseStream) -> Result<Self> {
let mut expr = Expr::factor(input)?;
loop {
if input.peek(Punct!["+"]) {
expr = Expr::binary(Binary::Add(expr, input.parse()?, Expr::factor(input)?));
} else if input.peek(Punct!["-"]) {
expr = Expr::binary(Binary::Sub(expr, input.parse()?, Expr::factor(input)?));
} else {
break Ok(expr);
}
}
}
fn factor(input: ParseStream) -> Result<Self> {
let mut expr = Expr::unary(input)?;
loop {
if input.peek(Punct!["*"]) {
expr = Expr::binary(Binary::Mul(expr, input.parse()?, Expr::unary(input)?));
} else if input.peek(Punct!["/"]) {
expr = Expr::binary(Binary::Div(expr, input.parse()?, Expr::unary(input)?));
} else {
break Ok(expr);
}
}
}
fn unary(input: ParseStream) -> Result<Self> {
if input.peek(Punct!["-"]) {
Ok(Expr::Unary(Box::new(Unary::Neg(
input.parse()?,
Expr::unary(input)?,
))))
} else if input.peek(Punct!["!"]) {
Ok(Expr::Unary(Box::new(Unary::Not(
input.parse()?,
Expr::unary(input)?,
))))
} else {
Expr::call(input)
}
}
fn call(input: ParseStream) -> Result<Self> {
let mut expr = Expr::primary(input)?;
loop {
if input.peek(Punct!["("]) {
expr = Expr::finish_call(input, expr)?;
} else if input.peek(Punct!["."]) {
let _: Punct!["."] = input.parse()?;
let name: Ident = input.parse()?;
expr = Expr::Get {
object: Box::new(expr),
name,
};
} else {
break Ok(expr);
}
}
}
fn finish_call(input: ParseStream<'_>, callee: Expr) -> Result<Self> {
let content;
let paren: Parentheses = group!(content in input);
let arguments: Punctuated<Expr, Punct![","]> =
Punctuated::parse_separated_trailing(&content)?;
let arguments: Vec<_> = arguments.into_iter().collect();
if arguments.len() >= 255 {
input.add_error(input.new_error(
"Can't have more than 254 arguments".to_string(),
paren.0.clone(),
error_codes::TOO_MANY_ARGS,
));
}
Ok(Expr::Call {
callee: Box::new(callee),
paren,
arguments,
})
}
fn primary(input: ParseStream) -> Result<Self> {
let lookahead = input.lookahead();
if lookahead.peek(kw::kw_false) {
Ok(Expr::Literal(Literal::False(input.parse()?)))
} else if lookahead.peek(kw::kw_true) {
Ok(Expr::Literal(Literal::True(input.parse()?)))
} else if lookahead.peek(kw::nil) {
Ok(Expr::Literal(Literal::Nil(input.parse()?)))
} else if lookahead.peek(LitFloat) {
Ok(Expr::Literal(Literal::Float(input.parse()?)))
} else if lookahead.peek(LitInt) {
Ok(Expr::Literal(Literal::Int(input.parse()?)))
} else if lookahead.peek(LitStr) {
Ok(Expr::Literal(Literal::String(input.parse()?)))
} else if input.peek(kw::kw_super) {
Ok(Expr::Super {
keyword: input.parse()?,
distance: Cell::new(None),
dot: input.parse()?,
method: kw::ident(input)?,
})
} else if input.peek(kw::this) {
Ok(Expr::This {
keyword: input.parse()?,
distance: Cell::new(None),
})
} else if lookahead.peek(Ident) {
Ok(Expr::Variable {
name: kw::ident(input)?,
distance: Cell::new(None),
})
} else if lookahead.peek(Punct!["("]) {
let content;
let _: Parentheses = group!(content in input);
Ok(Expr::Group(Box::new(content.parse()?)))
} else {
Err(lookahead.error())
}
}
}
impl Parse for Expr {
fn parse(input: ParseStream) -> Result<Self> {
Expr::assignment(input)
}
}
#[derive(Debug, Clone, PartialEq)]
struct Function {
name: Ident,
params: Vec<Ident>,
body: Vec<Stmt>,
}
impl Parse for Function {
fn parse(input: ParseStream) -> Result<Self> {
let name: Ident = input.parse()?;
let mut contents: Group<Parentheses> = input.parse()?;
contents.remove_whitespace();
let Parentheses(span) = contents.delimiters();
let tokens = contents.into_token_stream();
let params: Vec<Ident> = if tokens.is_empty() {
vec![]
} else {
let params: Punctuated<Ident, Punct![","]> =
Punctuated::parse_separated.parse(tokens)?;
params.into_iter().collect()
};
if params.len() >= 255 {
input.add_error(input.new_error(
"Can't have more than 254 parameters".to_string(),
span,
error_codes::TOO_MANY_ARGS,
));
}
let mut contents: Group<Braces> = input.parse()?;
contents.remove_whitespace();
let body = block.parse(contents.into_token_stream())?;
Ok(Function { name, params, body })
}
}
#[derive(Debug, Clone, PartialEq)]
enum Stmt {
Block(Vec<Stmt>),
Class {
name: Ident,
superclass: Option<Ident>,
superclass_distance: Cell<Option<usize>>,
methods: Vec<Function>,
},
Expr(Expr),
Function(Function),
If {
condition: Expr,
then_branch: Box<Stmt>,
else_branch: Option<Box<Stmt>>,
},
Print(Expr),
Return {
keyword: kw::kw_return,
value: Option<Expr>,
},
Variable {
name: Ident,
initialiser: Option<Expr>,
},
While {
condition: Expr,
body: Box<Stmt>,
},
}
fn block(input: ParseStream) -> Result<Vec<Stmt>> {
let mut statements = vec![];
while !input.is_empty() {
statements.push(Stmt::declaration(input)?);
}
Ok(statements)
}
impl Stmt {
fn declaration(input: ParseStream) -> Result<Self> {
if input.peek(kw::class) {
Stmt::class_declaration(input)
} else if input.peek(kw::fun) {
let _: kw::fun = input.parse()?;
Ok(Stmt::Function(Function::parse(input)?))
} else if input.peek(kw::var) {
Stmt::var_declaration(input)
} else {
Stmt::statement(input)
}
}
fn class_declaration(input: ParseStream) -> Result<Self> {
let _: kw::class = input.parse()?;
let name: Ident = input.parse()?;
let superclass = if input.peek(Punct!["<"]) {
let _: Punct!["<"] = input.parse()?;
Some(input.parse()?)
} else {
None
};
let content;
let _: Braces = group!(content in input);
let methods = parse_repeated(&content)?;
Ok(Stmt::Class {
name,
superclass,
superclass_distance: Cell::new(None),
methods,
})
}
fn var_declaration(input: ParseStream) -> Result<Self> {
let _: kw::var = input.parse()?;
let name = kw::ident(input)?;
let initialiser = if input.peek(Punct!["="]) {
let _: Punct!["="] = input.parse()?;
Some(input.parse()?)
} else {
None
};
let _: Punct![";"] = input.parse()?;
Ok(Stmt::Variable { name, initialiser })
}
fn statement(input: ParseStream) -> Result<Self> {
if input.peek(kw::kw_if) {
Stmt::if_statement(input)
} else if input.peek(kw::kw_for) {
Stmt::for_statement(input)
} else if input.peek(kw::print) {
Stmt::print_statement(input)
} else if input.peek(kw::kw_return) {
Stmt::return_statement(input)
} else if input.peek(kw::kw_while) {
Stmt::while_statement(input)
} else if input.peek(Punct!["{"]) {
let content;
let _: Braces = group!(content in input);
Ok(Stmt::Block(block(&content)?))
} else {
Stmt::expression_statement(input)
}
}
fn for_statement(input: ParseStream) -> Result<Self> {
let _: kw::kw_for = input.parse()?;
let content;
let _: Parentheses = group!(content in input);
let initialiser = if content.peek(Punct![";"]) {
let _: Punct![";"] = content.parse()?;
None
} else if content.peek(kw::var) {
Some(Stmt::var_declaration(&content)?)
} else {
Some(Stmt::expression_statement(&content)?)
};
let condition = if content.peek(Punct![";"]) {
Expr::Literal(Literal::True(kw::kw_true::new(&content)))
} else {
content.parse()?
};
let _: Punct![";"] = content.parse()?;
let increment = if content.is_empty() {
None
} else {
Some(content.parse()?)
};
let mut body = Stmt::statement(input)?;
if let Some(increment) = increment {
body = Stmt::Block(vec![body, Stmt::Expr(increment)]);
}
body = Stmt::While {
condition,
body: Box::new(body),
};
if let Some(initialiser) = initialiser {
body = Stmt::Block(vec![initialiser, body]);
}
Ok(body)
}
fn if_statement(input: ParseStream) -> Result<Self> {
let _: kw::kw_if = input.parse()?;
let content;
let _: Parentheses = group!(content in input);
let condition = content.parse()?;
let then_branch = Box::new(Stmt::statement(input)?);
let else_branch = if input.peek(kw::kw_else) {
Some(Box::new(Stmt::statement(input)?))
} else {
None
};
Ok(Stmt::If {
condition,
then_branch,
else_branch,
})
}
fn print_statement(input: ParseStream) -> Result<Self> {
let _: kw::print = input.parse()?;
let value = input.parse()?;
let _: Punct![";"] = input.parse()?;
Ok(Self::Print(value))
}
fn return_statement(input: ParseStream) -> Result<Self> {
let keyword: kw::kw_return = input.parse()?;
let value = if input.peek(Punct![";"]) {
None
} else {
Some(input.parse()?)
};
let _: Punct![";"] = input.parse()?;
Ok(Stmt::Return { keyword, value })
}
fn while_statement(input: ParseStream) -> Result<Self> {
let _: kw::kw_while = input.parse()?;
let content;
let _: Parentheses = group!(content in input);
let condition = content.parse()?;
let body = Box::new(Stmt::statement(input)?);
Ok(Stmt::While { condition, body })
}
fn expression_statement(input: ParseStream) -> Result<Self> {
let expr = input.parse()?;
let _: Punct![";"] = input.parse()?;
Ok(Self::Expr(expr))
}
}
impl Parse for Stmt {
fn parse(input: ParseStream) -> Result<Self> {
Stmt::declaration(input)
}
}
struct Ast(Vec<Stmt>);
impl Ast {
#[allow(clippy::wildcard_imports)]
fn synchronise(input: ParseStream) {
input.synchronise(|input| {
use kw::*;
input.peek(Punct![";"]) && !input.peek2(Punct!["}"])
|| peek2_any!(input, class, kw_for, fun, kw_if, print, kw_return, var, kw_while)
});
}
sourcepub fn peek2<T: Peek>(&self, token: T) -> bool
pub fn peek2<T: Peek>(&self, token: T) -> bool
Returns true if the next token is an instance of T
.
Note that for the purposes of this function, multi-character punctuation
like +=
is considered to be two tokens, and float literals are
considered to be three tokens (start, .
, end).
sourcepub fn current_span(&self) -> Result<Span>
pub fn current_span(&self) -> Result<Span>
sourcepub fn fork(&self) -> ParseBuffer<'a>
pub fn fork(&self) -> ParseBuffer<'a>
Creates a new ParseBuffer
at the same position as self
.
Changes to self
will not affect the fork, and vice versa.
sourcepub fn commit(&self, fork: &Self)
pub fn commit(&self, fork: &Self)
Commits a forked buffer into self
, updating self
to reflect fork
.
Panics
This function will panic if fork
wasn’t forked from self
or if
self
is further ahead than fork
.
sourcepub fn unexpected_token(&self, expected: HashSet<String>) -> Error
pub fn unexpected_token(&self, expected: HashSet<String>) -> Error
Creates an error with the message Unexpected token
and the given
expected tokens.
Use of this function is generally discouraged in favour of
Lookahead::error
.
sourcepub fn lookahead(&self) -> Lookahead<'a>
pub fn lookahead(&self) -> Lookahead<'a>
Creates a helper struct for peeking at the next token.
Examples found in repository?
81 82 83 84 85 86 87 88 89 90 91 92 93
fn primary(input: ParseStream) -> Result<Expr> {
let lookahead = input.lookahead();
if lookahead.peek(token::LitFloat) {
Ok(Expr::Num(input.parse::<token::LitFloat>()?.value()))
} else if lookahead.peek(token::LitInt) {
Ok(Expr::Num(input.parse::<token::LitInt>()?.value() as f64))
} else if lookahead.peek(token::LeftParen) {
let group: Group<Parentheses> = input.parse()?;
parse(group.into_token_stream())
} else {
Err(lookahead.error())
}
}
More examples
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
fn primary(input: ParseStream) -> Result<Self> {
let lookahead = input.lookahead();
if lookahead.peek(kw::kw_false) {
Ok(Expr::Literal(Literal::False(input.parse()?)))
} else if lookahead.peek(kw::kw_true) {
Ok(Expr::Literal(Literal::True(input.parse()?)))
} else if lookahead.peek(kw::nil) {
Ok(Expr::Literal(Literal::Nil(input.parse()?)))
} else if lookahead.peek(LitFloat) {
Ok(Expr::Literal(Literal::Float(input.parse()?)))
} else if lookahead.peek(LitInt) {
Ok(Expr::Literal(Literal::Int(input.parse()?)))
} else if lookahead.peek(LitStr) {
Ok(Expr::Literal(Literal::String(input.parse()?)))
} else if input.peek(kw::kw_super) {
Ok(Expr::Super {
keyword: input.parse()?,
distance: Cell::new(None),
dot: input.parse()?,
method: kw::ident(input)?,
})
} else if input.peek(kw::this) {
Ok(Expr::This {
keyword: input.parse()?,
distance: Cell::new(None),
})
} else if lookahead.peek(Ident) {
Ok(Expr::Variable {
name: kw::ident(input)?,
distance: Cell::new(None),
})
} else if lookahead.peek(Punct!["("]) {
let content;
let _: Parentheses = group!(content in input);
Ok(Expr::Group(Box::new(content.parse()?)))
} else {
Err(lookahead.error())
}
}
sourcepub fn skip_whitespace(&self)
pub fn skip_whitespace(&self)
Skips over all whitespace tokens before the next non-whitespace token.
This method will not skip newlines.
sourcepub fn empty_span(&self) -> Span
pub fn empty_span(&self) -> Span
Creates a new empty Span with this stream’s source file.