private void CheckUniqueArgument(List<Arg> names, Arg arg) { if (arg != null && arg.Name != null) { string name = arg.Name; for (int i = 0; i < names.Count; i++) { if (names[i].Name == arg.Name) { ReportSyntaxError("duplicate keyword argument"); } } } }
// arglist: // expression rest_of_arguments // expression "=" expression rest_of_arguments // expression "for" gen_expr_rest // private Arg[] FinishArgListOrGenExpr(out List<string> commaWhiteSpace, out bool ateTerminator) { Arg a = null; commaWhiteSpace = MakeWhiteSpaceList(); Token t = PeekToken(); if (t.Kind != TokenKind.RightParenthesis && t.Kind != TokenKind.Multiply && t.Kind != TokenKind.Power) { Expression e = ParseExpression(); if (e is ErrorExpression) { ateTerminator = false; return new[] { new Arg(e) }; } if (MaybeEat(TokenKind.Assign)) { // Keyword argument a = FinishKeywordArgument(e); } else if (PeekToken(Tokens.KeywordForToken)) { // Generator expression var genExpr = ParseGeneratorExpression(e); AddIsAltForm(genExpr); a = new Arg(genExpr); ateTerminator = Eat(TokenKind.RightParenthesis); a.SetLoc(e.StartIndex, GetEnd()); return new Arg[1] { a }; // Generator expression is the argument } else { a = new Arg(e); a.SetLoc(e.StartIndex, e.EndIndex); } // Was this all? // if (MaybeEat(TokenKind.Comma)) { if (commaWhiteSpace != null) { commaWhiteSpace.Add(_tokenWhiteSpace); } } else { ateTerminator = Eat(TokenKind.RightParenthesis); a.SetLoc(e.StartIndex, GetEnd()); return new Arg[1] { a }; } } return FinishArgumentList(a, commaWhiteSpace, out ateTerminator); // TODO: Use ateTerminator }
private Arg FinishKeywordArgument(Expression t) { Debug.Assert(_token.Token.Kind == TokenKind.Assign); string equalWhiteSpace = _tokenWhiteSpace; NameExpression n = t as NameExpression; string name; if (n == null) { ReportSyntaxError(t.StartIndex, t.EndIndex, "expected name"); name = null; } else { name = n.Name; } Expression val = ParseExpression(); Arg arg = new Arg(t, val); arg.SetLoc(t.StartIndex, val.EndIndex); if (_verbatim) { AddPreceedingWhiteSpace(arg, equalWhiteSpace); } // we're losing the name expression... return arg; }
//classdef: 'class' NAME ['(' testlist ')'] ':' suite private Statement ParseClassDef() { Eat(TokenKind.KeywordClass); string classWhiteSpace = _tokenWhiteSpace; var start = GetStart(); var name = ReadName(); var nameExpr = MakeName(name); nameExpr.SetLoc(GetStart(), GetEnd()); string nameWhiteSpace = _tokenWhiteSpace; if (name.RealName == null) { // no name, assume there's no class. return ErrorStmt(_verbatim ? (classWhiteSpace + "class") : null); } bool isParenFree = false; string leftParenWhiteSpace = null, rightParenWhiteSpace = null; List<string> commaWhiteSpace = null; Arg[] args; bool ateTerminator = true; if (MaybeEat(TokenKind.LeftParenthesis)) { leftParenWhiteSpace = _tokenWhiteSpace; commaWhiteSpace = MakeWhiteSpaceList(); if (_langVersion.Is7x()) { args = FinishArgumentList(null, commaWhiteSpace, out ateTerminator); rightParenWhiteSpace = _tokenWhiteSpace; } else { bool trailingComma; List<Expression> l = ParseTestListAsExpr(null, out commaWhiteSpace, out trailingComma); if (l.Count == 1 && l[0] is ErrorExpression) { // error handling, classes is incomplete. return ErrorStmt( _verbatim ? (classWhiteSpace + "class" + nameWhiteSpace + name.VerbatimName + leftParenWhiteSpace + "(" + ((ErrorExpression)l[0]).VerbatimImage) : null ); } args = new Arg[l.Count]; for (int i = 0; i < l.Count; i++) { args[i] = new Arg(l[i]); } ateTerminator = Eat(TokenKind.RightParenthesis); rightParenWhiteSpace = _tokenWhiteSpace; } } else { isParenFree = true; args = new Arg[0]; } var mid = GetEnd(); // Save private prefix string savedPrefix = SetPrivatePrefix(name.VerbatimName); _classDepth++; // Parse the class body Statement body = ParseClassOrFuncBody(); _classDepth--; // Restore the private prefix _privatePrefix = savedPrefix; ClassDefinition ret = new ClassDefinition(nameExpr, args, body); AddVerbatimName(name, ret); if (_verbatim) { if (isParenFree) { AddIsAltForm(ret); } AddPreceedingWhiteSpace(ret, classWhiteSpace); AddSecondPreceedingWhiteSpace(ret, nameWhiteSpace); if (leftParenWhiteSpace != null) { AddThirdPreceedingWhiteSpace(ret, leftParenWhiteSpace); } if (rightParenWhiteSpace != null) { AddFourthPreceedingWhiteSpace(ret, rightParenWhiteSpace); } if (commaWhiteSpace != null) { AddListWhiteSpace(ret, commaWhiteSpace.ToArray()); } if (!ateTerminator) { AddErrorMissingCloseGrouping(ret); } } ret.HeaderIndex = mid; ret.SetLoc(start, body.EndIndex); return ret; }
//arglist: (argument ',')* (argument [',']| '*' expression [',' '**' expression] | '**' expression) //argument: [expression '='] expression # Really [keyword '='] expression private Arg[] FinishArgumentList(Arg first, List<string> commaWhiteSpace, out bool ateTerminator) { const TokenKind terminator = TokenKind.RightParenthesis; List<Arg> l = new List<Arg>(); if (first != null) { l.Add(first); } // Parse remaining arguments while (true) { if (MaybeEat(terminator)) { ateTerminator = true; break; } int start; Arg a; if (MaybeEat(TokenKind.Multiply)) { string starWhiteSpace = _tokenWhiteSpace; start = GetStart(); Expression t = ParseExpression(); var name = new NameExpression("*"); a = new Arg(name, t); if (_verbatim) { AddPreceedingWhiteSpace(name, starWhiteSpace); } } else if (MaybeEat(TokenKind.Power)) { string starStarWhiteSpace = _tokenWhiteSpace; start = GetStart(); Expression t = ParseExpression(); var name = new NameExpression("**"); a = new Arg(name, t); if (_verbatim) { AddPreceedingWhiteSpace(name, starStarWhiteSpace); } } else { Expression e = ParseExpression(); start = e.StartIndex; if (MaybeEat(TokenKind.Assign)) { a = FinishKeywordArgument(e); CheckUniqueArgument(l, a); } else { a = new Arg(e); } } a.SetLoc(start, GetEnd()); l.Add(a); if (MaybeEat(TokenKind.Comma)) { if (commaWhiteSpace != null) { commaWhiteSpace.Add(_tokenWhiteSpace); } } else { ateTerminator = Eat(terminator); break; } } return l.ToArray(); }
private JVariable _variable; // Variable corresponding to the class name #endregion Fields #region Constructors public ClassDefinition(NameExpression/*!*/ name, Arg[] bases, Statement body) { _name = name; _bases = bases; _body = body; }
public CallExpression(Expression target, Arg[] args) { _target = target; _args = args; }
// Arg public override bool Walk(Arg node) { return ShouldWalkWorker(node); }
public override void PostWalk(Arg node) { PostWalkWorker(node); }