Skip to content

Commit

Permalink
Merge pull request #49 from ndw/pragma-options
Browse files Browse the repository at this point in the history
Pragma options
  • Loading branch information
ndw committed Jun 15, 2023
2 parents ee8064b + eac44a2 commit 3c58321
Show file tree
Hide file tree
Showing 9 changed files with 202 additions and 53 deletions.
8 changes: 4 additions & 4 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
sacksName=coffeesacks
sacksTitle=CoffeeSacks
sacksVersion=2.2.1
sacksVersion=2.2.3

filterName=coffeefilter
filterVersion=2.2.1
filterVersion=2.2.3

grinderName=coffeegrinder
grinderVersion=2.2.1
grinderVersion=2.2.2

xmlresolverVersion=5.2.0
docbookVersion=5.2CR5
xslTNGversion=2.1.2
xslTNGversion=2.1.6

saxonVersion=11.5
saxonGroup=net.sf.saxon
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ public int startAlternative(ForestNode tree, List<RuleChoice> ruleAlternatives)

value--; // convert back to 0-based index

// If we called an extension function to choose, assume the selection was unambiguous
madeAmbiguousChoice = false;

// Map the number back to the "right" number in the un-reordered list
return ((int) (value <= selected ? value - 1 : value));
}
Expand Down
22 changes: 21 additions & 1 deletion src/main/java/org/nineml/coffeesacks/CommonDefinition.java
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ protected void checkOptions(Map<String,String> options) throws XPathException {
Set<String> booleanOptions = new HashSet<>(Arrays.asList("ignoreTrailingWhitespace",
"allowUndefinedSymbols", "allowUnreachableSymbols", "allowUnproductiveSymobls",
"allowMultipleDefinitions", "showMarks", "showBnfNonterminals",
"suppressAmbiguousState", "suppressPrefixState"));
"suppressAmbiguousState", "suppressPrefixState", "strictAmbiguity"));

//Set<String> stringOptions = new HashSet<>(Collections.singletonList("parser"));

Expand Down Expand Up @@ -268,6 +268,9 @@ protected void checkOptions(Map<String,String> options) throws XPathException {
parserOptions.exposeState("prefix");
}
break;
case "strictAmbiguity":
parserOptions.setStrictAmbiguity(bool);
break;
case "allowUndefinedSymbols":
parserOptions.setAllowUndefinedSymbols(bool);
break;
Expand All @@ -290,6 +293,11 @@ protected void checkOptions(Map<String,String> options) throws XPathException {
parserOptions.setParserType(value);
break;

case "enablePragmas":
case "disablePragmas":
// See below
break;

case "format":
Set<String> formats = new HashSet<>(Arrays.asList("xml", "json", "json-data", "json-tree", "json-text"));
if (!formats.contains(value)) {
Expand All @@ -310,6 +318,18 @@ protected void checkOptions(Map<String,String> options) throws XPathException {
parserOptions.getLogger().warn(logcategory, "Ignoring unexpected option: %s", key);
}
}

// Disable first, then enable
if (options.containsKey("disablePragmas")) {
for (String name : options.get("disablePragmas").split(",")) {
parserOptions.disablePragma(name.trim());
}
}
if (options.containsKey("enablePragmas")) {
for (String name : options.get("enablePragmas").split(",")) {
parserOptions.enablePragma(name.trim());
}
}
}

