cairo-lang-parser 2.18.0

Cairo parser.
Documentation
//! > Test if-else without extra braces

//! > test_runner_name
test_partial_parser_tree(expect_diagnostics: false)

//! > cairo_code
fn f() {
    if 1 == 2 {
        3
    } else if 4 == 5 {
        6
    } else if 7 == 8 {
        9
    } else {
        10
    }
}

//! > top_level_kind
ExprIf

//! > ignored_kinds
ExprBinary

//! > expected_diagnostics

//! > expected_tree
└── Top level kind: ExprIf
    ├── if_kw (kind: TokenIf): 'if'
    ├── conditions (kind: ConditionListAnd)
    │   └── item #0 (kind: ConditionExpr)
    │       └── expr (kind: ExprBinary) <ignored>
    ├── if_block (kind: ExprBlock)
    │   ├── lbrace (kind: TokenLBrace): '{'
    │   ├── statements (kind: StatementList)
    │   │   └── child #0 (kind: StatementExpr)
    │   │       ├── attributes (kind: AttributeList) []
    │   │       ├── expr (kind: TokenLiteralNumber): '3'
    │   │       └── semicolon (kind: OptionTerminalSemicolonEmpty) []
    │   └── rbrace (kind: TokenRBrace): '}'
    └── else_clause (kind: ElseClause)
        ├── else_kw (kind: TokenElse): 'else'
        └── else_block_or_if (kind: ExprIf)
            ├── if_kw (kind: TokenIf): 'if'
            ├── conditions (kind: ConditionListAnd)
            │   └── item #0 (kind: ConditionExpr)
            │       └── expr (kind: ExprBinary) <ignored>
            ├── if_block (kind: ExprBlock)
            │   ├── lbrace (kind: TokenLBrace): '{'
            │   ├── statements (kind: StatementList)
            │   │   └── child #0 (kind: StatementExpr)
            │   │       ├── attributes (kind: AttributeList) []
            │   │       ├── expr (kind: TokenLiteralNumber): '6'
            │   │       └── semicolon (kind: OptionTerminalSemicolonEmpty) []
            │   └── rbrace (kind: TokenRBrace): '}'
            └── else_clause (kind: ElseClause)
                ├── else_kw (kind: TokenElse): 'else'
                └── else_block_or_if (kind: ExprIf)
                    ├── if_kw (kind: TokenIf): 'if'
                    ├── conditions (kind: ConditionListAnd)
                    │   └── item #0 (kind: ConditionExpr)
                    │       └── expr (kind: ExprBinary) <ignored>
                    ├── if_block (kind: ExprBlock)
                    │   ├── lbrace (kind: TokenLBrace): '{'
                    │   ├── statements (kind: StatementList)
                    │   │   └── child #0 (kind: StatementExpr)
                    │   │       ├── attributes (kind: AttributeList) []
                    │   │       ├── expr (kind: TokenLiteralNumber): '9'
                    │   │       └── semicolon (kind: OptionTerminalSemicolonEmpty) []
                    │   └── rbrace (kind: TokenRBrace): '}'
                    └── else_clause (kind: ElseClause)
                        ├── else_kw (kind: TokenElse): 'else'
                        └── else_block_or_if (kind: ExprBlock)
                            ├── lbrace (kind: TokenLBrace): '{'
                            ├── statements (kind: StatementList)
                            │   └── child #0 (kind: StatementExpr)
                            │       ├── attributes (kind: AttributeList) []
                            │       ├── expr (kind: TokenLiteralNumber): '10'
                            │       └── semicolon (kind: OptionTerminalSemicolonEmpty) []
                            └── rbrace (kind: TokenRBrace): '}'

//! > ==========================================================================

//! > Test if-let

//! > test_runner_name
test_partial_parser_tree(expect_diagnostics: false)

//! > cairo_code
fn f() {
    if let Some(x) = 2 {
        3
    } else if 4 == 5 {
        6
    } else if let None(y) = 8 {
        9
    }
}

//! > top_level_kind
ExprIf

//! > ignored_kinds
ExprBlock

//! > expected_diagnostics

