Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
mihnita committed Jan 29, 2025
1 parent c532790 commit 01b1a3e
Show file tree
Hide file tree
Showing 24 changed files with 1,350 additions and 491 deletions.
55 changes: 23 additions & 32 deletions icu4j/main/core/src/main/java/com/ibm/icu/message2/MFDataModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ public static class StringPart implements PatternPart {
@Deprecated
public interface Expression extends PatternPart {
// Provides a common type for all kind of expressions:
// LiteralExpression, VariableExpression, FunctionExpression, UnsupportedExpression, Markup
// LiteralExpression, VariableExpression, FunctionExpression, Markup
}

/**
Expand All @@ -223,17 +223,17 @@ public interface Expression extends PatternPart {
@Deprecated
public static class LiteralExpression implements Expression {
public final Literal arg;
public final Annotation annotation;
public final Function function;
public final List<Attribute> attributes;

/**
* @internal ICU 72 technology preview
* @deprecated This API is for technology preview only.
*/
@Deprecated
public LiteralExpression(Literal arg, Annotation annotation, List<Attribute> attributes) {
public LiteralExpression(Literal arg, Function function, List<Attribute> attributes) {
this.arg = arg;
this.annotation = annotation;
this.function = function;
this.attributes = attributes;
}
}
Expand All @@ -245,7 +245,7 @@ public LiteralExpression(Literal arg, Annotation annotation, List<Attribute> att
@Deprecated
public static class VariableExpression implements Expression {
public final VariableRef arg;
public final Annotation annotation;
public final Function function;
public final List<Attribute> attributes;

/**
Expand All @@ -254,9 +254,9 @@ public static class VariableExpression implements Expression {
*/
@Deprecated
public VariableExpression(
VariableRef arg, Annotation annotation, List<Attribute> attributes) {
VariableRef arg, Function function, List<Attribute> attributes) {
this.arg = arg;
this.annotation = annotation;
this.function = function;
this.attributes = attributes;
}
}
Expand All @@ -266,8 +266,19 @@ public VariableExpression(
* @deprecated This API is for technology preview only.
*/
@Deprecated
public interface Annotation {
// Provides a common type for FunctionAnnotation, UnsupportedAnnotation
public static class Function {
public final String name;
public final Map<String, Option> options;

/**
* @internal ICU 72 technology preview
* @deprecated This API is for technology preview only.
*/
@Deprecated
public Function(String name, Map<String, Option> options) {
this.name = name;
this.options = options;
}
}

/**
Expand All @@ -276,16 +287,16 @@ public interface Annotation {
*/
@Deprecated
public static class FunctionExpression implements Expression {
public final FunctionAnnotation annotation;
public final Function function;
public final List<Attribute> attributes;

/**
* @internal ICU 72 technology preview
* @deprecated This API is for technology preview only.
*/
@Deprecated
public FunctionExpression(FunctionAnnotation annotation, List<Attribute> attributes) {
this.annotation = annotation;
public FunctionExpression(Function function, List<Attribute> attributes) {
this.function = function;
this.attributes = attributes;
}
}
Expand Down Expand Up @@ -359,26 +370,6 @@ public VariableRef(String name) {
}
}

/**
* @internal ICU 72 technology preview
* @deprecated This API is for technology preview only.
*/
@Deprecated
public static class FunctionAnnotation implements Annotation {
public final String name;
public final Map<String, Option> options;

/**
* @internal ICU 72 technology preview
* @deprecated This API is for technology preview only.
*/
@Deprecated
public FunctionAnnotation(String name, Map<String, Option> options) {
this.name = name;
this.options = options;
}
}

/**
* @internal ICU 72 technology preview
* @deprecated This API is for technology preview only.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
import java.util.Locale;
import java.util.Map;

import com.ibm.icu.message2.MFDataModel.Annotation;
import com.ibm.icu.message2.MFDataModel.Function;
import com.ibm.icu.message2.MFDataModel.CatchallKey;
import com.ibm.icu.message2.MFDataModel.Declaration;
import com.ibm.icu.message2.MFDataModel.Expression;
import com.ibm.icu.message2.MFDataModel.FunctionAnnotation;
import com.ibm.icu.message2.MFDataModel.Function;
import com.ibm.icu.message2.MFDataModel.FunctionExpression;
import com.ibm.icu.message2.MFDataModel.InputDeclaration;
import com.ibm.icu.message2.MFDataModel.Literal;
Expand Down Expand Up @@ -163,14 +163,14 @@ private Pattern findBestMatchingPattern(
} else if (fph.getInput() instanceof MFDataModel.VariableExpression) {
MFDataModel.VariableExpression ve = (MFDataModel.VariableExpression) fph.getInput();
argument = resolveLiteralOrVariable(ve.arg, variables, arguments);
if (ve.annotation instanceof FunctionAnnotation) {
functionName = ((FunctionAnnotation) ve.annotation).name;
if (ve.function instanceof Function) {
functionName = ((Function) ve.function).name;
}
} else if (fph.getInput() instanceof LiteralExpression) {
LiteralExpression le = (LiteralExpression) fph.getInput();
argument = le.arg;
if (le.annotation instanceof FunctionAnnotation) {
functionName = ((FunctionAnnotation) le.annotation).name;
if (le.function instanceof Function) {
functionName = ((Function) le.function).name;
}
}
SelectorFactory funcFactory = standardFunctions.getSelector(functionName);
Expand Down Expand Up @@ -477,7 +477,7 @@ private static Map<String, Object> convertOptions(
private FormattedPlaceholder formatExpression(
Expression expression, Map<String, Object> variables, Map<String, Object> arguments) {

Annotation annotation = null; // function name
Function function = null; // function name
String functionName = null;
Object toFormat = null;
Map<String, Object> options = new HashMap<>();
Expand All @@ -486,7 +486,7 @@ private FormattedPlaceholder formatExpression(
if (expression instanceof MFDataModel.VariableExpression) {
MFDataModel.VariableExpression varPart = (MFDataModel.VariableExpression) expression;
fallbackString = "{$" + varPart.arg.name + "}";
annotation = varPart.annotation; // function name & options
function = varPart.function; // function name & options
Object resolved = resolveLiteralOrVariable(varPart.arg, variables, arguments);
if (resolved instanceof FormattedPlaceholder) {
Object input = ((FormattedPlaceholder) resolved).getInput();
Expand All @@ -504,11 +504,11 @@ private FormattedPlaceholder formatExpression(
} else if (expression
instanceof MFDataModel.FunctionExpression) { // Function without arguments
MFDataModel.FunctionExpression fe = (FunctionExpression) expression;
fallbackString = "{:" + fe.annotation.name + "}";
annotation = fe.annotation;
fallbackString = "{:" + fe.function.name + "}";
function = fe.function;
} else if (expression instanceof MFDataModel.LiteralExpression) {
MFDataModel.LiteralExpression le = (LiteralExpression) expression;
annotation = le.annotation;
function = le.function;
fallbackString = "{|" + le.arg.value + "|}";
toFormat = resolveLiteralOrVariable(le.arg, variables, arguments);
} else if (expression instanceof MFDataModel.Markup) {
Expand All @@ -523,8 +523,8 @@ private FormattedPlaceholder formatExpression(
}
}

if (annotation instanceof FunctionAnnotation) {
FunctionAnnotation fa = (FunctionAnnotation) annotation;
if (function instanceof Function) {
Function fa = (Function) function;
if (functionName != null && !functionName.equals(fa.name)) {
fatalFormattingError(
"invalid function overrides, '" + functionName + "' <> '" + fa.name + "'");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
import java.util.Set;
import java.util.StringJoiner;

import com.ibm.icu.message2.MFDataModel.Annotation;
import com.ibm.icu.message2.MFDataModel.Function;
import com.ibm.icu.message2.MFDataModel.CatchallKey;
import com.ibm.icu.message2.MFDataModel.Declaration;
import com.ibm.icu.message2.MFDataModel.Expression;
import com.ibm.icu.message2.MFDataModel.FunctionAnnotation;
import com.ibm.icu.message2.MFDataModel.Function;
import com.ibm.icu.message2.MFDataModel.FunctionExpression;
import com.ibm.icu.message2.MFDataModel.InputDeclaration;
import com.ibm.icu.message2.MFDataModel.Literal;
Expand Down Expand Up @@ -141,30 +141,30 @@ private void validateExpression(Expression expression, boolean fromInput)
throws MFParseException {
String argName = null;
boolean wasLiteral = false;
Annotation annotation = null;
Function function = null;
if (expression instanceof Literal) {
// ...{foo}... or ...{|foo|}... or ...{123}...
// does not declare anything
} else if (expression instanceof LiteralExpression) {
LiteralExpression le = (LiteralExpression) expression;
argName = le.arg.value;
annotation = le.annotation;
function = le.function;
wasLiteral = true;
} else if (expression instanceof VariableExpression) {
VariableExpression ve = (VariableExpression) expression;
// ...{$foo :bar opt1=|str| opt2=$x opt3=$y}...
// .input {$foo :number} => declares `foo`, if already declared is an error
// .local $a={$foo} => declares `a`, but only used `foo`, does not declare it
argName = ve.arg.name;
annotation = ve.annotation;
function = ve.function;
} else if (expression instanceof FunctionExpression) {
// ...{$foo :bar opt1=|str| opt2=$x opt3=$y}...
FunctionExpression fe = (FunctionExpression) expression;
annotation = fe.annotation;
function = fe.function;
}

if (annotation instanceof FunctionAnnotation) {
FunctionAnnotation fa = (FunctionAnnotation) annotation;
if (function instanceof Function) {
Function fa = (Function) function;
if (fa.options != null) {
for (Option opt : fa.options.values()) {
LiteralOrVariableRef val = opt.value;
Expand Down
Loading

0 comments on commit 01b1a3e

Please sign in to comment.