Skip to content

Commit

Permalink
add support for tryItem and not equal
Browse files Browse the repository at this point in the history
  • Loading branch information
pchalamet committed Jun 3, 2024
1 parent cdac775 commit 855ce5f
Show file tree
Hide file tree
Showing 15 changed files with 1,218 additions and 1,017 deletions.
3 changes: 2 additions & 1 deletion src/Terrabuild.Configuration.Tests/TestFiles/WORKSPACE2
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ configuration release {
list: [ 1
2+3
"tutu"
nothing ?? 42 ]
nothing ?? 42
42 != "toto" ]
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/Terrabuild.Configuration.Tests/Workspace.fs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ let parseWorkspace2() =
"list", Expr.List [ Expr.Number 1
Expr.Function (Function.Plus, [ Expr.Number 2; Expr.Number 3 ])
Expr.String "tutu"
Expr.Function (Function.Coalesce, [ Expr.Nothing; Expr.Number 42 ]) ] ] }
Expr.Function (Function.Coalesce, [ Expr.Nothing; Expr.Number 42 ])
Expr.Function (Function.NotEqual, [Expr.Number 42; Expr.String "toto" ]) ] ] }
let envDummy =
{ Variables = Map.empty }
let envSecret =
Expand Down
630 changes: 324 additions & 306 deletions src/Terrabuild.Configuration/Gen/ProjectLexer.fs

Large diffs are not rendered by default.

408 changes: 223 additions & 185 deletions src/Terrabuild.Configuration/Gen/ProjectParser.fs

Large diffs are not rendered by default.

16 changes: 10 additions & 6 deletions src/Terrabuild.Configuration/Gen/ProjectParser.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,18 @@ type token =
| UPPER
| LOWER
| VERSION
| COALESCE
| TERNARY_IF
| TERNARY_ELSE
| DOUBLE_QUESTION
| QUESTION
| COLON
| MINUS
| PLUS
| COMMA
| EQUAL
| NOT_EQUAL
| LPAREN
| RPAREN
| DOT_LSQBRACKET
| DOT_QUESTION_LSQBRACKET
| LSQBRACKET
| RSQBRACKET
| LBRACE
Expand Down Expand Up @@ -66,16 +68,18 @@ type tokenId =
| TOKEN_UPPER
| TOKEN_LOWER
| TOKEN_VERSION
| TOKEN_COALESCE
| TOKEN_TERNARY_IF
| TOKEN_TERNARY_ELSE
| TOKEN_DOUBLE_QUESTION
| TOKEN_QUESTION
| TOKEN_COLON
| TOKEN_MINUS
| TOKEN_PLUS
| TOKEN_COMMA
| TOKEN_EQUAL
| TOKEN_NOT_EQUAL
| TOKEN_LPAREN
| TOKEN_RPAREN
| TOKEN_DOT_LSQBRACKET
| TOKEN_DOT_QUESTION_LSQBRACKET
| TOKEN_LSQBRACKET
| TOKEN_RSQBRACKET
| TOKEN_LBRACE
Expand Down
574 changes: 296 additions & 278 deletions src/Terrabuild.Configuration/Gen/WorkspaceLexer.fs

Large diffs are not rendered by default.

394 changes: 216 additions & 178 deletions src/Terrabuild.Configuration/Gen/WorkspaceParser.fs

Large diffs are not rendered by default.

16 changes: 10 additions & 6 deletions src/Terrabuild.Configuration/Gen/WorkspaceParser.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,18 @@ type token =
| UPPER
| LOWER
| VERSION
| COALESCE
| TERNARY_IF
| TERNARY_ELSE
| DOUBLE_QUESTION
| QUESTION
| COLON
| MINUS
| PLUS
| COMMA
| EQUAL
| NOT_EQUAL
| LPAREN
| RPAREN
| DOT_LSQBRACKET
| DOT_QUESTION_LSQBRACKET
| LSQBRACKET
| RSQBRACKET
| LBRACE
Expand Down Expand Up @@ -60,16 +62,18 @@ type tokenId =
| TOKEN_UPPER
| TOKEN_LOWER
| TOKEN_VERSION
| TOKEN_COALESCE
| TOKEN_TERNARY_IF
| TOKEN_TERNARY_ELSE
| TOKEN_DOUBLE_QUESTION
| TOKEN_QUESTION
| TOKEN_COLON
| TOKEN_MINUS
| TOKEN_PLUS
| TOKEN_COMMA
| TOKEN_EQUAL
| TOKEN_NOT_EQUAL
| TOKEN_LPAREN
| TOKEN_RPAREN
| TOKEN_DOT_LSQBRACKET
| TOKEN_DOT_QUESTION_LSQBRACKET
| TOKEN_LSQBRACKET
| TOKEN_RSQBRACKET
| TOKEN_LBRACE
Expand Down
38 changes: 20 additions & 18 deletions src/Terrabuild.Configuration/ProjectParser/Lexer.fsl
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,26 @@ rule token = parse
| "upper" { UPPER }
| "lower" { LOWER }
| "version" { VERSION }
| "?" { TERNARY_IF }
| ":" { TERNARY_ELSE }
| "??" { COALESCE }
| "?" { QUESTION }
| ":" { COLON }
| "??" { DOUBLE_QUESTION }
| ".[" { DOT_LSQBRACKET }
| ".?[" { DOT_QUESTION_LSQBRACKET }

