/// <summary> /// Marks this tuple expression as having no parenthesis for the purposes of round tripping. /// </summary> public void RoundTripHasNoParenthesis(PythonAst ast) => ast.SetAttribute(this, NodeAttributes.IsAltFormValue, NodeAttributes.IsAltFormValue);
internal abstract void AppendCodeStringStmt(StringBuilder res, PythonAst ast, CodeFormattingOptions format);
/// <summary> /// Gets the variable reference for the specific assignment to the variable for this function definition. /// </summary> public PythonReference GetVariableReference(PythonAst ast) => GetVariableReference(this, ast);
internal override void AppendCodeString(StringBuilder res, PythonAst ast, CodeFormattingOptions format) { Target.AppendCodeString(res, ast, format); res.Append(" := "); Value.AppendCodeString(res, ast, format); }
internal virtual void AppendParameterName(StringBuilder res, PythonAst ast, CodeFormattingOptions format, string leadingWhiteSpace) => NameExpression?.AppendCodeString(res, ast, format, leadingWhiteSpace);
public override void SetLeadingWhiteSpace(PythonAst ast, string whiteSpace) => Target.SetLeadingWhiteSpace(ast, whiteSpace);
// PythonAst public override bool Walk(PythonAst node) { return(false); }
public void AddPreceedingWhiteSpace(PythonAst ast, string whiteSpace) => ast.SetAttribute(this, NodeAttributes.PreceedingWhiteSpace, whiteSpace);
public PythonReference[] GetReferences(PythonAst ast) => GetVariableReferences(this, ast);
/// <summary> /// Returns a new FromImport statement that is identical to this one but has /// removed the specified import statement. Otherwise preserves any attributes /// for the statement. /// /// New in 1.1. /// <param name="ast">The parent AST whose attributes should be updated for the new node.</param> /// <param name="index">The index in Names of the import to be removed.</param> /// </summary> public FromImportStatement RemoveImport(PythonAst ast, int index) { if (index < 0 || index >= Names.Count) { throw new ArgumentOutOfRangeException("index"); } if (ast == null) { throw new ArgumentNullException("ast"); } var names = new NameExpression[Names.Count - 1]; var asNames = AsNames == null ? null : new NameExpression[AsNames.Count - 1]; var asNameWhiteSpace = this.GetNamesWhiteSpace(ast); var newAsNameWhiteSpace = new List <string>(); var importIndex = ImportIndex; var asIndex = 0; for (int i = 0, write = 0; i < Names.Count; i++) { var includingCurrentName = i != index; // track the white space, this needs to be kept in sync w/ ToCodeString and how the // parser creates the white space. if (asNameWhiteSpace != null && asIndex < asNameWhiteSpace.Length) { if (write > 0) { if (includingCurrentName) { newAsNameWhiteSpace.Add(asNameWhiteSpace[asIndex++]); } else { asIndex++; } } else if (i > 0) { asIndex++; } } if (asNameWhiteSpace != null && asIndex < asNameWhiteSpace.Length) { if (includingCurrentName) { if (newAsNameWhiteSpace.Count == 0) { // no matter what we want the 1st entry to have the whitespace after the import keyword newAsNameWhiteSpace.Add(asNameWhiteSpace[0]); asIndex++; } else { newAsNameWhiteSpace.Add(asNameWhiteSpace[asIndex++]); } } else { asIndex++; } } if (includingCurrentName) { names[write] = Names[i]; if (AsNames != null) { asNames[write] = AsNames[i]; } write++; } if (AsNames != null && AsNames[i] != null) { if (asNameWhiteSpace != null && asIndex < asNameWhiteSpace.Length) { if (i != index) { newAsNameWhiteSpace.Add(asNameWhiteSpace[asIndex++]); } else { asIndex++; } } if (AsNames[i].Name.Length != 0) { if (asNameWhiteSpace != null && asIndex < asNameWhiteSpace.Length) { if (i != index) { newAsNameWhiteSpace.Add(asNameWhiteSpace[asIndex++]); } else { asIndex++; } } } else { asIndex++; } } } if (asNameWhiteSpace != null && asIndex < asNameWhiteSpace.Length) { // trailing comma newAsNameWhiteSpace.Add(asNameWhiteSpace[asNameWhiteSpace.Length - 1]); } var res = new FromImportStatement(Root, names, asNames, IsFromFuture, ForceAbsolute, importIndex); ast.CopyAttributes(this, res); ast.SetAttribute(res, NodeAttributes.NamesWhiteSpace, newAsNameWhiteSpace.ToArray()); return(res); }
internal override void AppendCodeStringStmt(StringBuilder res, PythonAst ast, CodeFormattingOptions format) { format.ReflowComment(res, this.GetPreceedingWhiteSpace(ast)); res.Append("from"); Root.AppendCodeString(res, ast, format); if (!this.IsIncompleteNode(ast)) { res.Append(this.GetSecondWhiteSpace(ast)); res.Append("import"); if (!this.IsAltForm(ast)) { res.Append(this.GetThirdWhiteSpace(ast)); res.Append('('); } var asNameWhiteSpace = this.GetNamesWhiteSpace(ast); var asIndex = 0; for (var i = 0; i < Names.Count; i++) { if (i > 0) { if (asNameWhiteSpace != null && asIndex < asNameWhiteSpace.Length) { res.Append(asNameWhiteSpace[asIndex++]); } res.Append(','); } if (asNameWhiteSpace != null && asIndex < asNameWhiteSpace.Length) { res.Append(asNameWhiteSpace[asIndex++]); } else { res.Append(' '); } Names[i].AppendCodeString(res, ast, format); if (AsNames != null && AsNames[i] != null) { if (asNameWhiteSpace != null && asIndex < asNameWhiteSpace.Length) { res.Append(asNameWhiteSpace[asIndex++]); } res.Append("as"); if (AsNames[i].Name.Length != 0) { if (asNameWhiteSpace != null && asIndex < asNameWhiteSpace.Length) { res.Append(asNameWhiteSpace[asIndex++]); } AsNames[i].AppendCodeString(res, ast, format); } else { asIndex++; } } } if (asNameWhiteSpace != null && asIndex < asNameWhiteSpace.Length) { // trailing comma res.Append(asNameWhiteSpace[asNameWhiteSpace.Length - 1]); res.Append(","); } if (!this.IsAltForm(ast) && !this.IsMissingCloseGrouping(ast)) { res.Append(this.GetFourthWhiteSpace(ast)); res.Append(')'); } } }
internal override void AppendCodeStringStmt(StringBuilder res, PythonAst ast, CodeFormattingOptions format) => ListExpression.AppendItems(res, ast, format, "del", string.Empty, this, Expressions);
internal override void AppendCodeString(StringBuilder res, PythonAst ast, CodeFormattingOptions format) => ListExpression.AppendItems(res, ast, format, "{", this.IsMissingCloseGrouping(ast) ? "" : "}", this, Items);
internal override void AppendCodeString(StringBuilder res, PythonAst ast, CodeFormattingOptions format, string leadingWhiteSpace) { var kwOnlyText = this.GetExtraVerbatimText(ast); if (kwOnlyText != null) { if (leadingWhiteSpace != null) { res.Append(leadingWhiteSpace); res.Append(kwOnlyText.TrimStart()); leadingWhiteSpace = null; } else { res.Append(kwOnlyText); } } var writeName = true; switch (Kind) { case ParameterKind.Dictionary: res.Append(leadingWhiteSpace ?? this.GetPreceedingWhiteSpaceDefaultNull(ast) ?? string.Empty); leadingWhiteSpace = null; res.Append("**"); break; case ParameterKind.List: res.Append(leadingWhiteSpace ?? this.GetPreceedingWhiteSpaceDefaultNull(ast) ?? string.Empty); leadingWhiteSpace = null; res.Append('*'); break; case ParameterKind.Normal: if (this.IsAltForm(ast)) { res.Append(leadingWhiteSpace ?? this.GetPreceedingWhiteSpaceDefaultNull(ast) ?? string.Empty); leadingWhiteSpace = null; res.Append('('); AppendParameterName(res, ast, format, leadingWhiteSpace); if (!this.IsMissingCloseGrouping(ast)) { res.Append(this.GetSecondWhiteSpace(ast)); res.Append(')'); } writeName = false; } break; case ParameterKind.KeywordOnly: break; default: throw new InvalidOperationException(); } if (writeName) { AppendParameterName(res, ast, format, leadingWhiteSpace); } if (Annotation != null) { res.Append(this.GetThirdWhiteSpaceDefaultNull(ast) ?? ""); res.Append(':'); Annotation.AppendCodeString(res, ast, format); } if (DefaultValue != null) { format.Append( res, format.SpaceAroundDefaultValueEquals, " ", string.Empty, NodeAttributes.GetWhiteSpace(this, ast, WhitespacePrecedingAssign) ); res.Append('='); if (format.SpaceAroundDefaultValueEquals != null) { DefaultValue.AppendCodeString(res, ast, format, format.SpaceAroundDefaultValueEquals.Value ? " " : string.Empty); } else { DefaultValue.AppendCodeString(res, ast, format); } } }
public override void PostWalk(PythonAst node) { }
public PythonVariable GetVariable(PythonAst ast) => ast.TryGetAttribute(this, NodeAttributes.Variable, out var reference) ? (PythonVariable)reference : null;
// PythonAst public override bool Walk(PythonAst node) { return(Contains(node)); }
internal override void AppendCodeString(StringBuilder res, PythonAst ast, CodeFormattingOptions format) => AppendCodeString(res, ast, format, null);
// PythonAst public virtual bool Walk(PythonAst node) { return(true); }
public override string GetLeadingWhiteSpace(PythonAst ast) => Target.GetLeadingWhiteSpace(ast);
public virtual void PostWalk(PythonAst node) { }
/// <summary> /// Returns the span of the name component of the expression /// </summary> public SourceSpan GetNameSpan(PythonAst parent) => new SourceSpan(parent.IndexToLocation(NameHeader), GetEnd(parent));
internal override void AppendCodeStringStmt(StringBuilder res, PythonAst ast, CodeFormattingOptions format) { _body.AppendCodeString(res, ast, format); res.Append(this.GetExtraVerbatimText(ast)); }
internal override void AppendCodeStringStmt(StringBuilder res, PythonAst ast, CodeFormattingOptions format) { Decorators?.AppendCodeString(res, ast, format); format.ReflowComment(res, this.GetPreceedingWhiteSpaceDefaultNull(ast)); if (IsCoroutine) { res.Append("async"); res.Append(NodeAttributes.GetWhiteSpace(this, ast, WhitespaceAfterAsync)); } res.Append("def"); var name = this.GetVerbatimImage(ast) ?? Name; if (!string.IsNullOrEmpty(name)) { res.Append(this.GetSecondWhiteSpace(ast)); res.Append(name); if (!this.IsIncompleteNode(ast)) { format.Append( res, format.SpaceBeforeFunctionDeclarationParen, " ", "", this.GetThirdWhiteSpaceDefaultNull(ast) ); res.Append('('); if (Parameters.Length != 0) { var commaWhiteSpace = this.GetListWhiteSpace(ast); ParamsToString(res, ast, commaWhiteSpace, format, format.SpaceWithinFunctionDeclarationParens != null ? format.SpaceWithinFunctionDeclarationParens.Value ? " " : "" : null ); } var namedOnly = this.GetExtraVerbatimText(ast); if (namedOnly != null) { res.Append(namedOnly); } format.Append( res, Parameters.Length != 0 ? format.SpaceWithinFunctionDeclarationParens : format.SpaceWithinEmptyParameterList, " ", "", this.GetFourthWhiteSpaceDefaultNull(ast) ); if (!this.IsMissingCloseGrouping(ast)) { res.Append(')'); } if (ReturnAnnotation != null) { format.Append( res, format.SpaceAroundAnnotationArrow, " ", string.Empty, this.GetFifthWhiteSpace(ast) ); res.Append("->"); ReturnAnnotation.AppendCodeString( res, ast, format, format.SpaceAroundAnnotationArrow != null ? format.SpaceAroundAnnotationArrow.Value ? " " : string.Empty : null ); } Body?.AppendCodeString(res, ast, format); } } }
internal override void AppendCodeString(StringBuilder res, PythonAst ast, CodeFormattingOptions format) { Target.AppendCodeString(res, ast, format); format.Append( res, format.SpaceBeforeCallParen, " ", string.Empty, this.GetPreceedingWhiteSpaceDefaultNull(ast) ); res.Append('('); if (Args.Count == 0) { if (format.SpaceWithinEmptyCallArgumentList != null && format.SpaceWithinEmptyCallArgumentList.Value) { res.Append(' '); } } else { var listWhiteSpace = format.SpaceBeforeComma == null?this.GetListWhiteSpace(ast) : null; var spaceAfterComma = format.SpaceAfterComma.HasValue ? (format.SpaceAfterComma.Value ? " " : string.Empty) : (string)null; for (var i = 0; i < Args.Count; i++) { if (i > 0) { if (format.SpaceBeforeComma == true) { res.Append(' '); } else if (listWhiteSpace != null) { res.Append(listWhiteSpace[i - 1]); } res.Append(','); } else if (format.SpaceWithinCallParens != null) { Args[i].AppendCodeString(res, ast, format, format.SpaceWithinCallParens.Value ? " " : string.Empty); continue; } Args[i].AppendCodeString(res, ast, format, spaceAfterComma); } if (listWhiteSpace != null && listWhiteSpace.Length == Args.Count) { // trailing comma res.Append(listWhiteSpace[listWhiteSpace.Length - 1]); res.Append(","); } } if (!this.IsMissingCloseGrouping(ast)) { if (Args.Count != 0 || format.SpaceWithinEmptyCallArgumentList == null || !string.IsNullOrWhiteSpace(this.GetSecondWhiteSpaceDefaultNull(ast))) { format.Append( res, format.SpaceWithinCallParens, " ", string.Empty, this.GetSecondWhiteSpaceDefaultNull(ast) ); } res.Append(')'); } }
internal override void AppendCodeStringStmt(StringBuilder res, PythonAst ast, CodeFormattingOptions format) { // SuiteStatement comes in 3 forms: // 1. The body of a if/else/while/for/etc... where there's an opening colon // 2. A set of semi-colon separated items // 3. A top-level group of statements in a top-level PythonAst node. var itemWhiteSpace = this.GetListWhiteSpace(ast); var colonWhiteSpace = this.GetPreceedingWhiteSpaceDefaultNull(ast); if (this.IsAltForm(ast)) { // suite statement in top-level PythonAst, we have no colons or other delimiters foreach (var statement in _statements) { statement.AppendCodeString(res, ast, format); } } else if (itemWhiteSpace != null) { if (format.BreakMultipleStatementsPerLine) { var leadingWhiteSpace = ""; for (var i = 0; i < _statements.Length; i++) { if (i == 0) { var tmp = new StringBuilder(); _statements[i].AppendCodeString(tmp, ast, format); var stmt = tmp.ToString(); res.Append(stmt); // figure out the whitespace needed for the next statement based upon the current statement for (var curChar = 0; curChar < stmt.Length; curChar++) { if (!char.IsWhiteSpace(stmt[curChar])) { leadingWhiteSpace = format.GetNextLineProceedingText(stmt.Substring(0, curChar)); break; } } } else { _statements[i].AppendCodeString(res, ast, format, leadingWhiteSpace); } } } else { // form 2, semi-colon seperated list. for (var i = 0; i < _statements.Length; i++) { if (i > 0) { if (i - 1 < itemWhiteSpace.Length) { res.Append(itemWhiteSpace[i - 1]); } res.Append(';'); } _statements[i].AppendCodeString(res, ast, format); } } if (itemWhiteSpace != null && itemWhiteSpace.Length == _statements.Length && _statements.Length != 0) { // trailing semi-colon if (!format.RemoveTrailingSemicolons) { res.Append(itemWhiteSpace[itemWhiteSpace.Length - 1]); res.Append(";"); } } } else { // 3rd form, suite statement as the body of a class/function, we include the colon. if (colonWhiteSpace != null) { res.Append(colonWhiteSpace); } res.Append(':'); foreach (var statement in _statements) { statement.AppendCodeString(res, ast, format); } } }
internal override void AppendCodeString(StringBuilder res, PythonAst ast, CodeFormattingOptions format) => BinaryExpression.BinaryToCodeString(res, ast, format, this, Left, Right, "or");
internal override void AppendCodeStringStmt(StringBuilder res, PythonAst ast, CodeFormattingOptions format) { format.ReflowComment(res, this.GetPreceedingWhiteSpace(ast)); res.Append("pass"); }
internal sealed override void AppendCodeString(StringBuilder res, PythonAst ast, CodeFormattingOptions format) => AppendCodeStringStmt(res, ast, format);