Skip to content

Commit

Permalink
Fix names for functions with units in the signature (#866)
Browse files Browse the repository at this point in the history
* Add TextTools

* Fix parsing start node path and properties in GraphPath

* Fix names ConcreteFunctionDefinitions with units in the signature

* Add test for checking package children have valid identifiers as names
  • Loading branch information
kevin-m-knight-gs authored Sep 25, 2024
1 parent 38ba4fb commit 2dda522
Show file tree
Hide file tree
Showing 9 changed files with 631 additions and 204 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ private static StringBuilder appendSignatureStringForType(StringBuilder builder,
}
if (Measure.isUnit(rawType, processorSupport))
{
return builder.append(rawType.getValueForMetaPropertyToOne(M3Properties.measure).getName()).append('~').append(rawType.getName());
return builder.append(rawType.getValueForMetaPropertyToOne(M3Properties.measure).getName()).append('$').append(rawType.getName());
}
if (PackageableElement.isPackageableElement(rawType, processorSupport))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@
import org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.TypeContext;
import org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.UnitNameContext;
import org.finos.legend.pure.m4.coreinstance.CoreInstance;
import org.finos.legend.pure.m4.serialization.grammar.StringEscape;
import org.finos.legend.pure.m4.tools.SafeAppendable;
import org.finos.legend.pure.m4.tools.TextTools;

import java.util.List;

Expand Down Expand Up @@ -206,7 +208,7 @@ private static void descriptorTypeAndMultToId(StringBuilder builder, FunctionTyp
private static StringBuilder descriptorTypeToId(StringBuilder builder, TypeContext typeContext)
{
UnitNameContext unitNameContext = typeContext.unitName();
return builder.append((unitNameContext == null) ? typeContext.qualifiedName().getText() : unitNameContext.getText());
return builder.append((unitNameContext == null) ? typeContext.qualifiedName().getText() : unitNameContext.getText().replace('~', '$'));
}

private static StringBuilder descriptorMultToId(StringBuilder builder, MultiplicityArgumentContext multContext)
Expand Down Expand Up @@ -265,12 +267,10 @@ private static FunctionDescriptorContext parse(String text) throws InvalidFuncti
private static FunctionDescriptorContext validateParseResult(FunctionDescriptorContext result, String text)
{
// ensure there's no unparsed text left over
for (int i = result.getStop().getStopIndex() + 1, len = text.length(); i < len; i++)
int nonWhitespaceIndex = TextTools.indexOfNonWhitespace(text, result.getStop().getStopIndex() + 1);
if (nonWhitespaceIndex != -1)
{
if (!Character.isWhitespace(text.charAt(i)))
{
throw new RuntimeException("Unparsed text from index " + i + ": '" + text.substring(i) + "'");
}
throw new RuntimeException("Unparsed text from index " + nonWhitespaceIndex + ": '" + StringEscape.escape(text.substring(nonWhitespaceIndex)) + "'");
}

// validate types
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.finos.legend.pure.m4.coreinstance.SourceInformation;
import org.finos.legend.pure.m4.serialization.grammar.StringEscape;
import org.finos.legend.pure.m4.tools.SafeAppendable;
import org.finos.legend.pure.m4.tools.TextTools;

import java.util.List;
import java.util.Objects;
Expand Down Expand Up @@ -440,6 +441,14 @@ public void setStartNodePath(String path)
{
throw new IllegalArgumentException("Invalid GraphPath start node path '" + StringEscape.escape(path) + "'", (e instanceof ParseCancellationException) ? e.getCause() : e);
}

// check that there's nothing more in the string (except possibly whitespace)
int nonWhitespace = TextTools.indexOfNonWhitespace(path, context.getStop().getStopIndex() + 1);
if (nonWhitespace != -1)
{
throw new IllegalArgumentException("Invalid GraphPath start node path '" + StringEscape.escape(path) + "': error at index " + nonWhitespace);
}

this.startNodePath = context.getText();
}

Expand Down Expand Up @@ -479,14 +488,10 @@ public Builder fromDescription(String description)
}

// check that there's nothing more in the string (except possibly whitespace)
for (int i = context.getStop().getStopIndex() + 1, len = description.length(); i < len;)
int nonWhitespace = TextTools.indexOfNonWhitespace(description, context.getStop().getStopIndex() + 1);
if (nonWhitespace != -1)
{
int codePoint = description.codePointAt(i);
if (!Character.isWhitespace(codePoint))
{
throw new IllegalArgumentException("Invalid GraphPath description '" + StringEscape.escape(description) + "': error at index " + i);
}
i += Character.charCount(codePoint);
throw new IllegalArgumentException("Invalid GraphPath description '" + StringEscape.escape(description) + "': error at index " + nonWhitespace);
}

// build graph path
Expand Down Expand Up @@ -582,14 +587,24 @@ private Builder addEdge(Edge pathElement)
private String validateProperty(String property)
{
initParser(Objects.requireNonNull(property, "property may not be null"));
M3Parser.PropertyNameContext context;
try
{
return this.parser.propertyName().getText();
context = this.parser.propertyName();
}
catch (Exception e)
{
throw new IllegalArgumentException("Invalid property name '" + StringEscape.escape(property) + "'", (e instanceof ParseCancellationException) ? e.getCause() : e);
}

// check that there's nothing more in the string (except possibly whitespace)
int nonWhitespace = TextTools.indexOfNonWhitespace(property, context.getStop().getStopIndex() + 1);
if (nonWhitespace != -1)
{
throw new IllegalArgumentException("Invalid property name '" + StringEscape.escape(property) + "': error at index " + nonWhitespace);
}

return context.getText();
}

private int validateIndex(int index)
Expand All @@ -604,14 +619,24 @@ private int validateIndex(int index)
private String validateKeyProperty(String keyProperty)
{
initParser(Objects.requireNonNull(keyProperty, "key property may not be null"));
M3Parser.PropertyNameContext context;
try
{
return this.parser.propertyName().getText();
context = this.parser.propertyName();
}
catch (Exception e)
{
throw new IllegalArgumentException("Invalid key property name '" + StringEscape.escape(keyProperty) + "\"", (e instanceof ParseCancellationException) ? e.getCause() : e);
throw new IllegalArgumentException("Invalid key property name '" + StringEscape.escape(keyProperty) + "'", (e instanceof ParseCancellationException) ? e.getCause() : e);
}

// check that there's nothing more in the string (except possibly whitespace)
int nonWhitespace = TextTools.indexOfNonWhitespace(keyProperty, context.getStop().getStopIndex() + 1);
if (nonWhitespace != -1)
{
throw new IllegalArgumentException("Invalid key property name '" + StringEscape.escape(keyProperty) + "': error at index " + nonWhitespace);
}

return context.getText();
}

private String validateKey(String key)
Expand Down
Loading

0 comments on commit 2dda522

Please sign in to comment.