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()
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)
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())
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
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)
def test_binary_operators(self): for i in SyntaxKind.str: if SyntaxKind.get_binary_operator_precedence(i) > 0: pass