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

Helper to get static string from parser #2736

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
29 changes: 29 additions & 0 deletions src/libraries/Microsoft.PowerFx.Core/Parser/ParseResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,5 +118,34 @@ public string GetAnonymizedFormula()
{
return StructuralPrint.Print(Root);
}

/// <summary>
/// Checks if the expression does not depend on extra evaluations.
/// Examples:
/// "Lorem ipsum" // is a static expression since there is no extra evaluation needed.
/// "Today is " & Today() // is not a static expression since the function call 'Today()' needs to be evaluated.
/// $"Today is {""some text""}" // is not a static expression since the interpolation needs to be evaluated.
/// </summary>
/// <returns>True if no extra evaluation is needed. False otherwise.</returns>
public bool TryGetStaticTextExpression(out string staticExpression)
{
if (Options.TextFirst &&
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Options.TextFirst &&

can we avoid this?

Root is StrInterpNode strInterpNode &&
strInterpNode.ChildNodes.Count == 1 &&
strInterpNode.ChildNodes.First() is StrLitNode strLitNode)
{
staticExpression = strLitNode.Value;
return true;
}

if (Root is StrLitNode strLitNode1)
{
staticExpression = strLitNode1.Value;
return true;
}

staticExpression = null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about numbers? (this gets to the purpose of the feature)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Today, this requirement is limited to string type arguments, but in the future, we might need to extend it.

return false;
}
}
}
16 changes: 16 additions & 0 deletions src/tests/Microsoft.PowerFx.Core.Tests.Shared/ParseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1026,5 +1026,21 @@ public void TestTypeLiteralInNamedFormula(string script, int namedFormulaCount)
Assert.Equal(namedFormulaCount, parseResult.NamedFormulas.Count());
Assert.Contains(parseResult.Errors, e => e.MessageKey.Contains("ErrUserDefinedTypeIncorrectSyntax"));
}

[Theory]
[InlineData("\"static text\"", "static text", false, true)]
[InlineData("$\"not static {\"text\"}\"", null, false, false)]
[InlineData("\"not\" & \"static\" & \"text\"", null, false, false)]

// TextFirst
[InlineData("static text", "static text", true, true)]
[InlineData("not static ${\"text\"}", null, true, false)]
public void TryGetStaticTextExpressionTest(string expression, string result, bool textFirst, bool isStaticText)
{
var opt = new ParserOptions() { TextFirst = textFirst };
var parseResult = Engine.Parse(expression, options: opt);
Assert.Equal(isStaticText, parseResult.TryGetStaticTextExpression(out var outResult));
Assert.Equal(result, outResult);
}
}
}
Loading