private void Validate(Argument item) { if (!string.IsNullOrEmpty(item.Name) && Contains(item.Name)) { throw new ArgumentException(Utilities.InvariantFormat("Argument with name \"{0}\" already exists.", item.Name)); } }
protected override object DoVisit(UnaryExpression unaryExpression) { switch (unaryExpression.Operator.Token) { case '-': dynamic operand = Visit(unaryExpression.Operand); try { return(-operand); } catch (RuntimeBinderException) { errorLogger.LogError( unaryExpression.Operator.Location, string.Format( "Operator \"-\" cannot be applied to operand of type \"{0}\".", operand.GetType())); return(Error); } default: errorLogger.LogError( unaryExpression.Operator.Location, Utilities.InvariantFormat("Invalid operator \"{0}\".", unaryExpression.Operator.Text)); return(Error); } }
protected override object DoVisit(BinaryExpression binaryExpression) { dynamic leftOperand = Visit(binaryExpression.LeftExpression); dynamic rightOperand = Visit(binaryExpression.RightExpression); if ((object)leftOperand == Error || (object)rightOperand == Error) { return(Error); } try { switch (binaryExpression.Operator.Token) { case '=': return(leftOperand == rightOperand); case '!': return(leftOperand != rightOperand); case '>': return(leftOperand > rightOperand); case '<': return(leftOperand < rightOperand); case ',': return(leftOperand || rightOperand); case Token.LessOrEqual: return(leftOperand <= rightOperand); case Token.GreaterOrEqual: return(leftOperand >= rightOperand); case Token.And: return(leftOperand && rightOperand); default: errorLogger.LogError( binaryExpression.Operator.Location, Utilities.InvariantFormat("Invalid operator \"{0}\".", binaryExpression.Operator.Text)); return(Error); } } catch (RuntimeBinderException) { errorLogger.LogError( binaryExpression.Operator.Location, string.Format( "Operator \"{0}\" cannot be applied to operands of type \"{1}\" and \"{2}\".", binaryExpression.Operator.Text, leftOperand.GetType(), rightOperand.GetType())); return(Error); } }
private FormatString ParseFormatString(int start) { var items = new List <FormatStringItem>(); while (true) { switch (nextTokenInfo.Token) { case '{': try { items.Add(ParseFormat()); } catch (FormattingException e) { foreach (Error error in e.Errors) { errorLogger.LogError(error); } scanner.State = ScannerState.ScanningText; while (nextTokenInfo.Token != '}' && nextTokenInfo.Token != Token.EndOfInput) { Consume(); } break; } continue; case '}': if (start > 0) { return(CreateFormatString(start, items)); } errorLogger.LogError(nextTokenInfo.Location, "Unescaped \"}\"."); break; case Token.EndOfInput: return(CreateFormatString(start, items)); case Token.Text: items.Add(new Text(nextTokenInfo.Location, nextTokenInfo.Text)); break; default: // this should not happen throw new InvalidOperationException( Utilities.InvariantFormat("Token {0} is not valid here.", Token.ToString(nextTokenInfo.Token))); } Consume(); } }
protected override object DoVisit(ArgumentName argumentName) { if (!arguments.Contains(argumentName.Name)) { errorLogger.LogError( argumentName.Location, Utilities.InvariantFormat("Argument with name \"{0}\" doesn't exist.", argumentName.Name)); return(Error); } return(arguments[argumentName.Name].Value); }
protected override object DoVisit(ArgumentIndex argumentIndex) { if (argumentIndex.Index >= arguments.Count) { errorLogger.LogError( argumentIndex.Location, Utilities.InvariantFormat("Argument index {0} is out of range.", argumentIndex.Index)); return(Error); } return(arguments[argumentIndex.Index].Value); }
public static string ToString(int token) { if (token >= char.MinValue && token <= char.MaxValue) { return(((char)token).ToString(CultureInfo.InvariantCulture)); } string name; if (TokenNames.TryGetValue(token, out name)) { return(name); } throw new ArgumentException(Utilities.InvariantFormat("{0} is not valid token.", token)); }
private int ScanText() { do { char c = input[positionInInput++]; switch (c) { case '{': case '}': if (textBuilder.Length == 0) { textBuilder.Append(c); return(c); } positionInInput--; return(Token.Text); case '\\': if (positionInInput == input.Length) { errorLogger.LogError( new Location(positionInInput, positionInInput), "Unexpected end of input."); } else if (!Utilities.MustBeEscaped(c = input[positionInInput++])) { errorLogger.LogError( new Location(positionInInput - 2, positionInInput), Utilities.InvariantFormat("\"{0}\" cannot be escaped.", c)); } break; } textBuilder.Append(c); }while(positionInInput < input.Length); return(Token.Text); }
public TokenInfo Scan() { textBuilder.Clear(); if (State == ScannerState.ScanningTokens) { SkipWhile(char.IsWhiteSpace); } int token; if ((tokenStart = positionInInput) < input.Length) { switch (State) { case ScannerState.ScanningText: token = ScanText(); break; case ScannerState.ScanningTokens: token = ScanTokens(); break; default: // this should not happen throw new InvalidOperationException( Utilities.InvariantFormat("State \"{0}\" is not supported.", State)); } } else { token = Token.EndOfInput; } return(new TokenInfo( new Location(tokenStart, positionInInput), token, textBuilder.ToString())); }
private static FormattingException SyntaxError(TokenInfo tokenInfo, string format, params object[] arguments) { return(tokenInfo.Token == Token.EndOfInput ? new FormattingException(tokenInfo.Location, "Unexpected end of input.") : new FormattingException(tokenInfo.Location, Utilities.InvariantFormat(format, arguments))); }
public override string ToString() { // ReSharper disable PossibleUnintendedReferenceComparison return(this == Unknown ? "Unknown" : Utilities.InvariantFormat("{0}, {1}", Start, End)); // ReSharper restore PossibleUnintendedReferenceComparison }