Skip to content

Commit

Permalink
Report errors when trying to using optional ranges
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Vetter <[email protected]>
  • Loading branch information
VeaaC committed Nov 14, 2024
1 parent 31a03e7 commit f199d05
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
5 changes: 4 additions & 1 deletion flatdata-generator/flatdata/generator/tree/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from flatdata.generator.tree.errors import (
InvalidEnumWidthError, InvalidRangeName, InvalidRangeReference,
InvalidConstReference, InvalidConstValueReference, DuplicateInvalidValueReference,
InvalidStructInExplicitReference)
InvalidStructInExplicitReference, OptionalRange)
from flatdata.generator.tree.nodes.explicit_reference import ExplicitReference
from flatdata.generator.tree.nodes.archive import Archive
from flatdata.generator.tree.nodes.node import Node
Expand Down Expand Up @@ -181,6 +181,9 @@ def _check_ranges(root):
for sibling in field.parent.fields:
if sibling.name == name:
raise InvalidRangeName(name)
# Also check that the range is not optional
if field.invalid_value:
raise OptionalRange(name)

# Now check that structs with ranges are only used in vectors
for reference in root.iterate(StructureReference):
Expand Down
6 changes: 6 additions & 0 deletions flatdata-generator/flatdata/generator/tree/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,10 @@ class InvalidRangeReference(FlatdataSyntaxError):
def __init__(self, name):
super().__init__(
"Structs with @range can only be used in vectors: {name}"
.format(name=name))

class OptionalRange(FlatdataSyntaxError):
def __init__(self, name):
super().__init__(
"@range cannot be combined with @optional, store empty ranges instead: {name}"
.format(name=name))
15 changes: 12 additions & 3 deletions flatdata-generator/tests/tree/test_syntax_tree_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
sys.path.insert(0, "..")
from flatdata.generator.tree.errors import MissingSymbol, InvalidRangeName, InvalidRangeReference, \
InvalidConstReference, InvalidConstValueReference, DuplicateInvalidValueReference, \
InvalidStructInExplicitReference
InvalidStructInExplicitReference, OptionalRange
from flatdata.generator.tree.builder import build_ast
from flatdata.generator.tree.nodes.trivial import Namespace, Structure, Field, Constant, Enumeration, EnumerationValue
from flatdata.generator.tree.nodes.archive import Archive
Expand All @@ -21,8 +21,6 @@
FieldReference, ArchiveReference, BuiltinStructureReference, ConstantValueReference, \
EnumerationReference, InvalidValueReference



def test_validating_archive_with_no_structure_defined_raises_missing_symbol_error():
def __test(resource_type):
with assert_raises(MissingSymbol):
Expand Down Expand Up @@ -103,6 +101,17 @@ def test_range_cannot_be_used_in_struct_resource():
}
""")

def test_optional_range():
with assert_raises(OptionalRange):
build_ast("""namespace foo{
const u32 NO_EDGES_REF = 200;
struct Node {
@range(edges_range)
@optional( NO_EDGES_REF )
first_edge_ref : u32;
}
}""")

def test_ranges_can_be_used_in_normally():
build_ast("""namespace foo{
struct A {
Expand Down

0 comments on commit f199d05

Please sign in to comment.