Skip to content

Commit

Permalink
add support for OCCURS on a field level
Browse files Browse the repository at this point in the history
  • Loading branch information
zalmane committed Jan 18, 2022
1 parent b5ac48b commit 2ff69a6
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 1 deletion.
1 change: 1 addition & 0 deletions copybook/field.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ def __init__(self,s,lok,toks):
self.toks = toks
self.list_of_values = {}
self.redefine_target = toks.get("redefine_target")
self.repeat = int(toks.get("occurs","1"))
self.length = 0
self.precision = 0
self.signed = False
Expand Down
2 changes: 1 addition & 1 deletion copybook/field_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def _normalize_reoccurs(self):
for i in range(childcount,0,-1):
# we reverse loop since we are changing the list within the loop
child = self.children[i-1]
if type(child)==FieldGroup and child.repeat>1:
if child.repeat>1:
# deep copy
for clone_number in range(child.repeat,1,-1):
newchild = copy.deepcopy(child)
Expand Down
6 changes: 6 additions & 0 deletions copybook/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
+ Optional(
"OCCURS"
+ Word(nums)("occurs")
+ Optional("TIMES")
)
+ "."
)
Expand All @@ -88,6 +89,11 @@
Suppress("REDEFINES")
+ Word(alphanums+"-")("redefine_target")
)
+ Optional(
"OCCURS"
+ Word(nums)("occurs")
+ Optional("TIMES")
)
+ pic_expr
+ "."
+ ZeroOrMore(
Expand Down
12 changes: 12 additions & 0 deletions tests/test_parse_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,15 @@ def test_redefines_field():
assert result[0].get_total_length()==15
assert result[2].get_total_length()==10 and result[3].get_total_length()==10
assert [i.start_pos for i in result]==[0,0,5,5]

def test_occurs_field():
test_str = """
01 POLICY.
05 POLICY-KEY PIC 9(10).
05 ROW-EFFECTIVE-DT OCCURS 3 TIMES PIC 9(8).
"""
result = copybook.parse_string(test_str).flatten()
assert len(result)==5
assert [type(i) for i in result]==[copybook.FieldGroup,copybook.Field,copybook.Field,copybook.Field,copybook.Field]
assert result[0].get_total_length()==10+8*3
assert [i.start_pos for i in result]==[0,0,10,18,26]

0 comments on commit 2ff69a6

Please sign in to comment.