Esempio n. 1
0
def test_tree_end_column():
    """
    Ensures Tree.end_column can find the end column of a tree.
    """
    token = Token("WORD", "word")
    token.end_column = 1
    tree = Tree("outer", [Tree("path", [token])])
    assert tree.end_column() == "1"
Esempio n. 2
0
def test_tree_end_column():
    """
    Ensures Tree.end_column can find the end column of a tree.
    """
    token = Token('WORD', 'word')
    token.end_column = 1
    tree = Tree('outer', [Tree('path', [token])])
    assert tree.end_column() == '1'
Esempio n. 3
0
 def create_token(self, name, data):
     """
     Create a token from the current tree fragment and use the current tree
     for the position of the to-be-created token (line, column, end_column).
     """
     line = self.line()
     column = self.column()
     end_column = self.end_column()
     tok = Token(name, data, line=line, column=column)
     tok.end_column = end_column
     return tok
Esempio n. 4
0
 def assignment_path(self, path, value, line, eq_tok=None):
     """
     Adds a new assignment: `path` = `value`
     """
     # updates all tokens
     self.mark_line(value, line)
     equals = Token('EQUALS', '=', line=line)
     if eq_tok:
         # We accept and use the equals token from original expression
         # to copy over the token meta data which helps with error messages.
         equals.column = eq_tok.column
         equals.end_column = eq_tok.end_column
     if value.data == 'base_expression':
         expr = value
     else:
         expr = Tree('base_expression', [value])
     fragment = Tree('assignment_fragment', [equals, expr])
     return Tree('assignment', [path, fragment])
Esempio n. 5
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)