private static bool TryTakeCloseParens(TokenKeeper pos) { if (pos.Next.TokenType == TokenType.CloseParens) { pos.Take(); return(true); } return(false); }
private static bool TryTakeToken(TokenType tokenType, TokenKeeper pos) { if (pos.Next.TokenType == tokenType) { pos.Take(); return(true); } return(false); }
private static bool TryTakeTerm(TokenKeeper pos, out ExpressionNode node) { if (TryTakeNumericLiteral(pos, out node) || TryTakeParensExp(pos, out node)) { return(true); } node = null; return(false); }
private static bool TryTakeCalcExp(TokenKeeper pos, out ExpressionNode node) { if (TryTakeOperatorExp(pos, out node) || TryTakeParensExp(pos, out node)) { return(true); } node = null; return(false); }
private static bool TryTakeOperator(TokenKeeper pos, out OperatorToken op) { if (pos.Next.TokenType == TokenType.Operator) { op = pos.Take() as OperatorToken; return(true); } op = null; return(false); }
private static bool TryTakeNumericLiteral(TokenKeeper pos, out ExpressionNode node) { if (pos.Next.TokenType == TokenType.NumericLiteral) { node = new NumericLiteralNode(pos.Take()); return(true); } node = null; return(false); }
private static bool TryTakeCalc(TokenKeeper pos, out ExpressionNode calculation) { if (TryTakeOperatorExp(pos, out calculation) || TryTakeLiteral(pos, out calculation) || TryTakeParensExp(pos, out calculation)) { return(true); } calculation = null; return(false); }
public void SwapExchangesPositionWithOtherTokenKeeper() { //Arrange var other = new TokenKeeper(_keeper); other.Take(); //Act _keeper.Swap(other); //Assert other.IsNext(TokenType.OpenParens).Should().BeTrue(); }
public void SwapTakesPositionFromOtherTokenKeeper() { //Arrange var other = new TokenKeeper(_keeper); other.Take(); //Act _keeper.Swap(other); //Assert _keeper.IsNext(TokenType.NumericLiteral).Should().BeTrue(); }
public static ArithmeticExpression Parse(string input) { var tokens = LexicalAnalyser.ExtractTokens(input ?? string.Empty); var pos = new TokenKeeper(tokens); if (TryTakeCalc(pos, out var calculation) && pos.Finished) { return(new ArithmeticExpression(calculation)); } else { var errorMessage = $"Unable to interpret calculation at \"{pos.RemainingData()}\""; return(new ArithmeticExpression(errorMessage)); } }
private static bool TryTakeOperatorExp(TokenKeeper pos, out ExpressionNode node) { var work = new TokenKeeper(pos); if (TryTakeTerm(work, out var left) && TryTakeOperator(work, out var op) && TryTakeSecondTerm(work, out var right)) { node = new OperatorExpNode(left, op, right); pos.Swap(work); return(true); } node = null; return(false); }
private static bool TryTakeParensExp(TokenKeeper pos, out ExpressionNode node) { var work = new TokenKeeper(pos); if (TryTakeOpenParens(work) && TryTakeCalcExp(work, out var calc) && TryTakeCloseParens(work)) { node = new ParensExpressionNode(calc); pos.Swap(work); return(true); } node = null; return(false); }
public void DiscardWhileShouldConsumeAllMatchingTokens() { //Arrange var original = new TokenKeeper(_keeper); //Act _keeper.DiscardWhile(TokenType.OpenParens, TokenType.NumericLiteral, TokenType.Operator); //Assert while (original.Next.TokenType != TokenType.CloseParens) { original.Take(); } var remaining = _keeper.RemainingData(); remaining.Should().Be(original.RemainingData()); }
public TestTokenKeeper() { _tokens = LexicalAnalyser.ExtractTokens(TestExpression).ToList(); _keeper = new TokenKeeper(_tokens); }