Skip to content

Commit

Permalink
Implemented array postfix and unary lookups.
Browse files Browse the repository at this point in the history
  • Loading branch information
david-waltermire committed May 25, 2024
1 parent b83d36f commit 7af34c0
Show file tree
Hide file tree
Showing 24 changed files with 733 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public String getMessage() {
*
* @return the error code value
*/
protected int getCode() {
public int getCode() {
return code;
}

Expand All @@ -106,7 +106,7 @@ protected int getCode() {
*
* @return the error code family
*/
protected abstract String getCodePrefix();
public abstract String getCodePrefix();

/**
* Get a combination of the error code family and value.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public DynamicMetapathException(int code, Throwable cause) {
}

@Override
protected String getCodePrefix() {
public String getCodePrefix() {
return "MPDY";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public StaticMetapathException(int code, Throwable cause) {
}

@Override
protected String getCodePrefix() {
public String getCodePrefix() {
return "MPST";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public TypeMetapathException(int code, Throwable cause) {
}

@Override
protected String getCodePrefix() {
public String getCodePrefix() {
return "MPTY";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,10 @@
import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.GeneralcompContext;
import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.IfexprContext;
import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.IntersectexceptexprContext;
import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.KeyspecifierContext;
import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.LetexprContext;
import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.LiteralContext;
import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.LookupContext;
import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.MetapathContext;
import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.MultiplicativeexprContext;
import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.NametestContext;
Expand All @@ -78,6 +80,7 @@
import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.StepexprContext;
import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.StringconcatexprContext;
import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.UnaryexprContext;
import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.UnarylookupContext;
import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.UnionexprContext;
import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.ValuecompContext;
import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.ValueexprContext;
Expand Down Expand Up @@ -358,10 +361,34 @@ public R visitPostfixexpr(PostfixexprContext ctx) {
return handle(ctx, (context) -> handlePostfixexpr(ctx));
}

/**
* Handle the provided expression.
*
* @param ctx
* the provided expression context
* @return the result
*/
protected abstract R handlePredicate(@NonNull PredicateContext ctx);

@Override
public R visitPredicate(PredicateContext ctx) {
// should never be called, since this is handled by the parent expression
throw new IllegalStateException();
assert ctx != null;
return handlePredicate(ctx);
}

/**
* Handle the provided expression.
*
* @param ctx
* the provided expression context
* @return the result
*/
protected abstract R handleLookup(@NonNull LookupContext ctx);

@Override
public R visitLookup(LookupContext ctx) {
assert ctx != null;
return handleLookup(ctx);
}

/*
Expand Down Expand Up @@ -880,6 +907,27 @@ public R visitCurlyarrayconstructor(CurlyarrayconstructorContext ctx) {
return handleArrayConstructor(ctx);
}

@Override
public R visitKeyspecifier(KeyspecifierContext ctx) {
// should never be called, since this is handled by the parent expression
throw new IllegalStateException();
}

/**
* Handle the provided expression.
*
* @param ctx
* the provided expression context
* @return the result
*/
protected abstract R handleUnarylookup(@NonNull UnarylookupContext ctx);

@Override
public R visitUnarylookup(UnarylookupContext ctx) {
assert ctx != null;
return handleUnarylookup(ctx);
}

// =========================================================================
// Conditional Expressions - https://www.w3.org/TR/xpath-31/#id-conditionals
// =========================================================================
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,12 +295,11 @@ protected <CONTEXT extends ParserRuleContext> IExpression handleGroupedNAiry(
IExpression retval = null;
if (numChildren > 0) {
ParseTree leftTree = context.getChild(startingIndex);
IExpression result = ObjectUtils.notNull(leftTree.accept(this));
retval = ObjectUtils.notNull(leftTree.accept(this));

for (int i = startingIndex + 1; i < numChildren; i = i + step) {
result = parser.apply(context, i, result);
retval = parser.apply(context, i, retval);
}
retval = result;
}
return retval;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -362,4 +362,19 @@ public RESULT visitArray(ArraySequence expr, CONTEXT context) {
public RESULT visitArray(ArraySquare expr, CONTEXT context) {
return visitChildren(expr, context);
}

@Override
public RESULT visitPostfixLookup(PostfixLookup expr, CONTEXT context) {
return visitChildren(expr, context);
}

@Override
public RESULT visitFunctionCallAccessor(FunctionCallAccessor expr, CONTEXT context) {
return visitChildren(expr, context);
}

@Override
public RESULT visitUnaryLookup(UnaryLookup expr, CONTEXT context) {
return defaultResult();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
/*
* Portions of this software was developed by employees of the National Institute
* of Standards and Technology (NIST), an agency of the Federal Government and is
* being made available as a public service. Pursuant to title 17 United States
* Code Section 105, works of NIST employees are not subject to copyright
* protection in the United States. This software may be subject to foreign
* copyright. Permission in the United States and in foreign countries, to the
* extent that NIST may hold copyright, to use, copy, modify, create derivative
* works, and distribute this software and its documentation without fee is hereby
* granted on a non-exclusive basis, provided that this notice and disclaimer
* of warranty appears in all copies.
*
* THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER
* EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY
* THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM
* INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE
* SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT
* SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT,
* INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM,
* OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY,
* CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR
* PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT
* OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER.
*/

package gov.nist.secauto.metaschema.core.metapath.cst;

import gov.nist.secauto.metaschema.core.metapath.DynamicContext;
import gov.nist.secauto.metaschema.core.metapath.ISequence;
import gov.nist.secauto.metaschema.core.metapath.InvalidTypeMetapathException;
import gov.nist.secauto.metaschema.core.metapath.function.library.FnData;
import gov.nist.secauto.metaschema.core.metapath.item.IItem;
import gov.nist.secauto.metaschema.core.metapath.item.atomic.IAnyAtomicItem;
import gov.nist.secauto.metaschema.core.metapath.item.atomic.IIntegerItem;
import gov.nist.secauto.metaschema.core.metapath.item.function.ArrayException;
import gov.nist.secauto.metaschema.core.metapath.item.function.IArrayItem;

import java.util.stream.Stream;

import edu.umd.cs.findbugs.annotations.NonNull;

public abstract class AbstractLookup implements IExpression {
@NonNull
private final IKeySpecifier keySpecifier;

protected AbstractLookup(@NonNull IKeySpecifier keySpecifier) {
this.keySpecifier = keySpecifier;
}

@NonNull
public IKeySpecifier getKeySpecifier() {
return keySpecifier;
}

protected interface IKeySpecifier {

default Stream<? extends IItem> lookup(
@NonNull IItem item,
@NonNull DynamicContext dynamicContext,
@NonNull ISequence<?> focus) {
Stream<? extends IItem> result;
if (item instanceof IArrayItem) {
result = lookupInArray((IArrayItem<?>) item, dynamicContext, focus);
} else {
throw new InvalidTypeMetapathException(item,
String.format("Item type '%s' is not an array or map.", item.getClass().getName()));
}
return result;
}

Stream<? extends IItem> lookupInArray(
@NonNull IArrayItem<?> item,
@NonNull DynamicContext dynamicContext,
@NonNull ISequence<?> focus);
}

protected static class NCNameKeySpecifier implements IKeySpecifier {
@NonNull
private final String name;

public NCNameKeySpecifier(String name) {
this.name = name;
}

public String getName() {
return name;
}

@Override
public Stream<? extends IItem> lookupInArray(
IArrayItem<?> item,
DynamicContext dynamicContext,
ISequence<?> focus) {
throw new InvalidTypeMetapathException(item,
String.format("The key name-based lookup '%s' is not appropriate for an array.", getName()));
}
}

protected static class IntegerLiteralKeySpecifier implements IKeySpecifier {
private final int index;

public IntegerLiteralKeySpecifier(IIntegerItem literal) {
index = literal.asInteger().intValueExact() - 1;
}

@Override
public Stream<? extends IItem> lookupInArray(
IArrayItem<?> item,
DynamicContext dynamicContext,
ISequence<?> focus) {
try {
return Stream.of(item.get(index));
} catch (IndexOutOfBoundsException ex) {
throw new ArrayException(
ArrayException.INDEX_OUT_OF_BOUNDS,
String.format("The index %d is outside the range of values for the array size '%d'.",
index + 1,
item.size()),
ex);
}
}
}

protected static class WildcardKeySpecifier implements IKeySpecifier {

@Override
public Stream<? extends IItem> lookupInArray(
IArrayItem<?> item,
DynamicContext dynamicContext,
ISequence<?> focus) {
return item.stream();
}
}

public static class ParenthesizedExprKeySpecifier implements IKeySpecifier {
@NonNull
private final IExpression keyExpression;

public ParenthesizedExprKeySpecifier(@NonNull IExpression keyExpression) {
this.keyExpression = keyExpression;
}

public IExpression getKeyExpression() {
return keyExpression;
}

@Override
public Stream<? extends IItem> lookupInArray(
IArrayItem<?> item,
DynamicContext dynamicContext,
ISequence<?> focus) {
ISequence<IAnyAtomicItem> keys = FnData.fnData(getKeyExpression().accept(dynamicContext, focus));

return keys.stream()
.map(key -> {
if (key instanceof IIntegerItem) {
int index = ((IIntegerItem) key).asInteger().intValueExact() - 1;
try {
return item.get(index);
} catch (IndexOutOfBoundsException ex) {
throw new ArrayException(
ArrayException.INDEX_OUT_OF_BOUNDS,
String.format("The index %d is outside the range of values for the array size '%d'.",
index + 1,
item.size()),
ex);
}
}
throw new InvalidTypeMetapathException(item,
String.format("The key '%s' of type '%s' is not appropriate for an array lookup.",
key.asString(),
key.getClass().getName()));

});
}
}

}
Loading

0 comments on commit 7af34c0

Please sign in to comment.