| "{" { LBRACE }
| "}" { RBRACE }
| "[" { LSQBRACKET }
| "]" { RSQBRACKET }
| "(" { LPAREN }
| ")" { RPAREN }
| "=" { EQUAL }
| "!=" { NOT_EQUAL }
| "," { COMMA }
| "-" { MINUS }
| "+" { PLUS }

| "#" { singleLineComment lexbuf }
| "//" { singleLineComment lexbuf }

| "project" { PROJECT }
| "target" { TARGET }
Expand All @@ -46,9 +63,6 @@ rule token = parse
| "rebuild" { REBUILD }
| "defaults" { DEFAULTS }

| "#" { singleLineComment lexbuf }
| "//" { singleLineComment lexbuf }

| identifier { IDENTIFIER (lexeme lexbuf |> string) }
| extensionIdentifier { EXTENSION_IDENTIFIER (lexeme lexbuf |> string) }
| targetIdentifier { TARGET_IDENTIFIER (lexeme lexbuf |> string) }
Expand All @@ -70,18 +84,6 @@ rule token = parse
NUMBER (s)
}

| "{" { LBRACE }
| "}" { RBRACE }
| ".[" { DOT_LSQBRACKET }
| "[" { LSQBRACKET }
| "]" { RSQBRACKET }
| "(" { LPAREN }
| ")" { RPAREN }
| "=" { EQUAL }
| "," { COMMA }
| "-" { MINUS }
| "+" { PLUS }

| whitespace { token lexbuf }
| newline { lexbuf.EndPos <- lexbuf.EndPos.NextLine; token lexbuf }
| eof { EOF }
Expand Down
22 changes: 12 additions & 10 deletions src/Terrabuild.Configuration/ProjectParser/Parser.fsy
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ let debugPrint s = ignore s
%token <int> NUMBER

%token LBRACE RBRACE
%token DOT_LSQBRACKET LSQBRACKET RSQBRACKET
%token DOT_LSQBRACKET DOT_QUESTION_LSQBRACKET LSQBRACKET RSQBRACKET
%token LPAREN RPAREN
%token EQUAL
%token EQUAL NOT_EQUAL
%token COMMA
%token MINUS PLUS
%token TRIM UPPER LOWER VERSION COALESCE TERNARY_IF TERNARY_ELSE
%token TRIM UPPER LOWER VERSION DOUBLE_QUESTION QUESTION COLON
%token EOF
%token PROJECT EXTENSION TARGET
%token DEPENDENCIES OUTPUTS IGNORES FILES LABELS VARIABLES CONTAINER INIT SCRIPT DEPENDS_ON REBUILD DEFAULTS
Expand All @@ -39,13 +39,13 @@ let debugPrint s = ignore s

%right LSQBRACKET
%left RSQBRACKET
%left TERNARY_ELSE
%right TERNARY_IF
%left COALESCE
%left EQUAL
%left COLON
%right QUESTION
%left DOUBLE_QUESTION
%left EQUAL NOT_EQUAL
%left MINUS
%left PLUS
%left DOT_LSQBRACKET
%left DOT_LSQBRACKET DOT_QUESTION_LSQBRACKET

