Esempio n. 1
0
    def process_as(self, stack, parse_state):
        """
        Process an upcoming 'as' rule and decide whether it can occur.
        """
        if parse_state.in_assignment:
            # use of 'as' in an assignment is definitely invalid
            return
        if parse_state.nestedness != 0:
            # use of 'as' inside parenthesis is definitely invalid
            return

        as_keyword = KeywordCompletionSymbol(
            "as", sort_group=SortGroup.ContextKeyword)
        suffixes = ["service_op", "service_suffix"]

        try:
            last_rule = Stack.find_closest_rule(stack, suffixes)
            service_name = Stack.extract(stack, "value",
                                         "service_suffix")[0].value
            service_command = Stack.extract(stack, last_rule)[0].value
        except Exception:
            # attempt to parse service_block failed - must be a when_block or
            # similar
            yield as_keyword
            return

        action = self.service_handler.action(service_name, service_command)
        if action is None:
            return

        # only yield 'as' iff the service can start an event block (=it has
        # events)
        if len(action.events()) > 0:
            yield as_keyword
Esempio n. 2
0
    def process_args(self, stack):
        """
        Extract previous token for service or function argument completion
        with at least one previous argument. This looks for seen args
        and filters them out.
        """
        suffixes = ["fn_suffix", "service_suffix"]
        last_rule = Stack.find_closest_rule(stack, suffixes)

        # second or further arguments -> filter used arguments
        # <name> ':' <expr>
        prev_args = [
            *Stack.find_all_until(
                stack, "arglist", suffixes, start=0, offset=3)
        ]

        if last_rule == "service_suffix":
            yield from self.service.process_args(stack, prev_args)
        else:
            assert last_rule == "fn_suffix"
            yield from self.function.process_args(stack, prev_args)
Esempio n. 3
0
def test_stack_find_closest_rule_no_rule():
    stack = Parser().stack("foo bar foo:1")
    with raises(AssertionError):
        Stack.find_closest_rule(stack, ["no_rule"])
Esempio n. 4
0
def test_stack_find_closest_rule():
    stack = Parser().stack("foo bar foo:1")
    res = Stack.find_closest_rule(stack, ["arglist", "no_rule"])
    assert res == "arglist"