Skip to content

Example Python

Benedict Albrecht edited this page Jun 2, 2026 · 6 revisions

Example: Python

This page demonstrates a Crodox grammar definition for Python (.py) files.

Work in progress: This grammar is not complete. It is a starting point meant to be extended and adapted to your project.

Tip: You can generate this definition automatically using the Template Generator:

python crodox_template_generator.py python

Full Definition

<~"FROM".py~>

    <*comment_line*> # -->> \n <*>
    <*comment_block*> <| """ -->> """ <||> ''' -->> ''' |> <*>

    <*()*> ( <---> ) <*>
    <*[]*> [ <---> ] <*>
    <*{}*> { <---> } <*>

    <:import:>
        import <<name:up>> <| as <<name:up>> <||> |> \n
    <:>

    <:from_import:>
        from <<"FROM">> import <| <<name:up>> <||> ( <? <<name:up>> <| , <||> |> ?> ) |> \n
    <:>

    <:decorator:>
        @ <<'name'>> <| ( <? <| <<'name'>> <||> ' <<'name'>> ' <||> " <<'name'>> " <||> <<name>> |> <| , <||> |> ?> ) <||> |> \n
    <:>

    <:variable:>
        <<name>> <| : <<'name'>> <||> |> = -->> \n
    <:>

    <:parameter:>
        <<name>> <| : <<'name'>> <||> |> <| = -->> <| , <||> ) |> <||> <| , <||> ) |> |>
    <:>

    <:function:>
        <| async <||> |> def <<name:up>> ( <? <-{parameter}-> ?> ) -->> :
    <:>

    <:class:>
        class <<name:up>> <| ( <? <<'name'>> <| , <||> |> ?> ) <||> |> :
    <:>

    <:return:>
        return -->> \n
    <:>

    <:raise:>
        raise <<'name'>> ( -->> ) \n
    <:>

    <:if:>
        if -->> :
    <:>

    <:elif:>
        elif -->> :
    <:>

    <:else:>
        else :
    <:>

    <:for:>
        for <<name>> in -->> :
    <:>

    <:while:>
        while -->> :
    <:>

    <:try:>
        try :
    <:>

    <:except:>
        except <| <<'name'>> <| as <<name>> <||> |> <||> |> :
    <:>

    <:finally:>
        finally :
    <:>

    <:with:>
        with -->> as <<name>> :
    <:>

    <:assert:>
        assert -->> \n
    <:>

    <:pass:>
        pass \n
    <:>

<~>

Breakdown

Structural Objects (Hidden)

Object Description
comment_line Single-line comments (# ... \n) using Jump
comment_block Docstrings / block comments (""" ... """ or ''' ... ''')
(), [], {} Structural delimiters for grouping, lists, and dicts

Import Objects

Object Description
import import module with optional as alias - uses :up scope so the name is visible to all siblings
from_import from module import name - uses path variable <<"FROM">> for cross-file dependency

Decorators

Object Description
decorator @name(args) - uses reference variable <<'name'>> to link to the decorator definition without overwriting

Functions & Classes

Object Description
parameter Function parameter with optional type annotation and default value - used as a Reference inside function
function def name(params): with optional async and return type annotation. Uses :up scope and typed sub-body <-{parameter}-> for params
class class Name(bases): with optional inheritance list. Uses <-function-> typed sub-body so methods depend on the class but not vice versa

Variables & Assignments

Object Description
variable name = value with optional type annotation (: Type)

Control Flow

Object Description
if / elif / else Conditional blocks
for For loop - captures the loop variable <<name>>
while While loop
try / except / finally Exception handling - except captures the exception type and optional alias
with Context manager - captures the alias <<name>>
return Return statement
raise Raise statement - references the exception class
assert Assert statement
pass Pass placeholder

Key Patterns Used

  • No braces: Python uses : + indentation instead of { }, so sub-bodies use : <---> to capture indented blocks
  • :up scope: Used for imports, functions, and classes so they are visible to all siblings (matching Python's module-level scoping)
  • References: <-{parameter}-> keeps parameters as variable contributors to the function, not as child nodes
  • Or Statements: Used for async/sync, optional type annotations, and string quote styles
  • Jump: Used for comments and expression skipping (-->> \n)

Clone this wiki locally