// v1a Render an ixml trace, recorded with xmq --traceixml=trace.xmq (or xmqj), as a web page:
//   xmq trace.xmq transform render-trace-to-html.xslq to-html > trace.html
// Every Earley item is a row: a cell spans the input it matched, below the tokens,
// and the rule with the dot is to the right. The items used in the tree are marked.
// Then the tree building: the tree is built top-down from the start rule, and every
// row adds a node. A column shows the latest node added over it, so a new node
// replaces its parent over the input it matched. When a choice is made by cost the
// alternatives are all built, the ones not chosen are struck out, and the last row
// shows the chosen tree.
xsl:stylesheet(version   = 1.0
               xmlns:xsl = http://www.w3.org/1999/XSL/Transform)
{
    xsl:output(method         = html
               doctype-system = about:legacy-compat)
    // The nodes of the chosen tree, to find the items used in the tree.
    xsl:key(name  = used
            match = "/trace/tree/node[not(@chosen = 'no')]"
            use   = "concat(@id, ',', @from, ',', @to)")
    // The number of tokens, the input and the end of input ω.
    xsl:variable(name   = n
                 select = 'count(/trace/input/token)')
    xsl:template(match = /)
    {
        html {
            head {
                meta(http-equiv = content-type
                     content    = text/html;charset=utf-8)
                title {
                    'ixml trace ('
                    xsl:value-of(select = /trace/@engine)
                    ')'
                }
                style = 'body { font-family: monospace; }
                         table { border-collapse: collapse; }
                         th, td { border: 1px solid #888; min-width: 2em; text-align: center; white-space: nowrap; }
                         th { background: cyan; position: sticky; top: 0; }
                         th.eof { background: lightgrey; }
                         td.rule { text-align: left; background: #e0ffff; padding: 0 1em; }
                         td.read { background: #ddd; }
                         td.predicted { background: lightyellow; }
                         td.partial { background: orange; }
                         td.complete { background: #c8e6c8; }
                         td.blocked { background: red; color: white; }
                         td.complete.used { background: #2e9e2e; color: white; font-weight: bold; }
                         span.matched { color: #2e7d32; }
                         span.expected { color: #c66900; }
                         span.id { font-size: 0.7em; color: #555; }
                         pre.grammar { border: 1px solid #888; padding: 1em; display: inline-block; }
                         td.node { background: #c8e6c8; }
                         td.node.parent { background: orange; }
                         td.node.new { background: #2e9e2e; color: white; font-weight: bold; }
                         td.node.rejected { background: #eee; color: #888; text-decoration: line-through; }
                         td.node.new.rejected { background: #888; color: white; }
                         td.node.parent.rejected { background: #f5d6a0; }
                         tr.chosen td { border-top: 3px solid #333; }
                         span.rejected { color: #c00; }'
            }
            body {
                h3 {
                    'ixml trace, '
                    xsl:value-of(select = 'count(/trace/steps/item)')
                    ' Earley items by '
                    xsl:value-of(select = /trace/@engine)
                    ', '
                    xsl:value-of(select = 'count(/trace/tree/node)')
                    ' used in the tree'
                }
                pre(class = grammar)
                {
                    xsl:for-each(select = /trace/grammar/rule)
                    {
                        xsl:value-of(select = @name)
                        ' → '
                        xsl:value-of(select = @rhs)
                        br
                    }
                }
                table {
                    tr {
                        xsl:for-each(select = /trace/input/token)
                        {
                            th {
                                xsl:if(test = 'position() = last()')
                                {
                                    xsl:attribute(name = class) = eof
                                }
                                xsl:value-of(select = .)
                            }
                        }
                        th = rule
                    }
                    xsl:apply-templates(select = /trace/steps/*)
                }
                h3 {
                    'Tree building, '
                    xsl:value-of(select = 'count(/trace/tree/node)')
                    ' nodes added top-down, the new node is dark green and its parent orange'
                    xsl:if(test = "/trace/tree/node[@chosen = 'no']")
                    {
                        ', '
                        xsl:value-of(select = "count(/trace/tree/node[@chosen = 'no'])")
                        ' not chosen by cost are struck out'
                    }
                }
                table {
                    tr {
                        xsl:for-each(select = /trace/input/token)
                        {
                            th {
                                xsl:if(test = 'position() = last()')
                                {
                                    xsl:attribute(name = class) = eof
                                }
                                xsl:value-of(select = .)
                            }
                        }
                        th = node
                    }
                    xsl:apply-templates(select = /trace/tree/node)
                    xsl:if(test = /trace/tree/node)
                    {
                        tr(class = chosen)
                        {
                            xsl:call-template(name = frontier)
                            {
                                xsl:with-param(name   = k
                                               select = 'count(/trace/tree/node)')
                                xsl:with-param(name   = c
                                               select = 0)
                                xsl:with-param(name   = chosen
                                               select = 'true()')
                            }
                            td(class = rule) = 'the chosen tree'
                        }
                    }
                }
            }
        }
    }
    // The parser reads the token at pos: mark its column.
    xsl:template(match = read)
    {
        tr {
            xsl:call-template(name = empty)
            {
                xsl:with-param(name   = count
                               select = @pos)
            }
            td(class = read) = ↓
            xsl:call-template(name = empty)
            {
                xsl:with-param(name   = count
                               select = '$n - @pos - 1')
            }
            td(class = rule)
            {
                'read '
                xsl:value-of(select = '/trace/input/token[@pos = current()/@pos]')
            }
        }
    }
    // An Earley item: the cell spans from..to (at least one column).
    xsl:template(match = item)
    {
        xsl:variable(name = span)
        {
            xsl:choose {
                xsl:when(test = '@to > @from')
                {
                    xsl:value-of(select = '@to - @from')
                }
                xsl:otherwise = 1
            }
        }
        // Do not go beyond the last column (the C start rule ends after ω).
        xsl:variable(name = width)
        {
            xsl:choose {
                xsl:when(test = '@from + $span > $n')
                {
                    xsl:value-of(select = '$n - @from')
                }
                xsl:otherwise {
                    xsl:value-of(select = $span)
                }
            }
        }
        xsl:variable(name   = used
                     select = "count(key('used', concat(@id, ',', @from, ',', @to))) > 0")
        tr {
            xsl:call-template(name = empty)
            {
                xsl:with-param(name   = count
                               select = @from)
            }
            td(colspan = '{$width}')
            {
                xsl:attribute(name = class)
                {
                    xsl:value-of(select = @state)
                    xsl:if(test = '$used and @state = "complete"') = ' used'
                }
                xsl:value-of(select = @rule)
            }
            xsl:call-template(name = empty)
            {
                xsl:with-param(name   = count
                               select = '$n - @from - $width')
            }
            td(class = rule)
            {
                xsl:value-of(select = @rule)
                ' → '
                span(class = matched)
                {
                    xsl:value-of(select = @matched)
                }
                xsl:if(test = '@state != "complete"') = ' · '
                span(class = expected)
                {
                    xsl:value-of(select = @expected)
                }
                span(class = id)
                {
                    ' (d'
                    xsl:value-of(select = @id)
                    ','
                    xsl:value-of(select = @from)
                    '-'
                    xsl:value-of(select = @to)
                    ')'
                }
            }
        }
    }
    // A node added to the tree: the frontier after adding it, and the node.
    xsl:template(match = node)
    {
        tr {
            xsl:call-template(name = frontier)
            {
                xsl:with-param(name   = k
                               select = 'count(preceding-sibling::node) + 1')
                xsl:with-param(name   = c
                               select = 0)
            }
            td(class = rule)
            {
                xsl:value-of(select = @step)
                '. '
                xsl:value-of(select = @rule)
                ' → '
                span(class = matched)
                {
                    xsl:value-of(select = @rhs)
                }
                span(class = id)
                {
                    ' (d'
                    xsl:value-of(select = @id)
                    ','
                    xsl:value-of(select = @from)
                    '-'
                    xsl:value-of(select = @to)
                    ') in '
                    xsl:value-of(select = @parent)
                }
                xsl:if(test = @cost)
                {
                    ' cost '
                    xsl:value-of(select = @cost)
                }
                xsl:if(test = "@chosen = 'no'")
                {
                    span(class = rejected) = ' not chosen'
                }
            }
        }
    }
    // The cells from column c after the first k nodes were added: a column shows the
    // latest node over it, the columns with the same node are one cell. With chosen,
    // the deepest node of the final tree over it (a node can be used by another parent
    // than the one it was built for, so it is not always the latest).
    xsl:template(name = frontier)
    {
        xsl:param(name = k)
        xsl:param(name = c)
        xsl:param(name   = chosen
                  select = 'false()')
        xsl:if(test = '$c < $n')
        {
            xsl:variable(name   = x
                         select = "/trace/tree/node[position() <= $k][@from <= $c and @to > $c][not($chosen) or (@chosen = 'yes' and not(@step = /trace/tree/node[@chosen = 'yes'][@from <= $c and @to > $c]/@tree-parent))][last()]")
            xsl:variable(name = end)
            {
                xsl:call-template(name = run-end)
                {
                    xsl:with-param(name   = k
                                   select = $k)
                    xsl:with-param(name   = c
                                   select = '$c + 1')
                    xsl:with-param(name   = id
                                   select = 'generate-id($x)')
                    xsl:with-param(name   = chosen
                                   select = $chosen)
                }
            }
            td(colspan = '{$end - $c}')
            {
                xsl:if(test = $x)
                {
                    xsl:attribute(name = class)
                    {
                        'node'
                        xsl:if(test = 'generate-id($x) = generate-id(current())') = ' new'
                        xsl:if(test = '$x/@step = current()/@parent') = ' parent'
                        xsl:if(test = "$x/@chosen = 'no'") = ' rejected'
                    }
                    xsl:value-of(select = $x/@rule)
                }
            }
            xsl:call-template(name = frontier)
            {
                xsl:with-param(name   = k
                               select = $k)
                xsl:with-param(name   = c
                               select = $end)
                xsl:with-param(name   = chosen
                               select = $chosen)
            }
        }
    }
    // The first column from c that does not show the node with the id.
    xsl:template(name = run-end)
    {
        xsl:param(name = k)
        xsl:param(name = c)
        xsl:param(name = id)
        xsl:param(name = chosen)
        xsl:choose {
            xsl:when(test = "$c < $n and generate-id(/trace/tree/node[position() <= $k][@from <= $c and @to > $c][not($chosen) or (@chosen = 'yes' and not(@step = /trace/tree/node[@chosen = 'yes'][@from <= $c and @to > $c]/@tree-parent))][last()]) = $id")
            {
                xsl:call-template(name = run-end)
                {
                    xsl:with-param(name   = k
                                   select = $k)
                    xsl:with-param(name   = c
                                   select = '$c + 1')
                    xsl:with-param(name   = id
                                   select = $id)
                    xsl:with-param(name   = chosen
                                   select = $chosen)
                }
            }
            xsl:otherwise {
                xsl:value-of(select = $c)
            }
        }
    }
    // Write count empty cells.
    xsl:template(name = empty)
    {
        xsl:param(name = count)
        xsl:if(test = '$count > 0')
        {
            td
            xsl:call-template(name = empty)
            {
                xsl:with-param(name   = count
                               select = '$count - 1')
            }
        }
    }
}