%type <Terrabuild.Configuration.Project.AST.ProjectFile> ProjectFile
%%
Expand Down Expand Up @@ -158,15 +158,17 @@ Expr:
| ExprMap { Expr.Map $1 }
/* functions */
| Expr DOT_LSQBRACKET ExprIndex RSQBRACKET { Expr.Function (Function.Item, [$1; $3]) }
| Expr DOT_QUESTION_LSQBRACKET ExprIndex RSQBRACKET { Expr.Function (Function.TryItem, [$1; $3]) }
| Expr EQUAL Expr { Expr.Function (Function.Equal, [$1; $3]) }
| Expr NOT_EQUAL Expr { Expr.Function (Function.NotEqual, [$1; $3]) }
| Expr PLUS Expr { Expr.Function (Function.Plus, [$1; $3]) }
| Expr MINUS Expr { Expr.Function (Function.Minus, [$1; $3]) }
| TRIM LPAREN Expr RPAREN { Expr.Function (Function.Trim, [$3]) }
| UPPER LPAREN Expr RPAREN { Expr.Function (Function.Upper, [$3]) }
| LOWER LPAREN Expr RPAREN { Expr.Function (Function.Lower, [$3]) }
| VERSION LPAREN Expr RPAREN { Expr.Function (Function.Version, [$3]) }
| Expr COALESCE Expr { Expr.Function (Function.Coalesce, [$1; $3]) }
| Expr TERNARY_IF Expr TERNARY_ELSE Expr { Expr.Function (Function.Ternary, [$1; $3; $5] ) }
| Expr DOUBLE_QUESTION Expr { Expr.Function (Function.Coalesce, [$1; $3]) }
| Expr QUESTION Expr COLON Expr { Expr.Function (Function.Ternary, [$1; $3; $5] ) }

ExprIndex:
| Expr { $1 }
Expand Down
36 changes: 19 additions & 17 deletions src/Terrabuild.Configuration/WorkspaceParser/Lexer.fsl
Original file line number Diff line number Diff line change
Expand Up @@ -27,38 +27,40 @@ rule token = parse
| "upper" { UPPER }
| "lower" { LOWER }
| "version" { VERSION }
| "?" { TERNARY_IF }
| ":" { TERNARY_ELSE }
| "??" { COALESCE }

| "workspace" { WORKSPACE }
| "target" { TARGET }
| "configuration" { CONFIGURATION }
| "extension" { EXTENSION }

| "space" { SPACE }
| "depends_on" { DEPENDS_ON }
| "rebuild" { REBUILD }
| "variables" { VARIABLES }
| "container" { CONTAINER }
| "script" { SCRIPT }
| "defaults" { DEFAULTS }
| "?" { QUESTION }
| ":" { COLON }
| "??" { DOUBLE_QUESTION }
| ".[" { DOT_LSQBRACKET }
| ".?[" { DOT_QUESTION_LSQBRACKET }

| "{" { LBRACE }
| "}" { RBRACE }
| ".[" { DOT_LSQBRACKET }
| "[" { LSQBRACKET }
| "]" { RSQBRACKET }
| "(" { LPAREN }
| ")" { RPAREN }
| "=" { EQUAL }
| "!=" { NOT_EQUAL }
| "," { COMMA }
| "-" { MINUS }
| "+" { PLUS }

| "#" { singleLineComment lexbuf }
| "//" { singleLineComment lexbuf }

| "workspace" { WORKSPACE }
| "target" { TARGET }
| "configuration" { CONFIGURATION }
| "extension" { EXTENSION }

| "space" { SPACE }
| "depends_on" { DEPENDS_ON }
| "rebuild" { REBUILD }
| "variables" { VARIABLES }
| "container" { CONTAINER }
| "script" { SCRIPT }
| "defaults" { DEFAULTS }

| identifier { IDENTIFIER (lexeme lexbuf |> string) }
| extensionIdentifier { EXTENSION_IDENTIFIER (lexeme lexbuf |> string) }
| targetIdentifier { TARGET_IDENTIFIER (lexeme lexbuf |> string) }
Expand Down
22 changes: 12 additions & 10 deletions src/Terrabuild.Configuration/WorkspaceParser/Parser.fsy
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,26 @@ let debugPrint s = ignore s
%token <int> NUMBER

%token LBRACE RBRACE
%token DOT_LSQBRACKET LSQBRACKET RSQBRACKET
%token DOT_LSQBRACKET DOT_QUESTION_LSQBRACKET LSQBRACKET RSQBRACKET
%token LPAREN RPAREN
%token EQUAL
%token EQUAL NOT_EQUAL
%token COMMA
%token MINUS PLUS
%token TRIM UPPER LOWER VERSION COALESCE TERNARY_IF TERNARY_ELSE
%token TRIM UPPER LOWER VERSION DOUBLE_QUESTION QUESTION COLON
%token EOF
%token WORKSPACE TARGET CONFIGURATION EXTENSION
%token SPACE DEPENDS_ON REBUILD VARIABLES CONTAINER INIT SCRIPT DEFAULTS

