3
3
4
4
from lark .tree import Meta
5
5
6
+ from hcl2 .dict_transformer import START_LINE , END_LINE
6
7
from 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
9
10
10
11
from hcl2 .rule_transformer .rules .whitespace import NewLineOrCommentRule
11
- from hcl2 .rule_transformer .utils import SerializationOptions
12
+ from hcl2 .rule_transformer .utils import SerializationOptions , SerializationContext
12
13
13
14
14
15
class AttributeRule (LarkRule ):
15
16
_children : Tuple [
16
- IdentifierToken ,
17
- EQ_TOKEN ,
18
- Expression ,
17
+ NAME ,
18
+ EQ ,
19
+ ExpressionRule ,
19
20
]
20
21
21
- @property
22
- def lark_name (self ) -> str :
22
+ @staticmethod
23
+ def lark_name () -> str :
23
24
return "attribute"
24
25
25
26
@property
26
- def identifier (self ) -> IdentifierToken :
27
+ def identifier (self ) -> NAME :
27
28
return self ._children [0 ]
28
29
29
30
@property
30
- def expression (self ) -> Expression :
31
+ def expression (self ) -> ExpressionRule :
31
32
return self ._children [2 ]
32
33
33
- def serialize (self , options : SerializationOptions = SerializationOptions ()) -> Any :
34
+ def serialize (
35
+ self , options = SerializationOptions (), context = SerializationContext ()
36
+ ) -> Any :
34
37
return {self .identifier .serialize (options ): self .expression .serialize (options )}
35
38
36
39
@@ -44,11 +47,13 @@ class BodyRule(LarkRule):
44
47
]
45
48
]
46
49
47
- @property
48
- def lark_name (self ) -> str :
50
+ @staticmethod
51
+ def lark_name () -> str :
49
52
return "body"
50
53
51
- def serialize (self , options : SerializationOptions = SerializationOptions ()) -> Any :
54
+ def serialize (
55
+ self , options = SerializationOptions (), context = SerializationContext ()
56
+ ) -> Any :
52
57
blocks : List [BlockRule ] = []
53
58
attributes : List [AttributeRule ] = []
54
59
comments = []
@@ -99,11 +104,13 @@ class StartRule(LarkRule):
99
104
def body (self ) -> BodyRule :
100
105
return self ._children [0 ]
101
106
102
- @property
103
- def lark_name (self ) -> str :
107
+ @staticmethod
108
+ def lark_name () -> str :
104
109
return "start"
105
110
106
- def serialize (self , options : SerializationOptions = SerializationOptions ()) -> Any :
111
+ def serialize (
112
+ self , options = SerializationOptions (), context = SerializationContext ()
113
+ ) -> Any :
107
114
return self .body .serialize (options )
108
115
109
116
@@ -118,23 +125,31 @@ def __init__(self, children, meta: Optional[Meta] = None):
118
125
child for child in children if not isinstance (child , LarkToken )
119
126
]
120
127
121
- @property
122
- def lark_name (self ) -> str :
128
+ @staticmethod
129
+ def lark_name () -> str :
123
130
return "block"
124
131
125
132
@property
126
- def labels (self ) -> List [IdentifierToken ]:
133
+ def labels (self ) -> List [NAME ]:
127
134
return list (filter (lambda label : label is not None , self ._labels ))
128
135
129
136
@property
130
137
def body (self ) -> BodyRule :
131
138
return self ._body
132
139
133
140
def serialize (
134
- self , options : SerializationOptions = SerializationOptions ()
135
- ) -> BodyRule :
141
+ self , options = SerializationOptions (), context = SerializationContext ()
142
+ ) -> Any :
136
143
result = self ._body .serialize (options )
137
144
labels = self ._labels
138
- for label in reversed (labels ):
145
+ for label in reversed (labels [ 1 :] ):
139
146
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
+
140
155
return result
0 commit comments