Skip to content

Commit

Permalink
support constant [] and {} expression (#3240)
Browse files Browse the repository at this point in the history
* init

* init

* add anonyumousObject test

* change lg to support empty object

* add comments

* remove initProperty

* templateengine-> lgfile

* delete initproperty.schema

* align with stringExpression .etc.

* fix typo

Co-authored-by: Dong Lei <[email protected]>
  • Loading branch information
2 people authored and Tom Laird-McConnell committed Jan 28, 2020
1 parent c07ce32 commit b1d4183
Show file tree
Hide file tree
Showing 30 changed files with 195 additions and 386 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,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

This file was deleted.

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 @@ -310,7 +310,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 @@ -333,7 +333,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 @@ -371,7 +371,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 @@ -2347,6 +2347,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 @@ -2521,8 +2572,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($"Unrecognized constant: {text}");
}

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

Expand Down
Loading

0 comments on commit b1d4183

Please sign in to comment.