]> git.saurik.com Git - bison.git/blame - data/xslt/xml2text.xsl
* doc/yacc.1.in: New.
[bison.git] / data / xslt / xml2text.xsl
CommitLineData
9fb1f8d8
WP
1<?xml version="1.0" encoding="UTF-8"?>
2
3<!--
4 xml2text.xsl - transform Bison XML Report into plain text.
5 $Id$
6
7 Copyright (C) 2007 Free Software Foundation, Inc.
8
9 This file is part of Bison, the GNU Compiler Compiler.
10
11 This program is free software: you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation, either version 3 of the License, or
14 (at your option) any later version.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>.
23
24 Written by Wojciech Polak <polak@gnu.org>.
25 -->
26
27<xsl:stylesheet version="1.0"
21f1b063
JD
28 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
29 xmlns:bison="http://www.gnu.org/software/bison/">
30
31<xsl:import href="bison.xsl"/>
9fb1f8d8
WP
32<xsl:output method="text" encoding="UTF-8" indent="no"/>
33
34<xsl:template match="/">
35 <xsl:apply-templates select="bison-xml-report"/>
36</xsl:template>
37
38<xsl:template match="bison-xml-report">
39 <xsl:apply-templates select="reductions"/>
cff03fb2 40 <xsl:apply-templates select="rules-useless-in-parser"/>
21f1b063 41 <xsl:apply-templates select="automaton" mode="conflicts"/>
9fb1f8d8
WP
42 <xsl:apply-templates select="grammar"/>
43 <xsl:apply-templates select="automaton"/>
44</xsl:template>
45
cff03fb2 46<xsl:template match="rules-useless-in-parser">
9fb1f8d8 47 <xsl:if test="rule">
cff03fb2 48 <xsl:text>Rules useless in parser due to conflicts&#10;</xsl:text>
9fb1f8d8
WP
49 <xsl:apply-templates select="rule">
50 <xsl:with-param name="pad" select="'3'"/>
51 </xsl:apply-templates>
52 <xsl:text>&#10;&#10;</xsl:text>
53 </xsl:if>
54</xsl:template>
55
56<xsl:template match="reductions">
cff03fb2 57 <xsl:apply-templates select="useless-in-grammar/nonterminals"/>
9fb1f8d8 58 <xsl:apply-templates select="unused/terminals"/>
cff03fb2 59 <xsl:apply-templates select="useless-in-grammar/rules"/>
9fb1f8d8
WP
60</xsl:template>
61
cff03fb2 62<xsl:template match="useless-in-grammar/nonterminals">
9fb1f8d8 63 <xsl:if test="nonterminal">
cff03fb2 64 <xsl:text>Nonterminals useless in grammar&#10;&#10;</xsl:text>
9fb1f8d8
WP
65 <xsl:for-each select="nonterminal">
66 <xsl:text> </xsl:text>
67 <xsl:value-of select="."/>
68 <xsl:text>&#10;</xsl:text>
69 </xsl:for-each>
70 <xsl:text>&#10;&#10;</xsl:text>
71 </xsl:if>
72</xsl:template>
73
cff03fb2 74<xsl:template match="useless-in-grammar/rules">
9fb1f8d8 75 <xsl:if test="rule">
cff03fb2 76 <xsl:text>Rules useless in grammar&#10;</xsl:text>
9fb1f8d8
WP
77 <xsl:apply-templates select="rule">
78 <xsl:with-param name="pad" select="'3'"/>
79 </xsl:apply-templates>
80 <xsl:text>&#10;&#10;</xsl:text>
81 </xsl:if>
82</xsl:template>
83
84<xsl:template match="unused/terminals">
85 <xsl:if test="terminal">
86 <xsl:text>Terminals which are not used&#10;&#10;</xsl:text>
87 <xsl:for-each select="terminal">
88 <xsl:text> </xsl:text>
89 <xsl:value-of select="."/>
90 <xsl:text>&#10;</xsl:text>
91 </xsl:for-each>
92 <xsl:text>&#10;&#10;</xsl:text>
93 </xsl:if>
94</xsl:template>
95
9fb1f8d8
WP
96<xsl:template match="grammar">
97 <xsl:text>Grammar&#10;</xsl:text>
98 <xsl:apply-templates select="rules/rule">
99 <xsl:with-param name="pad" select="'3'"/>
100 </xsl:apply-templates>
101 <xsl:text>&#10;&#10;</xsl:text>
102 <xsl:apply-templates select="terminals"/>
103 <xsl:apply-templates select="nonterminals"/>
104</xsl:template>
105
106<xsl:template match="grammar/terminals">
107 <xsl:text>Terminals, with rules where they appear&#10;&#10;</xsl:text>
108 <xsl:apply-templates select="terminal"/>
109 <xsl:text>&#10;&#10;</xsl:text>
110</xsl:template>
111
112<xsl:template match="grammar/nonterminals">
113 <xsl:text>Nonterminals, with rules where they appear&#10;&#10;</xsl:text>
114 <xsl:apply-templates select="nonterminal"/>
115</xsl:template>
116
117<xsl:template match="terminal">
36c48187 118 <xsl:value-of select="@name"/>
21f1b063 119 <xsl:call-template name="line-wrap">
255a6b90
JD
120 <xsl:with-param name="first-line-length">
121 <xsl:choose>
122 <xsl:when test="string-length(@name) &gt; 66">0</xsl:when>
123 <xsl:otherwise>
124 <xsl:value-of select="66 - string-length(@name)" />
125 </xsl:otherwise>
126 </xsl:choose>
127 </xsl:with-param>
21f1b063
JD
128 <xsl:with-param name="line-length" select="66" />
129 <xsl:with-param name="text">
32f19b6b 130 <xsl:value-of select="concat(' (', @token-number, ')')"/>
21f1b063
JD
131 <xsl:apply-templates select="rule" />
132 </xsl:with-param>
133 </xsl:call-template>
9fb1f8d8
WP
134</xsl:template>
135
136<xsl:template match="terminal/rule">
137 <xsl:text> </xsl:text>
138 <xsl:value-of select="."/>
139</xsl:template>
140
141<xsl:template match="nonterminal">
36c48187 142 <xsl:value-of select="@name"/>
32f19b6b 143 <xsl:value-of select="concat(' (', @symbol-number, ')')"/>
9fb1f8d8 144 <xsl:text>&#10;</xsl:text>
21f1b063
JD
145 <xsl:variable name="output">
146 <xsl:call-template name="line-wrap">
147 <xsl:with-param name="line-length" select="66" />
148 <xsl:with-param name="text">
149 <xsl:text> </xsl:text>
150 <xsl:if test="left/rule">
151 <xsl:text>on@left:</xsl:text>
152 </xsl:if>
153 <xsl:apply-templates select="left/rule"/>
154 <xsl:if test="left/rule and right/rule">
155 <xsl:text>, </xsl:text>
156 </xsl:if>
157 <xsl:if test="right/rule">
158 <xsl:text>on@right:</xsl:text>
159 </xsl:if>
160 <xsl:apply-templates select="right/rule"/>
161 </xsl:with-param>
162 </xsl:call-template>
163 </xsl:variable>
164 <xsl:value-of select="translate($output, '@', ' ')" />
9fb1f8d8
WP
165</xsl:template>
166
167<xsl:template match="nonterminal/left/rule|nonterminal/right/rule">
168 <xsl:text> </xsl:text>
169 <xsl:value-of select="."/>
170</xsl:template>
171
21f1b063
JD
172<xsl:template match="automaton" mode="conflicts">
173 <xsl:variable name="conflict-report">
174 <xsl:apply-templates select="state" mode="conflicts"/>
175 </xsl:variable>
176 <xsl:if test="string-length($conflict-report) != 0">
177 <xsl:value-of select="$conflict-report"/>
178 <xsl:text>&#10;&#10;</xsl:text>
179 </xsl:if>
180</xsl:template>
181
182<xsl:template match="state" mode="conflicts">
183 <xsl:variable name="conflict-counts">
184 <xsl:apply-templates select="." mode="bison:count-conflicts" />
185 </xsl:variable>
186 <xsl:variable
187 name="sr-count" select="substring-before($conflict-counts, ',')"
188 />
189 <xsl:variable
190 name="rr-count" select="substring-after($conflict-counts, ',')"
191 />
192 <xsl:if test="$sr-count > 0 or $rr-count > 0">
193 <xsl:value-of select="concat('State ', @number, ' conflicts:')"/>
194 <xsl:if test="$sr-count > 0">
195 <xsl:value-of select="concat(' ', $sr-count, ' shift/reduce')"/>
196 <xsl:if test="$rr-count > 0">
197 <xsl:value-of select="(',')"/>
198 </xsl:if>
199 </xsl:if>
200 <xsl:if test="$rr-count > 0">
201 <xsl:value-of select="concat(' ', $rr-count, ' reduce/reduce')"/>
202 </xsl:if>
203 <xsl:value-of select="'&#10;'"/>
204 </xsl:if>
205</xsl:template>
206
9fb1f8d8
WP
207<xsl:template match="automaton">
208 <xsl:apply-templates select="state">
209 <xsl:with-param name="pad" select="'3'"/>
210 </xsl:apply-templates>
211</xsl:template>
212
213<xsl:template match="automaton/state">
214 <xsl:param name="pad"/>
215 <xsl:text>&#10;&#10;</xsl:text>
216 <xsl:text>state </xsl:text>
217 <xsl:value-of select="@number"/>
218 <xsl:text>&#10;&#10;</xsl:text>
95134bc1 219 <xsl:apply-templates select="itemset/item">
9fb1f8d8
WP
220 <xsl:with-param name="pad" select="$pad"/>
221 </xsl:apply-templates>
222 <xsl:apply-templates select="actions/transitions">
223 <xsl:with-param name="type" select="'shift'"/>
224 </xsl:apply-templates>
225 <xsl:apply-templates select="actions/errors"/>
226 <xsl:apply-templates select="actions/reductions"/>
227 <xsl:apply-templates select="actions/transitions">
228 <xsl:with-param name="type" select="'goto'"/>
229 </xsl:apply-templates>
230 <xsl:apply-templates select="solved-conflicts"/>
231</xsl:template>
232
233<xsl:template match="actions/transitions">
234 <xsl:param name="type"/>
235 <xsl:if test="transition[@type = $type]">
236 <xsl:text>&#10;</xsl:text>
237 <xsl:apply-templates select="transition[@type = $type]">
238 <xsl:with-param name="pad">
239 <xsl:call-template name="max-width-symbol">
240 <xsl:with-param name="node" select="transition[@type = $type]"/>
241 </xsl:call-template>
242 </xsl:with-param>
243 </xsl:apply-templates>
244 </xsl:if>
245</xsl:template>
246
247<xsl:template match="actions/errors">
248 <xsl:if test="error">
249 <xsl:text>&#10;</xsl:text>
250 <xsl:apply-templates select="error">
251 <xsl:with-param name="pad">
252 <xsl:call-template name="max-width-symbol">
253 <xsl:with-param name="node" select="error"/>
254 </xsl:call-template>
255 </xsl:with-param>
256 </xsl:apply-templates>
257 </xsl:if>
258</xsl:template>
259
260<xsl:template match="actions/reductions">
261 <xsl:if test="reduction">
262 <xsl:text>&#10;</xsl:text>
263 <xsl:apply-templates select="reduction">
264 <xsl:with-param name="pad">
265 <xsl:call-template name="max-width-symbol">
266 <xsl:with-param name="node" select="reduction"/>
267 </xsl:call-template>
268 </xsl:with-param>
269 </xsl:apply-templates>
270 </xsl:if>
271</xsl:template>
272
95134bc1
WP
273<xsl:template match="item">
274 <xsl:param name="pad"/>
275 <xsl:param name="prev-rule-number"
276 select="preceding-sibling::item[1]/@rule-number"/>
277 <xsl:apply-templates select="key('bison:ruleNumber', current()/@rule-number)">
278 <xsl:with-param name="itemset" select="'true'"/>
279 <xsl:with-param name="pad" select="$pad"/>
280 <xsl:with-param name="prev-lhs"
281 select="key('bison:ruleNumber', $prev-rule-number)/lhs[text()]"/>
282 <xsl:with-param name="point" select="@point"/>
283 <xsl:with-param name="lookaheads">
284 <xsl:apply-templates select="lookaheads"/>
285 </xsl:with-param>
286 </xsl:apply-templates>
287</xsl:template>
288
9fb1f8d8 289<xsl:template match="rule">
95134bc1 290 <xsl:param name="itemset"/>
9fb1f8d8 291 <xsl:param name="pad"/>
95134bc1
WP
292 <xsl:param name="prev-lhs"/>
293 <xsl:param name="point"/>
294 <xsl:param name="lookaheads"/>
295
296 <xsl:if test="$itemset != 'true'
297 and not(preceding-sibling::rule[1]/lhs[text()] = lhs[text()])">
9fb1f8d8
WP
298 <xsl:text>&#10;</xsl:text>
299 </xsl:if>
95134bc1 300
9fb1f8d8
WP
301 <xsl:text> </xsl:text>
302 <xsl:call-template name="lpad">
303 <xsl:with-param name="str" select="string(@number)"/>
304 <xsl:with-param name="pad" select="number($pad)"/>
305 </xsl:call-template>
306 <xsl:text> </xsl:text>
95134bc1
WP
307
308 <!-- LHS -->
9fb1f8d8 309 <xsl:choose>
95134bc1
WP
310 <xsl:when test="$itemset != 'true'
311 and preceding-sibling::rule[1]/lhs[text()] = lhs[text()]">
312 <xsl:call-template name="lpad">
313 <xsl:with-param name="str" select="'|'"/>
314 <xsl:with-param name="pad" select="number(string-length(lhs[text()])) + 1"/>
315 </xsl:call-template>
316 </xsl:when>
317 <xsl:when test="$itemset = 'true' and $prev-lhs = lhs[text()]">
9fb1f8d8
WP
318 <xsl:call-template name="lpad">
319 <xsl:with-param name="str" select="'|'"/>
320 <xsl:with-param name="pad" select="number(string-length(lhs[text()])) + 1"/>
321 </xsl:call-template>
322 </xsl:when>
323 <xsl:otherwise>
324 <xsl:value-of select="lhs"/>
325 <xsl:text>:</xsl:text>
326 </xsl:otherwise>
327 </xsl:choose>
95134bc1
WP
328
329 <!-- RHS -->
330 <xsl:for-each select="rhs/*">
331 <xsl:if test="position() = $point + 1">
332 <xsl:text> .</xsl:text>
333 </xsl:if>
334 <xsl:if test="$itemset = 'true' and name(.) != 'empty'">
335 <xsl:apply-templates select="."/>
336 </xsl:if>
337 <xsl:if test="$itemset != 'true'">
338 <xsl:apply-templates select="."/>
339 </xsl:if>
340 <xsl:if test="position() = last() and position() = $point">
341 <xsl:text> .</xsl:text>
342 </xsl:if>
343 </xsl:for-each>
344 <xsl:if test="$lookaheads">
345 <xsl:value-of select="$lookaheads"/>
346 </xsl:if>
347
9fb1f8d8
WP
348 <xsl:text>&#10;</xsl:text>
349</xsl:template>
350
351<xsl:template match="symbol">
352 <xsl:text> </xsl:text>
353 <xsl:value-of select="."/>
354</xsl:template>
355
9fb1f8d8
WP
356<xsl:template match="empty">
357 <xsl:text> /* empty */</xsl:text>
358</xsl:template>
359
360<xsl:template match="lookaheads">
361 <xsl:text> [</xsl:text>
362 <xsl:apply-templates select="symbol"/>
363 <xsl:text>]</xsl:text>
364</xsl:template>
365
366<xsl:template match="lookaheads/symbol">
367 <xsl:value-of select="."/>
368 <xsl:if test="position() != last()">
369 <xsl:text>, </xsl:text>
370 </xsl:if>
371</xsl:template>
372
373<xsl:template match="transition">
374 <xsl:param name="pad"/>
375 <xsl:text> </xsl:text>
376 <xsl:call-template name="rpad">
377 <xsl:with-param name="str" select="string(@symbol)"/>
378 <xsl:with-param name="pad" select="number($pad) + 2"/>
379 </xsl:call-template>
380 <xsl:choose>
381 <xsl:when test="@type = 'shift'">
382 <xsl:text>shift, and go to state </xsl:text>
383 <xsl:value-of select="@state"/>
384 </xsl:when>
385 <xsl:when test="@type = 'goto'">
386 <xsl:text>go to state </xsl:text>
387 <xsl:value-of select="@state"/>
388 </xsl:when>
389 </xsl:choose>
390 <xsl:text>&#10;</xsl:text>
391</xsl:template>
392
393<xsl:template match="error">
394 <xsl:param name="pad"/>
395 <xsl:text> </xsl:text>
396 <xsl:call-template name="rpad">
397 <xsl:with-param name="str" select="string(@symbol)"/>
398 <xsl:with-param name="pad" select="number($pad) + 2"/>
399 </xsl:call-template>
400 <xsl:text>error</xsl:text>
401 <xsl:text> (</xsl:text>
402 <xsl:value-of select="text()"/>
403 <xsl:text>)</xsl:text>
404 <xsl:text>&#10;</xsl:text>
405</xsl:template>
406
407<xsl:template match="reduction">
408 <xsl:param name="pad"/>
409 <xsl:text> </xsl:text>
410 <xsl:call-template name="rpad">
411 <xsl:with-param name="str" select="string(@symbol)"/>
412 <xsl:with-param name="pad" select="number($pad) + 2"/>
413 </xsl:call-template>
414 <xsl:if test="@enabled = 'false'">
415 <xsl:text>[</xsl:text>
416 </xsl:if>
417 <xsl:choose>
418 <xsl:when test="@rule = 'accept'">
419 <xsl:text>accept</xsl:text>
420 </xsl:when>
421 <xsl:otherwise>
422 <xsl:text>reduce using rule </xsl:text>
423 <xsl:value-of select="@rule"/>
424 <xsl:text> (</xsl:text>
425 <xsl:value-of
95134bc1 426 select="key('bison:ruleNumber', current()/@rule)/lhs[text()]"/>
9fb1f8d8
WP
427 <xsl:text>)</xsl:text>
428 </xsl:otherwise>
429 </xsl:choose>
430 <xsl:if test="@enabled = 'false'">
431 <xsl:text>]</xsl:text>
432 </xsl:if>
433 <xsl:text>&#10;</xsl:text>
434</xsl:template>
435
436<xsl:template match="solved-conflicts">
437 <xsl:if test="resolution">
438 <xsl:text>&#10;</xsl:text>
439 <xsl:apply-templates select="resolution"/>
440 </xsl:if>
441</xsl:template>
442
443<xsl:template match="resolution">
444 <xsl:text> Conflict between rule </xsl:text>
445 <xsl:value-of select="@rule"/>
446 <xsl:text> and token </xsl:text>
447 <xsl:value-of select="@symbol"/>
448 <xsl:text> resolved as </xsl:text>
449 <xsl:if test="@type = 'error'">
450 <xsl:text>an </xsl:text>
451 </xsl:if>
452 <xsl:value-of select="@type"/>
453 <xsl:text> (</xsl:text>
454 <xsl:value-of select="."/>
455 <xsl:text>).&#10;</xsl:text>
456</xsl:template>
457
458<xsl:template name="max-width-symbol">
459 <xsl:param name="node"/>
460 <xsl:variable name="longest">
461 <xsl:for-each select="$node">
462 <xsl:sort data-type="number" select="string-length(@symbol)"
463 order="descending"/>
464 <xsl:if test="position() = 1">
465 <xsl:value-of select="string-length(@symbol)"/>
466 </xsl:if>
467 </xsl:for-each>
468 </xsl:variable>
469 <xsl:value-of select="$longest"/>
470</xsl:template>
471
472<xsl:template name="lpad">
473 <xsl:param name="str" select="''"/>
474 <xsl:param name="pad" select="0"/>
475 <xsl:variable name="diff" select="$pad - string-length($str)" />
476 <xsl:choose>
477 <xsl:when test="$diff &lt; 0">
478 <xsl:value-of select="$str"/>
479 </xsl:when>
480 <xsl:otherwise>
481 <xsl:call-template name="space">
482 <xsl:with-param name="repeat" select="$diff"/>
483 </xsl:call-template>
484 <xsl:value-of select="$str"/>
485 </xsl:otherwise>
486 </xsl:choose>
487</xsl:template>
488
489<xsl:template name="rpad">
490 <xsl:param name="str" select="''"/>
491 <xsl:param name="pad" select="0"/>
492 <xsl:variable name="diff" select="$pad - string-length($str)"/>
493 <xsl:choose>
494 <xsl:when test="$diff &lt; 0">
495 <xsl:value-of select="$str"/>
496 </xsl:when>
497 <xsl:otherwise>
498 <xsl:value-of select="$str"/>
499 <xsl:call-template name="space">
500 <xsl:with-param name="repeat" select="$diff"/>
501 </xsl:call-template>
502 </xsl:otherwise>
503 </xsl:choose>
504</xsl:template>
505
506<xsl:template name="space">
507 <xsl:param name="repeat">0</xsl:param>
508 <xsl:param name="fill" select="' '"/>
509 <xsl:if test="number($repeat) &gt;= 1">
510 <xsl:call-template name="space">
511 <xsl:with-param name="repeat" select="$repeat - 1"/>
512 <xsl:with-param name="fill" select="$fill"/>
513 </xsl:call-template>
514 <xsl:value-of select="$fill"/>
515 </xsl:if>
516</xsl:template>
517
21f1b063 518<xsl:template name="line-wrap">
95134bc1
WP
519 <xsl:param name="line-length"/> <!-- required -->
520 <xsl:param name="first-line-length" select="$line-length"/>
521 <xsl:param name="text"/> <!-- required -->
21f1b063 522 <xsl:choose>
255a6b90 523 <xsl:when test="normalize-space($text) = ''" />
21f1b063
JD
524 <xsl:when test="string-length($text) &lt;= $first-line-length">
525 <xsl:value-of select="concat($text, '&#10;')" />
526 </xsl:when>
527 <xsl:otherwise>
528 <xsl:variable name="break-pos">
529 <xsl:call-template name="ws-search">
530 <xsl:with-param name="text" select="$text" />
255a6b90 531 <xsl:with-param name="start" select="$first-line-length+1" />
21f1b063
JD
532 </xsl:call-template>
533 </xsl:variable>
534 <xsl:value-of select="substring($text, 1, $break-pos - 1)" />
535 <xsl:text>&#10;</xsl:text>
536 <xsl:call-template name="line-wrap">
537 <xsl:with-param name="line-length" select="$line-length" />
538 <xsl:with-param
539 name="text" select="concat(' ', substring($text, $break-pos+1))"
540 />
541 </xsl:call-template>
542 </xsl:otherwise>
543 </xsl:choose>
544</xsl:template>
545
546<xsl:template name="ws-search">
95134bc1 547 <xsl:param name="text"/> <!-- required -->
255a6b90
JD
548 <xsl:param name="start"/> <!-- required -->
549 <xsl:variable name="search-text" select="substring($text, $start)" />
21f1b063 550 <xsl:choose>
255a6b90
JD
551 <xsl:when test="not(contains($search-text, ' '))">
552 <xsl:value-of select="string-length($text)+1" />
21f1b063
JD
553 </xsl:when>
554 <xsl:otherwise>
255a6b90
JD
555 <xsl:value-of
556 select="$start + string-length(substring-before($search-text, ' '))"
557 />
21f1b063
JD
558 </xsl:otherwise>
559 </xsl:choose>
560</xsl:template>
561
9fb1f8d8 562</xsl:stylesheet>