Skip to content

Commit 2c970df

Browse files
authored
Merge pull request #1144 from SteveL-MSFT/boolean-functions
Fix expression grammar to work with boolean functions
2 parents 06fafb9 + 0f02a4e commit 2c970df

File tree

4 files changed

+52
-14
lines changed

4 files changed

+52
-14
lines changed

dsc/tests/dsc_functions.tests.ps1

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ Describe 'tests for function expressions' {
120120
@{ expression = "[intersection(parameters('firstObject'), parameters('firstArray'))]"; isError = $true }
121121
@{ expression = "[intersection(parameters('firstArray'), parameters('secondArray'), parameters('fifthArray'))]"; expected = @('cd') }
122122
@{ expression = "[intersection(parameters('firstObject'), parameters('secondObject'), parameters('sixthObject'))]"; expected = [pscustomobject]@{ two = 'b' } }
123-
@{ expression = "[intersection(parameters('nestedObject1'), parameters('nestedObject2'))]"; expected = [pscustomobject]@{
123+
@{ expression = "[intersection(parameters('nestedObject1'), parameters('nestedObject2'))]"; expected = [pscustomobject]@{
124124
shared = [pscustomobject]@{ value = 42; flag = $true }
125125
level = 1
126126
} }
@@ -711,9 +711,30 @@ Describe 'tests for function expressions' {
711711
properties:
712712
output: `"$expression`"
713713
"@
714-
$out = dsc -l trace config get -i $config_yaml 2>$TestDrive/error.log
714+
$null = dsc -l trace config get -i $config_yaml 2>$TestDrive/error.log
715715
$LASTEXITCODE | Should -Not -Be 0
716716
$errorContent = Get-Content $TestDrive/error.log -Raw
717717
$errorContent | Should -Match ([regex]::Escape($expectedError))
718718
}
719+
720+
It 'mixed booleans with functions works' -TestCases @(
721+
@{ expression = "[and(true(), false, not(false))]"; expected = $false }
722+
@{ expression = "[or(false, false(), not(false()))]"; expected = $true }
723+
@{ expression = "[and(true(), true, not(false))]"; expected = $true }
724+
@{ expression = "[or(false, false(), not(true()))]"; expected = $false }
725+
) {
726+
param($expression, $expected)
727+
728+
$config_yaml = @"
729+
`$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
730+
resources:
731+
- name: Echo
732+
type: Microsoft.DSC.Debug/Echo
733+
properties:
734+
output: "$expression"
735+
"@
736+
$out = dsc -l trace config get -i $config_yaml 2>$TestDrive/error.log | ConvertFrom-Json
737+
$LASTEXITCODE | Should -Be 0 -Because (Get-Content $TestDrive/error.log -Raw)
738+
$out.results[0].result.actualState.output | Should -BeExactly $expected
739+
}
719740
}

tree-sitter-dscexpression/grammar.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
const PREC = {
22
ESCAPEDSTRING: 2,
33
EXPRESSIONSTRING: 1,
4+
FUNCTION: 1,
5+
BOOLEAN: 0,
46
STRINGLITERAL: -11,
57
}
68

@@ -20,16 +22,20 @@ module.exports = grammar({
2022
expression: $ => seq(field('function', $.function), optional(field('accessor',$.accessor))),
2123
stringLiteral: $ => token(prec(PREC.STRINGLITERAL, /[^\[](.|\n)*?/)),
2224

23-
function: $ => seq(field('name', $.functionName), '(', field('args', optional($.arguments)), ')'),
24-
functionName: $ => /[a-zA-Z][a-zA-Z0-9]*(\.[a-zA-Z0-9]+)?/,
25+
function: $ => prec(PREC.FUNCTION, seq(field('name', $.functionName), '(', field('args', optional($.arguments)), ')')),
26+
functionName: $ => choice(
27+
/[a-zA-Z][a-zA-Z0-9]*(\.[a-zA-Z0-9]+)?/,
28+
$._booleanLiteral
29+
),
2530
arguments: $ => seq($._argument, repeat(seq(',', $._argument))),
2631
_argument: $ => choice($.expression, $._quotedString, $.number, $.boolean),
2732

2833
_quotedString: $ => seq('\'', $.string, '\''),
2934
// ARM strings are not allowed to contain single-quote characters unless escaped
3035
string: $ => /([^']|''|\n)*/,
3136
number: $ => /-?\d+/,
32-
boolean: $ => choice('true', 'false'),
37+
boolean: $ => prec(PREC.BOOLEAN, $._booleanLiteral),
38+
_booleanLiteral: $ => choice('true', 'false'),
3339

3440
accessor: $ => repeat1(choice($.memberAccess, $.index)),
3541

tree-sitter-dscexpression/test/corpus/invalid_expressions.txt

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ String parameter not in quotes
1818
(function
1919
(functionName)
2020
(ERROR
21-
(functionName)
2221
(functionName)))))
2322

2423
=====
@@ -79,8 +78,6 @@ String starting with bracket
7978
---
8079

8180
(ERROR
82-
(ERROR
83-
(functionName))
8481
(functionName))
8582

8683
=====
@@ -179,8 +176,7 @@ Expression with member accessor outside
179176
(functionName)
180177
(arguments
181178
(number))))
182-
(ERROR
183-
(functionName)))
179+
(ERROR))
184180

185181
=====
186182
Expression with index accessor outside
@@ -209,6 +205,4 @@ String with un-escaped single-quote
209205
(functionName)
210206
(arguments
211207
(string))
212-
(ERROR
213-
(functionName)
214-
(functionName)))))
208+
(ERROR))))

tree-sitter-dscexpression/test/corpus/valid_expressions.txt

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Simple expression
3434
=====
3535
Multiple arguments
3636
=====
37-
[myFunction('argString', 1, -1, true)]
37+
[myFunction('argString', 1, -1, true, false)]
3838
---
3939

4040
(statement
@@ -45,6 +45,7 @@ Multiple arguments
4545
(string)
4646
(number)
4747
(number)
48+
(boolean)
4849
(boolean)))))
4950

5051
=====
@@ -335,3 +336,19 @@ User Function
335336
(arguments
336337
(number)
337338
(number)))))
339+
340+
=====
341+
User Function with nested function
342+
=====
343+
[Environment.getEnvironmentConfig(true(), false)]
344+
---
345+
346+
(statement
347+
(expression
348+
(function
349+
(functionName)
350+
(arguments
351+
(expression
352+
(function
353+
(functionName)))
354+
(boolean)))))

0 commit comments

Comments
 (0)