Skip to content

Commit

Permalink
Work in progress towards a better type system and implementation of '…
Browse files Browse the repository at this point in the history
…instance of' Metapath expressions.
  • Loading branch information
david-waltermire committed Nov 17, 2024
1 parent be910a9 commit 0e26e6b
Show file tree
Hide file tree
Showing 63 changed files with 1,375 additions and 229 deletions.
64 changes: 31 additions & 33 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 All @@ -33,13 +33,12 @@ rangeexpr : additiveexpr ( KW_TO additiveexpr )? ;
additiveexpr : multiplicativeexpr ( (PLUS | MINUS) multiplicativeexpr )* ;
multiplicativeexpr : unionexpr ( (STAR | KW_DIV | KW_IDIV | KW_MOD) unionexpr )* ;
unionexpr : intersectexceptexpr ( (KW_UNION | P) intersectexceptexpr )* ;
// intersectexceptexpr : instanceofexpr ( ( KW_INTERSECT | KW_EXCEPT) instanceofexpr )* ;
intersectexceptexpr : arrowexpr ( ( KW_INTERSECT | KW_EXCEPT) arrowexpr )* ;
intersectexceptexpr : instanceofexpr ( ( KW_INTERSECT | KW_EXCEPT) instanceofexpr )* ;
// [25]
// instanceofexpr : treatexpr ( KW_INSTANCE KW_OF sequencetype )? ;
// treatexpr : castableexpr ( KW_TREAT KW_AS sequencetype )? ;
// castableexpr : castexpr ( KW_CASTABLE KW_AS singletype )? ;
// castexpr : arrowexpr ( KW_CAST KW_AS singletype )? ;
instanceofexpr : treatexpr ( KW_INSTANCE KW_OF sequencetype )? ;
treatexpr : castableexpr ( KW_TREAT KW_AS sequencetype )? ;
castableexpr : castexpr ( KW_CASTABLE KW_AS singletype )? ;
castexpr : arrowexpr ( KW_CAST KW_AS singletype )? ;
arrowexpr : unaryexpr ( EG arrowfunctionspecifier argumentlist )* ;
// [30]
unaryexpr : ( MINUS | PLUS)* valueexpr ;
Expand Down Expand Up @@ -74,10 +73,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 +87,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 @@ -103,13 +100,14 @@ squarearrayconstructor : OB (exprsingle ( COMMA exprsingle)*)? CB ;
// [75]
curlyarrayconstructor : KW_ARRAY enclosedexpr ;
unarylookup : QM keyspecifier ;
// singletype : simpletypename QM? ;
// typedeclaration : KW_AS sequencetype ;
// sequencetype : KW_EMPTY_SEQUENCE OP CP | itemtype occurrenceindicator? ;
singletype : simpletypename QM? ;
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 @@ -131,20 +129,20 @@ unarylookup : QM keyspecifier ;
// attributename : eqname ;
// elementname : eqname ;
// [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 ;
simpletypename : typename_ ;
typename_ : eqname ;
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 @@ -188,7 +188,6 @@ public MetaschemaDataTypeProvider() {
// is matched first
// before other String-based data types.
registerDatatype(STRING);

registerDatatype(BASE64);
registerDatatype(BOOLEAN);
registerDatatype(DATE);
Expand All @@ -202,7 +201,6 @@ public MetaschemaDataTypeProvider() {
registerDatatype(INTEGER);
registerDatatype(IP_V4_ADDRESS);
registerDatatype(IP_V6_ADDRESS);

registerDatatype(NON_NEGATIVE_INTEGER);
registerDatatype(POSITIVE_INTEGER);
registerDatatype(TOKEN);
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;

/**
* Utilities for managing qualified names.
Expand Down Expand Up @@ -46,7 +49,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 @@ -95,7 +98,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 @@ -123,11 +126,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 @@ -138,4 +154,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 @@ -19,6 +19,8 @@
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.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,9 @@

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.metapath.item.IItem;
import gov.nist.secauto.metaschema.core.metapath.type.IItemType;
import gov.nist.secauto.metaschema.core.util.CollectionUtil;
import gov.nist.secauto.metaschema.core.util.ObjectUtils;

Expand All @@ -15,6 +17,7 @@
import java.util.stream.Collectors;

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

import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;
Expand Down Expand Up @@ -217,7 +220,7 @@ public URI getDefaultFunctionNamespace() {
* @return the resolver
*/
@NonNull
public IEQNamePrefixResolver getFunctionPrefixResolver() {
public PrefixToNamespaceResolver getFunctionPrefixResolver() {
return this::resolveFunctionPrefix;
}

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

Expand All @@ -275,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 @@ -306,7 +309,7 @@ private String resolveModelReferencePrefix(@NonNull String prefix) {
* @return the resolver
*/
@NonNull
public IEQNamePrefixResolver getVariablePrefixResolver() {
public PrefixToNamespaceResolver getVariablePrefixResolver() {
return this::resolveVariablePrefix;
}

Expand Down Expand Up @@ -515,4 +518,12 @@ public StaticContext build() {
return new StaticContext(this);
}
}

@NonNull
public IItemType lookupDataType(QName name) {
}

@NonNull
public IItemType lookupDataType(Class<? extends IItem> clazz) {
}
}
Loading

0 comments on commit 0e26e6b

Please sign in to comment.