/************************************************************************
[23] FunctionDecl ::= <"declare" "function"> <QName "("> ParamList? (")" | (<")" "as"> SequenceType)) (EnclosedExpr | "external")
[26] EnclosedExpr ::= "{" Expr "}"
************************************************************************/
final public void parseFunctionDecl() throws ParseException, XQueryException {
final UserFunction func;
final QualifiedName funcName;
List<ParametricVariable> paramList = Collections.<ParametricVariable> emptyList();
Type returnType = Untyped.UNTYPED;
final XQExpression funcBody;
currentModule.pushVarScope();
currentToken = jj_consume_token(DefineFunction);
currentToken = jj_consume_token(QNameLpar);
funcName = QNameUtil.parse(currentToken.image.substring(0, currentToken.image.length() - 1).trim(), namespaceContext, staticContext.getDefaultFunctionNamespace());
switch(jj_nt.kind) {
case VariableIndicator:
paramList = parseParamList();
break;
default:
jj_la1[24] = jj_gen;
;
}
switch(jj_nt.kind) {
case Rpar:
currentToken = jj_consume_token(Rpar);
break;
case RparAs:
currentToken = jj_consume_token(RparAs);
returnType = parseSequenceType();
break;
default:
jj_la1[25] = jj_gen;
jj_consume_token(-1);
throw new ParseException();
}
final String nsuri = funcName.getNamespaceURI();
if(isLibraryModule) {
final String tn = currentModule.getNamespace();
if(tn == null) { // sanity check
error("err:XQST0046");
}
if(tn.equals(nsuri) == false) {
error("err:XQST0048");
}
} else {
UserFunction.checkAsPredefinedFunction(nsuri);
}
func = ExpressionFactory.createUserFunction(currentModule, funcName, paramList, returnType);
switch(jj_nt.kind) {
case LbraceExprEnclosure:
currentToken = jj_consume_token(LbraceExprEnclosure);
funcBody = parseExpr();
func.setBodyExpression(funcBody);
currentToken = jj_consume_token(Rbrace);
break;
case External:
currentToken = jj_consume_token(External);
func.setExternal(true);
break;
default:
jj_la1[26] = jj_gen;
jj_consume_token(-1);
throw new ParseException();