//! > expected_tree
└── Top level kind: ExprIf
    ├── if_kw (kind: TokenIf): 'if'
    ├── conditions (kind: ConditionListAnd)
    │   └── item #0 (kind: ConditionLet)
    │       ├── let_kw (kind: TokenLet): 'let'
    │       ├── patterns (kind: PatternListOr)
    │       │   └── item #0 (kind: PatternEnum)
    │       │       ├── path (kind: ExprPath)
    │       │       │   ├── dollar (kind: OptionTerminalDollarEmpty) []
    │       │       │   └── segments (kind: ExprPathInner)
    │       │       │       └── item #0 (kind: PathSegmentSimple)
    │       │       │           └── ident (kind: TokenIdentifier): 'Some'
    │       │       └── pattern (kind: PatternEnumInnerPattern)
    │       │           ├── lparen (kind: TokenLParen): '('
    │       │           ├── pattern (kind: ExprPath)
    │       │           │   ├── dollar (kind: OptionTerminalDollarEmpty) []
    │       │           │   └── segments (kind: ExprPathInner)
    │       │           │       └── item #0 (kind: PathSegmentSimple)
    │       │           │           └── ident (kind: TokenIdentifier): 'x'
    │       │           └── rparen (kind: TokenRParen): ')'
    │       ├── eq (kind: TokenEq): '='
    │       └── expr (kind: TokenLiteralNumber): '2'
    ├── if_block (kind: ExprBlock) <ignored>
    └── else_clause (kind: ElseClause)
        ├── else_kw (kind: TokenElse): 'else'
        └── else_block_or_if (kind: ExprIf)
            ├── if_kw (kind: TokenIf): 'if'
            ├── conditions (kind: ConditionListAnd)
            │   └── item #0 (kind: ConditionExpr)
            │       └── expr (kind: ExprBinary)
            │           ├── lhs (kind: TokenLiteralNumber): '4'
            │           ├── op (kind: TokenEqEq): '=='
            │           └── rhs (kind: TokenLiteralNumber): '5'
            ├── if_block (kind: ExprBlock) <ignored>
            └── else_clause (kind: ElseClause)
                ├── else_kw (kind: TokenElse): 'else'
                └── else_block_or_if (kind: ExprIf)
                    ├── if_kw (kind: TokenIf): 'if'
                    ├── conditions (kind: ConditionListAnd)
                    │   └── item #0 (kind: ConditionLet)
                    │       ├── let_kw (kind: TokenLet): 'let'
                    │       ├── patterns (kind: PatternListOr)
                    │       │   └── item #0 (kind: PatternEnum)
                    │       │       ├── path (kind: ExprPath)
                    │       │       │   ├── dollar (kind: OptionTerminalDollarEmpty) []
                    │       │       │   └── segments (kind: ExprPathInner)
                    │       │       │       └── item #0 (kind: PathSegmentSimple)
                    │       │       │           └── ident (kind: TokenIdentifier): 'None'
                    │       │       └── pattern (kind: PatternEnumInnerPattern)
                    │       │           ├── lparen (kind: TokenLParen): '('
                    │       │           ├── pattern (kind: ExprPath)
                    │       │           │   ├── dollar (kind: OptionTerminalDollarEmpty) []
                    │       │           │   └── segments (kind: ExprPathInner)
                    │       │           │       └── item #0 (kind: PathSegmentSimple)
                    │       │           │           └── ident (kind: TokenIdentifier): 'y'
                    │       │           └── rparen (kind: TokenRParen): ')'
                    │       ├── eq (kind: TokenEq): '='
                    │       └── expr (kind: TokenLiteralNumber): '8'
                    ├── if_block (kind: ExprBlock) <ignored>
                    └── else_clause (kind: OptionElseClauseEmpty) []

//! > ==========================================================================

//! > Test if-let boolean operators

//! > test_runner_name
test_partial_parser_tree(expect_diagnostics: false)

//! > cairo_code
fn f() {
    if let Some(x) = 2 > 2 && false {
        3
    }
}

//! > top_level_kind
ExprIf

//! > ignored_kinds
ExprBlock

//! > expected_diagnostics