// associativity and precedences
%right LSQBRACKET
%left RSQBRACKET
%left TERNARY_ELSE
%right TERNARY_IF
%left COALESCE
%left EQUAL
%left COLON
%right QUESTION
%left DOUBLE_QUESTION
%left EQUAL NOT_EQUAL
%left MINUS
%left PLUS
%left DOT_LSQBRACKET
%left DOT_LSQBRACKET DOT_QUESTION_LSQBRACKET

%type <Terrabuild.Configuration.Workspace.AST.WorkspaceFile> WorkspaceFile
%%
Expand Down Expand Up @@ -145,15 +145,17 @@ Expr:
| ExprMap { Expr.Map $1 }
/* functions */
| Expr DOT_LSQBRACKET ExprIndex RSQBRACKET { Expr.Function (Function.Item, [$1; $3]) }
| Expr DOT_QUESTION_LSQBRACKET ExprIndex RSQBRACKET { Expr.Function (Function.TryItem, [$1; $3]) }
| Expr EQUAL Expr { Expr.Function (Function.Equal, [$1; $3]) }
| Expr NOT_EQUAL Expr { Expr.Function (Function.NotEqual, [$1; $3]) }
| Expr PLUS Expr { Expr.Function (Function.Plus, [$1; $3]) }
| Expr MINUS Expr { Expr.Function (Function.Minus, [$1; $3]) }
| TRIM LPAREN Expr RPAREN { Expr.Function (Function.Trim, [$3]) }
| UPPER LPAREN Expr RPAREN { Expr.Function (Function.Upper, [$3]) }
| LOWER LPAREN Expr RPAREN { Expr.Function (Function.Lower, [$3]) }
| VERSION LPAREN Expr RPAREN { Expr.Function (Function.Version, [$3]) }
| Expr COALESCE Expr { Expr.Function (Function.Coalesce, [$1; $3]) }
| Expr TERNARY_IF Expr TERNARY_ELSE Expr { Expr.Function (Function.Ternary, [$1; $3; $5] ) }
| Expr DOUBLE_QUESTION Expr { Expr.Function (Function.Coalesce, [$1; $3]) }
| Expr QUESTION Expr COLON Expr { Expr.Function (Function.Ternary, [$1; $3; $5] ) }

ExprIndex:
| Expr { $1 }
Expand Down
44 changes: 44 additions & 0 deletions src/Terrabuild.Expressions.Tests/Eval.fs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,21 @@ let listItem() =
varUsed |> should equal expectedUsedVars
result |> should equal expected

[<Test>]
let listTryItem() =
let expected = Value.Nothing
let expectedUsedVars = Set ["tagada"]

let context = { evaluationContext
with Variables = Map [
"tagada", Expr.List [ Expr.String "toto"; Expr.Number 42 ]
] }

let varUsed, result =
eval context (Expr.Function (Function.TryItem, [ Expr.Variable "tagada"; Expr.Number 3]))
varUsed |> should equal expectedUsedVars
result |> should equal expected

[<Test>]
let mapItem() =
let expected = Value.Number 42
Expand All @@ -143,3 +158,32 @@ let mapItem() =
eval context (Expr.Function (Function.Item, [ Expr.Variable "tagada"; Expr.String "toto" ]))
varUsed |> should equal expectedUsedVars
result |> should equal expected

[<Test>]
let mapTryItem() =
let expected = Value.Nothing
let expectedUsedVars = Set ["tagada"]

let context = { evaluationContext
with Variables = Map [
"tagada", Expr.Map (Map [ "toto", Expr.Number 42 ])
] }

let varUsed, result =
eval context (Expr.Function (Function.TryItem, [ Expr.Variable "tagada"; Expr.String "titi" ]))
varUsed |> should equal expectedUsedVars
result |> should equal expected

[<Test>]
let equalValue() =
let expected = Value.Bool true
let varUsed, result = eval evaluationContext (Expr.Function (Function.Equal, [Expr.String "toto"; Expr.String "toto"]))
varUsed |> should be Empty
result |> should equal expected

[<Test>]
let notEqualValue() =
let expected = Value.Bool false
let varUsed, result = eval evaluationContext (Expr.Function (Function.Equal, [Expr.String "toto"; Expr.Number 42]))
varUsed |> should be Empty
result |> should equal expected
Loading

0 comments on commit 855ce5f

Please sign in to comment.