Skip to content

Commit 51eccd0

Browse files
Some work towards supporting array lookup. Still working on postfix and unary lookups.
1 parent b83d36f commit 51eccd0

File tree

11 files changed

+406
-23
lines changed

11 files changed

+406
-23
lines changed

core/src/main/java/gov/nist/secauto/metaschema/core/metapath/antlr/AbstractAstVisitor.java

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.IntersectexceptexprContext;
5353
import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.LetexprContext;
5454
import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.LiteralContext;
55+
import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.LookupContext;
5556
import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.MetapathContext;
5657
import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.MultiplicativeexprContext;
5758
import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.NametestContext;
@@ -358,10 +359,34 @@ public R visitPostfixexpr(PostfixexprContext ctx) {
358359
return handle(ctx, (context) -> handlePostfixexpr(ctx));
359360
}
360361

362+
/**
363+
* Handle the provided expression.
364+
*
365+
* @param ctx
366+
* the provided expression context
367+
* @return the result
368+
*/
369+
protected abstract R handlePredicate(@NonNull PredicateContext ctx);
370+
361371
@Override
362372
public R visitPredicate(PredicateContext ctx) {
363-
// should never be called, since this is handled by the parent expression
364-
throw new IllegalStateException();
373+
assert ctx != null;
374+
return handlePredicate(ctx);
375+
}
376+
377+
/**
378+
* Handle the provided expression.
379+
*
380+
* @param ctx
381+
* the provided expression context
382+
* @return the result
383+
*/
384+
protected abstract R handleLookup(@NonNull LookupContext ctx);
385+
386+
@Override
387+
public R visitLookup(LookupContext ctx) {
388+
assert ctx != null;
389+
return handleLookup(ctx);
365390
}
366391

367392
/*

core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/AbstractCSTVisitorBase.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -295,12 +295,11 @@ protected <CONTEXT extends ParserRuleContext> IExpression handleGroupedNAiry(
295295
IExpression retval = null;
296296
if (numChildren > 0) {
297297
ParseTree leftTree = context.getChild(startingIndex);
298-
IExpression result = ObjectUtils.notNull(leftTree.accept(this));
298+
retval = ObjectUtils.notNull(leftTree.accept(this));
299299

300300
for (int i = startingIndex + 1; i < numChildren; i = i + step) {
301-
result = parser.apply(context, i, result);
301+
retval = parser.apply(context, i, retval);
302302
}
303-
retval = result;
304303
}
305304
return retval;
306305
}

core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/AbstractExpressionVisitor.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,4 +362,14 @@ public RESULT visitArray(ArraySequence expr, CONTEXT context) {
362362
public RESULT visitArray(ArraySquare expr, CONTEXT context) {
363363
return visitChildren(expr, context);
364364
}
365+
366+
@Override
367+
public RESULT visitPostfixLookup(PostfixLookup expr, CONTEXT context) {
368+
return visitChildren(expr, context);
369+
}
370+
371+
@Override
372+
public RESULT visitFunctionCallAccessor(FunctionCallAccessor expr, CONTEXT context) {
373+
return visitChildren(expr, context);
374+
}
365375
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
* Portions of this software was developed by employees of the National Institute
3+
* of Standards and Technology (NIST), an agency of the Federal Government and is
4+
* being made available as a public service. Pursuant to title 17 United States
5+
* Code Section 105, works of NIST employees are not subject to copyright
6+
* protection in the United States. This software may be subject to foreign
7+
* copyright. Permission in the United States and in foreign countries, to the
8+
* extent that NIST may hold copyright, to use, copy, modify, create derivative
9+
* works, and distribute this software and its documentation without fee is hereby
10+
* granted on a non-exclusive basis, provided that this notice and disclaimer
11+
* of warranty appears in all copies.
12+
*
13+
* THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER
14+
* EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY
15+
* THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF
16+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM
17+
* INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE
18+
* SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT
19+
* SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT,
20+
* INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM,
21+
* OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY,
22+
* CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR
23+
* PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT
24+
* OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER.
25+
*/
26+
27+
package gov.nist.secauto.metaschema.core.metapath.cst;
28+
29+
import gov.nist.secauto.metaschema.core.metapath.DynamicContext;
30+
import gov.nist.secauto.metaschema.core.metapath.ISequence;
31+
import gov.nist.secauto.metaschema.core.metapath.item.IItem;
32+
import gov.nist.secauto.metaschema.core.metapath.item.atomic.IIntegerItem;
33+
34+
import java.util.List;
35+
36+
import edu.umd.cs.findbugs.annotations.NonNull;
37+
38+
public abstract class AbstractLookup implements IExpression {
39+
40+
@NonNull
41+
private final IExpression base;
42+
@NonNull
43+
private final IKeySpecifier keySpecifier;
44+
45+
protected AbstractLookup(@NonNull IExpression base, @NonNull IKeySpecifier keySpecifier) {
46+
this.base = base;
47+
this.keySpecifier = keySpecifier;
48+
}
49+
50+
/**
51+
* Get the base sub-expression.
52+
*
53+
* @return the sub-expression
54+
*/
55+
@NonNull
56+
public IExpression getBase() {
57+
return base;
58+
}
59+
60+
@NonNull
61+
public IKeySpecifier getKeySpecifier() {
62+
return keySpecifier;
63+
}
64+
65+
@SuppressWarnings("null")
66+
@Override
67+
public List<? extends IExpression> getChildren() {
68+
return List.of(getBase());
69+
}
70+
71+
@Override
72+
public ISequence<? extends IItem> accept(DynamicContext dynamicContext, ISequence<?> focus) {
73+
ISequence<?> retval = getBase().accept(dynamicContext, focus);
74+
75+
// getKeySpecifier().accept(dynamicContext, retval);
76+
77+
return retval;
78+
}
79+
80+
protected interface IKeySpecifier {
81+
82+
}
83+
84+
protected static class NCNameKeySpecifier implements IKeySpecifier {
85+
86+
public NCNameKeySpecifier(String ncname) {
87+
// TODO Auto-generated constructor stub
88+
}
89+
90+
}
91+
92+
protected static class IntegerLiteralKeySpecifier implements IKeySpecifier {
93+
94+
public IntegerLiteralKeySpecifier(IIntegerItem literal) {
95+
// TODO Auto-generated constructor stub
96+
}
97+
98+
}
99+
100+
protected static class WildcardKeySpecifier implements IKeySpecifier {
101+
102+
}
103+
}

