Основы объектно-ориентированного программирования на языке C# book | Page 191
8.5 Комплексна вправа до роздiлу 7
private readonly Expression content;
public Expression Content { get { return content; } }
public GroupExpression(Expression content)
{
if (content == null)
throw new ArgumentNullException();
if (content is EmptyExpression)
throw new ArgumentException("Group expression cannot
be empty");
this.content = content;
}
public override long Eval()
{
return content.Eval();
}
internal static bool TryRead(IInput input, out IToken result)
{
if (input.IsEmpty())
{
result = null;
return false;
}
else
{
if (input.Current == Symbols.OpenBrace)
{
input.MoveNext();
int OpenBracePos = input.Position;
Expression body = Expression.Parse(input,
Symbols.CloseBrace);
if (body is EmptyExpression)
throw new ExpressionSyntaxError(
ExpressionSyntaxError.ErrorReason.
EmptyParentheses, OpenBracePos);
result = new GroupExpression(body);
return true;
}
else
{
result = null;
return false;
}
}
}
}
private sealed class RawExpression : Expression
{
private List tokens = new List();
private bool AcceptNum = true;
private bool AcceptOp = true;
private bool AcceptGrp = true;
private RawExpression() { }
191