mirror of
https://github.com/actions/runner.git
synced 2025-12-15 06:26:46 +00:00
Add case function
This commit is contained in:
@@ -10,6 +10,7 @@ namespace GitHub.Actions.Expressions
|
|||||||
{
|
{
|
||||||
static ExpressionConstants()
|
static ExpressionConstants()
|
||||||
{
|
{
|
||||||
|
AddFunction<Case>("case", 3, Byte.MaxValue);
|
||||||
AddFunction<Contains>("contains", 2, 2);
|
AddFunction<Contains>("contains", 2, 2);
|
||||||
AddFunction<EndsWith>("endsWith", 2, 2);
|
AddFunction<EndsWith>("endsWith", 2, 2);
|
||||||
AddFunction<Format>("format", 1, Byte.MaxValue);
|
AddFunction<Format>("format", 1, Byte.MaxValue);
|
||||||
|
|||||||
@@ -17,9 +17,10 @@ namespace GitHub.Actions.Expressions
|
|||||||
String expression,
|
String expression,
|
||||||
ITraceWriter trace,
|
ITraceWriter trace,
|
||||||
IEnumerable<INamedValueInfo> namedValues,
|
IEnumerable<INamedValueInfo> namedValues,
|
||||||
IEnumerable<IFunctionInfo> functions)
|
IEnumerable<IFunctionInfo> functions,
|
||||||
|
Boolean allowCaseFunction = false)
|
||||||
{
|
{
|
||||||
var context = new ParseContext(expression, trace, namedValues, functions);
|
var context = new ParseContext(expression, trace, namedValues, functions, allowCaseFunction: allowCaseFunction);
|
||||||
context.Trace.Info($"Parsing expression: <{expression}>");
|
context.Trace.Info($"Parsing expression: <{expression}>");
|
||||||
return CreateTree(context);
|
return CreateTree(context);
|
||||||
}
|
}
|
||||||
@@ -349,6 +350,10 @@ namespace GitHub.Actions.Expressions
|
|||||||
{
|
{
|
||||||
throw new ParseException(ParseExceptionKind.TooManyParameters, token: @operator, expression: context.Expression);
|
throw new ParseException(ParseExceptionKind.TooManyParameters, token: @operator, expression: context.Expression);
|
||||||
}
|
}
|
||||||
|
else if (functionInfo.Name.Equals("case", StringComparison.OrdinalIgnoreCase) && function.Parameters.Count % 2 == 0)
|
||||||
|
{
|
||||||
|
throw new ParseException(ParseExceptionKind.EvenParameters, token: @operator, expression: context.Expression);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -411,6 +416,12 @@ namespace GitHub.Actions.Expressions
|
|||||||
String name,
|
String name,
|
||||||
out IFunctionInfo functionInfo)
|
out IFunctionInfo functionInfo)
|
||||||
{
|
{
|
||||||
|
if (String.Equals(name, "case", StringComparison.OrdinalIgnoreCase) && !context.AllowCaseFunction)
|
||||||
|
{
|
||||||
|
functionInfo = null;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
return ExpressionConstants.WellKnownFunctions.TryGetValue(name, out functionInfo) ||
|
return ExpressionConstants.WellKnownFunctions.TryGetValue(name, out functionInfo) ||
|
||||||
context.ExtensionFunctions.TryGetValue(name, out functionInfo);
|
context.ExtensionFunctions.TryGetValue(name, out functionInfo);
|
||||||
}
|
}
|
||||||
@@ -418,6 +429,7 @@ namespace GitHub.Actions.Expressions
|
|||||||
private sealed class ParseContext
|
private sealed class ParseContext
|
||||||
{
|
{
|
||||||
public Boolean AllowUnknownKeywords;
|
public Boolean AllowUnknownKeywords;
|
||||||
|
public Boolean AllowCaseFunction;
|
||||||
public readonly String Expression;
|
public readonly String Expression;
|
||||||
public readonly Dictionary<String, IFunctionInfo> ExtensionFunctions = new Dictionary<String, IFunctionInfo>(StringComparer.OrdinalIgnoreCase);
|
public readonly Dictionary<String, IFunctionInfo> ExtensionFunctions = new Dictionary<String, IFunctionInfo>(StringComparer.OrdinalIgnoreCase);
|
||||||
public readonly Dictionary<String, INamedValueInfo> ExtensionNamedValues = new Dictionary<String, INamedValueInfo>(StringComparer.OrdinalIgnoreCase);
|
public readonly Dictionary<String, INamedValueInfo> ExtensionNamedValues = new Dictionary<String, INamedValueInfo>(StringComparer.OrdinalIgnoreCase);
|
||||||
@@ -433,7 +445,8 @@ namespace GitHub.Actions.Expressions
|
|||||||
ITraceWriter trace,
|
ITraceWriter trace,
|
||||||
IEnumerable<INamedValueInfo> namedValues,
|
IEnumerable<INamedValueInfo> namedValues,
|
||||||
IEnumerable<IFunctionInfo> functions,
|
IEnumerable<IFunctionInfo> functions,
|
||||||
Boolean allowUnknownKeywords = false)
|
Boolean allowUnknownKeywords = false,
|
||||||
|
Boolean allowCaseFunction = false)
|
||||||
{
|
{
|
||||||
Expression = expression ?? String.Empty;
|
Expression = expression ?? String.Empty;
|
||||||
if (Expression.Length > ExpressionConstants.MaxLength)
|
if (Expression.Length > ExpressionConstants.MaxLength)
|
||||||
@@ -454,6 +467,7 @@ namespace GitHub.Actions.Expressions
|
|||||||
|
|
||||||
LexicalAnalyzer = new LexicalAnalyzer(Expression);
|
LexicalAnalyzer = new LexicalAnalyzer(Expression);
|
||||||
AllowUnknownKeywords = allowUnknownKeywords;
|
AllowUnknownKeywords = allowUnknownKeywords;
|
||||||
|
AllowCaseFunction = allowCaseFunction;
|
||||||
}
|
}
|
||||||
|
|
||||||
private class NoOperationTraceWriter : ITraceWriter
|
private class NoOperationTraceWriter : ITraceWriter
|
||||||
|
|||||||
@@ -29,6 +29,9 @@ namespace GitHub.Actions.Expressions
|
|||||||
case ParseExceptionKind.TooManyParameters:
|
case ParseExceptionKind.TooManyParameters:
|
||||||
description = "Too many parameters supplied";
|
description = "Too many parameters supplied";
|
||||||
break;
|
break;
|
||||||
|
case ParseExceptionKind.EvenParameters:
|
||||||
|
description = "Even number of parameters supplied, requires an odd number of parameters";
|
||||||
|
break;
|
||||||
case ParseExceptionKind.UnexpectedEndOfExpression:
|
case ParseExceptionKind.UnexpectedEndOfExpression:
|
||||||
description = "Unexpected end of expression";
|
description = "Unexpected end of expression";
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ namespace GitHub.Actions.Expressions
|
|||||||
ExceededMaxLength,
|
ExceededMaxLength,
|
||||||
TooFewParameters,
|
TooFewParameters,
|
||||||
TooManyParameters,
|
TooManyParameters,
|
||||||
|
EvenParameters,
|
||||||
UnexpectedEndOfExpression,
|
UnexpectedEndOfExpression,
|
||||||
UnexpectedSymbol,
|
UnexpectedSymbol,
|
||||||
UnrecognizedFunction,
|
UnrecognizedFunction,
|
||||||
|
|||||||
Reference in New Issue
Block a user