예제 #1
0
    def _consumeNumber(self,
                       stream: StringStream) -> Optional[NumberExpression]:
        if not self._can_consumeDigit(stream):
            return None

        token = stream.next()
        while self._can_consumeDigit(stream):
            token += stream.next()
        return token
예제 #2
0
    def parse(self, stream: StringStream) -> Optional[NumberExpression]:
        if not self.can_consume(stream):
            return None

        token: str = stream.peek()
        if token.isspace():
            stream.next()
            return self.parse(stream)

        isNegative = self._isNegativeSign(token)

        if isNegative:
            stream.next()

        numberToken = self._consumeNumber(stream)
        return NumberExpression(numberToken, isNegative)
예제 #3
0
    def parse(self, stream: StringStream) -> Optional[Expression]:
        while stream.has_chars() and not stream.peek() == ')':

            if not self._can_consume(stream):
                stream.next()
                continue

            if self._consume_number(stream):
                continue

            self._consume_operator(stream)

            if stream.peek() == '(':
                stream.next()
                self._evaluate_bracket_expression(stream)

        return self._tree.create_expression()
예제 #4
0
    def parse(self, stream: StringStream) -> Optional[Operator]:
        if not self.can_consume(stream):
            return None

        token: str = stream.next()

        if token == '+':
            return AddOperator()
        elif token == '-':
            return SubtractOperator()
        elif token == '*':
            return MultiplyOperator()
        elif token == '/':
            return DivideOperator()
        elif token.isspace():
            return self.parse(stream)