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
def f(): stream = StringStream(expression) _numberParser = NumberParser() _operatorParser = OperatorParser() number = _numberParser.parse(stream) operator = _operatorParser.parse(stream) number = _numberParser.parse(stream) if stream.has_chars(): return "Stream should now be empty"
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)
def f(): stream = StringStream(expression) _stack = ParserStack() _logic = ParserStackInteractor(_stack) _numberParser = NumberParser() _operatorParser = OperatorParser() _expressionParser = ExpressionParser(_logic, _numberParser, _operatorParser) result = _expressionParser.parse(stream) if not result.evaluate() == expectedResult: return "Result is: %s. \n object is: %s" % (str( result.evaluate()), str(result))
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)
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()
def testStream(): stream = StringStream("1+1") def f(): stream.peek() stream.next() stream.peek() stream.next() stream.peek() stream.peek() stream.peek() stream.next() stream.peek() stream.peek() if stream.has_chars(): return "Stream should be empty" return utils.Test("Test that stream operators work", f)
def _can_consumeDigit(self, stream: StringStream) -> bool: token = stream.peek() return self._isSingleDigitNum(token)
def can_consume(self, stream: StringStream) -> bool: token = stream.peek() return self._isSingleDigitNum(token) or self._isNegativeSign(token)
def _is_subtract_character(self, stream: StringStream) -> bool: return self._stack_interactor.query.is_awaiting_operator( ) and stream.peek() == '-'
def _is_open_bracket(self, stream: StringStream) -> bool: return stream.peek() == '('
def evaluate(expressionString): stream = StringStream(expressionString) return parse(stream).evaluate()
def can_consume(self, stream: StringStream) -> bool: token = stream.peek() return self._isOperator(token)