Ejemplo n.º 1
0
    def eval(self, orig_node, code_string, fake_tree):
        """
        Evaluates a string by parsing it to its AST representation.
        Inserts the AST expression as fake_node and returns the path
        reference to the inserted fake_node.
        """
        line = orig_node.line()
        column = int(orig_node.column()) + 1
        # add whitespace as padding to fixup the column location of the
        # resulting tokens.
        from storyscript.Story import Story

        story = Story(" " * column + code_string, features=self.features)
        story.parse(self.parser, allow_single_quotes=True)
        new_node = story.tree

        new_node = new_node.block
        orig_node.expect(new_node, "string_templates_no_assignment")
        # go to the actual node -> jump into block.rules or block.service
        for i in range(2):
            orig_node.expect(
                len(new_node.children) == 1, "string_templates_no_assignment"
            )
            new_node = new_node.children[0]
        # for now only expressions or service_blocks are allowed inside string
        # templates
        if (
            new_node.data == "service_block"
            and new_node.service_fragment is None
        ):
            # it was a plain-old path initially
            name = Token("NAME", code_string.strip(), line=line, column=column)
            name.end_column = int(orig_node.end_column()) - 1
            return Tree("path", [name])
        if new_node.data == "absolute_expression":
            new_node = new_node.children[0]
        else:
            orig_node.expect(
                new_node.data == "service", "string_templates_no_assignment"
            )

        # the new assignment should be inserted at the top of the current block
        return fake_tree.add_assignment(new_node, original_line=line)
Ejemplo n.º 2
0
 def format(self, ws, doc):
     last_line = doc.nr_lines() - 1
     story = Story(doc.text(), features=None)
     new_story = story.parse(parser=None).format()
     resp = [{
         "range": {
             "start": {
                 "line": 0,
                 "character": 0
             },
             "end": {
                 "line": last_line,
                 "character": len(doc.line(last_line)),
             },
         },
         "newText": new_story,
     }]
     return resp