// v0g Cleanup after the ixml parse of Java with java.ixml, version v0g as the grammar.
// Fredrik Öhrström (c) 2026 spdx: MIT
// 
// The grammar is written for the generated LL(*) parser, so its expressions have an
// element for every precedence level they pass and the method calls, field and array
// accesses are chains of selectors on a primary. This stylesheet:
// 
//   - removes the levels without an operator, eg assignment { conditional_expression {
//     logical_or { postfix { name = x } } } } is name = x, and names a level with an
//     operator after the operator, eg additive for + and -,
//   - conditional_expression with ? : is conditional and a level with instanceof is
//     instanceof,
//   - a primary (or postfix) with a selector is method_call, field_access,
//     array_access, method_reference, new (outer.new), this (Outer.this), super
//     (Outer.super) or class_literal (a.b.class),
//   - a chain of names is a dotted name, a.b.c is name = a.b.c, and a method call on
//     it has the dotted name: a.b.c(x) is method_call(name = a.b.c) { arguments }.
//   - a leading comment (on its own line) is moved to the declaration or statement
//     that follows it, or to the end of the block before a closing brace, and the
//     comments of a token with text, eg a name, are after its element.
// 
//   xmq ixml:lang/java Foo.java           (uses this cleanup automatically)
//   xmq library/lang/java.ixml Foo.java transform library/lang/java.xslq
xsl:stylesheet(version   = 1.0
               xmlns:xsl = http://www.w3.org/1999/XSL/Transform)
{
    // A leading comment, on its own line, belongs to the declaration or statement that
    // follows it, the first element after the comment. It is the first child of that
    // element, eg the javadoc of a method. A leading comment before a closing brace (the
    // raw element rbrace) is the last child of the block or body. A leading comment at
    // the end of the file is the last child of the compilation_unit. A leading comment
    // before anything else, eg in an expression, stays where it is. (The element after
    // the comment is found with the ancestors and their siblings, the following axis is
    // slow.)
    xsl:key(name  = target
            match = leading_comment
            use   = 'generate-id(ancestor-or-self::*[following-sibling::*[not(self::comment or self::leading_comment)]][1]/following-sibling::*[not(self::comment or self::leading_comment)][1])')
    xsl:template(match = '@*|text()')
    {
        xsl:copy
    }
    // An element is copied. A leaf element with text, eg a name, a literal or an import,
    // gets the comments of its tokens after it, and the leading comments that belong
    // to it before it, so that its text is not mixed with comments.
    xsl:template(match = *)
    {
        xsl:variable(name   = leaf
                     select = 'boolean(text()[normalize-space()])')
        xsl:if(test = '$leaf and (self::package or self::import or self::static_import or self::class or self::interface or self::enum or self::record or self::constant or self::field or self::method or self::constructor or self::compact_constructor or self::initializer or self::local_variable or self::empty or self::label or self::expression_statement or self::if or self::else or self::assert or self::switch or self::switch_rule or self::switch_group or self::while or self::do or self::for or self::foreach or self::break or self::continue or self::return or self::throw or self::yield or self::synchronized or self::try or self::catch or self::finally or self::constructor_call or self::block)')
        {
            xsl:apply-templates(select = "key('target', generate-id())"
                                mode   = moved)
        }
        xsl:copy {
            xsl:apply-templates(select = @*)
            xsl:if(test = 'not($leaf) and (self::package or self::import or self::static_import or self::class or self::interface or self::enum or self::record or self::constant or self::field or self::method or self::constructor or self::compact_constructor or self::initializer or self::local_variable or self::empty or self::label or self::expression_statement or self::if or self::else or self::assert or self::switch or self::switch_rule or self::switch_group or self::while or self::do or self::for or self::foreach or self::break or self::continue or self::return or self::throw or self::yield or self::synchronized or self::try or self::catch or self::finally or self::constructor_call or self::block)')
            {
                xsl:apply-templates(select = "key('target', generate-id())"
                                    mode   = moved)
            }
            xsl:choose {
                xsl:when(test = $leaf)
                {
                    xsl:for-each(select = 'text()')
                    {
                        xsl:value-of(select = .)
                    }
                }
                xsl:otherwise {
                    xsl:apply-templates(select = 'node()')
                }
            }
            xsl:if(test = self::compilation_unit)
            {
                xsl:apply-templates(select = '//leading_comment[not(ancestor-or-self::*[following-sibling::*[not(self::comment or self::leading_comment)]])]'
                                    mode   = moved)
            }
        }
        xsl:if(test = $leaf)
        {
            xsl:apply-templates(select = comment|leading_comment)
        }
    }
    xsl:template(match = rbrace)
    {
        xsl:apply-templates(select = "key('target', generate-id())"
                            mode   = moved)
    }
    xsl:template(match = leading_comment)
    {
        xsl:variable(name   = f
                     select = 'ancestor-or-self::*[following-sibling::*[not(self::comment or self::leading_comment)]][1]/following-sibling::*[not(self::comment or self::leading_comment)][1]')
        xsl:if(test = '$f and not($f[self::rbrace or self::package or self::import or self::static_import or self::class or self::interface or self::enum or self::record or self::constant or self::field or self::method or self::constructor or self::compact_constructor or self::initializer or self::local_variable or self::empty or self::label or self::expression_statement or self::if or self::else or self::assert or self::switch or self::switch_rule or self::switch_group or self::while or self::do or self::for or self::foreach or self::break or self::continue or self::return or self::throw or self::yield or self::synchronized or self::try or self::catch or self::finally or self::constructor_call or self::block])')
        {
            comment {
                xsl:value-of(select = .)
            }
        }
    }
    xsl:template(match = leading_comment
                 mode  = moved)
    {
        comment {
            xsl:value-of(select = .)
        }
    }
    // The raw tree has an element for the level of every operand, and an operator with
    // its operands is in the element of the level above (or the element where the
    // expression is used). So the levels are recognized by their children, not their names:
    // a level with one child is the child, one with [left, operator, right] is the element
    // of the operator, eg additive for + and -.
    xsl:template(match = '*[(self::assignment or self::conditional_expression or self::logical_or or self::logical_and or self::bitwise_or or self::bitwise_xor or self::bitwise_and or self::equality or self::relational or self::shift or self::additive or self::multiplicative or self::postfix or self::primary) and count(*[not(self::comment or self::leading_comment)]) = 1]')
    {
        xsl:apply-templates(select = *)
    }
    xsl:template(match = '*[(self::assignment or self::conditional_expression or self::logical_or or self::logical_and or self::bitwise_or or self::bitwise_xor or self::bitwise_and or self::equality or self::relational or self::shift or self::additive or self::multiplicative or self::postfix or self::primary) and count(*[not(self::comment or self::leading_comment)]) = 3 and *[not(self::comment or self::leading_comment)][2][self::OR]]')
    {
        logical_or {
            xsl:apply-templates(select = *)
        }
    }
    xsl:template(match = '*[(self::assignment or self::conditional_expression or self::logical_or or self::logical_and or self::bitwise_or or self::bitwise_xor or self::bitwise_and or self::equality or self::relational or self::shift or self::additive or self::multiplicative or self::postfix or self::primary) and count(*[not(self::comment or self::leading_comment)]) = 3 and *[not(self::comment or self::leading_comment)][2][self::AND]]')
    {
        logical_and {
            xsl:apply-templates(select = *)
        }
    }
    xsl:template(match = '*[(self::assignment or self::conditional_expression or self::logical_or or self::logical_and or self::bitwise_or or self::bitwise_xor or self::bitwise_and or self::equality or self::relational or self::shift or self::additive or self::multiplicative or self::postfix or self::primary) and count(*[not(self::comment or self::leading_comment)]) = 3 and *[not(self::comment or self::leading_comment)][2][self::BITOR]]')
    {
        bitwise_or {
            xsl:apply-templates(select = *)
        }
    }
    xsl:template(match = '*[(self::assignment or self::conditional_expression or self::logical_or or self::logical_and or self::bitwise_or or self::bitwise_xor or self::bitwise_and or self::equality or self::relational or self::shift or self::additive or self::multiplicative or self::postfix or self::primary) and count(*[not(self::comment or self::leading_comment)]) = 3 and *[not(self::comment or self::leading_comment)][2][self::CARET]]')
    {
        bitwise_xor {
            xsl:apply-templates(select = *)
        }
    }
    xsl:template(match = '*[(self::assignment or self::conditional_expression or self::logical_or or self::logical_and or self::bitwise_or or self::bitwise_xor or self::bitwise_and or self::equality or self::relational or self::shift or self::additive or self::multiplicative or self::postfix or self::primary) and count(*[not(self::comment or self::leading_comment)]) = 3 and *[not(self::comment or self::leading_comment)][2][self::BITAND]]')
    {
        bitwise_and {
            xsl:apply-templates(select = *)
        }
    }
    xsl:template(match = '*[(self::assignment or self::conditional_expression or self::logical_or or self::logical_and or self::bitwise_or or self::bitwise_xor or self::bitwise_and or self::equality or self::relational or self::shift or self::additive or self::multiplicative or self::postfix or self::primary) and count(*[not(self::comment or self::leading_comment)]) = 3 and *[not(self::comment or self::leading_comment)][2][self::EQ or self::NE]]')
    {
        equality {
            xsl:apply-templates(select = *)
        }
    }
    xsl:template(match = '*[(self::assignment or self::conditional_expression or self::logical_or or self::logical_and or self::bitwise_or or self::bitwise_xor or self::bitwise_and or self::equality or self::relational or self::shift or self::additive or self::multiplicative or self::postfix or self::primary) and count(*[not(self::comment or self::leading_comment)]) = 3 and *[not(self::comment or self::leading_comment)][2][self::LT or self::GT or self::LE or self::GE]]')
    {
        relational {
            xsl:apply-templates(select = *)
        }
    }
    xsl:template(match = '*[(self::assignment or self::conditional_expression or self::logical_or or self::logical_and or self::bitwise_or or self::bitwise_xor or self::bitwise_and or self::equality or self::relational or self::shift or self::additive or self::multiplicative or self::postfix or self::primary) and count(*[not(self::comment or self::leading_comment)]) = 3 and *[not(self::comment or self::leading_comment)][2][self::SHL or self::SHR or self::USHR]]')
    {
        shift {
            xsl:apply-templates(select = *)
        }
    }
    xsl:template(match = '*[(self::assignment or self::conditional_expression or self::logical_or or self::logical_and or self::bitwise_or or self::bitwise_xor or self::bitwise_and or self::equality or self::relational or self::shift or self::additive or self::multiplicative or self::postfix or self::primary) and count(*[not(self::comment or self::leading_comment)]) = 3 and *[not(self::comment or self::leading_comment)][2][self::PLUS or self::MINUS]]')
    {
        additive {
            xsl:apply-templates(select = *)
        }
    }
    xsl:template(match = '*[(self::assignment or self::conditional_expression or self::logical_or or self::logical_and or self::bitwise_or or self::bitwise_xor or self::bitwise_and or self::equality or self::relational or self::shift or self::additive or self::multiplicative or self::postfix or self::primary) and count(*[not(self::comment or self::leading_comment)]) = 3 and *[not(self::comment or self::leading_comment)][2][self::STAR or self::SLASH or self::PERCENT]]')
    {
        multiplicative {
            xsl:apply-templates(select = *)
        }
    }
    xsl:template(match = '*[(self::assignment or self::conditional_expression or self::logical_or or self::logical_and or self::bitwise_or or self::bitwise_xor or self::bitwise_and or self::equality or self::relational or self::shift or self::additive or self::multiplicative or self::postfix or self::primary) and count(*[not(self::comment or self::leading_comment)]) = 3 and *[not(self::comment or self::leading_comment)][2][self::instanceof]]')
    {
        instanceof {
            xsl:apply-templates(select = '*[not(self::instanceof)]|instanceof/*')
        }
    }
    // a ? b : c, the condition, the true branch and the false branch.
    xsl:template(match = 'conditional_expression[count(*[not(self::comment or self::leading_comment)]) = 3]')
    {
        conditional {
            xsl:apply-templates(select = *)
        }
    }
    // An unqualified method call, foo(x).
    xsl:template(match = call)
    {
        method_call(name = '{@name}')
        {
            xsl:apply-templates(select = *)
        }
    }
    // A target with a selector. The outermost selector of a chain is in the postfix.
    xsl:template(match = '*[(self::primary or self::postfix) and count(*[not(self::comment or self::leading_comment)]) = 2 and *[not(self::comment or self::leading_comment)][2][self::call]]')
    {
        xsl:variable(name = pure)
        {
            xsl:call-template(name = pure)
            {
                xsl:with-param(name   = n
                               select = '*[not(self::comment or self::leading_comment)][1]')
            }
        }
        method_call {
            xsl:attribute(name = name)
            {
                xsl:if(test = '$pure = 1')
                {
                    xsl:call-template(name = dotted)
                    {
                        xsl:with-param(name   = n
                                       select = '*[not(self::comment or self::leading_comment)][1]')
                    }
                    xsl:text = .
                }
                xsl:value-of(select = '*[not(self::comment or self::leading_comment)][2]/@name')
            }
            xsl:if(test = 'not($pure = 1)')
            {
                xsl:apply-templates(select = '*[not(self::comment or self::leading_comment)][1]')
            }
            xsl:if(test = '$pure = 1')
            {
                xsl:apply-templates(select = '*[not(self::comment or self::leading_comment)][1]//comment|*[not(self::comment or self::leading_comment)][1]//leading_comment')
            }
            xsl:apply-templates(select = comment|leading_comment)
            xsl:apply-templates(select = '*[not(self::comment or self::leading_comment)][2]/*')
        }
    }
    xsl:template(match = '*[(self::primary or self::postfix) and count(*[not(self::comment or self::leading_comment)]) = 2 and *[not(self::comment or self::leading_comment)][2][self::field_select]]')
    {
        xsl:variable(name = pure)
        {
            xsl:call-template(name = pure)
            {
                xsl:with-param(name   = n
                               select = '*[not(self::comment or self::leading_comment)][1]')
            }
        }
        xsl:choose {
            xsl:when(test = '$pure = 1')
            {
                name {
                    xsl:call-template(name = dotted)
                    {
                        xsl:with-param(name   = n
                                       select = .)
                    }
                }
                xsl:apply-templates(select = .//comment|.//leading_comment)
            }
            xsl:otherwise {
                field_access(name = '{*[not(self::comment or self::leading_comment)][2]/@name}')
                {
                    xsl:apply-templates(select = '*[not(self::comment or self::leading_comment)][1]')
                    xsl:apply-templates(select = 'comment|leading_comment|*[not(self::comment or self::leading_comment)][2]/comment|*[not(self::comment or self::leading_comment)][2]/leading_comment')
                }
            }
        }
    }
    xsl:template(match = '*[(self::primary or self::postfix) and count(*[not(self::comment or self::leading_comment)]) = 2 and *[not(self::comment or self::leading_comment)][2][self::index]]')
    {
        array_access {
            xsl:apply-templates(select = '*[not(self::comment or self::leading_comment)][1]')
            xsl:apply-templates(select = comment|leading_comment)
            xsl:apply-templates(select = '*[not(self::comment or self::leading_comment)][2]/*')
        }
    }
    xsl:template(match = '*[(self::primary or self::postfix) and count(*[not(self::comment or self::leading_comment)]) = 2 and *[not(self::comment or self::leading_comment)][2][self::reference]]')
    {
        method_reference(name = '{*[not(self::comment or self::leading_comment)][2]/@name}')
        {
            xsl:choose {
                xsl:when(test = '*[not(self::comment or self::leading_comment)][2]/dim')
                {
                    type {
                        xsl:apply-templates(select = '*[not(self::comment or self::leading_comment)][1]')
                        xsl:apply-templates(select = '*[not(self::comment or self::leading_comment)][2]/dim')
                    }
                }
                xsl:otherwise {
                    xsl:apply-templates(select = '*[not(self::comment or self::leading_comment)][1]')
                }
            }
            xsl:apply-templates(select = '*[not(self::comment or self::leading_comment)][2]/type_arguments')
            xsl:apply-templates(select = 'comment|leading_comment|*[not(self::comment or self::leading_comment)][2]/comment|*[not(self::comment or self::leading_comment)][2]/leading_comment')
        }
    }
    xsl:template(match = '*[(self::primary or self::postfix) and count(*[not(self::comment or self::leading_comment)]) = 2 and *[not(self::comment or self::leading_comment)][2][self::class_selector]]')
    {
        class_literal {
            type {
                xsl:apply-templates(select = '*[not(self::comment or self::leading_comment)][1]')
                xsl:apply-templates(select = '*[not(self::comment or self::leading_comment)][2]/dim')
            }
            xsl:apply-templates(select = 'comment|leading_comment|*[not(self::comment or self::leading_comment)][2]/comment|*[not(self::comment or self::leading_comment)][2]/leading_comment')
        }
    }
    xsl:template(match = '*[(self::primary or self::postfix) and count(*[not(self::comment or self::leading_comment)]) = 2 and *[not(self::comment or self::leading_comment)][2][self::outer_new]]')
    {
        new {
            xsl:apply-templates(select = '*[not(self::comment or self::leading_comment)][1]')
            xsl:apply-templates(select = 'comment|leading_comment|*[not(self::comment or self::leading_comment)][2]/comment|*[not(self::comment or self::leading_comment)][2]/leading_comment')
            xsl:apply-templates(select = '*[not(self::comment or self::leading_comment)][2]/new/*')
        }
    }
    xsl:template(match = '*[(self::primary or self::postfix) and count(*[not(self::comment or self::leading_comment)]) = 2 and *[not(self::comment or self::leading_comment)][2][self::outer_this]]')
    {
        this {
            xsl:apply-templates(select = '*[not(self::comment or self::leading_comment)][1]')
            xsl:apply-templates(select = 'comment|leading_comment|*[not(self::comment or self::leading_comment)][2]/comment|*[not(self::comment or self::leading_comment)][2]/leading_comment')
        }
    }
    xsl:template(match = '*[(self::primary or self::postfix) and count(*[not(self::comment or self::leading_comment)]) = 2 and *[not(self::comment or self::leading_comment)][2][self::outer_super]]')
    {
        super {
            xsl:apply-templates(select = '*[not(self::comment or self::leading_comment)][1]')
            xsl:apply-templates(select = 'comment|leading_comment|*[not(self::comment or self::leading_comment)][2]/comment|*[not(self::comment or self::leading_comment)][2]/leading_comment')
        }
    }
    // 1 if the raw target n is a chain of names, eg a or a.b.c.
    xsl:template(name = pure)
    {
        xsl:param(name = n)
        xsl:choose {
            xsl:when(test = $n/self::name)
            {
                xsl:text = 1
            }
            xsl:when(test = '($n/self::primary or $n/self::postfix) and count($n/*[not(self::comment or self::leading_comment)]) = 1')
            {
                xsl:call-template(name = pure)
                {
                    xsl:with-param(name   = n
                                   select = '$n/*[not(self::comment or self::leading_comment)][1]')
                }
            }
            xsl:when(test = '($n/self::primary or $n/self::postfix) and count($n/*[not(self::comment or self::leading_comment)]) = 2 and $n/*[not(self::comment or self::leading_comment)][2][self::field_select]')
            {
                xsl:call-template(name = pure)
                {
                    xsl:with-param(name   = n
                                   select = '$n/*[not(self::comment or self::leading_comment)][1]')
                }
            }
            xsl:otherwise {
                xsl:text = 0
            }
        }
    }
    // The dotted name of a chain of names.
    xsl:template(name = dotted)
    {
        xsl:param(name = n)
        xsl:choose {
            xsl:when(test = $n/self::name)
            {
                xsl:for-each(select = '$n/text()')
                {
                    xsl:value-of(select = .)
                }
            }
            xsl:when(test = 'count($n/*[not(self::comment or self::leading_comment)]) = 1')
            {
                xsl:call-template(name = dotted)
                {
                    xsl:with-param(name   = n
                                   select = '$n/*[not(self::comment or self::leading_comment)][1]')
                }
            }
            xsl:otherwise {
                xsl:call-template(name = dotted)
                {
                    xsl:with-param(name   = n
                                   select = '$n/*[not(self::comment or self::leading_comment)][1]')
                }
                xsl:text = .
                xsl:value-of(select = '$n/*[not(self::comment or self::leading_comment)][2]/@name')
            }
        }
    }
}