protected Item jsonToXDM(Processor processor, String json) throws SaxonApiException {
Expand Down
17 changes: 16 additions & 1 deletion src/test/java/org/nineml/coffeesacks/StylesheetTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,20 @@ public void hygieneOutputReportJson() {
public void resolveAmbiguityFunctionXml() {
XdmNode stylesheet = loadStylesheet("src/test/resources/ambiguity-xml.xsl");
XdmNode result = transform(stylesheet, stylesheet);
Assert.assertEquals("<doc><s xmlns:ixml=\"http://invisiblexml.org/NS\" ixml:state=\"ambiguous\"><n>123</n></s></doc>", serialize(result));
Assert.assertEquals("<doc><s><n>123</n></s></doc>", serialize(result));
}

@Test
public void resolveAmbiguityFunctionRootXml() {
XdmNode stylesheet = loadStylesheet("src/test/resources/root-ambiguity-xml.xsl");
XdmNode result = transform(stylesheet, stylesheet);
Assert.assertEquals("<doc><s><b>a</b></s></doc>", serialize(result));
}

@Test
public void resolveAmbiguityStrictFunctionRootXml() {
XdmNode stylesheet = loadStylesheet("src/test/resources/root-ambiguity-strict-xml.xsl");
XdmNode result = transform(stylesheet, stylesheet);
Assert.assertEquals("<doc><s xmlns:ixml=\"http://invisiblexml.org/NS\" ixml:state=\"ambiguous\"><b>a</b></s></doc>", serialize(result));
}

Expand All @@ -122,6 +129,13 @@ public void resolveAmbiguityFunctionNumbers() {
public void resolveAmbiguityFunctionJson() {
XdmNode stylesheet = loadStylesheet("src/test/resources/ambiguity-json.xsl");
XdmNode result = transform(stylesheet, stylesheet);
Assert.assertEquals("<doc>{ \"s\":{ \"n\":123 } }</doc>", serialize(result));
}

@Test
public void resolveAmbiguityStrictFunctionJson() {
XdmNode stylesheet = loadStylesheet("src/test/resources/ambiguity-strict-json.xsl");
XdmNode result = transform(stylesheet, stylesheet);
Assert.assertEquals("<doc>{ \"s\":{ \"ixml:state\":\"ambiguous\", \"n\":123 } }</doc>", serialize(result));
}

Expand All @@ -130,5 +144,6 @@ public void alternativeChoice01() {
XdmNode stylesheet = loadStylesheet("src/test/resources/alt-01.xsl");
XdmNode result = transform(stylesheet, stylesheet);
Assert.assertTrue(serialize(result).contains("<B>"));
Assert.assertFalse(serialize(result).contains("ambiguous"));
}
}
33 changes: 33 additions & 0 deletions src/test/resources/ambiguity-strict-json.xsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:cs="http://nineml.com/ns/coffeesacks"
xmlns:f="http://coffeesacks.nineml.com/fn/testing"
xmlns:map="http://www.w3.org/2005/xpath-functions/map"
exclude-result-prefixes="#all"
version="3.0">

<xsl:output method="xml" encoding="utf-8" indent="no"/>

<xsl:mode on-no-match="shallow-copy"/>

<xsl:template match="/">
<xsl:variable name="grammar" select="'s: n+ . n: [''0''-''9'']+ .'"/>
<xsl:variable name="parser"
select="cs:make-parser($grammar, map{'choose-alternative': f:choose#1,
'strictAmbiguity': true(),
'format': 'json'})"/>
<doc>
<xsl:sequence select="serialize($parser('123'), map{'method':'json','indent':true()})"/>
</doc>
</xsl:template>

<xsl:function name="f:choose" as="xs:integer">
<xsl:param name="alternatives" as="element()+"/>
<!-- select the alternative that contains only a single 'n' -->
<!--<xsl:message select="$alternatives[1]!serialize(.,map{'method':'xml','indent':true()})"/>-->
<xsl:sequence
select="$alternatives[count(.//n) = 1 and .//n/@from = 0 and .//n/@to = 3]/@alternative/data()"/>
</xsl:function>

</xsl:stylesheet>
2 changes: 2 additions & 0 deletions src/test/resources/numbers.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
<xsl:function name="f:choose" as="xs:integer">
<xsl:param name="alternatives" as="element()+"/>
<!-- select the alternative that contains 'decimal' -->
<!--
<xsl:message select="$alternatives/root()!serialize(.,map{'method':'xml','indent':true()})"/>
-->
<xsl:sequence select="$alternatives[decimal]/@alternative"/>
</xsl:function>

Expand Down
31 changes: 31 additions & 0 deletions src/test/resources/root-ambiguity-strict-xml.xsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:cs="http://nineml.com/ns/coffeesacks"
xmlns:f="http://coffeesacks.nineml.com/fn/testing"
xmlns:map="http://www.w3.org/2005/xpath-functions/map"
exclude-result-prefixes="#all"
version="3.0">

<xsl:output method="xml" encoding="utf-8" indent="no"/>

<xsl:mode on-no-match="shallow-copy"/>

<xsl:template match="/">
<xsl:variable name="grammar" select="'s = a | b. a = ''a''. b = ''a''.'"/>
<xsl:variable name="parser" select="cs:make-parser($grammar,
map{'strictAmbiguity': true(),
'choose-alternative': f:choose#1})"/>
<doc>
<xsl:sequence select="$parser('a')"/>
</doc>
</xsl:template>

<xsl:function name="f:choose" as="xs:integer">
<xsl:param name="alternatives" as="element()+"/>
<!-- select the alternative that contains only a single 'n' -->
<!--<xsl:message select="$alternatives/root()!serialize(.,map{'method':'xml','indent':true()})"/>-->
<xsl:sequence select="$alternatives[b]/@alternative"/>
</xsl:function>

</xsl:stylesheet>
26 changes: 26 additions & 0 deletions src/website/xml/changelog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,32 @@
<title>Change log</title>

<revhistory>
<revision>
<revnumber>2.2.3</revnumber>
<date>2023-06-15</date>
<revdescription>
<itemizedlist>
<listitem>
<para>If a user-supplied function is called to choose an alternative, assume an
<emphasis>unambiguous</emphasis> choice has always been made.
Support the <literal>strictAmbiguity</literal> option to override this behavior.
</para>
</listitem>
<listitem>
<para>Support the <code>disablePragmas</code> and <code>enablePragmas</code> options
to selectively disable or enable individual pragma types.
</para>
</listitem>
</itemizedlist>
</revdescription>
</revision>
<revision>
<revnumber>2.2.2</revnumber>
<date>2023-06-15</date>
<revdescription>
<para>Never released.</para>
</revdescription>
</revision>
<revision>
<revnumber>2.2.1</revnumber>
<date>2023-05-17</date>
Expand Down
Loading

0 comments on commit 3c58321

Please sign in to comment.