Skip to content

Commit

Permalink
Work in progress towards a better type system.
Browse files Browse the repository at this point in the history
  • Loading branch information
david-waltermire committed Nov 16, 2024
1 parent 47a13f2 commit 1e35c10
Show file tree
Hide file tree
Showing 52 changed files with 605 additions and 68 deletions.
47 changes: 23 additions & 24 deletions core/src/main/antlr4/Metapath10.g4
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ options { tokenVocab=Metapath10Lexer; superClass=Metapath10ParserBase; }

// [1]
metapath : expr EOF ;
// paramlist : param ( COMMA param)* ;
// param : DOLLAR eqname typedeclaration? ;
// functionbody : enclosedexpr ;
paramlist : param ( COMMA param)* ;
param : DOLLAR eqname typedeclaration? ;
functionbody : enclosedexpr ;
// [5]
enclosedexpr : OC expr? CC ;
expr : exprsingle ( COMMA exprsingle)* ;
Expand Down Expand Up @@ -74,10 +74,8 @@ predicate : OB expr CB ;
lookup : QM keyspecifier ;
keyspecifier : NCName | IntegerLiteral | parenthesizedexpr | STAR ;
// [55]
//arrowfunctionspecifier : eqname | varref | parenthesizedexpr ;
arrowfunctionspecifier : eqname;
// primaryexpr : literal | varref | parenthesizedexpr | contextitemexpr | functioncall | functionitemexpr | mapconstructor | arrayconstructor | unarylookup ;
primaryexpr : literal | varref | parenthesizedexpr | contextitemexpr | functioncall | mapconstructor | arrayconstructor | unarylookup;
arrowfunctionspecifier : eqname | varref | parenthesizedexpr ;
primaryexpr : literal | varref | parenthesizedexpr | contextitemexpr | functioncall | functionitemexpr | mapconstructor | arrayconstructor | unarylookup ;
literal : numericliteral | StringLiteral ;
numericliteral : IntegerLiteral | DecimalLiteral | DoubleLiteral ;
varref : DOLLAR varname ;
Expand All @@ -90,9 +88,9 @@ functioncall : { this.isFuncCall() }? eqname argumentlist ;
argument : exprsingle ;
// [65]
// argumentplaceholder : QM ;
// functionitemexpr : namedfunctionref | inlinefunctionexpr ;
// namedfunctionref : eqname POUND IntegerLiteral /* xgc: reserved-function-names */;
// inlinefunctionexpr : KW_FUNCTION OP paramlist? CP ( KW_AS sequencetype)? functionbody ;
functionitemexpr : namedfunctionref | inlinefunctionexpr ;
namedfunctionref : eqname POUND IntegerLiteral /* xgc: reserved-function-names */;
inlinefunctionexpr : KW_FUNCTION OP paramlist? CP ( KW_AS sequencetype)? functionbody ;
mapconstructor : KW_MAP OC (mapconstructorentry ( COMMA mapconstructorentry)*)? CC ;
// [70]
mapconstructorentry : mapkeyexpr COLON mapvalueexpr ;
Expand All @@ -104,12 +102,13 @@ squarearrayconstructor : OB (exprsingle ( COMMA exprsingle)*)? CB ;
curlyarrayconstructor : KW_ARRAY enclosedexpr ;
unarylookup : QM keyspecifier ;
// singletype : simpletypename QM? ;
// typedeclaration : KW_AS sequencetype ;
// sequencetype : KW_EMPTY_SEQUENCE OP CP | itemtype occurrenceindicator? ;
typedeclaration : KW_AS sequencetype ;
sequencetype : KW_EMPTY_SEQUENCE OP CP | itemtype occurrenceindicator? ;
// [80]
// occurrenceindicator : QM | STAR | PLUS ;
occurrenceindicator : QM | STAR | PLUS ;
itemtype : KW_ITEM OP CP | functiontest | maptest | arraytest | atomicoruniontype | parenthesizeditemtype ;
// itemtype : kindtest | KW_ITEM OP CP | functiontest | maptest | arraytest | atomicoruniontype | parenthesizeditemtype ;
// atomicoruniontype : eqname ;
atomicoruniontype : eqname ;
// kindtest : documenttest | elementtest | attributetest | schemaelementtest | schemaattributetest | pitest | commenttest | texttest | namespacenodetest | anykindtest ;
// anykindtest : KW_NODE OP CP ;
// [85]
Expand All @@ -133,18 +132,18 @@ unarylookup : QM keyspecifier ;
// [100]
// simpletypename : typename_ ;
// typename_ : eqname ;
// functiontest : anyfunctiontest | typedfunctiontest ;
// anyfunctiontest : KW_FUNCTION OP STAR CP ;
// typedfunctiontest : KW_FUNCTION OP (sequencetype ( COMMA sequencetype)*)? CP KW_AS sequencetype ;
functiontest : anyfunctiontest | typedfunctiontest ;
anyfunctiontest : KW_FUNCTION OP STAR CP ;
typedfunctiontest : KW_FUNCTION OP (sequencetype ( COMMA sequencetype)*)? CP KW_AS sequencetype ;
// [105]
// maptest : anymaptest | typedmaptest ;
// anymaptest : KW_MAP OP STAR CP ;
// typedmaptest : KW_MAP OP atomicoruniontype COMMA sequencetype CP ;
// arraytest : anyarraytest | typedarraytest ;
// anyarraytest : KW_ARRAY OP STAR CP ;
maptest : anymaptest | typedmaptest ;
anymaptest : KW_MAP OP STAR CP ;
typedmaptest : KW_MAP OP atomicoruniontype COMMA sequencetype CP ;
arraytest : anyarraytest | typedarraytest ;
anyarraytest : KW_ARRAY OP STAR CP ;
// [110]
// typedarraytest : KW_ARRAY OP sequencetype CP ;
// parenthesizeditemtype : OP itemtype CP ;
typedarraytest : KW_ARRAY OP sequencetype CP ;
parenthesizeditemtype : OP itemtype CP ;


