[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>
|
---|
[2206] | 21 | <xsl:if test="not(starts-with($abnf,'p1-'))"><t>
|
---|
[2629] | 22 | In the collected ABNF below, list rules are expanded as per <xref target='RFC7230' x:rel='#notation'/>.
|
---|
[2206] | 23 | </t></xsl:if>
|
---|
| 24 | <figure>
|
---|
[427] | 25 | <xsl:text> </xsl:text>
|
---|
| 26 | <artwork type="abnf" name="{$abnf}">
|
---|
[454] | 27 | <xsl:for-each select="$lines[substring(.,1,2)!='; ']">
|
---|
| 28 |
|
---|
| 29 | <!-- Group by start character -->
|
---|
[427] | 30 | <xsl:variable name="lineno" select="position()"/>
|
---|
| 31 | <xsl:variable name="sc1" select="substring(.,1,1)"/>
|
---|
[428] | 32 | <xsl:variable name="sc0" select="x:laststartchar($lines, $lineno - 1)"/>
|
---|
[427] | 33 | <xsl:if test="$sc1!=' ' and $sc0!=' ' and $sc1!=$sc0">
|
---|
| 34 | <xsl:text> </xsl:text>
|
---|
| 35 | </xsl:if>
|
---|
[454] | 36 |
|
---|
| 37 | <!-- Add cross-refs for terms -->
|
---|
[427] | 38 | <xsl:analyze-string select="." regex='^([A-Za-z0-9\-]+) = ' flags="sm">
|
---|
| 39 | <xsl:matching-substring>
|
---|
| 40 | <xsl:variable name="term" select="regex-group(1)"/>
|
---|
| 41 | <xsl:choose>
|
---|
| 42 | <xsl:when test="$src//*[@anchor=$term] or $src//x:anchor-alias[@value=$term]">
|
---|
| 43 | <x:ref><xsl:value-of select="$term"/></x:ref>
|
---|
| 44 | </xsl:when>
|
---|
| 45 | <xsl:otherwise>
|
---|
| 46 | <xsl:value-of select="$term"/>
|
---|
| 47 | </xsl:otherwise>
|
---|
| 48 | </xsl:choose>
|
---|
| 49 | <xsl:text> = </xsl:text>
|
---|
| 50 | </xsl:matching-substring>
|
---|
| 51 | <xsl:non-matching-substring>
|
---|
| 52 | <xsl:value-of select="."/>
|
---|
| 53 | </xsl:non-matching-substring>
|
---|
| 54 | </xsl:analyze-string>
|
---|
[454] | 55 |
|
---|
[427] | 56 | <xsl:text> </xsl:text>
|
---|
| 57 | </xsl:for-each>
|
---|
| 58 | </artwork>
|
---|
| 59 | <xsl:text> </xsl:text>
|
---|
| 60 | </figure>
|
---|
| 61 | <xsl:text> </xsl:text>
|
---|
[454] | 62 |
|
---|
[1773] | 63 | <xsl:variable name="diags">
|
---|
| 64 | <xsl:for-each select="$lines[substring(.,1,2)='; ']">
|
---|
| 65 | <xsl:variable name="prod" select="substring-before(substring-after(.,'; '),' defined but not used')"/>
|
---|
| 66 | <xsl:choose>
|
---|
| 67 | <xsl:when test="$prod!='' and $src//iref[@item='Header Fields' and @subitem=$prod]">
|
---|
| 68 | <!-- header field; expected not to be reference -->
|
---|
| 69 | </xsl:when>
|
---|
[1788] | 70 | <xsl:when test="$prod!='' and $src//iref[@item='Grammar' and @subitem=$prod and comment()='terminal production']">
|
---|
| 71 | <!-- known to be a terminal production -->
|
---|
| 72 | </xsl:when>
|
---|
| 73 | <xsl:when test="$prod!='' and $src//iref[@item='Grammar' and @subitem=$prod and comment()='exported production']">
|
---|
| 74 | <!-- known to be used in another part -->
|
---|
| 75 | </xsl:when>
|
---|
| 76 | <xsl:when test="$prod!='' and $src//iref[@item='Grammar' and @subitem=$prod and comment()='unused production']">
|
---|
| 77 | <!-- known to be unused, but we want to include it anyway -->
|
---|
| 78 | </xsl:when>
|
---|
[1893] | 79 | <xsl:when test="$prod!='' and $src//x:ref=$prod">
|
---|
| 80 | <!-- referenced otherwise -->
|
---|
| 81 | </xsl:when>
|
---|
[1773] | 82 | <xsl:otherwise>
|
---|
| 83 | <xsl:value-of select="."/>
|
---|
| 84 | <xsl:text> </xsl:text>
|
---|
| 85 | </xsl:otherwise>
|
---|
| 86 | </xsl:choose>
|
---|
| 87 | </xsl:for-each>
|
---|
| 88 | </xsl:variable>
|
---|
| 89 |
|
---|
| 90 | <xsl:if test="$diags!=''">
|
---|
| 91 | <figure>
|
---|
| 92 | <preamble>ABNF diagnostics:</preamble>
|
---|
| 93 | <artwork type="inline">
|
---|
[454] | 94 | <xsl:text> </xsl:text>
|
---|
[1773] | 95 | <xsl:value-of select="$diags"/>
|
---|
| 96 | </artwork></figure>
|
---|
| 97 | </xsl:if>
|
---|
| 98 | </section></xsl:variable>
|
---|
[420] | 99 |
|
---|
[1773] | 100 | <xsl:copy-of select="$generated"/>
|
---|
| 101 |
|
---|
[425] | 102 | <!-- check whether it's up-to-date... -->
|
---|
[454] | 103 | <xsl:variable name="src">
|
---|
| 104 | <xsl:for-each select="//section[@anchor='collected.abnf']//artwork">
|
---|
| 105 | <xsl:value-of select="."/>
|
---|
| 106 | </xsl:for-each>
|
---|
| 107 | </xsl:variable>
|
---|
[421] | 108 |
|
---|
[1777] | 109 | <xsl:variable name="new">
|
---|
| 110 | <xsl:value-of select="$generated//artwork"/>
|
---|
| 111 | </xsl:variable>
|
---|
| 112 |
|
---|
| 113 | <xsl:if test="not(//section[@anchor='collected.abnf']) or normalize-space($src) != normalize-space($new)">
|
---|
[421] | 114 | <xsl:message>WARNING: appendix contained inside source document needs to be updated</xsl:message>
|
---|
[1711] | 115 | <xsl:call-template name="showdiff">
|
---|
| 116 | <xsl:with-param name="actual" select="normalize-space($src)"/>
|
---|
[1777] | 117 | <xsl:with-param name="expected" select="normalize-space($new)"/>
|
---|
[1711] | 118 | </xsl:call-template>
|
---|
[421] | 119 | </xsl:if>
|
---|
[420] | 120 | </xsl:template>
|
---|
[428] | 121 |
|
---|
[1711] | 122 | <xsl:template name="showdiff">
|
---|
| 123 | <xsl:param name="actual"/>
|
---|
| 124 | <xsl:param name="expected"/>
|
---|
| 125 | <xsl:param name="prev"/>
|
---|
| 126 |
|
---|
| 127 | <xsl:choose>
|
---|
| 128 | <xsl:when test="$actual=''">
|
---|
| 129 | <!-- done -->
|
---|
| 130 | </xsl:when>
|
---|
| 131 | <xsl:when test="substring($actual,1,1)=substring($expected,1,1)">
|
---|
| 132 | <xsl:call-template name="showdiff">
|
---|
| 133 | <xsl:with-param name="actual" select="substring($actual,2)"/>
|
---|
| 134 | <xsl:with-param name="expected" select="substring($expected,2)"/>
|
---|
| 135 | <xsl:with-param name="prev" select="concat($prev,substring($actual,1,1))"/>
|
---|
| 136 | </xsl:call-template>
|
---|
| 137 | </xsl:when>
|
---|
| 138 | <xsl:otherwise>
|
---|
| 139 | <xsl:message>WARNING: at position <xsl:value-of select="string-length($prev)"/></xsl:message>
|
---|
| 140 | <xsl:message>WARNING: prefix <xsl:value-of select="$prev"/></xsl:message>
|
---|
| 141 | <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>
|
---|
| 142 | </xsl:otherwise>
|
---|
| 143 | </xsl:choose>
|
---|
| 144 | </xsl:template>
|
---|
| 145 |
|
---|
[428] | 146 | <xsl:function name="x:laststartchar">
|
---|
| 147 | <xsl:param name="lines"/>
|
---|
| 148 | <xsl:param name="position"/>
|
---|
[420] | 149 |
|
---|
[428] | 150 | <xsl:choose>
|
---|
| 151 | <xsl:when test="' '!=substring($lines[$position],1,1)">
|
---|
| 152 | <xsl:value-of select="substring($lines[$position],1,1)"/>
|
---|
| 153 | </xsl:when>
|
---|
| 154 | <xsl:otherwise>
|
---|
| 155 | <xsl:value-of select="x:laststartchar($lines, $position - 1)"/>
|
---|
| 156 | </xsl:otherwise>
|
---|
| 157 | </xsl:choose>
|
---|
| 158 | </xsl:function>
|
---|
[420] | 159 |
|
---|
[428] | 160 |
|
---|
[420] | 161 | </xsl:transform> |
---|