Skip to content

Commit

Permalink
Feature/multiple chars (#5)
Browse files Browse the repository at this point in the history
* Implement additional chars
* fix warnings
  • Loading branch information
coenm committed Apr 14, 2022
1 parent c0bf17f commit 6e7af50
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
8 changes: 6 additions & 2 deletions src/ExpressionStringEvaluator/Parser/Language.g4
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,17 @@ fragment DOT : '.' ;
fragment MINUS : '-' ;
fragment SEMICOLUMN : ':' ;
fragment SLASH : '/' ;
fragment PERCENT : '%' ;
fragment BACKSLASH : '\\' '\\' ;
fragment PERCENT : '\\' '%' ;
fragment BRACKET_OPEN : '\\' '{' ;
fragment BRACKET_CLOSE : '\\' '}' ;
fragment ATSIGN : '@' ;
fragment SYMBOLS : ('?'|'!'|'#'|'$'|'^'|'&'|'*'|'=') ;

TRUE : ( T R U E );
FALSE : ( F A L S E );

KEY : LETTER(LETTER|DIGIT|UNDERSCORE|DOT|MINUS)+ ;
WORD : (LETTER|DIGIT|UNDERSCORE|DOT|MINUS|SLASH|ATSIGN)+;
WORD : (LETTER|DIGIT|UNDERSCORE|DOT|MINUS|SLASH|BACKSLASH|PERCENT|BRACKET_OPEN|BRACKET_CLOSE|ATSIGN|SYMBOLS)+;
NEWLINE : ('\r'? '\n' | '\r') -> channel(HIDDEN) ;
WHITESPACE : (' ' | '\t' ) -> channel(HIDDEN) ;
14 changes: 12 additions & 2 deletions src/ExpressionStringEvaluator/Parser/LanguageVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ public override CombinedTypeContainer VisitVariable(LanguageParser.VariableConte

public override CombinedTypeContainer VisitTextWithSpaces(LanguageParser.TextWithSpacesContext context)
{
var text = context.GetText();
var text = context.GetText()
.Replace("\\%", "%")
.Replace("\\{", "{")
.Replace("\\}", "}")
.Replace("\\\\", "\\");
return new CombinedTypeContainer(text);
}

Expand Down Expand Up @@ -119,7 +123,13 @@ public override CombinedTypeContainer VisitBooleanexpression(LanguageParser.Bool

public override CombinedTypeContainer VisitEnvvariable(LanguageParser.EnvvariableContext context)
{
var envVariable = context.KEY().GetText();
ITerminalNode envVarKey = context.KEY();
if (envVarKey == null)
{
throw new Exception("Could not determine key in environment variable.");
}

var envVariable = envVarKey.GetText();
IParseTree? closingSymbol = context.children[2];
if (!"%".Equals(closingSymbol.GetText(), StringComparison.CurrentCultureIgnoreCase))
{
Expand Down
11 changes: 10 additions & 1 deletion tests/ExpressionStringEvaluator.Tests/IntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,15 @@ public void Dispose()
[InlineData("{StringContains(abc, A)}", "false")] // case sensitive.
[InlineData("{StringContains(abc , bc)}", "true")]
[InlineData("{StringContains(abc def , \"c d\")}", "true")]
[InlineData("https://dev.azure.com/xx/_git {Env.ExpressionStringEvaluatorDummy}", "https://dev.azure.com/xx/_git Dummy value")]
[InlineData("https://dev.azure.com/xx/_git{Env.ExpressionStringEvaluatorDummy}", "https://dev.azure.com/xx/_gitDummy value")]
[InlineData("https://dev.azure.com/xx/_git2/{Env.ExpressionStringEvaluatorDummy}", "https://dev.azure.com/xx/_git2/Dummy value")]
[InlineData("https://dev.azure.com/xx/_git2/%ExpressionStringEvaluatorDummy%", "https://dev.azure.com/xx/_git2/Dummy value")]
[InlineData("https://dev.azure.com/xx/_git2/\\%%ExpressionStringEvaluatorDummy%", "https://dev.azure.com/xx/_git2/%Dummy value")]
[InlineData("https://dev.azure.com/xx/build?definitionScope=\\%5CIntegrations\\%5CData\\%5CAPI", "https://dev.azure.com/xx/build?definitionScope=%5CIntegrations%5CData%5CAPI")]
[InlineData("ABC\\{DEF", "ABC{DEF")]
[InlineData("ABC\\}DEF", "ABC}DEF")]
[InlineData("ABC\\\\DEF", "ABC\\DEF")]
public void Parse(string input, string expectedOutput)
{
// arrange
Expand All @@ -191,7 +200,7 @@ public void Parse(string input, string expectedOutput)
[InlineData("{trimEnd(tRue)}")] // this occurs becuase tRue is evaluated as string, not as text.
[InlineData("x {ifthenelse({FileExists(dummyfile2.json)}, exist, )} y")] // todo fix, third argument is null
[InlineData("{ifthenelse({FileExists(dummyfile2.json)}, exist, )}")]
[InlineData("{StringContains(\"abc def\" , \"c d\")}")] //todo fix, first argument is different type as second.
[InlineData("{StringContains(\"abc def\" , \"c d\")}")] // todo fix, first argument is different type as second.
public void Parse_ShouldThrow_WhenInvalidInput(string input)
{
// arrange
Expand Down

0 comments on commit 6e7af50

Please sign in to comment.