public ForStatement(Expression left, Expression list, Statement body, Statement else_) { _left = left; _list = list; _body = body; _else = else_; }
internal static IEnumerable<IScopeNode> EnumerateBody(PythonAst ast, Statement body, bool includeAssignments = true) { SuiteStatement suite = body as SuiteStatement; if (suite != null) { foreach (Statement stmt in suite.Statements) { ClassDefinition klass = stmt as ClassDefinition; if (klass != null) { yield return new ClassScopeNode(klass); continue; } FunctionDefinition func = stmt as FunctionDefinition; if (func != null) { yield return new FunctionScopeNode(func); continue; } AssignmentStatement assign; if (includeAssignments && (assign = stmt as AssignmentStatement) != null) { foreach (var target in assign.Left) { NameExpression name = target as NameExpression; if (name != null) { yield return new AssignmentScopeNode(ast, assign, name); } } } } } }
public PythonAst(Statement body, int[] lineLocations, PythonLanguageVersion langVersion) { if (body == null) { throw new ArgumentNullException("body"); } _langVersion = langVersion; _body = body; _lineLocations = lineLocations; }
/// <summary> /// Returns the expression contained by the statement. /// /// Returns null if it's not an expression statement or return statement. /// /// New in 1.1. /// </summary> public static Expression GetExpression(Statement statement) { if (statement is ExpressionStatement) { return ((ExpressionStatement)statement).Expression; } else if (statement is ReturnStatement) { return ((ReturnStatement)statement).Expression; } else { return null; } }
public FunctionDefinition(NameExpression name, Parameter[] parameters, Statement body, DecoratorStatement decorators = null) { if (name == null) { _name = new NameExpression("<lambda>"); _isLambda = true; } else { _name = name; } _parameters = parameters; _body = body; _decorators = decorators; }
/// <summary> /// Returns a new SuiteStatement which is composed of a subset of the statements in this suite statement. /// </summary> /// <param name="start"></param> /// <param name="end"></param> /// <returns></returns> public SuiteStatement CloneSubset(PythonAst ast, int start, int end) { Statement[] statements = new Statement[end - start + 1]; for (int i = start; i <= end; i++) { statements[i - start] = Statements[i]; } var res = new SuiteStatement(statements); // propagate white space so we stay mostly the same... var itemWhiteSpace = this.GetListWhiteSpace(ast); var colonWhiteSpace = this.GetProceedingWhiteSpaceDefaultNull(ast); if (itemWhiteSpace != null) { // semi-colon list of statements, must end in a new line, but the original new line // could be multiple lines. ast.SetAttribute(res, NodeAttributes.ListWhiteSpace, new string[0]); } else { ast.SetAttribute(res, NodeAttributes.IsAltFormValue, NodeAttributes.IsAltFormValue); } return res; }
private static AP.Navigation GetNavigation(Statement stmt) { string type, name; FunctionDefinition funcDef = stmt as FunctionDefinition; if (funcDef != null) { name = funcDef.Name; type = "function"; if (funcDef.Decorators != null && funcDef.Decorators.Decorators.Count == 1) { foreach (var decorator in funcDef.Decorators.Decorators) { NameExpression nameExpr = decorator as NameExpression; if (nameExpr != null) { if (nameExpr.Name == "property") { type = "property"; break; } else if (nameExpr.Name == "staticmethod") { type = "staticmethod"; break; } else if (nameExpr.Name == "classmethod") { type = "classmethod"; break; } } } } } else { name = ((ClassDefinition)stmt).Name; type = "class"; } return new AP.Navigation() { type = type, name = name, startIndex = stmt.StartIndex, endIndex = stmt.EndIndex }; }
internal virtual string GetDocumentation(Statement /*!*/ stmt) { return(stmt.Documentation); }
public WithStatement(WithItem[] items, Statement body) { _items = items; _body = body; }
public ErrorStatement(Statement[] preceeding) { _preceeding = preceeding; }
public ForStatement(Expression left, Expression list, Statement body, Statement else_, bool isAsync) : this(left, list, body, else_) { _isAsync = isAsync; }
private Statement FinishSmallStmt(Statement stmt) { NextToken(); stmt.SetLoc(GetStart(), GetEnd()); if (_verbatim) { AddPreceedingWhiteSpace(stmt, _tokenWhiteSpace); } return stmt; }
private Statement ParseFinallySuite(Statement finallySuite) { bool inFinally = _inFinally; try { _inFinally = true; finallySuite = ParseSuite(); } finally { _inFinally = inFinally; } return finallySuite; }
internal virtual string GetDocumentation(Statement/*!*/ stmt) { return stmt.Documentation; }
public TryStatementHandler(Expression test, Expression target, Statement body) { Test = test; Target = target; Body = body; }
public WhileStatement(Expression test, Statement body, Statement else_) { _test = test; _body = body; _else = else_; }
public TryStatementHandler(Expression test, Expression target, Statement body) { _test = test; _target = target; _body = body; }
public TryStatement(Statement body, TryStatementHandler[] handlers, Statement else_, Statement finally_) { _body = body; _handlers = handlers; _else = else_; _finally = finally_; }
public WithStatement(WithItem[] items, Statement body, bool isAsync) : this(items, body) { _isAsync = isAsync; }
private bool IsCoroutine(Statement body) { if (_ast.LanguageVersion < PythonLanguageVersion.V35) { return false; } var walker = new AwaitWalker(); body.Walk(walker); return walker.SeenAwait; }
private PythonAst CreateAst(Statement ret) { var ast = new PythonAst(ret, _tokenizer.GetLineLocations(), _tokenizer.LanguageVersion); ast.HasVerbatim = _verbatim; ast.PrivatePrefix = _privatePrefix; if (_token.Token != null) { ast.SetLoc(0, GetEnd()); } if (_verbatim) { AddExtraVerbatimText(ast, _lookaheadWhiteSpace + _lookahead.Token.VerbatimImage); } foreach (var keyValue in _attributes) { foreach (var nodeAttr in keyValue.Value) { ast.SetAttribute(keyValue.Key, nodeAttr.Key, nodeAttr.Value); } } PythonNameBinder.BindAst(_langVersion, ast, _errors, _bindReferences); return ast; }
internal void SetBody(Statement body) { _body = body; }
private static Statement NestGenExpr(Statement current, Statement nested) { ForStatement fes = current as ForStatement; IfStatement ifs; if (fes != null) { fes.Body = nested; } else if ((ifs = current as IfStatement) != null) { ifs.Tests[0].Body = nested; } return nested; }
public DropDownEntryInfo(Statement body) { Body = body; }
private Expression ParseSubExpression(string expr) { var parser = Parser.CreateParser(new StringReader(expr), LanguageVersion); return(Statement.GetExpression(parser.ParseTopExpression()?.Body)); }
public ImportStatementInfo(Statement statement, StrongBox<int> siblings) { Statement = statement; Siblings = siblings; }
private void TrackImport(Statement node, string name) { var parent = _scopes[_scopes.Count - 1]; StrongBox<int> statementCount; if (!_statementCount.TryGetValue(parent, out statementCount)) { PythonAst outerParent = parent as PythonAst; if (outerParent != null) { // we don't care about the number of children at the top level statementCount = new StrongBox<int>(-1); } else { FunctionDefinition funcDef = parent as FunctionDefinition; if (funcDef != null) { statementCount = GetNumberOfChildStatements(funcDef.Body); } else { var classDef = (ClassDefinition)parent; statementCount = GetNumberOfChildStatements(classDef.Body); } } _statementCount[parent] = statementCount; } List<ImportStatementInfo> imports; if (!_importedNames.TryGetValue(name, out imports)) { _importedNames[name] = imports = new List<ImportStatementInfo>(); } imports.Add(new ImportStatementInfo(node, statementCount)); }
public SuiteStatement(Statement[] statements) { _statements = statements; }
private static StrongBox<int> GetNumberOfChildStatements(Statement body) { if (body is SuiteStatement) { return new StrongBox<int>(((SuiteStatement)body).Statements.Count); } else { return new StrongBox<int>(1); } }
public IfStatementTest(Expression test, Statement body) { _test = test; _body = body; }
private void UpdateChildRanges(Statement node) { var declScope = _curUnit.Scope; var prevScope = declScope.Children.LastOrDefault(); StatementScope prevStmtScope; IsInstanceScope prevInstanceScope; if ((prevStmtScope = prevScope as StatementScope) != null) { prevStmtScope.EndIndex = node.EndIndex; } else if ((prevInstanceScope = prevScope as IsInstanceScope) != null) { prevInstanceScope.EndIndex = node.EndIndex; } else { declScope.Children.Add(new StatementScope(node.StartIndex, declScope)); } }
private static NameExpression GetFirstNameExpression(Statement stmt) { return GetFirstNameExpression(Statement.GetExpression(stmt)); }
private PythonVariable _classVariable; // Variable for the classes __class__ cell var on 3.x public ClassDefinition(NameExpression/*!*/ name, Arg[] bases, Statement body) { _name = name; _bases = bases; _body = body; }
public IfStatement(IfStatementTest[] tests, Statement else_) { _tests = tests; _else = else_; }