(: tools to analyse xml parse tree
@author Andy Bunce
:)
module namespace hnd="lsp/handlers";


(:~ structure returned by tree walkers :)
declare record hnd:State( 
    result as item()*,
    skipchildren? as xs:boolean:=false(),
    extras? (: typically map with ?text:)
);

declare type hnd:actionFn as fn(
  $parse  as element(),
  $state as hnd:State  
) as hnd:State;

declare type hnd:actionMap as  map(xs:string,hnd:actionFn);

declare function hnd:walk($parse as element(), 
                         $actions as hnd:actionMap, 
                         $state as hnd:State )
as hnd:State
{
  let $action:=$actions(name($parse))
  let $state:= if(exists($action))
                then $action($parse,$state)
                else $state
 
  return if($state?skipchildren)
         then $state
         else fold-left(
                $parse/*,$state,
                fn($state,$this){hnd:walk($this, $actions, $state)}
                     )
};



