Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support constant [] and {} expression #3240

Merged
merged 18 commits into from
Jan 28, 2020
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ public override IEnumerable<TypeRegistration> GetTypes()
yield return new TypeRegistration<ForeachPage>(ForeachPage.DeclarativeType);
yield return new TypeRegistration<HttpRequest>(HttpRequest.DeclarativeType);
yield return new TypeRegistration<IfCondition>(IfCondition.DeclarativeType);
yield return new TypeRegistration<InitProperty>(InitProperty.DeclarativeType);
yield return new TypeRegistration<LogAction>(LogAction.DeclarativeType);
yield return new TypeRegistration<RepeatDialog>(RepeatDialog.DeclarativeType);
yield return new TypeRegistration<ReplaceDialog>(ReplaceDialog.DeclarativeType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ private AnalyzerResult AnalyzeExpressionDirectly(Expression exp)
private AnalyzerResult AnalyzeExpression(string exp)
{
var result = new AnalyzerResult();
exp = exp.TrimStart('@').TrimStart('{').TrimEnd('}');
exp = exp.TrimExpression();
var parsed = _expressionParser.Parse(exp);

var references = parsed.References();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ private bool EvalExpressionInCondition(string exp)
{
try
{
exp = exp.TrimStart('@').TrimStart('{').TrimEnd('}');
exp = exp.TrimExpression();
var (result, error) = EvalByExpressionEngine(exp, CurrentTarget().Scope);

if (error != null
Expand All @@ -328,7 +328,7 @@ private bool EvalExpressionInCondition(string exp)

private object EvalExpression(string exp)
{
exp = exp.TrimStart('@').TrimStart('{').TrimEnd('}');
exp = exp.TrimExpression();
var (result, error) = EvalByExpressionEngine(exp, CurrentTarget().Scope);
if (error != null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ private bool EvalExpressionInCondition(string exp)
{
try
{
exp = exp.TrimStart('@').TrimStart('{').TrimEnd('}');
exp = exp.TrimExpression();
var (result, error) = EvalByExpressionEngine(exp, CurrentTarget().Scope);

if (error != null
Expand All @@ -340,7 +340,7 @@ private bool EvalExpressionInCondition(string exp)

private List<string> EvalExpression(string exp)
{
exp = exp.TrimStart('@').TrimStart('{').TrimEnd('}');
exp = exp.TrimExpression();
var (result, error) = EvalByExpressionEngine(exp, CurrentTarget().Scope);
if (error != null)
{
Expand Down
17 changes: 17 additions & 0 deletions libraries/Microsoft.Bot.Builder.LanguageGeneration/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,23 @@ public static string Escape(this string text)
}));
}

/// <summary>
/// trim expression. @{abc} => abc, @{a == {}} => a == {}.
/// </summary>
/// <param name="expression">input expression string.</param>
/// <returns>pure expression string.</returns>
public static string TrimExpression(this string expression)
{
var result = expression.Trim().TrimStart('@').Trim();

if (result.StartsWith("{") && result.EndsWith("}"))
{
result = result.Substring(1, result.Length - 2);
}

return result;
}

/// <summary>
/// Normalize authored path to os path.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,11 @@ fragment NUMBER: '0'..'9';

fragment WHITESPACE : ' '|'\t'|'\ufeff'|'\u00a0';

fragment EMPTY_OBJECT: '{' WHITESPACE* '}';

fragment STRING_LITERAL : ('\'' (~['\r\n])* '\'') | ('"' (~["\r\n])* '"');

fragment EXPRESSION_FRAGMENT : '@' '{' (STRING_LITERAL| ~[\r\n{}'"] )*? '}';
fragment EXPRESSION_FRAGMENT : '@' '{' (STRING_LITERAL| ~[\r\n{}'"] | EMPTY_OBJECT )*? '}';

fragment ESCAPE_CHARACTER_FRAGMENT : '\\' ~[\r\n]?;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ public override List<Diagnostic> VisitNormalTemplateString([NotNull] LGFileParse
private List<Diagnostic> CheckExpression(string exp, ParserRuleContext context)
{
var result = new List<Diagnostic>();
exp = exp.TrimStart('@').TrimStart('{').TrimEnd('}');
exp = exp.TrimExpression();

try
{
Expand Down
56 changes: 54 additions & 2 deletions libraries/Microsoft.Bot.Expressions/BuiltInFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2403,6 +2403,57 @@ private static (object, string) IndicesAndValues(Expression expression, object s

private static bool IsSameDay(DateTime date1, DateTime date2) => date1.Year == date2.Year && date1.Month == date2.Month && date1.Day == date2.Day;

private static bool IsEqual(IReadOnlyList<dynamic> args)
{
if (args[0] == null)
{
return args[1] == null;
}

if (TryParseList(args[0], out IList l0) && l0.Count == 0 && (TryParseList(args[1], out IList l1) && l1.Count == 0))
{
return true;
}

if (GetPropertyCount(args[0]) == 0 && GetPropertyCount(args[1]) == 0)
{
return true;
}

try
{
return args[0] == args[1];
}
catch
{
return false;
}
}

/// <summary>
/// Get the property count of an object.
/// </summary>
/// <param name="obj">input object.</param>
/// <returns>property count.</returns>
private static int GetPropertyCount(dynamic obj)
{
if (obj is IDictionary dictionary)
{
return dictionary.Count;
}
else if (obj is JObject jobj)
{
return jobj.Properties().Count();
}
else if (!(obj is JValue) && obj.GetType().IsValueType == false && obj.GetType().FullName != "System.String")
{
// exclude constant type.
return obj.GetType().GetProperties().Length;
}

return -1;
}

private static Dictionary<string, ExpressionEvaluator> BuildFunctionLookup()
{
var functions = new List<ExpressionEvaluator>
Expand Down Expand Up @@ -2577,8 +2628,9 @@ private static Dictionary<string, ExpressionEvaluator> BuildFunctionLookup()
// Booleans
Comparison(ExpressionType.LessThan, args => args[0] < args[1], ValidateBinaryNumberOrString, VerifyNumberOrString),
Comparison(ExpressionType.LessThanOrEqual, args => args[0] <= args[1], ValidateBinaryNumberOrString, VerifyNumberOrString),
Comparison(ExpressionType.Equal, args => args[0] == args[1], ValidateBinary),
Comparison(ExpressionType.NotEqual, args => args[0] != args[1], ValidateBinary),

Comparison(ExpressionType.Equal, IsEqual, ValidateBinary),
Comparison(ExpressionType.NotEqual, args => !IsEqual(args), ValidateBinary),
Comparison(ExpressionType.GreaterThan, args => args[0] > args[1], ValidateBinaryNumberOrString, VerifyNumberOrString),
Comparison(ExpressionType.GreaterThanOrEqual, args => args[0] >= args[1], ValidateBinaryNumberOrString, VerifyNumberOrString),
Comparison(ExpressionType.Exists, args => args[0] != null, ValidateUnary, VerifyNotNull),
Expand Down
3 changes: 3 additions & 0 deletions libraries/Microsoft.Bot.Expressions/parser/Expression.g4
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ expression

primaryExpression
: '(' expression ')' #parenthesisExp
| CONSTANT #constantAtom
| NUMBER #numericAtom
| STRING #stringAtom
| IDENTIFIER #idAtom
Expand All @@ -45,4 +46,6 @@ NEWLINE : '\r'? '\n' -> skip;

STRING : ('\'' (~'\'')* '\'') | ('"' (~'"')* '"');

CONSTANT : ('[' WHITESPACE* ']') | ('{' WHITESPACE* '}');

INVALID_TOKEN_DEFAULT_MODE : . ;
17 changes: 17 additions & 0 deletions libraries/Microsoft.Bot.Expressions/parser/ExpressionEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Antlr4.Runtime;
using Antlr4.Runtime.Misc;
using Antlr4.Runtime.Tree;
using Newtonsoft.Json.Linq;

namespace Microsoft.Bot.Expressions
{
Expand Down Expand Up @@ -171,6 +172,22 @@ public override Expression VisitStringAtom([NotNull] ExpressionParser.StringAtom
}
}

public override Expression VisitConstantAtom([NotNull] ExpressionParser.ConstantAtomContext context)
{
var text = context.GetText();
if (string.IsNullOrWhiteSpace(text.TrimStart('[').TrimEnd(']')))
{
return Expression.ConstantExpression(new JArray());
}

if (string.IsNullOrWhiteSpace(text.TrimStart('{').TrimEnd('}')))
{
return Expression.ConstantExpression(new JObject());
}

throw new Exception($"Unregonized constant: {text}");
}

private Expression MakeExpression(string type, params Expression[] children)
=> Expression.MakeExpression(_lookup(type), children);

Expand Down
Loading