Skip to content

Commit

Permalink
[MOD] better reporting for rule/let, re #21
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewSales committed Aug 8, 2023
1 parent 1695398 commit 4e78ed7
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 6 deletions.
8 changes: 8 additions & 0 deletions evaluate.xqm
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,13 @@ declare function eval:rule(
as element()*
{
let $_ := utils:check-duplicate-variable-names($rule/sch:let)
let $variable-errors := utils:evaluate-rule-variables(
$rule/sch:let,
$prolog,
map:merge((map{'':$context?instance}, $context?globals)),
$context,
()
)
let $query := string-join(
($prolog, utils:local-variable-decls($rule/sch:let),
if($rule/sch:let) then 'return ' else '', $rule/@context),
Expand All @@ -203,6 +210,7 @@ as element()*
if($context?dry-run eq 'true')
then
(
$variable-errors[self::svrl:*],
$rule-context[self::svrl:*],
eval:assertions($rule, $prolog, <_/>, $context) (:pass dummy context node:)
)
Expand Down
19 changes: 13 additions & 6 deletions test/test-evaluate.xqm
Original file line number Diff line number Diff line change
Expand Up @@ -1399,7 +1399,7 @@ declare %unit:test function _:dry-run-all-rules-processed()
)
};

declare %unit:ignore function _:rule-variable-syntax-error()
declare %unit:test function _:rule-variable-syntax-error()
{
let $result :=
eval:schema(
Expand All @@ -1408,6 +1408,7 @@ declare %unit:ignore function _:rule-variable-syntax-error()
<sch:pattern>
<sch:rule context="*">
<sch:let name='bar' value=''/>
<sch:assert test='foo'/>
</sch:rule>
<sch:rule context='*'></sch:rule>
</sch:pattern>
Expand All @@ -1418,13 +1419,12 @@ declare %unit:ignore function _:rule-variable-syntax-error()
return
(
unit:assert-equals(
count($result/svrl:failed-assert[ends-with(@location, '/Q{http://purl.oclc.org/dsdl/schematron}pattern[1]/Q{http://purl.oclc.org/dsdl/schematron}rule[1]/@context')]),
count($result/svrl:failed-assert[ends-with(@location, '/Q{http://purl.oclc.org/dsdl/schematron}pattern[1]/Q{http://purl.oclc.org/dsdl/schematron}rule[1]/Q{http://purl.oclc.org/dsdl/schematron}let[1]/@value')]),
1
),
unit:assert-equals(
$result/svrl:failed-assert[ends-with(@location, '/Q{http://purl.oclc.org/dsdl/schematron}pattern[1]/Q{http://purl.oclc.org/dsdl/schematron}rule[1]/@context')]
/svrl:text,
<svrl:text>TODO</svrl:text>
$result/svrl:failed-assert[ends-with(@location, '/Q{http://purl.oclc.org/dsdl/schematron}pattern[1]/Q{http://purl.oclc.org/dsdl/schematron}rule[1]/Q{http://purl.oclc.org/dsdl/schematron}let[1]/@value')]/svrl:text,
<svrl:text>Incomplete FLWOR expression, expecting 'return'. @value=''</svrl:text>
)
)
};
Expand Down Expand Up @@ -1511,4 +1511,11 @@ declare %unit:test function _:value-of-select-syntax-error()
<svrl:text>Unexpected end of query: '.'. @select='...'</svrl:text>
)
)
};
};

(:TODO
pattern/@documents
diagnostics
properties
rule/let
:)
36 changes: 36 additions & 0 deletions utils.xqm
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,42 @@ declare function utils:check-duplicate-variable-names($decls as element(sch:let)
) else()
};

(:~ In dry-run mode only, evaluate rule variables.
: Provides more localized information if syntax errors are present in rule
: variable declarations.
:)
declare function utils:evaluate-rule-variables(
$variables as element(sch:let)*,
$prolog as xs:string?,
$bindings as map(*),
$context as map(*),
$errors as element()*
)
as element()*
{
if($context?dry-run eq 'true')
then
if(exists($variables))
then
let $var := head($variables)
let $prolog := $prolog || utils:local-variable-decls($var)
let $errs := utils:eval(
$prolog || ' return $' || $var/@name => utils:escape(),
$bindings,
map{'dry-run':$context?dry-run},
$var/@value
)
return utils:evaluate-rule-variables(
tail($variables),
$prolog,
$bindings,
$context,
($errors,$errs)
)
else $errors
else ()
};

(:~ Wrapper around xquery:eval(). In "dry-run" mode, the query passed in is
: parsed only, and any errors caught reported as svrl:failed-assert.
: @param $query string of the query to evaluate
Expand Down

0 comments on commit 4e78ed7

Please sign in to comment.