Debug print
nextNode nextTree fix
Segfault fix

Explanation:
For code(using labels e1, e2) like this:

sample_clause
    :    sample_key block_key?
        LEFT_PAREN e1=expression (COMMA e2=expression)? RIGHT_PAREN
        seed_part?
        -> ^(sample_key block_key? ^(EXPR $e1) ^(EXPR $e2)? seed_part?)
    ;

ANTLR generates code like this:
	e1=expression();
	...
	if ( this->get_backtracking()==0 ) stream_expression.add(e1.tree);
	...
	...
        // AST REWRITE
        // elements: e1, e2, block_key, sample_key, seed_part
        // token labels:
        // rule labels: retval, e1, e2
        // token list labels:
        // rule list labels:
        // wildcard labels:
        if ( this->get_backtracking()==0 ) {
            retval.tree = std::move(root_0);
            RewriteRuleSubtreeStream<ImplTraits> stream_retval(get_psrstate()->get_treeAdaptor(), "rule retval",retval.tree); // retval
            RewriteRuleSubtreeStream<ImplTraits> stream_e1(get_psrstate()->get_treeAdaptor(), "rule e1",e1.tree); // rewrite alias
            RewriteRuleSubtreeStream<ImplTraits> stream_e2(get_psrstate()->get_treeAdaptor(), "rule e2",e2.tree); // rewrite alias
	...

The variable e1 of type expression_return is assigned into two different SubTreeStreams.
Since destructive copying is used(unique_ptr) the second assignment inserts NULL ptr into stream_e1.

A new variable of type TreeType* (pure pointer) is added. A value of this pointer is used ONLY when
inserting a tree into alias'es RewriteRuleSubtreeStream. When there are no aliases declared in the rule the "last pointer" remains unused.
When inserting TreeTypePtr into a stream value is moved. When inserting a pure pointer into the aliases stream the whole tree is deeply dupped
(assuming that the original tree was already moved into some other RewriteRuleSubtreeStream).

New result:
        if ( this->get_backtracking()==0 ) {
            retval.tree = std::move(root_0);
            RewriteRuleSubtreeStream<ImplTraits> stream_retval(get_psrstate()->get_treeAdaptor(), "rule retval",retval.tree); // retval
            RewriteRuleSubtreeStream<ImplTraits> stream_e1(get_psrstate()->get_treeAdaptor(), "rule e1",e1_last); // rewrite alias
            RewriteRuleSubtreeStream<ImplTraits> stream_e2(get_psrstate()->get_treeAdaptor(), "rule e2",e2_last); // rewrite alias

Note: we have also deal with this situation.

unpivot_in_elements
    :   (    column_name
        |    LEFT_PAREN column_name (COMMA column_name)* RIGHT_PAREN
        )
        (     as_key
         (    constant
         |    (LEFT_PAREN)=> LEFT_PAREN constant (COMMA constant)* RIGHT_PAREN
         )
        )?
        -> column_name+ ^(PIVOT_ALIAS constant+)?
    ;

The AST tree contains multiple instances of "column_name".
RewriteRuleSubtreeStream can not contain reference(pointer) into matched tree as it would get overwritten
in every "loop". It really needs to be moved.
memory leak fix
SQL AST

SQL AST

SQL AST

AST creation

AST generation

s004 start rule

AST generation

AST generation

AST generation - SEGFAULT fix

AST generation - standard_function fixes

AST generation fixes (model)
test a005 - rewrites having predicates
a006