Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Statement Parsing

BSharp provides comprehensive parsing for all C# statement types, from simple expressions to complex control flow constructs.

Statement Categories

1. Declaration Statements

Local Variable Declarations

int x = 5;
var name = "John";
const double PI = 3.14159;

Local Function Declarations

void LocalFunction(int parameter)
{
    // function body
}

T GenericLocalFunction<T>(T value) where T : class
{
    return value;
}

2. Expression Statements

Any expression followed by a semicolon:

x++;                    // Increment
Method();              // Method call
obj.Property = value;  // Assignment

3. Control Flow Statements

Conditional Statements

If Statements

if (condition)
    statement;

if (condition)
{
    // block
}
else if (otherCondition)
{
    // else if block
}
else
{
    // else block
}

Switch Statements

switch (expression)
{
    case constant1:
        statements;
        break;
    case constant2 when condition:
        statements;
        goto case constant1;
    default:
        statements;
        break;
}

Loop Statements

For Loops

for (int i = 0; i < 10; i++)
{
    // loop body
}

for (;;)  // infinite loop
{
    // body
}

Foreach Loops

foreach (var item in collection)
{
    // process item
}

foreach ((string key, int value) in dictionary)
{
    // deconstruction in foreach
}

While Loops

while (condition)
{
    // loop body
}

Do-While Loops

do
{
    // loop body
} while (condition);

Jump Statements

break;              // Break from loop/switch
continue;           // Continue to next iteration
return;             // Return from method
return value;       // Return with value
goto label;         // Jump to label
goto case 5;        // Jump to switch case
goto default;       // Jump to switch default

4. Exception Handling

Try-Catch-Finally

try
{
    // risky code
}
catch (SpecificException ex) when (ex.Code == 123)
{
    // specific exception handling
}
catch (Exception ex)
{
    // general exception handling
}
finally
{
    // cleanup code
}

Throw Statements

throw;                           // Rethrow current exception
throw new InvalidOperationException();
throw new CustomException("message");

5. Resource Management

Using Statements

using (var resource = new DisposableResource())
{
    // use resource
}

using var resource = new DisposableResource();
// resource disposed at end of scope

Lock Statements

lock (syncObject)
{
    // synchronized code
}

Fixed Statements

unsafe
{
    fixed (byte* ptr = array)
    {
        // work with fixed pointer
    }
}

6. Special Statements

Yield Statements

yield return value;     // Return value in iterator
yield break;           // End iterator

Checked/Unchecked Statements

checked
{
    // arithmetic overflow checking enabled
}

unchecked
{
    // arithmetic overflow checking disabled
}

Unsafe Statements

unsafe
{
    // unsafe code block
}

Statement Parsing Implementation

Use the spanned entrypoint and unwrap when spans are not needed:

#![allow(unused)]
fn main() {
use bsharp_parser::parser::statement_parser::parse_statement_ws_spanned;
use bsharp_syntax::span::Span;

let result = parse_statement_ws_spanned(Span::new(input))
    .map(|(rest, s)| (rest, s.node));
}

Block Statements

Block statements group multiple statements:

{
    int x = 5;
    Console.WriteLine(x);
    if (x > 0)
    {
        Console.WriteLine("Positive");
    }
}

Error Recovery

The statement parser implements robust error recovery:

  1. Statement-level recovery: Skip to next statement boundary (semicolon or brace)
  2. Block-level recovery: Skip to matching brace
  3. Context preservation: Maintain parsing context across errors

Statement Attributes

Statements can have attributes applied:

[Obsolete("Use NewMethod instead")]
void OldMethod() { }

[ConditionalAttribute("DEBUG")]
static void DebugMethod() { }

Top-Level Statements

Support for C# 9+ top-level statements:

// Program.cs
using System;

Console.WriteLine("Hello World!");
return 0;

The statement parser is designed to handle the full complexity of C# control flow while providing clear error messages and robust error recovery.

2025-11-17 15:18:26 • commit: 03a4e25