[420] | 1 | <xsl:transform
|
---|
| 2 | xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
|
---|
[427] | 3 | xmlns:xs="http://www.w3.org/2001/XMLSchema"
|
---|
| 4 | xmlns:x="http://purl.org/net/xml2rfc/ext"
|
---|
| 5 | version="2.0"
|
---|
| 6 | exclude-result-prefixes="xs">
|
---|
[420] | 7 |
|
---|
[427] | 8 | <xsl:output method="xml" omit-xml-declaration="yes" indent="no"/>
|
---|
| 9 |
|
---|
[420] | 10 | <xsl:param name="abnf"/>
|
---|
| 11 |
|
---|
| 12 | <xsl:template match="/">
|
---|
[427] | 13 | <xsl:variable name="src" select="."/>
|
---|
[421] | 14 | <xsl:variable name="collected" select="unparsed-text($abnf)"/>
|
---|
[420] | 15 |
|
---|
[581] | 16 | <xsl:variable name="all-lines" as="xs:string*" select="tokenize($collected, '\r?\n')"/>
|
---|
| 17 | <xsl:variable name="lines" select="$all-lines[normalize-space(.)!='']"/>
|
---|
[427] | 18 |
|
---|
[1773] | 19 | <xsl:variable name="generated"><section title="Collected ABNF" anchor="collected.abnf">
|
---|
[425] | 20 | <xsl:text> </xsl:text>
|
---|
[427] | 21 | <figure>
|
---|
| 22 | <xsl:text> </xsl:text>
|
---|
| 23 | <artwork type="abnf" name="{$abnf}">
|
---|
[454] | 24 | <xsl:for-each select="$lines[substring(.,1,2)!='; ']">
|
---|
| 25 |
|
---|
| 26 | <!-- Group by start character -->
|
---|
[427] | 27 | <xsl:variable name="lineno" select="position()"/>
|
---|
| 28 | <xsl:variable name="sc1" select="substring(.,1,1)"/>
|
---|
[428] | 29 | <xsl:variable name="sc0" select="x:laststartchar($lines, $lineno - 1)"/>
|
---|
[427] | 30 | <xsl:if test="$sc1!=' ' and $sc0!=' ' and $sc1!=$sc0">
|
---|
| 31 | <xsl:text> </xsl:text>
|
---|
| 32 | </xsl:if>
|
---|
[454] | 33 |
|
---|
| 34 | <!-- Add cross-refs for terms -->
|
---|
[427] | 35 | <xsl:analyze-string select="." regex='^([A-Za-z0-9\-]+) = ' flags="sm">
|
---|
| 36 | <xsl:matching-substring>
|
---|
| 37 | <xsl:variable name="term" select="regex-group(1)"/>
|
---|
| 38 | <xsl:choose>
|
---|
| 39 | <xsl:when test="$src//*[@anchor=$term] or $src//x:anchor-alias[@value=$term]">
|
---|
| 40 | <x:ref><xsl:value-of select="$term"/></x:ref>
|
---|
| 41 | </xsl:when>
|
---|
| 42 | <xsl:otherwise>
|
---|
| 43 | <xsl:value-of select="$term"/>
|
---|
| 44 | </xsl:otherwise>
|
---|
| 45 | </xsl:choose>
|
---|
| 46 | <xsl:text> = </xsl:text>
|
---|
| 47 | </xsl:matching-substring>
|
---|
| 48 | <xsl:non-matching-substring>
|
---|
| 49 | <xsl:value-of select="."/>
|
---|
| 50 | </xsl:non-matching-substring>
|
---|
| 51 | </xsl:analyze-string>
|
---|
[454] | 52 |
|
---|
[427] | 53 | <xsl:text> </xsl:text>
|
---|
| 54 | </xsl:for-each>
|
---|
| 55 | </artwork>
|
---|
| 56 | <xsl:text> </xsl:text>
|
---|
| 57 | </figure>
|
---|
| 58 | <xsl:text> </xsl:text>
|
---|
[454] | 59 |
|
---|
[1773] | 60 | <xsl:variable name="diags">
|
---|
| 61 | <xsl:for-each select="$lines[substring(.,1,2)='; ']">
|
---|
| 62 | <xsl:variable name="prod" select="substring-before(substring-after(.,'; '),' defined but not used')"/>
|
---|
| 63 | <xsl:choose>
|
---|
| 64 | <xsl:when test="$prod!='' and $src//iref[@item='Header Fields' and @subitem=$prod]">
|
---|
| 65 | <!-- header field; expected not to be reference -->
|
---|
| 66 | </xsl:when>
|
---|
[1788] | 67 | <xsl:when test="$prod!='' and $src//iref[@item='Grammar' and @subitem=$prod and comment()='terminal production']">
|
---|
| 68 | <!-- known to be a terminal production -->
|
---|
| 69 | </xsl:when>
|
---|
| 70 | <xsl:when test="$prod!='' and $src//iref[@item='Grammar' and @subitem=$prod and comment()='exported production']">
|
---|
| 71 | <!-- known to be used in another part -->
|
---|
| 72 | </xsl:when>
|
---|
| 73 | <xsl:when test="$prod!='' and $src//iref[@item='Grammar' and @subitem=$prod and comment()='unused production']">
|
---|
| 74 | <!-- known to be unused, but we want to include it anyway -->
|
---|
| 75 | </xsl:when>
|
---|
[1893] | 76 | <xsl:when test="$prod!='' and $src//x:ref=$prod">
|
---|
| 77 | <!-- referenced otherwise -->
|
---|
| 78 | </xsl:when>
|
---|
[1773] | 79 | <xsl:otherwise>
|
---|
| 80 | <xsl:value-of select="."/>
|
---|
| 81 | <xsl:text> </xsl:text>
|
---|
| 82 | </xsl:otherwise>
|
---|
| 83 | </xsl:choose>
|
---|
| 84 | </xsl:for-each>
|
---|
| 85 | </xsl:variable>
|
---|
| 86 |
|
---|
| 87 | <xsl:if test="$diags!=''">
|
---|
| 88 | <figure>
|
---|
| 89 | <preamble>ABNF diagnostics:</preamble>
|
---|
| 90 | <artwork type="inline">
|
---|
[454] | 91 | <xsl:text> </xsl:text>
|
---|
[1773] | 92 | <xsl:value-of select="$diags"/>
|
---|
| 93 | </artwork></figure>
|
---|
| 94 | </xsl:if>
|
---|
| 95 | </section></xsl:variable>
|
---|
[420] | 96 |
|
---|
[1773] | 97 | <xsl:copy-of select="$generated"/>
|
---|
| 98 |
|
---|
[425] | 99 | <!-- check whether it's up-to-date... -->
|
---|
[454] | 100 | <xsl:variable name="src">
|
---|
| 101 | <xsl:for-each select="//section[@anchor='collected.abnf']//artwork">
|
---|
| 102 | <xsl:value-of select="."/>
|
---|
| 103 | </xsl:for-each>
|
---|
| 104 | </xsl:variable>
|
---|
[421] | 105 |
|
---|
[1777] | 106 | <xsl:variable name="new">
|
---|
| 107 | <xsl:value-of select="$generated//artwork"/>
|
---|
| 108 | </xsl:variable>
|
---|
| 109 |
|
---|
| 110 | <xsl:if test="not(//section[@anchor='collected.abnf']) or normalize-space($src) != normalize-space($new)">
|
---|
[421] | 111 | <xsl:message>WARNING: appendix contained inside source document needs to be updated</xsl:message>
|
---|
[1711] | 112 | <xsl:call-template name="showdiff">
|
---|
| 113 | <xsl:with-param name="actual" select="normalize-space($src)"/>
|
---|
[1777] | 114 | <xsl:with-param name="expected" select="normalize-space($new)"/>
|
---|
[1711] | 115 | </xsl:call-template>
|
---|
[421] | 116 | </xsl:if>
|
---|
[420] | 117 | </xsl:template>
|
---|
[428] | 118 |
|
---|
[1711] | 119 | <xsl:template name="showdiff">
|
---|
| 120 | <xsl:param name="actual"/>
|
---|
| 121 | <xsl:param name="expected"/>
|
---|
| 122 | <xsl:param name="prev"/>
|
---|
| 123 |
|
---|
| 124 | <xsl:choose>
|
---|
| 125 | <xsl:when test="$actual=''">
|
---|
| 126 | <!-- done -->
|
---|
| 127 | </xsl:when>
|
---|
| 128 | <xsl:when test="substring($actual,1,1)=substring($expected,1,1)">
|
---|
| 129 | <xsl:call-template name="showdiff">
|
---|
| 130 | <xsl:with-param name="actual" select="substring($actual,2)"/>
|
---|
| 131 | <xsl:with-param name="expected" select="substring($expected,2)"/>
|
---|
| 132 | <xsl:with-param name="prev" select="concat($prev,substring($actual,1,1))"/>
|
---|
| 133 | </xsl:call-template>
|
---|
| 134 | </xsl:when>
|
---|
| 135 | <xsl:otherwise>
|
---|
| 136 | <xsl:message>WARNING: at position <xsl:value-of select="string-length($prev)"/></xsl:message>
|
---|
| 137 | <xsl:message>WARNING: prefix <xsl:value-of select="$prev"/></xsl:message>
|
---|
| 138 | <xsl:message>WARNING: actual text is: '<xsl:value-of select="substring($actual,1,20)"/>...' expected was: '<xsl:value-of select="substring($expected,1,20)"/>...'</xsl:message>
|
---|
| 139 | </xsl:otherwise>
|
---|
| 140 | </xsl:choose>
|
---|
| 141 | </xsl:template>
|
---|
| 142 |
|
---|
[428] | 143 | <xsl:function name="x:laststartchar">
|
---|
| 144 | <xsl:param name="lines"/>
|
---|
| 145 | <xsl:param name="position"/>
|
---|
[420] | 146 |
|
---|
[428] | 147 | <xsl:choose>
|
---|
| 148 | <xsl:when test="' '!=substring($lines[$position],1,1)">
|
---|
| 149 | <xsl:value-of select="substring($lines[$position],1,1)"/>
|
---|
| 150 | </xsl:when>
|
---|
| 151 | <xsl:otherwise>
|
---|
| 152 | <xsl:value-of select="x:laststartchar($lines, $position - 1)"/>
|
---|
| 153 | </xsl:otherwise>
|
---|
| 154 | </xsl:choose>
|
---|
| 155 | </xsl:function>
|
---|
[420] | 156 |
|
---|
[428] | 157 |
|
---|
[420] | 158 | </xsl:transform> |
---|