示例#1
0
    def _syntax_kind_str_round_trips(self, kind):
        text = SyntaxKind.value_exists(kind)

        if text is None:
            return

        tokens = SyntaxTree.parse_token(text)
        token = tokens[0]

        try:
            self._test_count += 1
            assert (len(tokens) == 2), f"Wrong amount of arguments: '{tokens}'"
        except:
            self._failed_test_count += 1
            traceback.print_exc()

        try:
            self._test_count += 1
            assert (kind == token.get_kind()), f"Unexpected kind <{SyntaxKind.kind_exists(token.get_kind())}>," \
                                               f" expected <{SyntaxKind.kind_exists(kind)}>"
        except:
            self._failed_test_count += 1
            traceback.print_exc()

        try:
            self._test_count += 1
            assert (text == token.get_value(
            )), f"Unexpected value '{token.get_value()}', expected '{text}'"
        except:
            self._failed_test_count += 1
            traceback.print_exc()
示例#2
0
    def _label_identifier(self):
        while self._is_letter(self._get_current_char()) or self._is_number(
                self._get_current_char()):
            self._next()

        self._value = self._text[self._start:self._pos]
        self._kind = SyntaxKind.get_keyword_kind(self._value)
        self._text_span = TextSpan(self._start, self._pos)
示例#3
0
    def _match_token(self, list_kind):
        error_text = ""

        if len(list_kind) == 0:
            return self._get_current_token()

        for i in list_kind:
            error_text += SyntaxKind.kind_exists(i) + "/"

            if self._get_current_token().get_kind() == i:
                return self._get_current_token()

        self._report_error(
            self._get_current_token().get_text_span(),
            DiagnosticBag.Message.token_unexpected_kind, [
                SyntaxKind.kind_exists(self._get_current_token().get_kind()),
                error_text[0:-1]
            ])

        return SyntaxToken(list_kind[0], 0,
                           self._get_current_token().get_text_span())
示例#4
0
    def _parse_expression(self, parent_precedence=0):
        unary_operator_precedence = SyntaxKind.get_unary_operator_precedence(
            self._get_current_token().get_kind())

        if unary_operator_precedence != 0 and unary_operator_precedence >= parent_precedence:
            operator_token = self._next_token()
            operand = self._parse_expression(unary_operator_precedence)
            left = UnaryExpressionSyntax(operator_token, operand)

        else:
            left = self._parse_primary()

        while True:
            precedence = SyntaxKind.get_binary_operator_precedence(
                self._get_current_token().get_kind())

            if precedence == 0 or precedence <= parent_precedence:
                break

            operator_token = self._next_token()
            right = self._parse_expression(precedence)
            left = BinaryExpressionSyntax(left, operator_token, right)

        return left
示例#5
0
    def _print(self, node, indent="", is_last=True):
        if is_last:
            maker = "└──"
        else:
            maker = "├──"

        print(indent, end='')
        print(maker, end='')
        print(SyntaxKind.kind_exists(node.get_kind()), end='')

        if isinstance(node, SyntaxToken) and node.get_value() is not None:
            print(f": {node.get_value()}")
        else:
            print()

        if is_last:
            indent += "   "
        else:
            indent += "│  "

        last_child = self._last_or_default(node.get_children())

        for child in node.get_children():
            self._print(child, indent, child == last_child)
示例#6
0
 def test_binary_operators(self):
     for i in SyntaxKind.str:
         if SyntaxKind.get_binary_operator_precedence(i) > 0:
             pass