Skip to content
Merged
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
25 changes: 23 additions & 2 deletions dsc/tests/dsc_functions.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ Describe 'tests for function expressions' {
@{ expression = "[intersection(parameters('firstObject'), parameters('firstArray'))]"; isError = $true }
@{ expression = "[intersection(parameters('firstArray'), parameters('secondArray'), parameters('fifthArray'))]"; expected = @('cd') }
@{ expression = "[intersection(parameters('firstObject'), parameters('secondObject'), parameters('sixthObject'))]"; expected = [pscustomobject]@{ two = 'b' } }
@{ expression = "[intersection(parameters('nestedObject1'), parameters('nestedObject2'))]"; expected = [pscustomobject]@{
@{ expression = "[intersection(parameters('nestedObject1'), parameters('nestedObject2'))]"; expected = [pscustomobject]@{
shared = [pscustomobject]@{ value = 42; flag = $true }
level = 1
} }
Expand Down Expand Up @@ -711,9 +711,30 @@ Describe 'tests for function expressions' {
properties:
output: `"$expression`"
"@
$out = dsc -l trace config get -i $config_yaml 2>$TestDrive/error.log
$null = dsc -l trace config get -i $config_yaml 2>$TestDrive/error.log
$LASTEXITCODE | Should -Not -Be 0
$errorContent = Get-Content $TestDrive/error.log -Raw
$errorContent | Should -Match ([regex]::Escape($expectedError))
}

It 'mixed booleans with functions works' -TestCases @(
@{ expression = "[and(true(), false, not(false))]"; expected = $false }
@{ expression = "[or(false, false(), not(false()))]"; expected = $true }
@{ expression = "[and(true(), true, not(false))]"; expected = $true }
@{ expression = "[or(false, false(), not(true()))]"; expected = $false }
) {
param($expression, $expected)

$config_yaml = @"
`$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
resources:
- name: Echo
type: Microsoft.DSC.Debug/Echo
properties:
output: "$expression"
"@
$out = dsc -l trace config get -i $config_yaml 2>$TestDrive/error.log | ConvertFrom-Json
$LASTEXITCODE | Should -Be 0 -Because (Get-Content $TestDrive/error.log -Raw)
$out.results[0].result.actualState.output | Should -BeExactly $expected
}
}
12 changes: 9 additions & 3 deletions tree-sitter-dscexpression/grammar.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const PREC = {
ESCAPEDSTRING: 2,
EXPRESSIONSTRING: 1,
FUNCTION: 1,
BOOLEAN: 0,
STRINGLITERAL: -11,
}

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

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

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

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

Expand Down
10 changes: 2 additions & 8 deletions tree-sitter-dscexpression/test/corpus/invalid_expressions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ String parameter not in quotes
(function
(functionName)
(ERROR
(functionName)
(functionName)))))

=====
Expand Down Expand Up @@ -79,8 +78,6 @@ String starting with bracket
---

(ERROR
(ERROR
(functionName))
(functionName))

=====
Expand Down Expand Up @@ -179,8 +176,7 @@ Expression with member accessor outside
(functionName)
(arguments
(number))))
(ERROR
(functionName)))
(ERROR))

=====
Expression with index accessor outside
Expand Down Expand Up @@ -209,6 +205,4 @@ String with un-escaped single-quote
(functionName)
(arguments
(string))
(ERROR
(functionName)
(functionName)))))
(ERROR))))
19 changes: 18 additions & 1 deletion tree-sitter-dscexpression/test/corpus/valid_expressions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Simple expression
=====
Multiple arguments
=====
[myFunction('argString', 1, -1, true)]
[myFunction('argString', 1, -1, true, false)]
---

(statement
Expand All @@ -45,6 +45,7 @@ Multiple arguments
(string)
(number)
(number)
(boolean)
(boolean)))))

=====
Expand Down Expand Up @@ -335,3 +336,19 @@ User Function
(arguments
(number)
(number)))))

=====
User Function with nested function
=====
[Environment.getEnvironmentConfig(true(), false)]
---

(statement
(expression
(function
(functionName)
(arguments
(expression
(function
(functionName)))
(boolean)))))
Loading