Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix] unary lookups #3966

Merged
merged 5 commits into from
Aug 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions exist-core/src/main/antlr/org/exist/xquery/parser/XQuery.g
Original file line number Diff line number Diff line change
Expand Up @@ -1326,6 +1326,12 @@ lookup throws XPathException
)
;

unaryLookup throws XPathException
:
l:lookup
{ #unaryLookup= #(#[PARENTHESIZED, "Parenthesized"], #l); }
;

dynamicFunCall throws XPathException
:
args:argumentList
Expand Down Expand Up @@ -1369,7 +1375,7 @@ primaryExpr throws XPathException
|
( eqName LPAREN ) => functionCall
|
( QUESTION ) => lookup
( QUESTION ) => unaryLookup
|
( STRING_CONSTRUCTOR_START ) => stringConstructor
|
Expand Down Expand Up @@ -1535,7 +1541,9 @@ argumentList throws XPathException

argument throws XPathException
:
argumentPlaceholder | exprSingle
(QUESTION! ( NCNAME | INTEGER_LITERAL | LPAREN | STAR )) => lookup
| argumentPlaceholder
| exprSingle
;

argumentPlaceholder throws XPathException : QUESTION ;
Expand Down
5 changes: 4 additions & 1 deletion exist-core/src/main/java/org/exist/xquery/Lookup.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,10 @@ public Sequence eval(Sequence contextSequence, Item contextItem) throws XPathExc
contextSequence = contextItem.toSequence();
}
Sequence leftSeq;
if (contextExpression == null) {
if (contextExpression == null && contextSequence == null) {
throw new XPathException(this, ErrorCodes.XPDY0002,
"Lookup has nothing to select, the context item is absent");
} else if (contextExpression == null) {
leftSeq = contextSequence;
} else {
leftSeq = contextExpression.eval(contextSequence);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public void dump(final ExpressionDumper dumper) {
dumper.display("map {");
for (final Mapping mapping : this.mappings) {
mapping.key.dump(dumper);
dumper.display(" := ");
dumper.display(" : ");
mapping.value.dump(dumper);
}
dumper.display("}");
Expand Down
36 changes: 28 additions & 8 deletions exist-core/src/test/xquery/maps/mapsLookup.xql
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,39 @@ function mlt:wildcard_lookup_on_map() {
declare
%test:assertEquals("Tom", "Dick", "Harry")
function mlt:postfix_lookup_on_maps() {
(map {"first": "Tom"}, map {"first": "Dick"}, map {"first": "Harry"})?first
(
map {"first": "Tom"},
map {"first": "Dick"},
map {"first": "Harry"}
)?first
};

declare
%test:assertEquals( "null", "null")
function mlt:null_lookup(){
let $serializationParams := <output:serialization-parameters> <output:method>json</output:method> </output:serialization-parameters>
%test:assertEquals("null", "null")
function mlt:null_lookup() {
let $serializationParams :=
<output:serialization-parameters>
<output:method>json</output:method>
</output:serialization-parameters>

let $json1 := parse-json( '{"total":[{"data":null}]}' )
let $json1 := parse-json('{"total":[{"data":null}]}')
let $test1 := $json1?total?*?foobar

let $json2 := parse-json( '{"total":[{"data":null}]}' )
let $json2 := parse-json('{"total":[{"data":null}]}')
let $test2 := $json2?total?*?foobar?aaa

return ( serialize($test1, $serializationParams) , serialize($test2, $serializationParams) )
};
return (
serialize($test1, $serializationParams),
serialize($test2, $serializationParams)
)
};

declare
%test:assertEquals("err:XPDY0002", "1", "1")
function mlt:wildcard-lookup-without-context () {
try {
util:eval("?noctx", false(), (), true())
} catch * {
xs:string($err:code), $err:line-number, $err:column-number
}
};
21 changes: 16 additions & 5 deletions exist-core/src/test/xquery/xquery3/bang.xql
Original file line number Diff line number Diff line change
Expand Up @@ -272,20 +272,31 @@ function bang:mixed-function-types-call-parenthesized-context-item () {
return (function ($a) {1}, $id, sum#1, [1], map{1:1}) ! (.)(1)
};

(:~
: parse error needs to be fixed first
: https://github.com/eXist-db/exist/issues/1655

(: https://github.com/eXist-db/exist/issues/1655 :)
declare
%test:assertEquals(1,2,3)
function bang:array-lookup-implicit-context () {
([1], [2], [3]) ! ?1
};

(: https://github.com/eXist-db/exist/issues/1655 :)
declare
%test:assertEquals(1,2,3)
function bang:map-lookup-implicit-context () {
(map {'a':1}, map {'a':2}, map {'a':3}) ! ?a
};

:)
(: https://github.com/eXist-db/exist/issues/3491 :)
declare
%test:assertEquals(3,4,5)
function bang:map-lookup-implicit-context-as-argument () {
(map {'a': (1,2)}, map {'a': (2,2)}, map {'a':(2,3)}) ! sum(?a)
};

(: https://github.com/eXist-db/exist/issues/3491 :)
declare
%test:assertEquals("1|2|3", "a|b|c")
function bang:array-to-sequence-as-parameter () {
([1,2,3], ['a', 'b', 'c'])
! string-join(?*, '|')
};