Files
regorus/tests/parser/cases/rules/else.yaml
Anand Krishnamoorthi 444b2970a1 feat!: Indexes for nodes in the AST (#414)
Indexes allow associating extra data with nodes in the AST
using an array and then quickly looking up the array to fetch
the extra data.

- Index eidx for expressions
- Index sidx for statements
- Index qidx for queries.

AST nodes are not cloneable. Therefore once a module is created,
it is not possible to accidentally create two nodes with the same
index inadvertently via clone.

Also added IndexChecker in debug builds. When a module is parsed,
it will assert that indexes have been constructed correctly.

AST Cleanup
- Make literal expressions (null, val, number, string etc) also structs
  to match all other expressions
- Merge True and False nodes into a single Bool node.

Also update dependencies.

Signed-off-by: Anand Krishnamoorthi <anakrish@microsoft.com>
2025-06-20 15:09:07 -05:00

356 lines
7.5 KiB
YAML

# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
cases:
- note: no-query
rego: |
package test
x = 10 else {
true
}
error: unexpected keyword `else`
- note: no-query
num_expressions: 5
num_statements: 2
num_queries: 2
rego: |
package test
x = 10 {
false
} {
true
}
policy:
- spec:
head:
compr:
refr:
var: x
eidx: 1
assign:
op: "="
value:
number: 10
eidx: 2
bodies:
- query:
qidx: 0
stmts:
- literal:
expr:
bool: false
eidx: 3
sidx: 0
- query:
qidx: 1
stmts:
- literal:
expr:
bool: true
eidx: 4
sidx: 1
- note: no-else
rego: |
package test
x = 10 {
false
} {
true
}
num_expressions: 5
num_statements: 2
num_queries: 2
policy:
- spec:
head:
compr:
refr:
var: x
eidx: 1
assign:
op: "="
value:
number: 10
eidx: 2
bodies:
- query:
qidx: 0
stmts:
- literal:
expr:
bool: false
eidx: 3
sidx: 0
- query:
qidx: 1
stmts:
- literal:
expr:
bool: true
eidx: 4
sidx: 1
- note: if-no-else
rego: |
package test
import future.keywords.if
x = 10 if {
false
} {
true
}
num_expressions: 8
num_statements: 2
num_queries: 2
policy:
- spec:
head:
compr:
refr:
var: x
eidx: 4
assign:
op: "="
value:
number: 10
eidx: 5
bodies:
- query:
qidx: 0
stmts:
- literal:
expr:
bool: false
eidx: 6
sidx: 0
- query:
qidx: 1
stmts:
- literal:
expr:
bool: true
eidx: 7
sidx: 1
- note: rule-named-if
rego: |
package test
x = 10 if {
false
} {
true
}
num_expressions: 6
num_statements: 2
num_queries: 2
policy:
- spec:
head:
compr:
refr:
var: x
eidx: 1
assign:
op: "="
value:
number: 10
eidx: 2
bodies: []
- spec:
head:
compr:
refr:
var: if
eidx: 3
bodies:
- query:
qidx: 0
stmts:
- literal:
expr:
bool: false
eidx: 4
sidx: 0
- query:
qidx: 1
stmts:
- literal:
expr:
bool: true
eidx: 5
sidx: 1
- note: query-else
rego: |
package test
x = 10 {
false
} else {
true
}
num_expressions: 5
num_queries: 2
num_statements: 2
policy:
- spec:
head:
compr:
refr:
var: x
eidx: 1
assign:
op: "="
value:
number: 10
eidx: 2
bodies:
- query:
qidx: 0
stmts:
- literal:
expr:
bool: false
eidx: 3
sidx: 0
- query:
qidx: 1
stmts:
- literal:
expr:
bool: true
eidx: 4
sidx: 1
- note: if-literal-else
rego: |
package test
import future.keywords.if
x = 10 if 1 < 0 else {
true
}
num_expressions: 10
num_statements: 2
num_queries: 2
policy:
- spec:
head:
compr:
refr:
var: x
eidx: 4
assign:
op: "="
value:
number: 10
eidx: 5
bodies:
- query:
qidx: 0
stmts:
- literal:
expr:
boolexpr:
op: "<"
lhs:
number: 1
eidx: 6
rhs:
number: 0
eidx: 7
eidx: 8
sidx: 0
- query:
qidx: 1
stmts:
- literal:
expr:
bool: true
eidx: 9
sidx: 1
- note: if-literal-else-assign
rego: |
package test
import future.keywords.if
# This will evaluate to 10
x = 10 if 1 < 0 else := 20 {
true
}
num_expressions: 11
num_statements: 2
num_queries: 2
policy:
- spec:
head:
compr:
refr:
var: x
eidx: 4
assign:
op: "="
value:
number: 10
eidx: 5
bodies:
- query:
qidx: 0
stmts:
- literal:
expr:
boolexpr:
op: "<"
lhs:
number: 1
eidx: 6
rhs:
number: 0
eidx: 7
eidx: 8
sidx: 0
- assign:
op: ":="
value:
number: 20
eidx: 9
query:
qidx: 1
stmts:
- literal:
expr:
bool: true
eidx: 10
sidx: 1
- note: contains-else-error
rego: |
package test
import rego.v1
a contains "b" if {
false
} else if {
true
}
error: else cannot be used with set rules
- note: old-style-set-else-error
rego: |
package test
a["b"] {
false
} else {
true
}
error: else cannot be used with set rules