xml2dot.xsl 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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 </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>digraph "</xsl:text>
  43. <xsl:call-template name="escape">
  44. <xsl:with-param name="subject" select="$filename"/>
  45. </xsl:call-template>
  46. <xsl:text>"&#10;{
  47. node [fontname = courier, shape = box, colorscheme = paired6]
  48. edge [fontname = courier]
  49. </xsl:text>
  50. <xsl:apply-templates select="state"/>
  51. <xsl:text>}&#10;</xsl:text>
  52. </xsl:template>
  53. <xsl:template match="automaton/state">
  54. <xsl:call-template name="output-node">
  55. <xsl:with-param name="number" select="@number"/>
  56. <xsl:with-param name="label">
  57. <xsl:apply-templates select="itemset/item"/>
  58. </xsl:with-param>
  59. </xsl:call-template>
  60. <xsl:apply-templates select="actions/transitions"/>
  61. <xsl:apply-templates select="actions/reductions">
  62. <xsl:with-param name="staten">
  63. <xsl:value-of select="@number"/>
  64. </xsl:with-param>
  65. </xsl:apply-templates>
  66. </xsl:template>
  67. <xsl:template match="actions/reductions">
  68. <xsl:param name="staten"/>
  69. <xsl:for-each select='reduction'>
  70. <!-- These variables are needed because the current context can't be
  71. refered to directly in XPath expressions. -->
  72. <xsl:variable name="rul">
  73. <xsl:value-of select="@rule"/>
  74. </xsl:variable>
  75. <xsl:variable name="ena">
  76. <xsl:value-of select="@enabled"/>
  77. </xsl:variable>
  78. <!-- The foreach's body is protected by this, so that we are actually
  79. going to iterate once per reduction rule, and not per lookahead. -->
  80. <xsl:if test='not(preceding-sibling::*[@rule=$rul and @enabled=$ena])'>
  81. <xsl:variable name="rule">
  82. <xsl:choose>
  83. <!-- The acceptation state is refered to as 'accept' in the XML, but
  84. just as '0' in the DOT. -->
  85. <xsl:when test="@rule='accept'">
  86. <xsl:text>0</xsl:text>
  87. </xsl:when>
  88. <xsl:otherwise>
  89. <xsl:value-of select="@rule"/>
  90. </xsl:otherwise>
  91. </xsl:choose>
  92. </xsl:variable>
  93. <!-- The edge's beginning -->
  94. <xsl:call-template name="reduction-edge-start">
  95. <xsl:with-param name="state" select="$staten"/>
  96. <xsl:with-param name="rule" select="$rule"/>
  97. <xsl:with-param name="enabled" select="@enabled"/>
  98. </xsl:call-template>
  99. <!-- The edge's tokens -->
  100. <!-- Don't show labels for the default action. In other cases, there will
  101. always be at least one token, so 'label="[]"' will not occur. -->
  102. <xsl:if test='$rule!=0 and not(../reduction[@enabled=$ena and @rule=$rule and @symbol="$default"])'>
  103. <xsl:text>label="[</xsl:text>
  104. <xsl:for-each select='../reduction[@enabled=$ena and @rule=$rule]'>
  105. <xsl:call-template name="escape">
  106. <xsl:with-param name="subject" select="@symbol"/>
  107. </xsl:call-template>
  108. <xsl:if test="position() != last ()">
  109. <xsl:text>, </xsl:text>
  110. </xsl:if>
  111. </xsl:for-each>
  112. <xsl:text>]", </xsl:text>
  113. </xsl:if>
  114. <!-- The edge's end -->
  115. <xsl:text>style=solid]&#10;</xsl:text>
  116. <!-- The diamond representing the reduction -->
  117. <xsl:call-template name="reduction-node">
  118. <xsl:with-param name="state" select="$staten"/>
  119. <xsl:with-param name="rule" select="$rule"/>
  120. <xsl:with-param name="color">
  121. <xsl:choose>
  122. <xsl:when test='@enabled="true"'>
  123. <xsl:text>3</xsl:text>
  124. </xsl:when>
  125. <xsl:otherwise>
  126. <xsl:text>5</xsl:text>
  127. </xsl:otherwise>
  128. </xsl:choose>
  129. </xsl:with-param>
  130. </xsl:call-template>
  131. </xsl:if>
  132. </xsl:for-each>
  133. </xsl:template>
  134. <xsl:template match="actions/transitions">
  135. <xsl:apply-templates select="transition"/>
  136. </xsl:template>
  137. <xsl:template match="item">
  138. <xsl:param name="prev-rule-number"
  139. select="preceding-sibling::item[1]/@rule-number"/>
  140. <xsl:apply-templates select="key('bison:ruleByNumber', @rule-number)">
  141. <xsl:with-param name="point" select="@point"/>
  142. <xsl:with-param name="num" select="@rule-number"/>
  143. <xsl:with-param name="prev-lhs"
  144. select="key('bison:ruleByNumber', $prev-rule-number)/lhs[text()]"
  145. />
  146. </xsl:apply-templates>
  147. <xsl:apply-templates select="lookaheads"/>
  148. </xsl:template>
  149. <xsl:template match="rule">
  150. <xsl:param name="point"/>
  151. <xsl:param name="num"/>
  152. <xsl:param name="prev-lhs"/>
  153. <xsl:text>&#10;</xsl:text>
  154. <xsl:choose>
  155. <xsl:when test="$num &lt; 10">
  156. <xsl:text> </xsl:text>
  157. </xsl:when>
  158. <xsl:when test="$num &lt; 100">
  159. <xsl:text> </xsl:text>
  160. </xsl:when>
  161. <xsl:otherwise>
  162. <xsl:text></xsl:text>
  163. </xsl:otherwise>
  164. </xsl:choose>
  165. <xsl:value-of select="$num"/>
  166. <xsl:text> </xsl:text>
  167. <xsl:choose>
  168. <xsl:when test="$prev-lhs = lhs[text()]">
  169. <xsl:call-template name="lpad">
  170. <xsl:with-param name="str" select="'|'"/>
  171. <xsl:with-param name="pad" select="number(string-length(lhs[text()])) + 1"/>
  172. </xsl:call-template>
  173. </xsl:when>
  174. <xsl:otherwise>
  175. <xsl:value-of select="lhs"/>
  176. <xsl:text>:</xsl:text>
  177. </xsl:otherwise>
  178. </xsl:choose>
  179. <xsl:if test="$point = 0">
  180. <xsl:text> .</xsl:text>
  181. </xsl:if>
  182. <!-- RHS -->
  183. <xsl:for-each select="rhs/symbol|rhs/empty">
  184. <xsl:apply-templates select="."/>
  185. <xsl:if test="$point = position()">
  186. <xsl:text> .</xsl:text>
  187. </xsl:if>
  188. </xsl:for-each>
  189. </xsl:template>
  190. <xsl:template match="symbol">
  191. <xsl:text> </xsl:text>
  192. <xsl:value-of select="."/>
  193. </xsl:template>
  194. <xsl:template match="empty">
  195. <xsl:text> %empty</xsl:text>
  196. </xsl:template>
  197. <xsl:template match="lookaheads">
  198. <xsl:text> [</xsl:text>
  199. <xsl:apply-templates select="symbol"/>
  200. <xsl:text>]</xsl:text>
  201. </xsl:template>
  202. <xsl:template match="lookaheads/symbol">
  203. <xsl:value-of select="."/>
  204. <xsl:if test="position() != last()">
  205. <xsl:text>, </xsl:text>
  206. </xsl:if>
  207. </xsl:template>
  208. <xsl:template name="reduction-edge-start">
  209. <xsl:param name="state"/>
  210. <xsl:param name="rule"/>
  211. <xsl:param name="enabled"/>
  212. <xsl:text> </xsl:text>
  213. <xsl:value-of select="$state"/>
  214. <xsl:text> -> "</xsl:text>
  215. <xsl:value-of select="$state"/>
  216. <xsl:text>R</xsl:text>
  217. <xsl:value-of select="$rule"/>
  218. <xsl:if test='$enabled = "false"'>
  219. <xsl:text>d</xsl:text>
  220. </xsl:if>
  221. <xsl:text>" [</xsl:text>
  222. </xsl:template>
  223. <xsl:template name="reduction-node">
  224. <xsl:param name="state"/>
  225. <xsl:param name="rule"/>
  226. <xsl:param name="color"/>
  227. <xsl:text> "</xsl:text>
  228. <xsl:value-of select="$state"/>
  229. <xsl:text>R</xsl:text>
  230. <xsl:value-of select="$rule"/>
  231. <xsl:if test="$color = 5">
  232. <xsl:text>d</xsl:text>
  233. </xsl:if>
  234. <xsl:text>" [label="</xsl:text>
  235. <xsl:choose>
  236. <xsl:when test="$rule = 0">
  237. <xsl:text>Acc", fillcolor=1</xsl:text>
  238. </xsl:when>
  239. <xsl:otherwise>
  240. <xsl:text>R</xsl:text>
  241. <xsl:value-of select="$rule"/>
  242. <xsl:text>", fillcolor=</xsl:text>
  243. <xsl:value-of select="$color"/>
  244. </xsl:otherwise>
  245. </xsl:choose>
  246. <xsl:text>, shape=diamond, style=filled]&#10;</xsl:text>
  247. </xsl:template>
  248. <xsl:template match="transition">
  249. <xsl:call-template name="output-edge">
  250. <xsl:with-param name="src" select="../../../@number"/>
  251. <xsl:with-param name="dst" select="@state"/>
  252. <xsl:with-param name="style">
  253. <xsl:choose>
  254. <xsl:when test="@symbol = 'error'">
  255. <xsl:text>dotted</xsl:text>
  256. </xsl:when>
  257. <xsl:when test="@type = 'shift'">
  258. <xsl:text>solid</xsl:text>
  259. </xsl:when>
  260. <xsl:otherwise>
  261. <xsl:text>dashed</xsl:text>
  262. </xsl:otherwise>
  263. </xsl:choose>
  264. </xsl:with-param>
  265. <xsl:with-param name="label">
  266. <xsl:if test="not(@symbol = 'error')">
  267. <xsl:value-of select="@symbol"/>
  268. </xsl:if>
  269. </xsl:with-param>
  270. </xsl:call-template>
  271. </xsl:template>
  272. <xsl:template name="output-node">
  273. <xsl:param name="number"/>
  274. <xsl:param name="label"/>
  275. <xsl:text> </xsl:text>
  276. <xsl:value-of select="$number"/>
  277. <xsl:text> [label="</xsl:text>
  278. <xsl:text>State </xsl:text>
  279. <xsl:value-of select="$number"/>
  280. <xsl:text>\n</xsl:text>
  281. <xsl:call-template name="escape">
  282. <xsl:with-param name="subject" select="$label"/>
  283. </xsl:call-template>
  284. <xsl:text>\l"]&#10;</xsl:text>
  285. </xsl:template>
  286. <xsl:template name="output-edge">
  287. <xsl:param name="src"/>
  288. <xsl:param name="dst"/>
  289. <xsl:param name="style"/>
  290. <xsl:param name="label"/>
  291. <xsl:text> </xsl:text>
  292. <xsl:value-of select="$src"/>
  293. <xsl:text> -> </xsl:text>
  294. <xsl:value-of select="$dst"/>
  295. <xsl:text> [style=</xsl:text>
  296. <xsl:value-of select="$style"/>
  297. <xsl:if test="$label and $label != ''">
  298. <xsl:text> label="</xsl:text>
  299. <xsl:call-template name="escape">
  300. <xsl:with-param name="subject" select="$label"/>
  301. </xsl:call-template>
  302. <xsl:text>"</xsl:text>
  303. </xsl:if>
  304. <xsl:text>]&#10;</xsl:text>
  305. </xsl:template>
  306. <xsl:template name="escape">
  307. <xsl:param name="subject"/> <!-- required -->
  308. <xsl:call-template name="string-replace">
  309. <xsl:with-param name="subject">
  310. <xsl:call-template name="string-replace">
  311. <xsl:with-param name="subject">
  312. <xsl:call-template name="string-replace">
  313. <xsl:with-param name="subject" select="$subject"/>
  314. <xsl:with-param name="search" select="'\'"/>
  315. <xsl:with-param name="replace" select="'\\'"/>
  316. </xsl:call-template>
  317. </xsl:with-param>
  318. <xsl:with-param name="search" select="'&quot;'"/>
  319. <xsl:with-param name="replace" select="'\&quot;'"/>
  320. </xsl:call-template>
  321. </xsl:with-param>
  322. <xsl:with-param name="search" select="'&#10;'"/>
  323. <xsl:with-param name="replace" select="'\l'"/>
  324. </xsl:call-template>
  325. </xsl:template>
  326. <xsl:template name="string-replace">
  327. <xsl:param name="subject"/>
  328. <xsl:param name="search"/>
  329. <xsl:param name="replace"/>
  330. <xsl:choose>
  331. <xsl:when test="contains($subject, $search)">
  332. <xsl:variable name="before" select="substring-before($subject, $search)"/>
  333. <xsl:variable name="after" select="substring-after($subject, $search)"/>
  334. <xsl:value-of select="$before"/>
  335. <xsl:value-of select="$replace"/>
  336. <xsl:call-template name="string-replace">
  337. <xsl:with-param name="subject" select="$after"/>
  338. <xsl:with-param name="search" select="$search"/>
  339. <xsl:with-param name="replace" select="$replace"/>
  340. </xsl:call-template>
  341. </xsl:when>
  342. <xsl:otherwise>
  343. <xsl:value-of select="$subject"/>
  344. </xsl:otherwise>
  345. </xsl:choose>
  346. </xsl:template>
  347. <xsl:template name="lpad">
  348. <xsl:param name="str" select="''"/>
  349. <xsl:param name="pad" select="0"/>
  350. <xsl:variable name="diff" select="$pad - string-length($str)" />
  351. <xsl:choose>
  352. <xsl:when test="$diff &lt; 0">
  353. <xsl:value-of select="$str"/>
  354. </xsl:when>
  355. <xsl:otherwise>
  356. <xsl:call-template name="space">
  357. <xsl:with-param name="repeat" select="$diff"/>
  358. </xsl:call-template>
  359. <xsl:value-of select="$str"/>
  360. </xsl:otherwise>
  361. </xsl:choose>
  362. </xsl:template>
  363. </xsl:stylesheet>