On Mon, Sep 27, 2021 at 06:13:49PM +0200, Christian Grün scripsit:
> No problem, Graydon, thanks for the clarification!

You're welcome!

Thank you for your prompt and useful feedback!

> You might need to rewrite your code as it’s not tail recursive (see
> [1] for some examples; you could also try your luck with fold-left).
> If you want to go the easy path, you can also increase the Java stack
> size when starting BaseX (by adding -Xss... to the startup script).
> Obviously, that’ll only postpone the problem ;·).

For posterity:

Increasing the stack size to 1 GB worked.

Fold-left when I'm trying to manage two stacks as well as the
accumulating thing seems like it would be more brave than sensible.

Thinking about the accumulating thing did cause the light bulb to
flicker about having to not rely on the individual function call's
accumulating return values if I wanted tail recursion to happen.  The
below works:

declare namespace fn = "http://www.w3.org/2005/xpath-functions";;

declare variable $NMToken as xs:string := '[\p{L}\p{Nd}._\-:]+';

declare function local:tag($nameList as xs:string*,$stack as xs:string*,$event 
as xs:string*,$built as xs:string*) {

   if (empty($event))
 then string-join($built)
 else if (head($event) eq '{')
    then 
local:tag(tail($nameList),(head($nameList),$stack),tail($event),($built,('<',head($nameList),'>')))
    else 
local:tag($nameList,tail($stack),tail($event),($built,concat('</',head($stack),'>')))
 
};

declare function local:emitPattern($in as xs:string) {

  let $chunked as element(fn:analyze-string-result) := 
analyze-string($in,$NMToken)
  let $names as array(xs:string) := array { $chunked/fn:match/string() }
  let $openClose as xs:string+ := ($chunked/fn:non-match => string-join() => 
string-to-codepoints()) ! codepoints-to-string(.)

  return local:tag($names,(),$openClose,())
};


-- 
Graydon Saunders  | graydon...@gmail.com
Þæs oferéode, ðisses swá mæg.
-- Deor  ("That passed, so may this.")

Reply via email to