diff options
author | Joel E. Denny <jdenny@ces.clemson.edu> | 2007-10-09 08:48:43 +0000 |
---|---|---|
committer | Joel E. Denny <jdenny@ces.clemson.edu> | 2007-10-09 08:48:43 +0000 |
commit | 21f1b063d812990caeb738fa8db21085ece53a30 (patch) | |
tree | ac3e068eb0d9829c5decab0fb77d0e15708187c2 /data/xslt | |
parent | 793fbca50acec434a8e60412394ef392fd03f315 (diff) | |
download | bison-21f1b063d812990caeb738fa8db21085ece53a30.tar.gz |
Make xml2dot.xsl and --graph produce the same output.
* data/xslt/xml2dot.xsl (xsl:template match="rule"): Use a ` '
instead of a `\n'. That is, don't add escapes yet or they'll be doubly
escaped later.
(xsl:template name="output-node"): Use the new escape template instead
of the string-replace template directly.
(xsl:template name="output-edge"): Likewise.
(xsl:template name="escape"): New, escapes backslashes and newlines in
addition to quotation marks.
* src/graphviz.c (start_graph, output_node, output_edge): Add
whitespace to output for legibility.
Make xml2text.xsl and --report produce the same output, and remove the
XML "conflicts" element since a conflict summary is easily extracted
from the automaton.
* data/xslt/bison.xsl: New.
(xsl:template match="state" mode="bison:count-conflicts): New.
* data/xslt/xml2text.xsl: Import bison.xsl.
(xsl:template match="bison-xml-report"): Instead of styling the
"conflicts" element, style the "automaton" element with mode
"conflicts". Unlike the former, the latter lists S/R and R/R
conflicts for a state on the same line.
(xsl:template match="conflicts"): Remove.
(xsl:template match="conflict"): Remove.
(xsl:template match="terminal"): Line-wrap the list of rules in which
the terminal is used.
(xsl:template match="nonterminal"): Likewise for nonterminals.
(xsl:template match="automaton" mode="conflicts"): New.
(xsl:template match="state" mode="conflicts"): New.
(xsl:template name="line-wrap"): New.
(xsl:template name="ws-search"): New.
* data/xslt/xml2xhtml.xsl: Import bison.xsl.
(xsl:template match="bison-xml-report"): Instead of styling the
"conflicts" element, style the "automaton" element with mode
"conflicts."
(xsl:template match="conflicts"): Remove.
(xsl:template match="conflict"): Remove.
(xsl:template match="automaton" mode="conflicts"): New.
(xsl:template match="state" mode="conflicts): New.
* src/conflicts.c (conflicts_output_xml): Remove.
* src/conflicts.h (conflicts_output_xml): Remove prototype.
* src/print-xml.c (print_xml): Don't invoke conflicts_output_xml.
* src/print.c (print_grammar): Consistently wrap at the 66th column so
the corresponding XSLT is easier. Also, never wrap between a word and
the comma that follows it.
Diffstat (limited to 'data/xslt')
-rw-r--r-- | data/xslt/bison.xsl | 64 | ||||
-rw-r--r-- | data/xslt/xml2dot.xsl | 73 | ||||
-rw-r--r-- | data/xslt/xml2text.xsl | 151 | ||||
-rw-r--r-- | data/xslt/xml2xhtml.xsl | 57 |
4 files changed, 265 insertions, 80 deletions
diff --git a/data/xslt/bison.xsl b/data/xslt/bison.xsl new file mode 100644 index 00000000..6d96e014 --- /dev/null +++ b/data/xslt/bison.xsl @@ -0,0 +1,64 @@ +<?xml version="1.0" encoding="UTF-8"?> + +<!-- + bison.xsl - common templates for Bison XSLT. + Copyright (C) 2007 Free Software Foundation, Inc. + + This file is part of Bison, the GNU Compiler Compiler. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. + --> + +<xsl:stylesheet version="1.0" + xmlns:xsl="http://www.w3.org/1999/XSL/Transform" + xmlns:bison="http://www.gnu.org/software/bison/"> + +<!-- For the specified state, output: #sr-conflicts,#rr-conflicts --> +<xsl:template match="state" mode="bison:count-conflicts"> + <xsl:variable name="transitions" select="actions/transitions"/> + <xsl:variable name="reductions" select="actions/reductions"/> + <xsl:variable + name="terminals" + select=" + $transitions/transition[@type='shift']/@symbol + | $reductions/reduction/@symbol + " + /> + <xsl:variable name="conflict-data"> + <xsl:for-each select="$terminals"> + <xsl:variable name="name" select="."/> + <xsl:if test="generate-id($terminals[. = $name][1]) = generate-id(.)"> + <xsl:variable + name="shift-count" + select="count($transitions/transition[@symbol=$name])" + /> + <xsl:variable + name="reduce-count" + select="count($reductions/reduction[@symbol=$name])" + /> + <xsl:if test="$shift-count > 0 and $reduce-count > 0"> + <xsl:text>s</xsl:text> + </xsl:if> + <xsl:if test="$reduce-count > 1"> + <xsl:text>r</xsl:text> + </xsl:if> + </xsl:if> + </xsl:for-each> + </xsl:variable> + <xsl:value-of select="string-length(translate($conflict-data, 'r', ''))"/> + <xsl:text>,</xsl:text> + <xsl:value-of select="string-length(translate($conflict-data, 's', ''))"/> +</xsl:template> + +</xsl:stylesheet> diff --git a/data/xslt/xml2dot.xsl b/data/xslt/xml2dot.xsl index 0f6a7f41..416ee13e 100644 --- a/data/xslt/xml2dot.xsl +++ b/data/xslt/xml2dot.xsl @@ -59,7 +59,7 @@ </xsl:template> <xsl:template match="rule"> - <xsl:text>\n</xsl:text> + <xsl:text> </xsl:text> <xsl:value-of select="lhs"/> <xsl:text> -></xsl:text> <xsl:apply-templates select="rhs/symbol|rhs/point|rhs/empty"/> @@ -123,10 +123,8 @@ <xsl:text> </xsl:text> <xsl:value-of select="$number"/> <xsl:text> [label="</xsl:text> - <xsl:call-template name="string-replace"> + <xsl:call-template name="escape"> <xsl:with-param name="subject" select="$label"/> - <xsl:with-param name="search" select="'"'"/> - <xsl:with-param name="replace" select="'\"'"/> </xsl:call-template> <xsl:text>"] </xsl:text> </xsl:template> @@ -144,36 +142,55 @@ <xsl:value-of select="$style"/> <xsl:if test="$label and $label != ''"> <xsl:text> label="</xsl:text> - <xsl:call-template name="string-replace"> + <xsl:call-template name="escape"> <xsl:with-param name="subject" select="$label"/> - <xsl:with-param name="search" select="'"'"/> - <xsl:with-param name="replace" select="'\"'"/> </xsl:call-template> <xsl:text>"</xsl:text> </xsl:if> <xsl:text>] </xsl:text> </xsl:template> - <xsl:template name="string-replace"> - <xsl:param name="subject"/> - <xsl:param name="search"/> - <xsl:param name="replace"/> - <xsl:choose> - <xsl:when test="contains($subject, $search)"> - <xsl:variable name="before" select="substring-before($subject, $search)"/> - <xsl:variable name="after" select="substring-after($subject, $search)"/> - <xsl:value-of select="$before"/> - <xsl:value-of select="$replace"/> - <xsl:call-template name="string-replace"> - <xsl:with-param name="subject" select="$after"/> - <xsl:with-param name="search" select="$search"/> - <xsl:with-param name="replace" select="$replace"/> - </xsl:call-template> - </xsl:when> - <xsl:otherwise> - <xsl:value-of select="$subject"/> - </xsl:otherwise> - </xsl:choose> - </xsl:template> +<xsl:template name="escape"> + <xsl:param name="subject" required="yes"/> + <xsl:call-template name="string-replace"> + <xsl:with-param name="subject"> + <xsl:call-template name="string-replace"> + <xsl:with-param name="subject"> + <xsl:call-template name="string-replace"> + <xsl:with-param name="subject" select="$subject"/> + <xsl:with-param name="search" select="'\'"/> + <xsl:with-param name="replace" select="'\\'"/> + </xsl:call-template> + </xsl:with-param> + <xsl:with-param name="search" select="'"'"/> + <xsl:with-param name="replace" select="'\"'"/> + </xsl:call-template> + </xsl:with-param> + <xsl:with-param name="search" select="' '"/> + <xsl:with-param name="replace" select="'\n'"/> + </xsl:call-template> +</xsl:template> + +<xsl:template name="string-replace"> + <xsl:param name="subject"/> + <xsl:param name="search"/> + <xsl:param name="replace"/> + <xsl:choose> + <xsl:when test="contains($subject, $search)"> + <xsl:variable name="before" select="substring-before($subject, $search)"/> + <xsl:variable name="after" select="substring-after($subject, $search)"/> + <xsl:value-of select="$before"/> + <xsl:value-of select="$replace"/> + <xsl:call-template name="string-replace"> + <xsl:with-param name="subject" select="$after"/> + <xsl:with-param name="search" select="$search"/> + <xsl:with-param name="replace" select="$replace"/> + </xsl:call-template> + </xsl:when> + <xsl:otherwise> + <xsl:value-of select="$subject"/> + </xsl:otherwise> + </xsl:choose> +</xsl:template> </xsl:stylesheet> diff --git a/data/xslt/xml2text.xsl b/data/xslt/xml2text.xsl index 7ec9edc8..74688525 100644 --- a/data/xslt/xml2text.xsl +++ b/data/xslt/xml2text.xsl @@ -25,8 +25,10 @@ --> <xsl:stylesheet version="1.0" - xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> - + xmlns:xsl="http://www.w3.org/1999/XSL/Transform" + xmlns:bison="http://www.gnu.org/software/bison/"> + +<xsl:import href="bison.xsl"/> <xsl:output method="text" encoding="UTF-8" indent="no"/> <xsl:template match="/"> @@ -36,7 +38,7 @@ <xsl:template match="bison-xml-report"> <xsl:apply-templates select="reductions"/> <xsl:apply-templates select="rules-never-reduced"/> - <xsl:apply-templates select="conflicts"/> + <xsl:apply-templates select="automaton" mode="conflicts"/> <xsl:apply-templates select="grammar"/> <xsl:apply-templates select="automaton"/> </xsl:template> @@ -91,23 +93,6 @@ </xsl:if> </xsl:template> -<xsl:template match="conflicts"> - <xsl:if test="conflict"> - <xsl:apply-templates select="conflict"/> - <xsl:text> </xsl:text> - </xsl:if> -</xsl:template> - -<xsl:template match="conflict"> - <xsl:text>State </xsl:text> - <xsl:value-of select="@state"/> - <xsl:text> conflicts: </xsl:text> - <xsl:value-of select="@num"/> - <xsl:text> </xsl:text> - <xsl:value-of select="@type"/> - <xsl:text> </xsl:text> -</xsl:template> - <xsl:template match="grammar"> <xsl:text>Grammar </xsl:text> <xsl:apply-templates select="rules/rule"> @@ -131,9 +116,16 @@ <xsl:template match="terminal"> <xsl:value-of select="@symbol"/> - <xsl:value-of select="concat(' (', @type, ')')"/> - <xsl:apply-templates select="rule"/> - <xsl:text> </xsl:text> + <xsl:call-template name="line-wrap"> + <xsl:with-param + name="first-line-length" select="66 - string-length(@symbol)" + /> + <xsl:with-param name="line-length" select="66" /> + <xsl:with-param name="text"> + <xsl:value-of select="concat(' (', @type, ')')"/> + <xsl:apply-templates select="rule" /> + </xsl:with-param> + </xsl:call-template> </xsl:template> <xsl:template match="terminal/rule"> @@ -144,19 +136,27 @@ <xsl:template match="nonterminal"> <xsl:value-of select="@symbol"/> <xsl:value-of select="concat(' (', @type, ')')"/> - <xsl:text> </xsl:text> - <xsl:if test="left/rule"> - <xsl:text>on left:</xsl:text> - </xsl:if> - <xsl:apply-templates select="left/rule"/> - <xsl:if test="left/rule and right/rule"> - <xsl:text>, </xsl:text> - </xsl:if> - <xsl:if test="right/rule"> - <xsl:text>on right:</xsl:text> - </xsl:if> - <xsl:apply-templates select="right/rule"/> <xsl:text> </xsl:text> + <xsl:variable name="output"> + <xsl:call-template name="line-wrap"> + <xsl:with-param name="line-length" select="66" /> + <xsl:with-param name="text"> + <xsl:text> </xsl:text> + <xsl:if test="left/rule"> + <xsl:text>on@left:</xsl:text> + </xsl:if> + <xsl:apply-templates select="left/rule"/> + <xsl:if test="left/rule and right/rule"> + <xsl:text>, </xsl:text> + </xsl:if> + <xsl:if test="right/rule"> + <xsl:text>on@right:</xsl:text> + </xsl:if> + <xsl:apply-templates select="right/rule"/> + </xsl:with-param> + </xsl:call-template> + </xsl:variable> + <xsl:value-of select="translate($output, '@', ' ')" /> </xsl:template> <xsl:template match="nonterminal/left/rule|nonterminal/right/rule"> @@ -164,6 +164,41 @@ <xsl:value-of select="."/> </xsl:template> +<xsl:template match="automaton" mode="conflicts"> + <xsl:variable name="conflict-report"> + <xsl:apply-templates select="state" mode="conflicts"/> + </xsl:variable> + <xsl:if test="string-length($conflict-report) != 0"> + <xsl:value-of select="$conflict-report"/> + <xsl:text> </xsl:text> + </xsl:if> +</xsl:template> + +<xsl:template match="state" mode="conflicts"> + <xsl:variable name="conflict-counts"> + <xsl:apply-templates select="." mode="bison:count-conflicts" /> + </xsl:variable> + <xsl:variable + name="sr-count" select="substring-before($conflict-counts, ',')" + /> + <xsl:variable + name="rr-count" select="substring-after($conflict-counts, ',')" + /> + <xsl:if test="$sr-count > 0 or $rr-count > 0"> + <xsl:value-of select="concat('State ', @number, ' conflicts:')"/> + <xsl:if test="$sr-count > 0"> + <xsl:value-of select="concat(' ', $sr-count, ' shift/reduce')"/> + <xsl:if test="$rr-count > 0"> + <xsl:value-of select="(',')"/> + </xsl:if> + </xsl:if> + <xsl:if test="$rr-count > 0"> + <xsl:value-of select="concat(' ', $rr-count, ' reduce/reduce')"/> + </xsl:if> + <xsl:value-of select="' '"/> + </xsl:if> +</xsl:template> + <xsl:template match="automaton"> <xsl:apply-templates select="state"> <xsl:with-param name="pad" select="'3'"/> @@ -429,4 +464,50 @@ </xsl:if> </xsl:template> +<xsl:template name="line-wrap"> + <xsl:param name="line-length" required="yes" /> + <xsl:param name="first-line-length" select="$line-length" /> + <xsl:param name="text" required="yes" /> + <xsl:choose> + <xsl:when test="string-length($text) = 0 or normalize-space($text) = ''" /> + <xsl:when test="string-length($text) <= $first-line-length"> + <xsl:value-of select="concat($text, ' ')" /> + </xsl:when> + <xsl:otherwise> + <xsl:variable name="break-pos"> + <xsl:call-template name="ws-search"> + <xsl:with-param name="text" select="$text" /> + <xsl:with-param name="pos" select="$first-line-length+1" /> + </xsl:call-template> + </xsl:variable> + <xsl:value-of select="substring($text, 1, $break-pos - 1)" /> + <xsl:text> </xsl:text> + <xsl:call-template name="line-wrap"> + <xsl:with-param name="line-length" select="$line-length" /> + <xsl:with-param + name="text" select="concat(' ', substring($text, $break-pos+1))" + /> + </xsl:call-template> + </xsl:otherwise> + </xsl:choose> +</xsl:template> + +<xsl:template name="ws-search"> + <xsl:param name="text" required="yes" /> + <xsl:param name="pos" required="yes" /> + <xsl:choose> + <xsl:when + test="$pos > string-length($text) or substring($text, $pos, 1) = ' '" + > + <xsl:value-of select="$pos" /> + </xsl:when> + <xsl:otherwise> + <xsl:call-template name="ws-search"> + <xsl:with-param name="text" select="$text" /> + <xsl:with-param name="pos" select="$pos+1" /> + </xsl:call-template> + </xsl:otherwise> + </xsl:choose> +</xsl:template> + </xsl:stylesheet> diff --git a/data/xslt/xml2xhtml.xsl b/data/xslt/xml2xhtml.xsl index bf36a504..5648d14c 100644 --- a/data/xslt/xml2xhtml.xsl +++ b/data/xslt/xml2xhtml.xsl @@ -26,7 +26,10 @@ <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" - xmlns="http://www.w3.org/1999/xhtml"> + xmlns="http://www.w3.org/1999/xhtml" + xmlns:bison="http://www.gnu.org/software/bison/"> + +<xsl:import href="bison.xsl"/> <xsl:output method="xml" encoding="UTF-8" doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN" @@ -123,7 +126,7 @@ </ul> <xsl:apply-templates select="reductions"/> <xsl:apply-templates select="rules-never-reduced"/> - <xsl:apply-templates select="conflicts"/> + <xsl:apply-templates select="automaton" mode="conflicts"/> <xsl:apply-templates select="grammar"/> <xsl:apply-templates select="automaton"/> </xsl:template> @@ -206,32 +209,52 @@ </xsl:if> </xsl:template> -<xsl:template match="conflicts"> +<xsl:template match="automaton" mode="conflicts"> <h2> <a name="conflicts"/> <xsl:text> Conflicts</xsl:text> </h2> <xsl:text> </xsl:text> - <xsl:if test="conflict"> + <xsl:variable name="conflict-report"> + <xsl:apply-templates select="state" mode="conflicts"/> + </xsl:variable> + <xsl:if test="string-length($conflict-report) != 0"> <p class="pre"> - <xsl:apply-templates select="conflict"/> + <xsl:copy-of select="$conflict-report"/> <xsl:text> </xsl:text> </p> </xsl:if> </xsl:template> -<xsl:template match="conflict"> - <a> - <xsl:attribute name="href"> - <xsl:value-of select="concat('#state_', @state)"/> - </xsl:attribute> - <xsl:value-of select="concat('State ', @state)"/> - </a> - <xsl:text> conflicts: </xsl:text> - <xsl:value-of select="@num"/> - <xsl:text> </xsl:text> - <xsl:value-of select="@type"/> - <xsl:text> </xsl:text> +<xsl:template match="state" mode="conflicts"> + <xsl:variable name="conflict-counts"> + <xsl:apply-templates select="." mode="bison:count-conflicts" /> + </xsl:variable> + <xsl:variable + name="sr-count" select="substring-before($conflict-counts, ',')" + /> + <xsl:variable + name="rr-count" select="substring-after($conflict-counts, ',')" + /> + <xsl:if test="$sr-count > 0 or $rr-count > 0"> + <a> + <xsl:attribute name="href"> + <xsl:value-of select="concat('#state_', @number)"/> + </xsl:attribute> + <xsl:value-of select="concat('State ', @number)"/> + </a> + <xsl:text> conflicts:</xsl:text> + <xsl:if test="$sr-count > 0"> + <xsl:value-of select="concat(' ', $sr-count, ' shift/reduce')"/> + <xsl:if test="$rr-count > 0"> + <xsl:value-of select="(',')"/> + </xsl:if> + </xsl:if> + <xsl:if test="$rr-count > 0"> + <xsl:value-of select="concat(' ', $rr-count, ' reduce/reduce')"/> + </xsl:if> + <xsl:value-of select="' '"/> + </xsl:if> </xsl:template> <xsl:template match="grammar"> |