//! > expected_tree
└── Top level kind: ExprIf
    ├── if_kw (kind: TokenIf): 'if'
    ├── conditions (kind: ConditionListAnd)
    │   ├── item #0 (kind: ConditionLet)
    │   │   ├── let_kw (kind: TokenLet): 'let'
    │   │   ├── patterns (kind: PatternListOr)
    │   │   │   └── item #0 (kind: PatternEnum)
    │   │   │       ├── path (kind: ExprPath)
    │   │   │       │   ├── dollar (kind: OptionTerminalDollarEmpty) []
    │   │   │       │   └── segments (kind: ExprPathInner)
    │   │   │       │       └── item #0 (kind: PathSegmentSimple)
    │   │   │       │           └── ident (kind: TokenIdentifier): 'Some'
    │   │   │       └── pattern (kind: PatternEnumInnerPattern)
    │   │   │           ├── lparen (kind: TokenLParen): '('
    │   │   │           ├── pattern (kind: ExprPath)
    │   │   │           │   ├── dollar (kind: OptionTerminalDollarEmpty) []
    │   │   │           │   └── segments (kind: ExprPathInner)
    │   │   │           │       └── item #0 (kind: PathSegmentSimple)
    │   │   │           │           └── ident (kind: TokenIdentifier): 'x'
    │   │   │           └── rparen (kind: TokenRParen): ')'
    │   │   ├── eq (kind: TokenEq): '='
    │   │   └── expr (kind: ExprBinary)
    │   │       ├── lhs (kind: TokenLiteralNumber): '2'
    │   │       ├── op (kind: TokenGT): '>'
    │   │       └── rhs (kind: TokenLiteralNumber): '2'
    │   ├── separator #0 (kind: TokenAndAnd): '&&'
    │   └── item #1 (kind: ConditionExpr)
    │       └── expr (kind: TokenFalse): 'false'
    ├── if_block (kind: ExprBlock) <ignored>
    └── else_clause (kind: OptionElseClauseEmpty) []

//! > ==========================================================================

//! > Test if-let with multiple conditions

//! > test_runner_name
test_partial_parser_tree(expect_diagnostics: false)

//! > cairo_code
fn f() {
    if let Some(x) = 2 && let Some(y) = (3 || 4) && false && true {
        3
    }
}

//! > top_level_kind
ExprIf

//! > ignored_kinds
ExprBlock

//! > expected_diagnostics

//! > expected_tree
└── Top level kind: ExprIf
    ├── if_kw (kind: TokenIf): 'if'
    ├── conditions (kind: ConditionListAnd)
    │   ├── item #0 (kind: ConditionLet)
    │   │   ├── let_kw (kind: TokenLet): 'let'
    │   │   ├── patterns (kind: PatternListOr)
    │   │   │   └── item #0 (kind: PatternEnum)
    │   │   │       ├── path (kind: ExprPath)
    │   │   │       │   ├── dollar (kind: OptionTerminalDollarEmpty) []
    │   │   │       │   └── segments (kind: ExprPathInner)
    │   │   │       │       └── item #0 (kind: PathSegmentSimple)
    │   │   │       │           └── ident (kind: TokenIdentifier): 'Some'
    │   │   │       └── pattern (kind: PatternEnumInnerPattern)
    │   │   │           ├── lparen (kind: TokenLParen): '('
    │   │   │           ├── pattern (kind: ExprPath)
    │   │   │           │   ├── dollar (kind: OptionTerminalDollarEmpty) []
    │   │   │           │   └── segments (kind: ExprPathInner)
    │   │   │           │       └── item #0 (kind: PathSegmentSimple)
    │   │   │           │           └── ident (kind: TokenIdentifier): 'x'
    │   │   │           └── rparen (kind: TokenRParen): ')'
    │   │   ├── eq (kind: TokenEq): '='
    │   │   └── expr (kind: TokenLiteralNumber): '2'
    │   ├── separator #0 (kind: TokenAndAnd): '&&'
    │   ├── item #1 (kind: ConditionLet)
    │   │   ├── let_kw (kind: TokenLet): 'let'
    │   │   ├── patterns (kind: PatternListOr)
    │   │   │   └── item #0 (kind: PatternEnum)
    │   │   │       ├── path (kind: ExprPath)
    │   │   │       │   ├── dollar (kind: OptionTerminalDollarEmpty) []
    │   │   │       │   └── segments (kind: ExprPathInner)
    │   │   │       │       └── item #0 (kind: PathSegmentSimple)
    │   │   │       │           └── ident (kind: TokenIdentifier): 'Some'
    │   │   │       └── pattern (kind: PatternEnumInnerPattern)
    │   │   │           ├── lparen (kind: TokenLParen): '('
    │   │   │           ├── pattern (kind: ExprPath)
    │   │   │           │   ├── dollar (kind: OptionTerminalDollarEmpty) []
    │   │   │           │   └── segments (kind: ExprPathInner)
    │   │   │           │       └── item #0 (kind: PathSegmentSimple)
    │   │   │           │           └── ident (kind: TokenIdentifier): 'y'
    │   │   │           └── rparen (kind: TokenRParen): ')'
    │   │   ├── eq (kind: TokenEq): '='
    │   │   └── expr (kind: ExprParenthesized)
    │   │       ├── lparen (kind: TokenLParen): '('
    │   │       ├── expr (kind: ExprBinary)
    │   │       │   ├── lhs (kind: TokenLiteralNumber): '3'
    │   │       │   ├── op (kind: TokenOrOr): '||'
    │   │       │   └── rhs (kind: TokenLiteralNumber): '4'
    │   │       └── rparen (kind: TokenRParen): ')'
    │   ├── separator #1 (kind: TokenAndAnd): '&&'
    │   ├── item #2 (kind: ConditionExpr)
    │   │   └── expr (kind: TokenFalse): 'false'
    │   ├── separator #2 (kind: TokenAndAnd): '&&'
    │   └── item #3 (kind: ConditionExpr)
    │       └── expr (kind: TokenTrue): 'true'
    ├── if_block (kind: ExprBlock) <ignored>
    └── else_clause (kind: OptionElseClauseEmpty) []

