|
| 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