public TryStatement(Statement body, TryStatementHandler[] handlers, Statement else_, Statement finally_) { _body = body; _handlers = handlers; _else = else_; _finally = finally_; }
/// <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(JAst 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 (colonWhiteSpace != null) { ast.SetAttribute(res, NodeAttributes.PreceedingWhiteSpace, ""); } else 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]); var trailingNewLine = this.GetTrailingNewLine(ast); if (trailingNewLine != null) { ast.SetAttribute(res, NodeAttributes.TrailingNewLine, "\r\n"); } } if (this.IsAltForm(ast)) { ast.SetAttribute(res, NodeAttributes.IsAltFormValue, NodeAttributes.IsAltFormValue); } return res; }
public ForStatement(Expression left, Expression list, Statement body, Statement else_) { _left = left; _list = list; _body = body; _else = else_; }
public JAst(Statement body, int[] lineLocations) { if (body == null) { throw new ArgumentNullException("body"); } _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; }
private JAst CreateAst(Statement ret) { var ast = new JAst(ret, _tokenizer.GetLineLocations()); ast.PrivatePrefix = _privatePrefix; if (_token.Token != null) { ast.SetLoc(0, GetEnd()); } if (_verbatim) { AddExtraVerbatimText(ast, _lookaheadWhiteSpace); } foreach (var keyValue in _attributes) { foreach (var nodeAttr in keyValue.Value) { ast.SetAttribute(keyValue.Key, nodeAttr.Key, nodeAttr.Value); } } JNameBinder.BindAst(_langVersion, ast, _errors, _bindReferences); return ast; }
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; }
private Statement ParseFinallySuite(Statement finallySuite) { bool inFinally = _inFinally; try { _inFinally = true; finallySuite = ParseSuite(); } finally { _inFinally = inFinally; } return finallySuite; }
public IfStatement(IfStatementTest[] tests, Statement else_) { _tests = tests; _else = else_; }
internal virtual string GetDocumentation(Statement/*!*/ stmt) { return stmt.Documentation; }
public WhileStatement(Expression test, Statement body, Statement else_) { _test = test; _body = body; _else = else_; }
public SuiteStatement(Statement[] statements) { _statements = statements; }
internal void SetBody(Statement body) { _body = body; }
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 WithStatement(WithItem[] items, Statement body) { _items = items; _body = body; }
private void TrackImport(Statement node, string name) { var parent = _scopes[_scopes.Count - 1]; StrongBox<int> statementCount; if (!_statementCount.TryGetValue(parent, out statementCount)) { JAst outerParent = parent as JAst; 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)); }
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 ImportStatementInfo(Statement statement, StrongBox<int> siblings) { Statement = statement; Siblings = siblings; }
public TryStatementHandler(Expression test, Expression target, Statement body) { _test = test; _target = target; _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)); } }
public IfStatementTest(Expression test, Statement body) { _test = test; _body = body; }
private void WalkLoop(Statement body, Statement elseStmt) { bool allReturn = true; _raises = Returns = false; body.Walk(this); allReturn &= Returns || _raises; if (elseStmt != null) { _raises = _raises = false; elseStmt.Walk(this); allReturn &= Returns || _raises; } Returns = allReturn; }
internal static IEnumerable<IScopeNode> EnumerateBody(JAst 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 ErrorStatement(Statement[] preceeding) { _preceeding = preceeding; }
private Statement FinishSmallStmt(Statement stmt) { NextToken(); stmt.SetLoc(GetStart(), GetEnd()); if (_verbatim) { AddPreceedingWhiteSpace(stmt, _tokenWhiteSpace); } return stmt; }
public DropDownEntryInfo(Statement body) { Body = body; }
public ExtractMethodResult GetExtractionResult(ExtractMethodRequest info) { bool isStaticMethod = false, isClassMethod = false; var parameters = new List<Parameter>(); string selfParam = null; if (info.TargetScope is ClassDefinition) { var fromScope = _scopes[_scopes.Length - 1] as FunctionDefinition; Debug.Assert(fromScope != null); // we don't allow extracting from classes, so we have to be coming from a function if (fromScope.Decorators != null) { foreach (var decorator in fromScope.Decorators.Decorators) { NameExpression name = decorator as NameExpression; if (name != null) { if (name.Name == "staticmethod") { isStaticMethod = true; } else if (name.Name == "classmethod") { isClassMethod = true; } } } } if (!isStaticMethod) { if (fromScope.Parameters.Count > 0) { selfParam = fromScope.Parameters[0].Name; parameters.Add(new Parameter(selfParam, ParameterKind.Normal)); } } } foreach (var param in info.Parameters) { var newParam = new Parameter(param, ParameterKind.Normal); if (parameters.Count > 0) { newParam.AddPreceedingWhiteSpace(_ast, " "); } parameters.Add(newParam); } // include any non-closed over parameters as well... foreach (var input in _inputVars) { var variableScope = input.Scope; var parentScope = info.TargetScope; // are these variables a child of the target scope so we can close over them? while (parentScope != null && parentScope != variableScope) { parentScope = parentScope.Parent; } if (parentScope == null && input.Name != selfParam) { // we can either close over or pass these in as parameters, add them to the list var newParam = new Parameter(input.Name, ParameterKind.Normal); if (parameters.Count > 0) { newParam.AddPreceedingWhiteSpace(_ast, " "); } parameters.Add(newParam); } } var body = _target.GetBody(_ast); if (_outputVars.Count > 0) { // need to add a return statement Expression retValue; Expression[] names = new Expression[_outputVars.Count]; int outputIndex = 0; foreach (var name in _outputVars) { var nameExpr = new NameExpression(name.Name); nameExpr.AddPreceedingWhiteSpace(_ast, " "); names[outputIndex++] = nameExpr; } var tuple = new TupleExpression(false, names); tuple.RoundTripHasNoParenthesis(_ast); retValue = tuple; var retStmt = new ReturnStatement(retValue); if (body is SuiteStatement) { SuiteStatement suite = (SuiteStatement)body; Node.CopyLeadingWhiteSpace(_ast, suite.Statements[0], retStmt); Node.CopyTrailingNewLine(_ast, suite.Statements[0], suite.Statements[suite.Statements.Count - 1]); Statement[] statements = new Statement[suite.Statements.Count + 1]; for (int i = 0; i < suite.Statements.Count; i++) { statements[i] = suite.Statements[i]; } statements[statements.Length - 1] = retStmt; body = new SuiteStatement(statements); } else { Node.CopyLeadingWhiteSpace(_ast, body, retStmt); body = new SuiteStatement( new Statement[] { body, retStmt } ); } } DecoratorStatement decorators = null; if (isStaticMethod) { decorators = new DecoratorStatement(new[] { new NameExpression("staticmethod") }); } else if (isClassMethod) { decorators = new DecoratorStatement(new[] { new NameExpression("classmethod") }); } var res = new FunctionDefinition(new NameExpression(info.Name), parameters.ToArray(), body, decorators); StringBuilder newCall = new StringBuilder(); newCall.Append(_target.IndentationLevel); var method = res.ToCodeString(_ast); // fix up indentation... for (int curScope = 0; curScope < _scopes.Length; curScope++) { if (_scopes[curScope] == info.TargetScope) { // this is our target indentation level. var indentationLevel = _scopes[curScope].Body.GetIndentationLevel(_ast); var lines = method.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None); int minWhiteSpace = Int32.MaxValue; for (int curLine = decorators == null ? 1 : 2; curLine < lines.Length; curLine++) { var line = lines[curLine]; for (int i = 0; i < line.Length; i++) { if (!Char.IsWhiteSpace(line[i])) { minWhiteSpace = Math.Min(minWhiteSpace, i); break; } } } StringBuilder newLines = new StringBuilder(); newLines.Append(indentationLevel); newLines.Append(lines[0]); if (decorators != null) { newLines.Append("\r\n"); newLines.Append(indentationLevel); newLines.Append(lines[1]); } // don't include a bunch of blank lines... int endLine = lines.Length - 1; for (; endLine >= 0 && String.IsNullOrWhiteSpace(lines[endLine]); endLine--) { } newLines.Append("\r\n"); for (int curLine = decorators == null ? 1 : 2; curLine <= endLine; curLine++) { var line = lines[curLine]; newLines.Append(indentationLevel); if (_insertTabs) { newLines.Append('\t'); } else { newLines.Append(' ', _indentSize); } if (line.Length > minWhiteSpace) { newLines.Append(line, minWhiteSpace, line.Length - minWhiteSpace); } newLines.Append("\r\n"); } newLines.Append("\r\n"); method = newLines.ToString(); break; } } string comma; if (_outputVars.Count > 0) { comma = ""; foreach (var outputVar in _outputVars) { newCall.Append(comma); newCall.Append(outputVar.Name); comma = ", "; } newCall.Append(" = "); } else if (_target.ContainsReturn) { newCall.Append("return "); } if (info.TargetScope is ClassDefinition) { var fromScope = _scopes[_scopes.Length - 1] as FunctionDefinition; Debug.Assert(fromScope != null); // we don't allow extracting from classes, so we have to be coming from a function if (isStaticMethod) { newCall.Append(info.TargetScope.Name); newCall.Append('.'); } else if (fromScope.Parameters.Count > 0) { newCall.Append(fromScope.Parameters[0].Name); newCall.Append('.'); } } newCall.Append(info.Name); newCall.Append('('); comma = ""; foreach (var param in parameters) { if (param.Name != selfParam) { newCall.Append(comma); newCall.Append(param.Name); comma = ", "; } } newCall.Append(')'); return new ExtractMethodResult( method, newCall.ToString() ); }