Exemple #1
0
 def lex(self):
     self._start = self._position
     self._kind = SyntaxKind.BAD_TOKEN
     self._value = None
     if self._current().isdigit():
         self._read_number()
     elif self._current().isspace():
         self._read_whitespace()
     elif self._current().isalpha():
         self._read_identifier_or_keyword()
     elif self._current() == "&" and self._lookahead() == "&":
         self._kind = SyntaxKind.AMPERSAND_AMPERSAND_TOKEN
         self._position += 2
     elif self._current() == "|" and self._lookahead() == "|":
         self._kind = SyntaxKind.PIPE_PIPE_TOKEN
         self._position += 2
     elif self._current() == "=" and self._lookahead() == "=":
         self._kind = SyntaxKind.EQUALS_EQUALS_TOKEN
         self._position += 2
     elif self._current() == "!" and self._lookahead() == "=":
         self._kind = SyntaxKind.BANG_EQUALS_TOKEN
         self._position += 2
     elif self._current() == "\0":
         self._kind = SyntaxKind.END_OF_FILE_TOKEN
         self._position += 1
     elif self._current() == "+":
         self._kind = SyntaxKind.PLUS_TOKEN
         self._position += 1
     elif self._current() == "-":
         self._kind = SyntaxKind.MINUS_TOKEN
         self._position += 1
     elif self._current() == "/":
         self._kind = SyntaxKind.SLASH_TOKEN
         self._position += 1
     elif self._current() == "*":
         self._kind = SyntaxKind.STAR_TOKEN
         self._position += 1
     elif self._current() == "(":
         self._kind = SyntaxKind.OPEN_PARENTHESIS_TOKEN
         self._position += 1
     elif self._current() == ")":
         self._kind = SyntaxKind.CLOSE_PARENTHESIS_TOKEN
         self._position += 1
     elif self._current() == "!":
         self._kind = SyntaxKind.BANG_TOKEN
         self._position += 1
     elif self._current() == "=":
         self._kind = SyntaxKind.EQUALS_TOKEN
         self._position += 1
     else:
         self.diagnostics.report_bad_character(
             TextSpan(self._position, 1), self._current()
         )
         self._position += 1
     text = syntax_facts.text_for(self._kind)
     if text is None:
         text = self._text[self._start:self._position]
     return SyntaxToken(self._kind, self._start, text, self._value)
 def __init__(self,
              kind: SyntaxKind,
              position: int,
              text: str,
              value: Any = None):
     self._span = TextSpan(position, len(text))
     self._kind = kind
     self.position = position
     self.text = text
     self.value = value
 def span(self) -> TextSpan:
     return TextSpan.from_bounds(self.operator_token.span().start,
                                 self.operand.span().end().start)
Exemple #4
0
 def span(self) -> TextSpan:
     return TextSpan.from_bounds(self.open_parenthesis_token.span().start,
                                 self.close_parenthesis_token.span().end())
 def span(self) -> TextSpan:
     return TextSpan.from_bounds(self.left.span().start,
                                 self.right.span().end())
Exemple #6
0
 def span_including_line_break(self) -> TextSpan:
     return TextSpan(self.start, self.length_including_line_break)
Exemple #7
0
 def span(self) -> TextSpan:
     return TextSpan(self.start, self.length)
Exemple #8
0
    compilation = Compilation(syntax_tree)
    result = compilation.evaluate(variables)
    diagnostics = result.diagnostics

    if show_tree:
        syntax_tree.write_to(console, True)
    text_builder = ""
    if len(diagnostics) > 0:
        text = syntax_tree.text
        for diagnostic in diagnostics:
            line_index = text.get_line_index(diagnostic.span.start)
            line_number = line_index + 1
            source_line = text.lines[line_index]
            character = diagnostic.span.start - source_line.start + 1

            prefix_span = TextSpan.from_bounds(source_line.start, diagnostic.span.start)
            suffix_span = TextSpan.from_bounds(diagnostic.span.end(), source_line.end)

            prefix = text[prefix_span]
            error = text[diagnostic.span.start:diagnostic.span.end()]
            suffix = text[suffix_span]

            console.print()
            console.print(f"({line_number}, {character}):", highlight=False)
            console.print(f"    {prefix}", end="", highlight=False)
            console.print(error, style="red", end="", highlight=False)
            console.print(suffix, highlight=False)

            console.print(str(diagnostic), style="red", highlight=False)
            console.print()
    else:
Exemple #9
0
 def span(self) -> TextSpan:
     return TextSpan.from_bounds(self.identifier_token.span().start,
                                 self.expression.span().end())