;;;;;;;;;;;;
;; Parser ;;
;;;;;;;;;;;;

;; Module
smudgle = *state-machine-def

;; Machine
state-machine-def = state-machine-name state-machine
state-machine-name = identifier
state-machine = state-machine-begin state-machine-body state-machine-end
state-machine-body = state-or-machine-def *(comma state-or-machine-def) [comma]
state-or-machine-def = state-def / state-machine-def

;; State
state-def = [start-sigil] state-name (arrow / become / state)
state-name = identifier
state = [enter-exit] state-begin state-body state-end [enter-exit]
enter-exit = enter-exit-begin [side-effect-list] enter-exit-end
state-body = event-or-machine-def *(comma event-or-machine-def) [comma]
event-or-machine-def = event-handler / state-machine-def

;; Handler
event-handler = event-name (arrow / dash / become)
event-name = identifier

;; Transitions
dash   =   dash-empty / dash-begin [side-effect-list] dash-end
arrow  = (arrow-empty / dash-begin [side-effect-list] arrow-end) state-name
become = (become-empty / become-begin [side-effect-list] become-end)
         [(state-machine / state-machine-name) (dash / arrow)]

;; Side effects
side-effect-list = side-effect *(comma side-effect) [comma]
side-effect = function-call / qualified-event
function-call = foreign-identifier
qualified-event = [state-machine-name dot] event-name
