33
44from lark .tree import Meta
55
6+ from hcl2 .dict_transformer import START_LINE , END_LINE
67from hcl2 .rule_transformer .rules .abstract import LarkRule , LarkToken
7- from hcl2 .rule_transformer .rules .expression import Expression
8- from hcl2 .rule_transformer .rules .tokens import IdentifierToken , EQ_TOKEN
8+ from hcl2 .rule_transformer .rules .expressions import ExpressionRule
9+ from hcl2 .rule_transformer .rules .tokens import NAME , EQ
910
1011from hcl2 .rule_transformer .rules .whitespace import NewLineOrCommentRule
11- from hcl2 .rule_transformer .utils import SerializationOptions
12+ from hcl2 .rule_transformer .utils import SerializationOptions , SerializationContext
1213
1314
1415class AttributeRule (LarkRule ):
1516 _children : Tuple [
16- IdentifierToken ,
17- EQ_TOKEN ,
18- Expression ,
17+ NAME ,
18+ EQ ,
19+ ExpressionRule ,
1920 ]
2021
21- @property
22- def lark_name (self ) -> str :
22+ @staticmethod
23+ def lark_name () -> str :
2324 return "attribute"
2425
2526 @property
26- def identifier (self ) -> IdentifierToken :
27+ def identifier (self ) -> NAME :
2728 return self ._children [0 ]
2829
2930 @property
30- def expression (self ) -> Expression :
31+ def expression (self ) -> ExpressionRule :
3132 return self ._children [2 ]
3233
33- def serialize (self , options : SerializationOptions = SerializationOptions ()) -> Any :
34+ def serialize (
35+ self , options = SerializationOptions (), context = SerializationContext ()
36+ ) -> Any :
3437 return {self .identifier .serialize (options ): self .expression .serialize (options )}
3538
3639
@@ -44,11 +47,13 @@ class BodyRule(LarkRule):
4447 ]
4548 ]
4649
47- @property
48- def lark_name (self ) -> str :
50+ @staticmethod
51+ def lark_name () -> str :
4952 return "body"
5053
51- def serialize (self , options : SerializationOptions = SerializationOptions ()) -> Any :
54+ def serialize (
55+ self , options = SerializationOptions (), context = SerializationContext ()
56+ ) -> Any :
5257 blocks : List [BlockRule ] = []
5358 attributes : List [AttributeRule ] = []
5459 comments = []
@@ -99,11 +104,13 @@ class StartRule(LarkRule):
99104 def body (self ) -> BodyRule :
100105 return self ._children [0 ]
101106
102- @property
103- def lark_name (self ) -> str :
107+ @staticmethod
108+ def lark_name () -> str :
104109 return "start"
105110
106- def serialize (self , options : SerializationOptions = SerializationOptions ()) -> Any :
111+ def serialize (
112+ self , options = SerializationOptions (), context = SerializationContext ()
113+ ) -> Any :
107114 return self .body .serialize (options )
108115
109116
@@ -118,23 +125,31 @@ def __init__(self, children, meta: Optional[Meta] = None):
118125 child for child in children if not isinstance (child , LarkToken )
119126 ]
120127
121- @property
122- def lark_name (self ) -> str :
128+ @staticmethod
129+ def lark_name () -> str :
123130 return "block"
124131
125132 @property
126- def labels (self ) -> List [IdentifierToken ]:
133+ def labels (self ) -> List [NAME ]:
127134 return list (filter (lambda label : label is not None , self ._labels ))
128135
129136 @property
130137 def body (self ) -> BodyRule :
131138 return self ._body
132139
133140 def serialize (
134- self , options : SerializationOptions = SerializationOptions ()
135- ) -> BodyRule :
141+ self , options = SerializationOptions (), context = SerializationContext ()
142+ ) -> Any :
136143 result = self ._body .serialize (options )
137144 labels = self ._labels
138- for label in reversed (labels ):
145+ for label in reversed (labels [ 1 :] ):
139146 result = {label .serialize (options ): result }
147+
148+ result .update (
149+ {
150+ START_LINE : self ._meta .line ,
151+ END_LINE : self ._meta .end_line ,
152+ }
153+ )
154+
140155 return result
0 commit comments