Skip to content

Commit e05f845

Browse files
transformer overhaul
1 parent 9693d42 commit e05f845

File tree

6 files changed

+604
-3
lines changed

6 files changed

+604
-3
lines changed

hcl2/api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
from lark.tree import Tree as AST
55
from hcl2.parser import parser
6-
from hcl2.transformer import DictTransformer
6+
from hcl2.dict_transformer import DictTransformer
77

88

99
def load(file: TextIO, with_meta=False) -> dict:

hcl2/transformer.py renamed to hcl2/dict_transformer.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,10 @@ def heredoc_template_trim(self, args: List) -> str:
257257
def new_line_or_comment(self, args: List) -> _DiscardType:
258258
return Discard
259259

260+
# def EQ(self, args: List):
261+
# print("EQ", args)
262+
# return args
263+
260264
def for_tuple_expr(self, args: List) -> str:
261265
args = self.strip_new_line_tokens(args)
262266
for_expr = " ".join([self.to_tf_inline(arg) for arg in args[1:-1]])

hcl2/rule_transformer.py

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# pylint: disable=missing-function-docstring,unused-argument
2+
from typing import List, Union
3+
4+
from lark import Transformer, Tree, Token
5+
from lark.visitors import _Leaf_T, _Return_T, Discard
6+
7+
from hcl2.serialization import (
8+
LarkRule,
9+
LarkToken,
10+
StartRule,
11+
BodyRule,
12+
BlockRule,
13+
IdentifierRule,
14+
IntLitRule,
15+
FloatLitRule,
16+
StringLitRule,
17+
ExprTermRule,
18+
ConditionalRule,
19+
BinaryOpRule,
20+
BinaryOperatorRule,
21+
BinaryTermRule,
22+
UnaryOpRule,
23+
AttributeRule,
24+
NewLineOrCommentRule,
25+
)
26+
27+
ArgsType = List[Union[Token, Tree]]
28+
29+
30+
class RuleTransformer(Transformer):
31+
"""Takes a syntax tree generated by the parser and
32+
transforms it to a tree of LarkRule instances
33+
"""
34+
35+
with_meta: bool
36+
37+
@staticmethod
38+
def is_type_keyword(value: str) -> bool:
39+
return value in {"bool", "number", "string"}
40+
41+
def __init__(self, with_meta: bool = False, with_comments: bool = True):
42+
"""
43+
:param with_meta: If set to true then adds `__start_line__` and `__end_line__`
44+
parameters to the output dict. Default to false.
45+
"""
46+
self._with_meta = with_meta
47+
self._with_comments = with_comments
48+
super().__init__()
49+
50+
def start(self, args: ArgsType) -> StartRule:
51+
return StartRule(args)
52+
53+
def body(self, args: ArgsType) -> BodyRule:
54+
return BodyRule(args)
55+
56+
def block(self, args: ArgsType) -> BlockRule:
57+
return BlockRule(args)
58+
59+
def identifier(self, args: ArgsType) -> IdentifierRule:
60+
return IdentifierRule(args)
61+
62+
def int_lit(self, args: ArgsType) -> IntLitRule:
63+
return IntLitRule(args)
64+
65+
def float_lit(self, args: ArgsType) -> FloatLitRule:
66+
return FloatLitRule(args)
67+
68+
def string_lit(self, args: ArgsType) -> StringLitRule:
69+
return StringLitRule(args)
70+
71+
def expr_term(self, args: ArgsType) -> ExprTermRule:
72+
return ExprTermRule(args)
73+
74+
def conditional(self, args: ArgsType) -> ConditionalRule:
75+
return ConditionalRule(args)
76+
77+
def binary_operator(self, args: ArgsType) -> BinaryOperatorRule:
78+
return BinaryOperatorRule(args)
79+
80+
def binary_term(self, args: ArgsType) -> BinaryTermRule:
81+
return BinaryTermRule(args)
82+
83+
def unary_op(self, args: ArgsType) -> UnaryOpRule:
84+
return UnaryOpRule(args)
85+
86+
def binary_op(self, args: ArgsType) -> BinaryOpRule:
87+
return BinaryOpRule(args)
88+
89+
def attribute(self, args: ArgsType) -> AttributeRule:
90+
return AttributeRule(args)
91+
92+
def new_line_or_comment(self, args: ArgsType) -> NewLineOrCommentRule:
93+
if self._with_comments:
94+
return NewLineOrCommentRule(args)
95+
return Discard
96+
97+
def transform(self, tree: Tree[_Leaf_T]) -> LarkRule:
98+
return super().transform(tree)
99+
100+
def __default_token__(self, token: Token) -> LarkToken:
101+
return LarkToken(token.type, token.value)

0 commit comments

Comments
 (0)