Пример #1
0
 def _codegen_impl(self, state: CodegenState) -> None:
     self.whitespace_before._codegen(state)
     with state.record_syntactic_position(self):
         state.add_token(self._get_tokens()[0])
         self.whitespace_between._codegen(state)
         state.add_token(self._get_tokens()[1])
     self.whitespace_after._codegen(state)
Пример #2
0
 def _codegen_impl(self, state: CodegenState) -> None:
     self.first_line._codegen(state)
     for line in self.empty_lines:
         line._codegen(state)
     if self.indent:
         state.add_indent_tokens()
     self.last_line._codegen(state)
Пример #3
0
 def _codegen_impl(self, state: CodegenState) -> None:
     if self.indent:
         state.add_indent_tokens()
     self.whitespace._codegen(state)
     if self.comment is not None:
         self.comment._codegen(state)
     self.newline._codegen(state)
Пример #4
0
 def _codegen_impl(self, state: CodegenState) -> None:
     state.increase_indent(self.value)
     with state.record_syntactic_position(self,
                                          start_node=self.child,
                                          end_node=self.child):
         self.child._codegen(state)
     state.decrease_indent()
Пример #5
0
    def _codegen_impl(self, state: CodegenState) -> None:
        with state.record_syntactic_position(self):
            self.target._codegen(state)

        self.whitespace_before_equal._codegen(state)
        # U+21D0 is "Leftwards Double Arrow" (a nice unicode rendering of
        # SystemVerilog's "<=" which doesn't collide with less-than-or-equal.
        state.add_token("\u21d0")
        self.whitespace_after_equal._codegen(state)
Пример #6
0
 def _codegen_impl(self, state: CodegenState) -> None:
     for h in self.header:
         h._codegen(state)
     for stmt in self.body:
         stmt._codegen(state)
     for f in self.footer:
         f._codegen(state)
     if self.has_trailing_newline:
         if len(state.tokens) == 0:
             # There was nothing in the header, footer, or body. Just add a newline
             # to preserve the trailing newline.
             state.add_token(state.default_newline)
     else:  # has_trailing_newline is false
         state.pop_trailing_newline()
Пример #7
0
 def get_modified_statement_code(self, node: BaseStatement) -> str:
     """
     Gets the new code for ``node`` as if it were in same location as the old
     statement being replaced. This means that it inherits details like the old
     statement's indentation.
     """
     new_codegen_state = CodegenState(
         default_indent=self._prev_codegen_state.default_indent,
         default_newline=self._prev_codegen_state.default_newline,
         indent_tokens=list(self._indent_tokens),
     )
     node._codegen(new_codegen_state)
     if not self.has_trailing_newline:
         new_codegen_state.pop_trailing_newline()
     return "".join(new_codegen_state.tokens)
Пример #8
0
 def _codegen_impl(self, state: CodegenState) -> None:
     for h in self.header:
         h._codegen(state)
     for stmt in self.body:
         stmt._codegen(state)
     for f in self.footer:
         f._codegen(state)
     if self.has_trailing_newline:
         if len(state.tokens) == 0:
             # There was nothing in the header, footer, or body. Just add a newline
             # to preserve the trailing newline.
             state.add_token(state.default_newline)
     else:  # has_trailing_newline is false
         if len(state.tokens) > 0:
             # EmptyLine and all statements generate newlines, so we can be sure that
             # the last token (if we're not an empty file) is a newline.
             state.tokens.pop()
Пример #9
0
def code(self):
    state = CodegenState(
        default_indent=' ' * 4,  # TODO: take in account the config
        default_newline="\n"  # TODO: take in account the config
    )
    self._codegen(state)

    return ''.join(state.tokens)
Пример #10
0
    def code_for_node(self, node: CSTNode) -> str:
        """
        Generates the code for the given node in the context of this module. This is a
        method of Module, not CSTNode, because we need to know the module's default
        indentation and newline formats.
        """

        state = CodegenState(default_indent=self.default_indent,
                             default_newline=self.default_newline)
        node._codegen(state)
        return "".join(state.tokens)
