private List<FormalParameter> ParseFormalParameterList()
{
var list = new List<FormalParameter>();
Match(TokenType.LeftParen);
while (Next().Is(TokenType.Multiply) ||
Next().Is(TokenType.Identifier))
{
var parameter = new FormalParameter() {Token = Next()};
if (Next().Is(TokenType.Multiply))
{
Match(TokenType.Multiply);
parameter.IsParams = true;
}
parameter.Identifier = ParseIdentifier();
if (list.Any(param => param.Identifier.Equals(parameter.Identifier)))
throw Error("Duplicated parameter name", Next());
list.Add(parameter);
if (Next().Is(TokenType.Comma))
{
Match(TokenType.Comma);
continue;
}
if (Next().Is(TokenType.RightParen))
break;
throw Error("Unexpected token", Next());
}
if (list.Any(p => p.IsParams && list.IndexOf(p) != list.Count - 1))
throw Error("Var-arg parameter must be the last parameter", Next());
Match(TokenType.RightParen);
return list;
}