Expand description

Statement Parsers

Parsers for each of the different statement types.

Example:

let source_code = "
while y > 5 do
  f(y)
  y = y - g(x, y)
end
";
let (_remaining, parsed_ast) = statement(source_code).expect("parse error");

let expected_ast = Statement::WhileStatement {
  condition: Expression::Binary {
    left: Box::new(Expression::Identifier(Identifier("y".to_owned()))),
    op: BinaryOperator::Gt,
    right: Box::new(Expression::Integer(IntegerLiteral(5_i64))),
  },
  body: vec![
    Statement::SingleStatement(Expression::FunctionCall {
      left: Box::new(Expression::Identifier(Identifier("f".to_owned()))),
      args: vec![Expression::Identifier(Identifier("y".to_owned()))],
    }),
    Statement::AssignStatement(Identifier("y".to_owned()), Expression::Binary {
      left: Box::new(Expression::Identifier(Identifier("y".to_owned()))),
      op: BinaryOperator::Sub,
      right: Box::new(Expression::FunctionCall {
        left: Box::new(Expression::Identifier(Identifier("g".to_owned()))),
        args: vec![
          Expression::Identifier(Identifier("x".to_owned())),
          Expression::Identifier(Identifier("y".to_owned())),
        ],
      }),
    })
  ],
};

assert_eq!(parsed_ast, expected_ast);

Functions

Assignment from an expression to an identifier

Body of a loop, function, etc.

If statement with optional else clause and elseif clauses

Assignment from an expression into an indexed container

Return statement with optional return expression

An expression treated as a single statement

Any statement

While loop