xml2gml.xsl 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!--
  3. xml2dot.xsl - transform Bison XML Report into DOT.
  4. Copyright (C) 2007-2015 Free Software Foundation, Inc.
  5. This file is part of Bison, the GNU Compiler Compiler.
  6. This program is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. Written by Wojciech Polak <polak@gnu.org>.
  17. -->
  18. <xsl:stylesheet version="1.0"
  19. xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  20. xmlns:bison="http://www.gnu.org/software/bison/">
  21. <xsl:import href="bison.xsl"/>
  22. <xsl:output method="text" encoding="UTF-8" indent="no"/>
  23. <xsl:template match="/">
  24. <xsl:apply-templates select="bison-xml-report"/>
  25. </xsl:template>
  26. <xsl:template match="bison-xml-report">
  27. <xsl:text># Generated by GNU Bison xml2gml.xsl </xsl:text>
  28. <xsl:value-of select="@version"/>
  29. <xsl:text>.&#10;</xsl:text>
  30. <xsl:text># Report bugs to &lt;</xsl:text>
  31. <xsl:value-of select="@bug-report"/>
  32. <xsl:text>&gt;.&#10;</xsl:text>
  33. <xsl:text># Home page: &lt;</xsl:text>
  34. <xsl:value-of select="@url"/>
  35. <xsl:text>&gt;.&#10;&#10;</xsl:text>
  36. <xsl:apply-templates select="automaton">
  37. <xsl:with-param name="filename" select="filename"/>
  38. </xsl:apply-templates>
  39. </xsl:template>
  40. <xsl:template match="automaton">
  41. <xsl:param name="filename"/>
  42. <xsl:text>graph</xsl:text>
  43. <xsl:text>&#10;[
  44. </xsl:text>
  45. <xsl:apply-templates select="state"/>
  46. <xsl:text>]&#10;</xsl:text>
  47. </xsl:template>
  48. <xsl:template match="automaton/state">
  49. <xsl:call-template name="output-node">
  50. <xsl:with-param name="number" select="@number"/>
  51. <xsl:with-param name="label">
  52. <xsl:apply-templates select="itemset/item"/>
  53. </xsl:with-param>
  54. </xsl:call-template>
  55. <xsl:apply-templates select="actions/transitions"/>
  56. <xsl:apply-templates select="actions/reductions">
  57. <xsl:with-param name="staten">
  58. <xsl:value-of select="@number"/>
  59. </xsl:with-param>
  60. </xsl:apply-templates>
  61. </xsl:template>
  62. <xsl:template match="actions/reductions">
  63. <xsl:param name="staten"/>
  64. <xsl:for-each select='reduction'>
  65. <!-- These variables are needed because the current context can't be
  66. refered to directly in XPath expressions. -->
  67. <xsl:variable name="rul">
  68. <xsl:value-of select="@rule"/>
  69. </xsl:variable>
  70. <xsl:variable name="ena">
  71. <xsl:value-of select="@enabled"/>
  72. </xsl:variable>
  73. <!-- The foreach's body is protected by this, so that we are actually
  74. going to iterate once per reduction rule, and not per lookahead. -->
  75. <xsl:if test='not(preceding-sibling::*[@rule=$rul and @enabled=$ena])'>
  76. <xsl:variable name="rule">
  77. <xsl:choose>
  78. <!-- The acceptation state is refered to as 'accept' in the XML, but
  79. just as '0' in the DOT. -->
  80. <xsl:when test="@rule='accept'">
  81. <xsl:text>0</xsl:text>
  82. </xsl:when>
  83. <xsl:otherwise>
  84. <xsl:value-of select="@rule"/>
  85. </xsl:otherwise>
  86. </xsl:choose>
  87. </xsl:variable>
  88. <!-- The edge's beginning -->
  89. <xsl:call-template name="reduction-edge-start">
  90. <xsl:with-param name="state" select="$staten"/>
  91. <xsl:with-param name="rule" select="$rule"/>
  92. <xsl:with-param name="enabled" select="@enabled"/>
  93. </xsl:call-template>
  94. <!-- The edge's tokens -->
  95. <!-- Don't show labels for the default action. In other cases, there will
  96. always be at least one token, so 'label="[]"' will not occur. -->
  97. <xsl:if test='$rule!=0 and not(../reduction[@enabled=$ena and @rule=$rule and @symbol="$default"])'>
  98. <xsl:text>label="[</xsl:text>
  99. <xsl:for-each select='../reduction[@enabled=$ena and @rule=$rule]'>
  100. <xsl:call-template name="escape">
  101. <xsl:with-param name="subject" select="@symbol"/>
  102. </xsl:call-template>
  103. <xsl:if test="position() != last ()">
  104. <xsl:text>, </xsl:text>
  105. </xsl:if>
  106. </xsl:for-each>
  107. <xsl:text>]", </xsl:text>
  108. </xsl:if>
  109. <!-- The edge's end -->
  110. <xsl:text>style=solid]&#10;</xsl:text>
  111. <!-- The diamond representing the reduction -->
  112. <xsl:call-template name="reduction-node">
  113. <xsl:with-param name="state" select="$staten"/>
  114. <xsl:with-param name="rule" select="$rule"/>
  115. <xsl:with-param name="color">
  116. <xsl:choose>
  117. <xsl:when test='@enabled="true"'>
  118. <xsl:text>3</xsl:text>
  119. </xsl:when>
  120. <xsl:otherwise>
  121. <xsl:text>5</xsl:text>
  122. </xsl:otherwise>
  123. </xsl:choose>
  124. </xsl:with-param>
  125. </xsl:call-template>
  126. </xsl:if>
  127. </xsl:for-each>
  128. </xsl:template>
  129. <xsl:template match="actions/transitions">
  130. <xsl:apply-templates select="transition"/>
  131. </xsl:template>
  132. <xsl:template match="item">
  133. <xsl:param name="prev-rule-number"
  134. select="preceding-sibling::item[1]/@rule-number"/>
  135. <xsl:apply-templates select="key('bison:ruleByNumber', @rule-number)">
  136. <xsl:with-param name="point" select="@point"/>
  137. <xsl:with-param name="num" select="@rule-number"/>
  138. <xsl:with-param name="prev-lhs"
  139. select="key('bison:ruleByNumber', $prev-rule-number)/lhs[text()]"
  140. />
  141. </xsl:apply-templates>
  142. <xsl:apply-templates select="lookaheads"/>
  143. </xsl:template>
  144. <xsl:template match="rule">
  145. <xsl:param name="point"/>
  146. <xsl:param name="num"/>
  147. <xsl:param name="prev-lhs"/>
  148. <xsl:text>&#10;</xsl:text>
  149. <xsl:choose>
  150. <xsl:when test="$num &lt; 10">
  151. <xsl:text> </xsl:text>
  152. </xsl:when>
  153. <xsl:when test="$num &lt; 100">
  154. <xsl:text> </xsl:text>
  155. </xsl:when>
  156. <xsl:otherwise>
  157. <xsl:text></xsl:text>
  158. </xsl:otherwise>
  159. </xsl:choose>
  160. <xsl:value-of select="$num"/>
  161. <xsl:text> </xsl:text>
  162. <xsl:choose>
  163. <xsl:when test="$prev-lhs = lhs[text()]">
  164. <xsl:call-template name="lpad">
  165. <xsl:with-param name="str" select="'|'"/>
  166. <xsl:with-param name="pad" select="number(string-length(lhs[text()])) + 1"/>
  167. </xsl:call-template>
  168. </xsl:when>
  169. <xsl:otherwise>
  170. <xsl:value-of select="lhs"/>
  171. <xsl:text>:</xsl:text>
  172. </xsl:otherwise>
  173. </xsl:choose>
  174. <xsl:if test="$point = 0">
  175. <xsl:text> .</xsl:text>
  176. </xsl:if>
  177. <!-- RHS -->
  178. <xsl:for-each select="rhs/symbol|rhs/empty">
  179. <xsl:apply-templates select="."/>
  180. <xsl:if test="$point = position()">
  181. <xsl:text> .</xsl:text>
  182. </xsl:if>
  183. </xsl:for-each>
  184. </xsl:template>
  185. <xsl:template match="symbol">
  186. <xsl:text> </xsl:text>
  187. <xsl:value-of select="."/>
  188. </xsl:template>
  189. <xsl:template match="empty">
  190. <xsl:text> %empty</xsl:text>
  191. </xsl:template>
  192. <xsl:template match="lookaheads">
  193. <xsl:text> [</xsl:text>
  194. <xsl:apply-templates select="symbol"/>
  195. <xsl:text>]</xsl:text>
  196. </xsl:template>
  197. <xsl:template match="lookaheads/symbol">
  198. <xsl:value-of select="."/>
  199. <xsl:if test="position() != last()">
  200. <xsl:text>, </xsl:text>
  201. </xsl:if>
  202. </xsl:template>
  203. <xsl:template name="reduction-edge-start">
  204. <xsl:param name="state"/>
  205. <xsl:param name="rule"/>
  206. <xsl:param name="enabled"/>
  207. <xsl:text> </xsl:text>
  208. <xsl:value-of select="$state"/>
  209. <xsl:text> -> "</xsl:text>
  210. <xsl:value-of select="$state"/>
  211. <xsl:text>R</xsl:text>
  212. <xsl:value-of select="$rule"/>
  213. <xsl:if test='$enabled = "false"'>
  214. <xsl:text>d</xsl:text>
  215. </xsl:if>
  216. <xsl:text>" [</xsl:text>
  217. </xsl:template>
  218. <xsl:template name="reduction-node">
  219. <xsl:param name="state"/>
  220. <xsl:param name="rule"/>
  221. <xsl:param name="color"/>
  222. <xsl:text> "</xsl:text>
  223. <xsl:value-of select="$state"/>
  224. <xsl:text>R</xsl:text>
  225. <xsl:value-of select="$rule"/>
  226. <xsl:if test="$color = 5">
  227. <xsl:text>d</xsl:text>
  228. </xsl:if>
  229. <xsl:text>" [label="</xsl:text>
  230. <xsl:choose>
  231. <xsl:when test="$rule = 0">
  232. <xsl:text>Acc", fillcolor=1</xsl:text>
  233. </xsl:when>
  234. <xsl:otherwise>
  235. <xsl:text>R</xsl:text>
  236. <xsl:value-of select="$rule"/>
  237. <xsl:text>", fillcolor=</xsl:text>
  238. <xsl:value-of select="$color"/>
  239. </xsl:otherwise>
  240. </xsl:choose>
  241. <xsl:text>, shape=diamond, style=filled]&#10;</xsl:text>
  242. </xsl:template>
  243. <xsl:template match="transition">
  244. <xsl:call-template name="output-edge">
  245. <xsl:with-param name="src" select="../../../@number"/>
  246. <xsl:with-param name="dst" select="@state"/>
  247. <xsl:with-param name="style">
  248. <xsl:choose>
  249. <xsl:when test="@symbol = 'error'">
  250. <xsl:text>dotted</xsl:text>
  251. </xsl:when>
  252. <xsl:when test="@type = 'shift'">
  253. <xsl:text>solid</xsl:text>
  254. </xsl:when>
  255. <xsl:otherwise>
  256. <xsl:text>dashed</xsl:text>
  257. </xsl:otherwise>
  258. </xsl:choose>
  259. </xsl:with-param>
  260. <xsl:with-param name="label">
  261. <xsl:if test="not(@symbol = 'error')">
  262. <xsl:value-of select="@symbol"/>
  263. </xsl:if>
  264. </xsl:with-param>
  265. </xsl:call-template>
  266. </xsl:template>
  267. <xsl:template name="output-node">
  268. <xsl:param name="number"/>
  269. <xsl:param name="label"/>
  270. <xsl:text> </xsl:text>
  271. <xsl:value-of select="$number"/>
  272. <xsl:text> [label="</xsl:text>
  273. <xsl:text>State </xsl:text>
  274. <xsl:value-of select="$number"/>
  275. <xsl:text>\n</xsl:text>
  276. <xsl:call-template name="escape">
  277. <xsl:with-param name="subject" select="$label"/>
  278. </xsl:call-template>
  279. <xsl:text>\l"]&#10;</xsl:text>
  280. </xsl:template>
  281. <xsl:template name="output-edge">
  282. <xsl:param name="src"/>
  283. <xsl:param name="dst"/>
  284. <xsl:param name="style"/>
  285. <xsl:param name="label"/>
  286. <xsl:text> </xsl:text>
  287. <xsl:value-of select="$src"/>
  288. <xsl:text> -> </xsl:text>
  289. <xsl:value-of select="$dst"/>
  290. <xsl:text> [style=</xsl:text>
  291. <xsl:value-of select="$style"/>
  292. <xsl:if test="$label and $label != ''">
  293. <xsl:text> label="</xsl:text>
  294. <xsl:call-template name="escape">
  295. <xsl:with-param name="subject" select="$label"/>
  296. </xsl:call-template>
  297. <xsl:text>"</xsl:text>
  298. </xsl:if>
  299. <xsl:text>]&#10;</xsl:text>
  300. </xsl:template>
  301. <!-- node label -->
  302. <xsl:template name="escape">
  303. <xsl:param name="subject"/> <!-- required -->
  304. <xsl:call-template name="string-replace">
  305. <xsl:with-param name="subject">
  306. <xsl:call-template name="string-replace">
  307. <xsl:with-param name="subject">
  308. <xsl:call-template name="string-replace">
  309. <xsl:with-param name="subject" select="$subject"/>
  310. <xsl:with-param name="search" select="'\'"/>
  311. <xsl:with-param name="replace" select="'\\'"/>
  312. </xsl:call-template>
  313. </xsl:with-param>
  314. <xsl:with-param name="search" select="'&quot;'"/>
  315. <xsl:with-param name="replace" select="'\&quot;'"/>
  316. </xsl:call-template>
  317. </xsl:with-param>
  318. <xsl:with-param name="search" select="'&#10;'"/>
  319. <xsl:with-param name="replace" select="'\n'"/>
  320. </xsl:call-template>
  321. </xsl:template>
  322. <xsl:template name="string-replace">
  323. <xsl:param name="subject"/>
  324. <xsl:param name="search"/>
  325. <xsl:param name="replace"/>
  326. <xsl:choose>
  327. <xsl:when test="contains($subject, $search)">
  328. <xsl:variable name="before" select="substring-before($subject, $search)"/>
  329. <xsl:variable name="after" select="substring-after($subject, $search)"/>
  330. <xsl:value-of select="$before"/>
  331. <xsl:value-of select="$replace"/>
  332. <xsl:call-template name="string-replace">
  333. <xsl:with-param name="subject" select="$after"/>
  334. <xsl:with-param name="search" select="$search"/>
  335. <xsl:with-param name="replace" select="$replace"/>
  336. </xsl:call-template>
  337. </xsl:when>
  338. <xsl:otherwise>
  339. <xsl:value-of select="$subject"/>
  340. </xsl:otherwise>
  341. </xsl:choose>
  342. </xsl:template>
  343. <xsl:template name="lpad">
  344. <xsl:param name="str" select="''"/>
  345. <xsl:param name="pad" select="0"/>
  346. <xsl:variable name="diff" select="$pad - string-length($str)" />
  347. <xsl:choose>
  348. <xsl:when test="$diff &lt; 0">
  349. <xsl:value-of select="$str"/>
  350. </xsl:when>
  351. <xsl:otherwise>
  352. <xsl:call-template name="space">
  353. <xsl:with-param name="repeat" select="$diff"/>
  354. </xsl:call-template>
  355. <xsl:value-of select="$str"/>
  356. </xsl:otherwise>
  357. </xsl:choose>
  358. </xsl:template>
  359. </xsl:stylesheet>