Пример #11
0
    def code_for_node(
            self,
            node: CSTNode,
            provider: Optional["_PositionProviderUnion"] = None) -> str:
        """
        Generates the code for the given node in the context of this module. This is a
        method of Module, not CSTNode, because we need to know the module's default
        indentation and newline formats.

        This can also be used with a :class:`~libcst.metadata.PositionProvider` or a
        :class:`~libcst.metadata.WhitespaceInclusivePositionProvider` to compute
        position information, but that's an implementation detail, and you should use
        :meth:`MetadataWrapper.resolve() <libcst.metadata.MetadataWrapper.resolve>`
        instead.

        See :ref:`Metadata<libcst-metadata>` for more information.
        """

        from libcst.metadata.position_provider import PositionProvider

        if provider is None:
            state = CodegenState(
                default_indent=self.default_indent,
                default_newline=self.default_newline,
                provider=provider,
            )
        elif isinstance(provider, PositionProvider):
            state = PositionProvidingCodegenState(
                default_indent=self.default_indent,
                default_newline=self.default_newline,
                provider=provider,
            )
        else:
            state = WhitespaceInclusivePositionProvidingCodegenState(
                default_indent=self.default_indent,
                default_newline=self.default_newline,
                provider=provider,
            )

        node._codegen(state)

        return "".join(state.tokens)
Пример #12
0
    def code_for_node(self,
                      node: CSTNode,
                      provider: Optional["PositionProvider"] = None) -> str:
        """
        Generates the code for the given node in the context of this module. This is a
        method of Module, not CSTNode, because we need to know the module's default
        indentation and newline formats.

        By default, this also generates syntactic line and column metadata for each
        node. Passing :class:`~libcst.BasicPositionProvider` will generate basic
        line and column metadata instead. See :ref:`Metadata<libcst-metadata>`
        for more information.
        """

        from libcst.metadata.position_provider import SyntacticPositionProvider

        if provider is None:
            state = CodegenState(
                default_indent=self.default_indent,
                default_newline=self.default_newline,
                provider=provider,
            )
        elif isinstance(provider, SyntacticPositionProvider):
            state = SyntacticCodegenState(
                default_indent=self.default_indent,
                default_newline=self.default_newline,
                provider=provider,
            )
        else:
            state = BasicCodegenState(
                default_indent=self.default_indent,
                default_newline=self.default_newline,
                provider=provider,
            )

        node._codegen(state)

        return "".join(state.tokens)
Пример #13
0
 def _codegen_impl(self, state: CodegenState) -> None:
     self.whitespace_before._codegen(state)
     state.add_token(self.value)
     self.whitespace_after._codegen(state)
Пример #14
0
 def _codegen_impl(self,
                   state: CodegenState,
                   default_semicolon: bool = False) -> None:
     with state.record_syntactic_position(self):
         self.target._codegen(state)
         self.value._codegen(state)
Пример #15
0
def cst_node_to_code(node):
    state = CodegenState(default_indent=4, default_newline='\n')
    node._codegen(state)
    return "".join(state.tokens)
Пример #16
0
 def _codegen_impl(self, state: CodegenState) -> None:
     state.add_token(self.value)
Пример #17
0
 def _codegen(self, state: CodegenState, **kwargs: Any) -> None:
     start = CodePosition(state.line, state.column)
     self._codegen_impl(state, **kwargs)
     end = CodePosition(state.line, state.column)
     state.record_position(self, CodeRange(start, end))
Пример #18
0
 def _codegen(self, state: CodegenState, **kwargs: Any) -> None:
     state.before_codegen(self)
     self._codegen_impl(state, **kwargs)
     state.after_codegen(self)
Пример #19
0
 def _codegen_impl(self, state: CodegenState) -> None:
     value = self.value
     state.add_token(state.default_newline if value is None else value)
Пример #20
0
 def _codegen_impl(self, state: CodegenState) -> None:
     state.add_token(self._get_token())
     self.whitespace_after._codegen(state)