Skip to content

Commit

Permalink
[MOD] parse user-defined functions, re #21 - test failing and ignored…
Browse files Browse the repository at this point in the history
… prior to merge into main
  • Loading branch information
AndrewSales committed Jan 9, 2024
1 parent 4e78ed7 commit 7b8766a
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 4 deletions.
9 changes: 6 additions & 3 deletions evaluate.xqm
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@ import module namespace output = 'http://www.andrewsales.com/ns/xqs-output' at
import module namespace utils = 'http://www.andrewsales.com/ns/xqs-utils' at
'utils.xqm';

declare namespace xqy = 'http://www.w3.org/2012/xquery';
declare namespace sch = "http://purl.oclc.org/dsdl/schematron";
declare namespace svrl = "http://purl.oclc.org/dsdl/svrl";

(:~ Evaluates the schema to produce SVRL output.
(:~ Evaluates the schema to produce SVRL output, applying the processing options
: specified.
: @param instance the document instance
: @param schema the Schematron schema
: @param phase the active phase
: @param options map of options
: @param options map of processing options
:)
declare function eval:schema(
$instance as node(),
Expand All @@ -34,7 +36,8 @@ declare function eval:schema(
{$schema/@schemaVersion}
{output:namespace-decls-as-svrl($schema/sch:ns)}
<svrl:active-pattern name='XQS Syntax Error Summary' documents='{$schema/base-uri()}'/>
{for $phase in ($schema/sch:phase/@id/data(), '')
{$schema/xqy:function ! utils:parse-function(., $options)[self::svrl:*]}
{for $phase in ($schema/sch:phase/@id, '')
let $context as map(*) := context:get-context($instance, $schema, $phase, $options)
return eval:phase($context)}
</svrl:schematron-output>
Expand Down
60 changes: 59 additions & 1 deletion test/test-evaluate.xqm
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module namespace _ = 'http://www.andrewsales.com/ns/xqs-evaluation-tests';

declare namespace sch = "http://purl.oclc.org/dsdl/schematron";
declare namespace svrl = "http://purl.oclc.org/dsdl/svrl";
declare namespace xqy = 'http://www.w3.org/2012/xquery';

import module namespace eval = 'http://www.andrewsales.com/ns/xqs-evaluate'
at '../evaluate.xqm';
Expand Down Expand Up @@ -1513,9 +1514,66 @@ declare %unit:test function _:value-of-select-syntax-error()
)
};

declare %unit:test function _:phase-variable-scope-error()
{
let $result :=
eval:schema(
document{<root/>},
<sch:schema>
<sch:phase id='one'>
<sch:let name='foo' value='bar'/>
<sch:active pattern='a'/>
</sch:phase>
<sch:phase id='two'>
<sch:active pattern='a'/>
</sch:phase>
<sch:pattern id='a'>
<sch:rule context="*">
<sch:report test="."><sch:value-of select='$foo'/></sch:report>
</sch:rule>
</sch:pattern>
</sch:schema>,
'two',
map{'dry-run':'true'}
)
return
(
unit:assert-equals(
($result/svrl:failed-assert)[1]/svrl:text,
<svrl:text>Undeclared variable: $foo. @select='$foo'</svrl:text>
)
)
};

(:TODO:)
declare %unit:ignore function _:function-syntax-error()
{
let $result :=
eval:schema(
document{<root/>},
<sch:schema>
<xqy:function></xqy:function>
<sch:pattern id='a'>
<sch:rule context="*">
<sch:report test="."><sch:value-of select='foo'/></sch:report>
</sch:rule>
</sch:pattern>
</sch:schema>,
'',
map{'dry-run':'true'}
)
return
(
unit:assert-equals(
($result/svrl:failed-assert)[1]/svrl:text,
<svrl:text>Calculation is incomplete. xqy:function='declare function local:foo(){{**}};'</svrl:text>
)
)
};

(:TODO
pattern/@documents
diagnostics
properties
rule/let
functions
:)
19 changes: 19 additions & 0 deletions utils.xqm
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ declare namespace xqs = 'http://www.andrewsales.com/ns/xqs';
declare namespace sch = "http://purl.oclc.org/dsdl/schematron";
declare namespace svrl = "http://purl.oclc.org/dsdl/svrl";
declare namespace map = "http://www.w3.org/2005/xpath-functions/map";
declare namespace xqy = 'http://www.w3.org/2012/xquery';

(:~ Builds the string of variable declarations in the prolog, for initial
: evaluation.
Expand Down Expand Up @@ -187,4 +188,22 @@ declare function utils:eval(
<svrl:text>{$err:description}{' @'||$node/name()}='{$node/data()}'</svrl:text></svrl:failed-assert>
})
else xquery:eval($query, $bindings, map{'pass':'true'})
};

declare function utils:parse-function(
$node as element(xqy:function),
$options as map(*)
)
as element()+
{
<svrl:fired-rule context='{$node/path()}'/>,
try{
xquery:parse($node || 0, map{'pass':'true'})
}
catch * {
<svrl:failed-assert err:code='{$err:code}' location='{$node/path()}'
test='xquery:parse(.)'>
<svrl:text>{$err:description}{' '||$node/name()}='{$node/data()}'</svrl:text>
</svrl:failed-assert>
}
};

0 comments on commit 7b8766a

Please sign in to comment.