1
+ from collections import defaultdict
1
2
from typing import Tuple , Any , List , Union , Optional
2
3
3
4
from lark .tree import Meta
7
8
from hcl2 .rule_transformer .rules .token_sequence import IdentifierRule
8
9
9
10
from hcl2 .rule_transformer .rules .whitespace import NewLineOrCommentRule
11
+ from hcl2 .rule_transformer .utils import SerializationOptions
10
12
11
13
12
14
class AttributeRule (LarkRule ):
@@ -28,8 +30,8 @@ def identifier(self) -> IdentifierRule:
28
30
def expression (self ) -> Expression :
29
31
return self ._children [2 ]
30
32
31
- def serialize (self ) -> Any :
32
- return {self .identifier .serialize (): self .expression .serialize ()}
33
+ def serialize (self , options : SerializationOptions = SerializationOptions () ) -> Any :
34
+ return {self .identifier .serialize (options ): self .expression .serialize (options )}
33
35
34
36
35
37
class BodyRule (LarkRule ):
@@ -46,34 +48,51 @@ class BodyRule(LarkRule):
46
48
def rule_name () -> str :
47
49
return "body"
48
50
49
- def serialize (self ) -> Any :
51
+ def serialize (self , options : SerializationOptions = SerializationOptions () ) -> Any :
50
52
blocks : List [BlockRule ] = []
51
53
attributes : List [AttributeRule ] = []
52
54
comments = []
53
-
55
+ inline_comments = []
54
56
for child in self ._children :
57
+
55
58
if isinstance (child , BlockRule ):
56
59
blocks .append (child )
60
+
57
61
if isinstance (child , AttributeRule ):
58
62
attributes .append (child )
63
+ # collect in-line comments from attribute assignments, expressions etc
64
+ inline_comments .extend (child .expression .inline_comments ())
65
+
59
66
if isinstance (child , NewLineOrCommentRule ):
60
- child_comments = child .actual_comments ()
67
+ child_comments = child .to_list ()
61
68
if child_comments :
62
69
comments .extend (child_comments )
63
70
64
71
result = {}
65
72
66
73
for attribute in attributes :
67
74
result .update (
68
- {attribute .identifier .serialize (): attribute .expression .serialize ()}
75
+ {
76
+ attribute .identifier .serialize (
77
+ options
78
+ ): attribute .expression .serialize (options )
79
+ }
69
80
)
70
81
71
- result .update (
72
- {block .labels [0 ].serialize (): block .serialize () for block in blocks }
73
- )
82
+ result_blocks = defaultdict (list )
83
+ for block in blocks :
84
+ name = block .labels [0 ].serialize (options )
85
+ if name in result .keys ():
86
+ raise RuntimeError (f"Attribute { name } is already defined." )
87
+ result_blocks [name ].append (block .serialize (options ))
88
+
89
+ result .update (** result_blocks )
74
90
75
- if comments :
76
- result ["__comments__" ] = comments
91
+ if options .with_comments :
92
+ if comments :
93
+ result ["__comments__" ] = comments
94
+ if inline_comments :
95
+ result ["__inline_comments__" ] = inline_comments
77
96
78
97
return result
79
98
@@ -90,8 +109,8 @@ def rule_name() -> str:
90
109
def body (self ) -> BodyRule :
91
110
return self ._children [0 ]
92
111
93
- def serialize (self ) -> Any :
94
- return self .body .serialize ()
112
+ def serialize (self , options : SerializationOptions = SerializationOptions () ) -> Any :
113
+ return self .body .serialize (options )
95
114
96
115
97
116
class BlockRule (LarkRule ):
@@ -103,7 +122,7 @@ def rule_name() -> str:
103
122
return "block"
104
123
105
124
def __init__ (self , children , meta : Optional [Meta ] = None ):
106
- super ().__init__ (children )
125
+ super ().__init__ (children , meta )
107
126
* self ._labels , self ._body = children
108
127
109
128
@property
@@ -114,9 +133,11 @@ def labels(self) -> List[IdentifierRule]:
114
133
def body (self ) -> BodyRule :
115
134
return self ._body
116
135
117
- def serialize (self ) -> BodyRule :
118
- result = self ._body .serialize ()
136
+ def serialize (
137
+ self , options : SerializationOptions = SerializationOptions ()
138
+ ) -> BodyRule :
139
+ result = self ._body .serialize (options )
119
140
labels = self ._labels
120
141
for label in reversed (labels [1 :]):
121
- result = {label .serialize (): result }
142
+ result = {label .serialize (options ): result }
122
143
return result
0 commit comments