/// <summary> /// Matches the specified string. /// </summary> /// <param name="target">The string to match.</param> /// <param name="position">The position at the <paramref name="target"/> to start with.</param> /// <param name="matchingResults">The mathing results.</param> /// <returns> /// <c>true</c> if matching was sucessfull; otherwise - <c>false</c>. /// </returns> public override MatchingResult Match(string target, int position, IList <string> matchingResults) { Assert.ArgumentNotNull(target, "target"); Assert.ArgumentNotNull(matchingResults, "matchingResults"); if (NextToken != null) { var res = NextToken.Match(target, position, matchingResults); return(res.IsSuccess ? new MatchingResult { IsSuccess = true, StartPosition = position, EndPosition = res.EndPosition } : new MatchingResult { IsSuccess = false }); } if (position < target.Length) { var value = target.Substring(position); return(TryToSetValue(value, matchingResults) ? new MatchingResult { IsSuccess = true, StartPosition = position, EndPosition = target.Length - 1 } : new MatchingResult { IsSuccess = false }); } return(new MatchingResult { IsSuccess = true, StartPosition = position, EndPosition = target.Length - 1 }); }
public PascalParser(PeekToken peekToken, NextToken nextToken, PeekAndNext peekAndNext, NextAndPeek nextAndPeek) { PeekToken = peekToken; NextToken = nextToken; PeekAndNext = peekAndNext; NextAndPeek = nextAndPeek; }
/// <summary> /// Queries the Reserve Table to determine if the current token is a reserve word. /// If it is then the proper token code is returned from the table. /// If it is not a reserve word it is given the identifier token code. /// </summary> /// <returns></returns> private int GetIdentifierCode() { int code = ReserveTable.LookupName(NextToken.ToUpper()); if (code == -1) { return(IDENTIFIER); } return(code); }
/// <summary> /// Gets the value of this pattern with applied matching results. /// </summary> /// <param name="matchingResults">The matching results.</param> /// <returns> /// A string value of this patthern. /// </returns> public override string GetValue(IList <string> matchingResults) { Assert.ArgumentNotNull(matchingResults, "matchingResults"); if (NextToken != null) { return(SearchValue + NextToken.GetValue(matchingResults)); } return(SearchValue); }
/// <summary> /// Gets the value of this pattern with applied matching results. /// </summary> /// <param name="matchingResults">The matching results.</param> /// <returns> /// A string value of this patthern. /// </returns> public override string GetValue(IList <string> matchingResults) { Assert.ArgumentNotNull(matchingResults, "matchingResults"); var val = matchingResults[_wildcardNumber] ?? string.Empty; if (NextToken != null) { return(val + NextToken.GetValue(matchingResults)); } return(val); }
public double Evaluation() { double value = 0; switch (this.Expression) { case Expression.Add: value = PreviousToken.Evaluation() + NextToken.Evaluation(); break; case Expression.Subtract: value = PreviousToken.Evaluation() - NextToken.Evaluation(); break; case Expression.Multiply: value = PreviousToken.Evaluation() * NextToken.Evaluation(); break; case Expression.Divide: if (NextToken.Evaluation() == 0) { throw new RuntimeError("You attempted to divide by zero."); } value = PreviousToken.Evaluation() / NextToken.Evaluation(); break; case Expression.Positive: value = NextToken.Evaluation(); break; case Expression.Negative: value = NextToken.Evaluation() * -1; break; case Expression.Exponent: value = Math.Pow(PreviousToken.Evaluation(), NextToken.Evaluation()); break; case Expression.OpenParenthesis: value = NextToken.Evaluation(); break; default: break; } return(value); }
/// <summary> /// Matches the specified string. /// </summary> /// <param name="target">The string to match.</param> /// <param name="position">The position at the <paramref name="target"/> to start with.</param> /// <param name="matchingResults">The mathing results.</param> /// <returns> /// <c>true</c> if matching was sucessfull; otherwise - <c>false</c>. /// </returns> public override MatchingResult Match(string target, int position, IList <string> matchingResults) { Assert.ArgumentNotNull(target, "target"); Assert.ArgumentNotNull(matchingResults, "matchingResults"); var foundPosition = target.IndexOf(SearchValue, position, StringComparison.Ordinal); if (foundPosition < 0) { return(new MatchingResult { IsSuccess = false }); } if (_anchored && foundPosition != position) { return(new MatchingResult { IsSuccess = false }); } var previousWildcardToken = _previousToken as WildcardToken; if (previousWildcardToken != null && !previousWildcardToken.TryToSetValue(target.Substring(position, foundPosition - position), matchingResults)) { return(new MatchingResult { IsSuccess = false }); } if (NextToken != null) { var res = NextToken.Match(target, foundPosition + SearchValue.Length, matchingResults); return(res.IsSuccess ? new MatchingResult { IsSuccess = true, StartPosition = foundPosition, EndPosition = res.EndPosition } : new MatchingResult { IsSuccess = false }); } return(new MatchingResult { IsSuccess = true, StartPosition = foundPosition, EndPosition = foundPosition + SearchValue.Length }); }
/// <summary> /// Checks if the token is already in the symbol table. /// If it is not then it is added, otherwise it does nothing. /// </summary> private void AddTokenToSymbolTable() { string tokenToAdd; if (TokenCode == IDENTIFIER) { tokenToAdd = NextToken.ToUpper(); } else { tokenToAdd = NextToken; } int symbolIndex = SymbolTable.LookupSymbol(tokenToAdd); if (symbolIndex == -1) { switch (TokenCode) { case IDENTIFIER: SymbolTable.AddSymbol(tokenToAdd, SymbolKind.Variable, 0); break; case INTEGER: SymbolTable.AddSymbol(tokenToAdd, SymbolKind.Constant, Int64.Parse(tokenToAdd)); break; case FLOATING_POINT: SymbolTable.AddSymbol(tokenToAdd, SymbolKind.Constant, Double.Parse(tokenToAdd)); break; case STRING: SymbolTable.AddSymbol(tokenToAdd, SymbolKind.Constant, tokenToAdd); break; } } }
/// <summary> /// Truncates the token if it is too long for the defined token type /// and displays a warning message. /// </summary> private void TruncateTokenIfTooLong() { // TODO: differentiate between numeric and identifiers. int maxLength; if (TokenCode == IDENTIFIER) { maxLength = MAX_IDENTIFIER_LENGTH; } else if (TokenCode == FLOATING_POINT || TokenCode == INTEGER) { maxLength = MAX_NUMERIC_LENGTH; } else { return; } if (NextToken.Length > maxLength) { Console.WriteLine("\tWARNING: Token length exceeds " + maxLength + ". Token has been truncated."); NextToken = NextToken.Substring(0, maxLength); } }