Skip to content

Commit

Permalink
[fix] raise static error for absolute XPaths in functions
Browse files Browse the repository at this point in the history
The Xquery parser will throw a static error XPDY0002
when an absolute XPath is used inside of a function body without a document context.

Examples:
- `function () { //a }`
- `function () { /a[foo] }`

Still allowed:
```
function ($docs as document()*) { $docs ! //a }
```
  • Loading branch information
line-o authored and adamretter committed Dec 1, 2020
1 parent 94b69cf commit 35249a7
Showing 1 changed file with 43 additions and 8 deletions.
51 changes: 43 additions & 8 deletions exist-core/src/main/antlr/org/exist/xquery/parser/XQuery.g
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,11 @@ functionBody throws XPathException
:
( LCURLY RCURLY ) => l:LCURLY^ RCURLY!
{ #functionBody= #(#l, #(#[PARENTHESIZED, "Parenthesized"], null)); }
| LCURLY^ expr RCURLY!
| LCURLY^
{ lexer.inFunctionBody = true; }
expr
{ lexer.inFunctionBody = false; }
RCURLY!
;
returnType throws XPathException:
Expand Down Expand Up @@ -1059,8 +1063,20 @@ unaryExpr throws XPathException
;
valueExpr throws XPathException
{ Boolean inFunctionBodyState = lexer.inFunctionBody; }
:
pathExpr (BANG^ pathExpr)*
pathExpr (
BANG^
{
// simple map operator might add new document context
lexer.inFunctionBody = false;
}
pathExpr
{
// reset state
lexer.inFunctionBody = inFunctionBodyState;
}
)*
|
extensionExpr
;
Expand Down Expand Up @@ -1111,15 +1127,33 @@ pathExpr throws XPathException
relativePathExpr
|
( SLASH relativePathExpr )
=> SLASH relPath:relativePathExpr
{ #pathExpr= #(#[ABSOLUTE_SLASH, "AbsoluteSlash"], #relPath); }
=> s1:SLASH relPath:relativePathExpr
{
if (lexer.inFunctionBody) {
throw new XPathException(#s1.getLine(), #s1.getColumn(), ErrorCodes.XPDY0002,
"Leading '/' selects nothing, ContextItem is absent in function body");
}
#pathExpr= #(#[ABSOLUTE_SLASH, "AbsoluteSlash"], #relPath);
}
// lone slash
|
SLASH
{ #pathExpr= #[ABSOLUTE_SLASH, "AbsoluteSlash"]; }
s2:SLASH
{
if (lexer.inFunctionBody) {
throw new XPathException(#s2.getLine(), #s2.getColumn(), ErrorCodes.XPDY0002,
"Leading '/' selects nothing, ContextItem is absent in function body");
}
#pathExpr= #[ABSOLUTE_SLASH, "AbsoluteSlash"];
}
|
DSLASH relPath2:relativePathExpr
{ #pathExpr= #(#[ABSOLUTE_DSLASH, "AbsoluteSlashSlash"], #relPath2); }
ds:DSLASH relPath2:relativePathExpr
{
if (lexer.inFunctionBody) {
throw new XPathException(#ds.getLine(), #ds.getColumn(), ErrorCodes.XPDY0002,
"Leading '//' selects nothing, ContextItem is absent in function body");
}
#pathExpr= #(#[ABSOLUTE_DSLASH, "AbsoluteSlashSlash"], #relPath2);
}
;
relativePathExpr throws XPathException
Expand Down Expand Up @@ -2188,6 +2222,7 @@ options {
protected boolean inStringConstructor = false;
protected boolean inElementContent= false;
protected boolean inAttributeContent= false;
protected boolean inFunctionBody= false;
protected char attrDelimChar = '"';
protected boolean inComment= false;
protected boolean inPragma = false;
Expand Down

0 comments on commit 35249a7

Please sign in to comment.