// Error in the spec. EQName also includes acceptable keywords.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@

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

import gov.nist.secauto.metaschema.core.util.ObjectUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.xml.XMLConstants;
import javax.xml.namespace.QName;

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

public final class EQNameUtils {
private static final Pattern URI_QUALIFIED_NAME = Pattern.compile("^Q\\{([^{}]*)\\}(.+)$");
Expand Down Expand Up @@ -43,7 +46,7 @@ private EQNameUtils() {
@NonNull
public static QName parseName(
@NonNull String name,
@NonNull IEQNamePrefixResolver resolver) {
@NonNull PrefixToNamespaceResolver resolver) {
Matcher matcher = URI_QUALIFIED_NAME.matcher(name);
return matcher.matches()
? newUriQualifiedName(matcher)
Expand Down Expand Up @@ -92,7 +95,7 @@ private static QName newUriQualifiedName(@NonNull Matcher matcher) {
@NonNull
public static QName parseLexicalQName(
@NonNull String name,
@NonNull IEQNamePrefixResolver resolver) {
@NonNull PrefixToNamespaceResolver resolver) {
Matcher matcher = LEXICAL_NAME.matcher(name);
if (!matcher.matches()) {
throw new IllegalArgumentException(
Expand Down Expand Up @@ -120,11 +123,24 @@ public static boolean isNcName(@NonNull String name) {
return NCNAME.matcher(name).matches();
}

@NonNull
public static String toEQName(
@NonNull QName qname,
@Nullable NamespaceToPrefixResolver resolver) {
String namespace = qname.getNamespaceURI();
String prefix = namespace.isEmpty() ? null : StaticContext.getWellKnownPrefixForUri(namespace);
if (prefix == null && resolver != null) {
prefix = resolver.resolve(namespace);
}

return prefix == null ? ObjectUtils.notNull(qname.toString()) : prefix + ":" + qname.getLocalPart();
}

/**
* Provides a callback for resolving namespace prefixes.
*/
@FunctionalInterface
public interface IEQNamePrefixResolver {
public interface PrefixToNamespaceResolver {
/**
* Get the URI string for the provided namespace prefix.
*
Expand All @@ -135,4 +151,20 @@ public interface IEQNamePrefixResolver {
@NonNull
String resolve(@NonNull String prefix);
}

/**
* Provides a callback for resolving namespace prefixes.
*/
@FunctionalInterface
public interface NamespaceToPrefixResolver {
/**
* Get the URI string for the provided namespace prefix.
*
* @param namespace
* the namespace URI
* @return the associated prefix or {@code null} if no prefix is associated
*/
@Nullable
String resolve(@NonNull String namespace);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import gov.nist.secauto.metaschema.core.metapath.impl.StreamSequence;
import gov.nist.secauto.metaschema.core.metapath.item.IItem;
import gov.nist.secauto.metaschema.core.metapath.item.function.IArrayItem;
import gov.nist.secauto.metaschema.core.metapath.type.InvalidTypeMetapathException;
import gov.nist.secauto.metaschema.core.metapath.type.TypeMetapathException;
import gov.nist.secauto.metaschema.core.util.ObjectUtils;

import java.util.ArrayList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import gov.nist.secauto.metaschema.core.metapath.item.atomic.IAnyAtomicItem;
import gov.nist.secauto.metaschema.core.metapath.item.atomic.IDecimalItem;
import gov.nist.secauto.metaschema.core.metapath.item.atomic.INumericItem;
import gov.nist.secauto.metaschema.core.metapath.type.InvalidTypeMetapathException;
import gov.nist.secauto.metaschema.core.metapath.type.TypeMetapathException;
import gov.nist.secauto.metaschema.core.util.ObjectUtils;

import org.antlr.v4.runtime.CharStreams;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

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

import gov.nist.secauto.metaschema.core.metapath.EQNameUtils.IEQNamePrefixResolver;
import gov.nist.secauto.metaschema.core.metapath.EQNameUtils.PrefixToNamespaceResolver;
import gov.nist.secauto.metaschema.core.util.CollectionUtil;
import gov.nist.secauto.metaschema.core.util.ObjectUtils;

Expand Down Expand Up @@ -220,7 +220,7 @@ public URI getDefaultFunctionNamespace() {
* @return the resolver
*/
@NonNull
public IEQNamePrefixResolver getFunctionPrefixResolver() {
public PrefixToNamespaceResolver getFunctionPrefixResolver() {
return this::resolveFunctionPrefix;
}

Expand Down Expand Up @@ -251,7 +251,7 @@ private String resolveFunctionPrefix(@NonNull String prefix) {
* @return the resolver
*/
@NonNull
public IEQNamePrefixResolver getFlagPrefixResolver() {
public PrefixToNamespaceResolver getFlagPrefixResolver() {
return this::resolveFlagReferencePrefix;
}

Expand All @@ -278,7 +278,7 @@ private String resolveFlagReferencePrefix(@NonNull String prefix) {
* @return the resolver
*/
@NonNull
public IEQNamePrefixResolver getModelPrefixResolver() {
public PrefixToNamespaceResolver getModelPrefixResolver() {
return this::resolveModelReferencePrefix;
}

Expand Down Expand Up @@ -309,7 +309,7 @@ private String resolveModelReferencePrefix(@NonNull String prefix) {
* @return the resolver
*/
@NonNull
public IEQNamePrefixResolver getVariablePrefixResolver() {
public PrefixToNamespaceResolver getVariablePrefixResolver() {
return this::resolveVariablePrefix;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
package gov.nist.secauto.metaschema.core.metapath.cst;

import gov.nist.secauto.metaschema.core.metapath.ISequence;
import gov.nist.secauto.metaschema.core.metapath.TypeMetapathException;
import gov.nist.secauto.metaschema.core.metapath.function.library.FnData;
import gov.nist.secauto.metaschema.core.metapath.item.atomic.IAnyAtomicItem;
import gov.nist.secauto.metaschema.core.metapath.type.TypeMetapathException;

import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
import gov.nist.secauto.metaschema.core.metapath.DynamicContext;
import gov.nist.secauto.metaschema.core.metapath.ICollectionValue;
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.function.IMapItem;
import gov.nist.secauto.metaschema.core.metapath.type.InvalidTypeMetapathException;
import gov.nist.secauto.metaschema.core.util.ObjectUtils;

import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
package gov.nist.secauto.metaschema.core.metapath.cst.math;

import gov.nist.secauto.metaschema.core.metapath.ISequence;
import gov.nist.secauto.metaschema.core.metapath.InvalidTypeMetapathException;
import gov.nist.secauto.metaschema.core.metapath.cst.IExpression;
import gov.nist.secauto.metaschema.core.metapath.cst.IExpressionVisitor;
import gov.nist.secauto.metaschema.core.metapath.function.FunctionUtils;
Expand All @@ -15,6 +14,7 @@
import gov.nist.secauto.metaschema.core.metapath.item.atomic.IDayTimeDurationItem;
import gov.nist.secauto.metaschema.core.metapath.item.atomic.INumericItem;
import gov.nist.secauto.metaschema.core.metapath.item.atomic.IYearMonthDurationItem;
import gov.nist.secauto.metaschema.core.metapath.type.InvalidTypeMetapathException;

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
package gov.nist.secauto.metaschema.core.metapath.cst.math;

import gov.nist.secauto.metaschema.core.metapath.ISequence;
import gov.nist.secauto.metaschema.core.metapath.InvalidTypeMetapathException;
import gov.nist.secauto.metaschema.core.metapath.cst.IExpression;
import gov.nist.secauto.metaschema.core.metapath.cst.IExpressionVisitor;
import gov.nist.secauto.metaschema.core.metapath.function.FunctionUtils;
Expand All @@ -15,6 +14,7 @@
import gov.nist.secauto.metaschema.core.metapath.item.atomic.IDayTimeDurationItem;
import gov.nist.secauto.metaschema.core.metapath.item.atomic.INumericItem;
import gov.nist.secauto.metaschema.core.metapath.item.atomic.IYearMonthDurationItem;
import gov.nist.secauto.metaschema.core.metapath.type.InvalidTypeMetapathException;

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
package gov.nist.secauto.metaschema.core.metapath.cst.math;

import gov.nist.secauto.metaschema.core.metapath.ISequence;
import gov.nist.secauto.metaschema.core.metapath.InvalidTypeMetapathException;
import gov.nist.secauto.metaschema.core.metapath.cst.IExpression;
import gov.nist.secauto.metaschema.core.metapath.cst.IExpressionVisitor;
import gov.nist.secauto.metaschema.core.metapath.function.FunctionUtils;
Expand All @@ -17,6 +16,7 @@
import gov.nist.secauto.metaschema.core.metapath.item.atomic.IDayTimeDurationItem;
import gov.nist.secauto.metaschema.core.metapath.item.atomic.INumericItem;
import gov.nist.secauto.metaschema.core.metapath.item.atomic.IYearMonthDurationItem;
import gov.nist.secauto.metaschema.core.metapath.type.InvalidTypeMetapathException;

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

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

import gov.nist.secauto.metaschema.core.metapath.type.ISequenceType;

import java.util.Objects;

import edu.umd.cs.findbugs.annotations.NonNull;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
package gov.nist.secauto.metaschema.core.metapath.function;

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.FnNot;
import gov.nist.secauto.metaschema.core.metapath.item.atomic.IAnyAtomicItem;
import gov.nist.secauto.metaschema.core.metapath.item.atomic.IBase64BinaryItem;
Expand All @@ -21,6 +20,7 @@
import gov.nist.secauto.metaschema.core.metapath.item.atomic.IStringItem;
import gov.nist.secauto.metaschema.core.metapath.item.atomic.IUntypedAtomicItem;
import gov.nist.secauto.metaschema.core.metapath.item.atomic.IYearMonthDurationItem;
import gov.nist.secauto.metaschema.core.metapath.type.InvalidTypeMetapathException;

import java.util.Locale;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@
import gov.nist.secauto.metaschema.core.metapath.DynamicContext;
import gov.nist.secauto.metaschema.core.metapath.DynamicMetapathException;
import gov.nist.secauto.metaschema.core.metapath.ISequence;
import gov.nist.secauto.metaschema.core.metapath.InvalidTypeMetapathException;
import gov.nist.secauto.metaschema.core.metapath.MetapathException;
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.TypeSystem;
import gov.nist.secauto.metaschema.core.metapath.item.IItemVisitor;
import gov.nist.secauto.metaschema.core.metapath.item.atomic.IAnyAtomicItem;
import gov.nist.secauto.metaschema.core.metapath.item.atomic.IAnyUriItem;
import gov.nist.secauto.metaschema.core.metapath.item.atomic.IStringItem;
import gov.nist.secauto.metaschema.core.metapath.type.ISequenceType;
import gov.nist.secauto.metaschema.core.metapath.type.InvalidTypeMetapathException;
import gov.nist.secauto.metaschema.core.metapath.type.TypeSystem;

import java.util.ArrayList;
import java.util.Collections;
Expand Down Expand Up @@ -339,4 +341,15 @@ public boolean equals(Object obj) {
return Objects.equals(arguments, other.arguments) && Objects.equals(contextItem, other.contextItem);
}
}

@Override
public Object getValue() {
// never a value
return null;
}

@Override
public void accept(IItemVisitor visitor) {
visitor.visit(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
package gov.nist.secauto.metaschema.core.metapath.function;

import gov.nist.secauto.metaschema.core.metapath.ISequence;
import gov.nist.secauto.metaschema.core.metapath.InvalidTypeMetapathException;
import gov.nist.secauto.metaschema.core.metapath.TypeMetapathException;
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.IDecimalItem;
import gov.nist.secauto.metaschema.core.metapath.item.atomic.INumericItem;
import gov.nist.secauto.metaschema.core.metapath.type.InvalidTypeMetapathException;
import gov.nist.secauto.metaschema.core.metapath.type.TypeMetapathException;
import gov.nist.secauto.metaschema.core.util.ObjectUtils;

import java.math.BigInteger;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
package gov.nist.secauto.metaschema.core.metapath.function;

import gov.nist.secauto.metaschema.core.metapath.item.IItem;
import gov.nist.secauto.metaschema.core.metapath.type.ISequenceType;
import gov.nist.secauto.metaschema.core.metapath.type.Occurrence;
import gov.nist.secauto.metaschema.core.util.ObjectUtils;

import java.util.Objects;
Expand Down
Loading

0 comments on commit 1e35c10

Please sign in to comment.