Skip to content

Commit

Permalink
Factorize function parsing code.
Browse files Browse the repository at this point in the history
Minor refactoring: the code to get the name of a function and its list
of arguments is shared by both the visitAction and the visitHeaderDecl
methods, so we take that code out in two helper methods.
  • Loading branch information
gouttegd committed Nov 7, 2024
1 parent 3674754 commit 7dc497b
Showing 1 changed file with 34 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -402,28 +402,8 @@ public Void visitPrefixDecl(SSSOMTransformParser.PrefixDeclContext ctx) {

@Override
public Void visitHeaderDecl(SSSOMTransformParser.HeaderDeclContext ctx) {
String name = ctx.action().FUNCTION().getText();
int nameLen = name.length();
name = name.substring(0, nameLen - 1);

ArrayList<String> arguments = new ArrayList<String>();

if ( ctx.action().arglist() != null ) {
for ( ArgumentContext argCtx : ctx.action().arglist().argument() ) {
if ( argCtx.string() != null ) {
arguments.add(unescape(argCtx.string().getText()));
} else if ( argCtx.IRI() != null ) {
String iri = argCtx.IRI().getText();
int iriLen = iri.length();
arguments.add(iri.substring(1, iriLen - 1));
} else if ( argCtx.CURIE() != null ) {
arguments.add(prefixManager.expandIdentifier(argCtx.CURIE().getText()));
}
}
}

try {
application.onHeaderAction(name, arguments);
application.onHeaderAction(getFunctionName(ctx.action()), getFunctionArguments(ctx.action()));
} catch ( SSSOMTransformError e ) {
errors.add(e);
}
Expand Down Expand Up @@ -490,28 +470,9 @@ public Void visitAction(SSSOMTransformParser.ActionContext ctx) {
filter = fs;
}

// Get function name
String name = ctx.FUNCTION().getText();
int nameLen = name.length();
name = name.substring(0, nameLen - 1);

// Assemble the arguments list
List<String> arguments = new ArrayList<String>();
if ( ctx.arglist() != null ) {
for ( ArgumentContext argCtx : ctx.arglist().argument() ) {
if ( argCtx.string() != null ) {
arguments.add(unescape(argCtx.string().getText()));
} else if ( argCtx.IRI() != null ) {
String iri = argCtx.IRI().getText();
int iriLen = iri.length();
arguments.add(iri.substring(1, iriLen - 1));
} else if ( argCtx.CURIE() != null ) {
arguments.add(prefixManager.expandIdentifier(argCtx.CURIE().getText()));
}
}
}

// Get the action from the application
String name = getFunctionName(ctx);
List<String> arguments = getFunctionArguments(ctx);
IMappingTransformer<Mapping> preprocessor = null;
IMappingTransformer<T> generator = null;
try {
Expand Down Expand Up @@ -543,6 +504,37 @@ public Void visitAction(SSSOMTransformParser.ActionContext ctx) {
return null;
}

// Remove the trailing '(' to get the name of the function
private String getFunctionName(SSSOMTransformParser.ActionContext ctx) {
String name = ctx.FUNCTION().getText();
int nameLen = name.length();
return name.substring(0, nameLen - 1);
}

// Process arguments in a function call:
// - unescape string arguments
// - remove enclosing brackets of IRI arguments
// - expand CURIE arguments
private List<String> getFunctionArguments(SSSOMTransformParser.ActionContext ctx) {
List<String> arguments = new ArrayList<String>();

if ( ctx.arglist() != null ) {
for ( ArgumentContext argCtx : ctx.arglist().argument() ) {
if ( argCtx.string() != null ) {
arguments.add(unescape(argCtx.string().getText()));
} else if ( argCtx.IRI() != null ) {
String iri = argCtx.IRI().getText();
int iriLen = iri.length();
arguments.add(iri.substring(1, iriLen - 1));
} else if ( argCtx.CURIE() != null ) {
arguments.add(prefixManager.expandIdentifier(argCtx.CURIE().getText()));
}
}
}

return arguments;
}

// Un-quote and un-escape the string as provided by the parser
// This is static because it does not depend on any state and is re-used below
// by the filter visitor.
Expand Down

0 comments on commit 7dc497b

Please sign in to comment.