//! > ==========================================================================

//! > Test let-chain starting with a boolean expression

//! > test_runner_name
test_partial_parser_tree(expect_diagnostics: false)

//! > cairo_code
fn f() {
    if x == 3 && y == 3 && let Some(x) = 2 && true {
        3
    }
}

//! > top_level_kind
ExprIf

//! > ignored_kinds
ExprBlock

//! > expected_diagnostics

//! > expected_tree
└── Top level kind: ExprIf
    ├── if_kw (kind: TokenIf): 'if'
    ├── conditions (kind: ConditionListAnd)
    │   ├── item #0 (kind: ConditionExpr)
    │   │   └── expr (kind: ExprBinary)
    │   │       ├── lhs (kind: ExprBinary)
    │   │       │   ├── lhs (kind: ExprPath)
    │   │       │   │   ├── dollar (kind: OptionTerminalDollarEmpty) []
    │   │       │   │   └── segments (kind: ExprPathInner)
    │   │       │   │       └── item #0 (kind: PathSegmentSimple)
    │   │       │   │           └── ident (kind: TokenIdentifier): 'x'
    │   │       │   ├── op (kind: TokenEqEq): '=='
    │   │       │   └── rhs (kind: TokenLiteralNumber): '3'
    │   │       ├── op (kind: TokenAndAnd): '&&'
    │   │       └── rhs (kind: ExprBinary)
    │   │           ├── lhs (kind: ExprPath)
    │   │           │   ├── dollar (kind: OptionTerminalDollarEmpty) []
    │   │           │   └── segments (kind: ExprPathInner)
    │   │           │       └── item #0 (kind: PathSegmentSimple)
    │   │           │           └── ident (kind: TokenIdentifier): 'y'
    │   │           ├── op (kind: TokenEqEq): '=='
    │   │           └── rhs (kind: TokenLiteralNumber): '3'
    │   ├── separator #0 (kind: TokenAndAnd): '&&'
    │   ├── item #1 (kind: ConditionLet)
    │   │   ├── let_kw (kind: TokenLet): 'let'
    │   │   ├── patterns (kind: PatternListOr)
    │   │   │   └── item #0 (kind: PatternEnum)
    │   │   │       ├── path (kind: ExprPath)
    │   │   │       │   ├── dollar (kind: OptionTerminalDollarEmpty) []
    │   │   │       │   └── segments (kind: ExprPathInner)
    │   │   │       │       └── item #0 (kind: PathSegmentSimple)
    │   │   │       │           └── ident (kind: TokenIdentifier): 'Some'
    │   │   │       └── pattern (kind: PatternEnumInnerPattern)
    │   │   │           ├── lparen (kind: TokenLParen): '('
    │   │   │           ├── pattern (kind: ExprPath)
    │   │   │           │   ├── dollar (kind: OptionTerminalDollarEmpty) []
    │   │   │           │   └── segments (kind: ExprPathInner)
    │   │   │           │       └── item #0 (kind: PathSegmentSimple)
    │   │   │           │           └── ident (kind: TokenIdentifier): 'x'
    │   │   │           └── rparen (kind: TokenRParen): ')'
    │   │   ├── eq (kind: TokenEq): '='
    │   │   └── expr (kind: TokenLiteralNumber): '2'
    │   ├── separator #1 (kind: TokenAndAnd): '&&'
    │   └── item #2 (kind: ConditionExpr)
    │       └── expr (kind: TokenTrue): 'true'
    ├── if_block (kind: ExprBlock) <ignored>
    └── else_clause (kind: OptionElseClauseEmpty) []