core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/BuildCSTVisitor.java

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.IntersectexceptexprContext;
5151
import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.LetexprContext;
5252
import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.LiteralContext;
53+
import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.LookupContext;
5354
import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.MultiplicativeexprContext;
5455
import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.NametestContext;
5556
import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.NodetestContext;
@@ -472,22 +473,48 @@ protected List<IExpression> parsePredicates(@NonNull ParseTree context, int star
472473
}
473474

474475
@Override
475-
protected IExpression handlePostfixexpr(PostfixexprContext ctx) {
476-
int numChildren = ctx.getChildCount();
477-
ParseTree primaryTree = ctx.getChild(0);
478-
IExpression retval = ObjectUtils.notNull(primaryTree.accept(this));
479-
480-
List<IExpression> predicates = numChildren > 1 ? parsePredicates(ctx, 1) : CollectionUtil.emptyList();
481-
482-
if (!predicates.isEmpty()) {
483-
retval = new PredicateExpression(retval, predicates);
484-
}
485-
return retval;
476+
protected IExpression handlePostfixexpr(PostfixexprContext context) {
477+
return handleGroupedNAiry(
478+
context,
479+
0,
480+
1,
481+
(ctx, idx, left) -> {
482+
ParseTree tree = ctx.getChild(idx);
483+
IExpression result;
484+
if (tree instanceof ArgumentlistContext) {
485+
// map or array access using function call syntax
486+
result = new FunctionCallAccessor(
487+
left,
488+
parseArgumentList((ArgumentlistContext) tree).findFirst().get());
489+
} else if (tree instanceof PredicateContext) {
490+
result = new PredicateExpression(
491+
left,
492+
CollectionUtil.singletonList(parsePredicate((PredicateContext) tree)));
493+
} else if (tree instanceof LookupContext) {
494+
result = new PostfixLookup(left, (LookupContext) tree);
495+
} else {
496+
result = visit(tree);
497+
}
498+
return result;
499+
});
486500
}
501+
487502
// ======================================================================
488503
// Path Expressions - https://www.w3.org/TR/xpath-31/#id-path-expressions
489504
// ======================================================================
490505

506+
@Override
507+
protected IExpression handlePredicate(PredicateContext ctx) {
508+
parsePredicate(ctx);
509+
return null;
510+
}
511+
512+
@Override
513+
protected IExpression handleLookup(LookupContext ctx) {
514+
// TODO Auto-generated method stub
515+
return null;
516+
}
517+
491518
@Override
492519
protected IExpression handlePathexpr(PathexprContext ctx) {
493520
int numChildren = ctx.getChildCount();

core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/CSTPrinter.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,17 @@ public String visitArray(ArraySequence expr, State context) {
345345
public String visitArray(ArraySquare expr, State context) {
346346
return appendNode(expr, super.visitArray(expr, context), context);
347347
}
348+
349+
@Override
350+
public String visitPostfixLookup(PostfixLookup expr, State context) {
351+
return appendNode(expr, super.visitPostfixLookup(expr, context), context);
352+
}
353+
354+
@Override
355+
public String visitFunctionCallAccessor(FunctionCallAccessor expr, State context) {
356+
return appendNode(expr, super.visitFunctionCallAccessor(expr, context), context);
357+
}
358+
348359
}
349360

350361
static class State {
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Portions of this software was developed by employees of the National Institute
3+
* of Standards and Technology (NIST), an agency of the Federal Government and is
4+
* being made available as a public service. Pursuant to title 17 United States
5+
* Code Section 105, works of NIST employees are not subject to copyright
6+
* protection in the United States. This software may be subject to foreign
7+
* copyright. Permission in the United States and in foreign countries, to the
8+
* extent that NIST may hold copyright, to use, copy, modify, create derivative
9+
* works, and distribute this software and its documentation without fee is hereby
10+
* granted on a non-exclusive basis, provided that this notice and disclaimer
11+
* of warranty appears in all copies.
12+
*
13+
* THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER
14+
* EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY
15+
* THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF
16+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM
17+
* INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE
18+
* SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT
19+
* SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT,
20+
* INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM,
21+
* OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY,
22+
* CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR
23+
* PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT
24+
* OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER.
25+
*/
26+
27+
package gov.nist.secauto.metaschema.core.metapath.cst;
28+
29+
import gov.nist.secauto.metaschema.core.metapath.DynamicContext;
30+
import gov.nist.secauto.metaschema.core.metapath.ISequence;
31+
import gov.nist.secauto.metaschema.core.metapath.function.library.ArrayGet;
32+
import gov.nist.secauto.metaschema.core.metapath.item.IItem;
33+
import gov.nist.secauto.metaschema.core.util.ObjectUtils;
34+
35+
import java.util.List;
36+
37+
import edu.umd.cs.findbugs.annotations.NonNull;
38+
39+
public class FunctionCallAccessor implements IExpression {
40+
@NonNull
41+
private final IExpression base;
42+
@NonNull
43+
private final IExpression argument;
44+
45+
public FunctionCallAccessor(@NonNull IExpression base, @NonNull IExpression argument) {
46+
this.base = base;
47+
this.argument = argument;
48+
}
49+
50+
/**
51+
* Get the base sub-expression.
52+
*
53+
* @return the sub-expression
54+
*/
55+
@NonNull
56+
public IExpression getBase() {
57+
return base;
58+
}
59+
60+
/**
61+
* Retrieve the argument to use for the lookup.
62+
*
63+
* @return the argument
64+
*/
65+
@NonNull
66+
public IExpression getArgument() {
67+
return argument;
68+
}
69+
70+
@SuppressWarnings("null")
71+
@Override
72+
public List<? extends IExpression> getChildren() {
73+
return List.of(getBase(), getArgument());
74+
}
75+
76+
@Override
77+
public ISequence<? extends IItem> accept(DynamicContext dynamicContext, ISequence<?> focus) {
78+
ISequence<?> collection = getBase().accept(dynamicContext, focus);
79+
ISequence<?> key = getArgument().accept(dynamicContext, focus);
80+
81+
return ArrayGet.SIGNATURE.execute(ObjectUtils.notNull(List.of(collection, key)), dynamicContext, focus);
82+
}
83+
84+
@Override
85+
public <RESULT, CONTEXT> RESULT accept(@NonNull IExpressionVisitor<RESULT, CONTEXT> visitor, CONTEXT context) {
86+
return visitor.visitFunctionCallAccessor(this, context);
87+
}
88+
}

core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/IExpressionVisitor.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,4 +532,26 @@ public interface IExpressionVisitor<RESULT, CONTEXT> {
532532
* @return the visitation result or {@code null} if no result was produced
533533
*/
534534
RESULT visitArray(@NonNull ArraySquare expr, @NonNull CONTEXT context);
535+
536+
/**
537+
* Visit the CST node.
538+
*
539+
* @param expr
540+
* the CST node to visit
541+
* @param context
542+
* the processing context
543+
* @return the visitation result or {@code null} if no result was produced
544+
*/
545+
RESULT visitPostfixLookup(@NonNull PostfixLookup expr, @NonNull CONTEXT context);
546+
547+
/**
548+
* Visit the CST node.
549+
*
550+
* @param expr
551+
* the CST node to visit
552+
* @param context
553+
* the processing context
554+
* @return the visitation result or {@code null} if no result was produced
555+
*/
556+
RESULT visitFunctionCallAccessor(@NonNull FunctionCallAccessor expr, @NonNull CONTEXT context);
535557
}

0 commit comments

Comments
 (0)