public static string PrintPythonAst(PyAst.Node node) { var printer = new PythonAstPrinter(); printer.Visit(node); return(printer.stringBuilder.ToString()); }
private AnalysisUnit(Node ast, InterpreterScope[] scopes, AnalysisUnit parent, bool forEval) { _ast = ast; _scopes = scopes; _parent = parent; _forEval = forEval; }
public override ISet<Namespace> Call(Node node, AnalysisUnit unit, ISet<Namespace>[] args, string[] keywordArgNames) { if (args.Length == 1) { _list.AppendItem(args[0]); } return ProjectState._noneInst.SelfSet; }
public override ISet<Namespace> Call(Node node, AnalysisUnit unit, ISet<Namespace>[] args, string[] keywordArgNames) { if (args.Length == 1) { _generator.AddSend(node, unit, args[0]); } return _generator.Yields; }
public override ISet<Namespace> GetMember(Node node, AnalysisUnit unit, string name) { var res = base.GetMember(node, unit, name); if (res.Count > 0) { _references.AddReference(node, unit, name); } return res; }
public override ISet<Namespace> Call(Node node, AnalysisUnit unit, ISet<Namespace>[] args, string[] keywordArgNames) { if (args.Length == 1) { foreach (var type in args[0]) { _list.AppendItem(type.GetEnumeratorTypes(node, unit)); } } return ProjectState._noneInst.SelfSet; }
public VariableDef CreateVariable(Node node, AnalysisUnit unit, string name, bool addRef = true) { var res = GetVariable(node, unit, name, addRef); if (res == null) { _variables[name] = res = new VariableDef(); if (addRef) { res.AddReference(node, unit); } } return res; }
public override ISet<Namespace> GetIndex(Node node, AnalysisUnit unit, ISet<Namespace> index) { // TODO: Return correct index value if we have a constant /*int? constIndex = SequenceInfo.GetConstantIndex(index); if (constIndex != null && constIndex.Value < _indexTypes.Count) { // TODO: Warn if outside known index and no appends? return _indexTypes[constIndex.Value]; }*/ return ProjectState._intType.SelfSet; }
public void Visit(PyAst.Node node) { switch (node) { case PyAst.Expression n: stringBuilder.Append(Visit(n)); return; case PyAst.Statement n: Visit(n); return; default: throw new NotImplementedException($"Printing of node {node} is not implemented"); } }
public void SetUpFixture() { componentCreator = new MockComponentCreator(); AssignmentStatement assignment = PythonParserHelper.GetAssignmentStatement(GetPythonCode()); rhsAssignmentNode = assignment.Right; mockDesignerLoaderHost = new MockDesignerLoaderHost(); typeResolutionService = mockDesignerLoaderHost.TypeResolutionService; PythonCodeDeserializer deserializer = new PythonCodeDeserializer(componentCreator); deserializedObject = deserializer.Deserialize(rhsAssignmentNode); }
/// <summary> /// Performs a call operation propagating the argument types into any user defined functions /// or classes and returns the set of types which result from the call. /// </summary> public static ISet<Namespace> Call(this ISet<Namespace> self, Node node, AnalysisUnit unit, ISet<Namespace>[] args, string[] keywordArgNames) { ISet<Namespace> res = EmptySet<Namespace>.Instance; bool madeSet = false; foreach (var ns in self) { var call = ns.Call(node, unit, args, keywordArgNames); Debug.Assert(call != null); res = res.Union(call, ref madeSet); } return res; }
public override ISet<Namespace> BinaryOperation(Node node, AnalysisUnit unit, PythonOperator operation, ISet<Namespace> rhs) { switch (operation) { case PythonOperator.GreaterThan: case PythonOperator.LessThan: case PythonOperator.LessThanOrEqual: case PythonOperator.GreaterThanOrEqual: case PythonOperator.Equal: case PythonOperator.NotEqual: case PythonOperator.Is: case PythonOperator.IsNot: return ProjectState._boolType.Instance; } return base.BinaryOperation(node, unit, operation, rhs); }
public static ISet<Namespace> BinaryOperation(this ISet<Namespace> self, Node node, AnalysisUnit unit, PythonOperator operation, ISet<Namespace> rhs) { ISet<Namespace> res = null; bool madeSet = false; foreach (var ns in self) { ISet<Namespace> got = ns.BinaryOperation(node, unit, operation, rhs); if (res == null) { res = got; continue; } else if (!madeSet) { res = new HashSet<Namespace>(res); madeSet = true; } res.UnionWith(got); } return res ?? EmptySet<Namespace>.Instance; }
public override ISet<Namespace> GetMember(Node node, AnalysisUnit unit, string name) { switch (name) { case "append": EnsureAppend(); return _appendMethod.SelfSet; case "pop": EnsurePop(); return _popMethod.SelfSet; case "insert": EnsureInsert(); return _insertMethod.SelfSet; case "extend": EnsureExtend(); return _extendMethod.SelfSet; } return base.GetMember(node, unit, name); }
/// <summary> /// Creates or gets the object specified in the python AST. /// </summary> /// <returns> /// Null if the node cannot be deserialized. /// </returns> public object Deserialize(Node node) { if (node == null) { throw new ArgumentNullException("node"); } if (node is CallExpression) { return Deserialize((CallExpression)node); } else if (node is BinaryExpression) { return Deserialize((BinaryExpression)node); } else if (node is MemberExpression) { return Deserialize((MemberExpression)node); } else if (node is UnaryExpression) { return Deserialize((UnaryExpression)node); } else if (node is ConstantExpression) { return Deserialize((ConstantExpression)node); } else if (node is NameExpression) { return Deserialize((NameExpression)node); } return null; }
public bool Locate(int line, int column, out Node node, out Scope scope) { Locator locator = new Locator(line, column); global.Walk(locator); node = locator.Candidate; scope = locator.Scope != null ? scopes[locator.Scope] : null; #if DEBUG if (node != null) { Debug.Print("Located {0} at {1}:{2}-{3}:{4}", node, node.Start.Line, node.Start.Column, node.End.Line, node.End.Column ); } #endif return node != null; }
private void CommonWalk(Node node) { tree.Push(node); }
private void CommonPostWalk(Node node, bool skip = false) { if (tree.Count > 0) { tree.Pop(); } if (skip) { return; } Node parent = tree.Count > 0 ? tree.Peek() : null; if (parent is SuiteStatement && content.Count > 0) { var s = Content(); Content("{0}{1};", Indent(), s); } }
private void CheckForIllegalWords(Node node, string word) { if (Array.IndexOf<string>(jsReservedWords, word) >= 0) { sink.Add(src, String.Format("\"{0}\" is reserved word in JavaScript and cannot be used.", word), node.Span, RESERVED_WORD, Severity.Error); } }
private void CompleteParameterName(Node node, SymbolId name, Dictionary<SymbolId, object> names) { SourceSpan span = GetSpan(); _sink.StartName(span, SymbolTable.IdToString(name)); CheckUniqueParameter(names, name); node.SetLoc(span); }
private void SaveCandidate(Node node) { if (candidateNode == null || Better(node, candidateNode)) { Debug.Print("Candidate: {0} at {1}:{2}-{3}:{4} ({5}:{6})", node, node.Start.Line, node.Start.Column, node.End.Line, node.End.Column, location.Line, location.Column); candidateNode = node; candidateScope = current; candidateContext = context != null && context.Count > 0 ? context.Peek() : null; } }
private void PushContext(Node n) { Debug.Assert(contextType != null && n.GetType() == contextType); if (context == null) { context = new Stack<Node>(); } context.Push(n); }
private bool Locate(Type contextType, int line, int column, out Node node, out Scope scope, out Node context) { Locator locator = new Locator(contextType, line, column); global.Walk(locator); node = locator.Candidate; scope = locator.Scope != null ? scopes[locator.Scope] : null; context = locator.Context; #if DEBUG if (node != null) { Debug.Print("Located {0} in {1} at {2}:{3}-{4}:{5}", node, context != null ? (object)context : (object)"<unknown>", node.Start.Line, node.Start.Column, node.End.Line, node.End.Column ); } #endif return node != null && context != null; }
internal static AST Convert(Node node) { AST ast; if (node is TryStatementHandler) ast = new ExceptHandler((TryStatementHandler)node); else throw new ArgumentTypeException("Unexpected node type: " + node.GetType()); ast.GetSourceLocation(node); return ast; }
private void CompleteParameterName(Node node, string name, Dictionary<string, object> names) { SourceSpan span = GetSpan(); _sink.StartName(span, name); CheckUniqueParameter(names, name); node.SetLoc(span); }
private static bool Better(Node one, Node two) { if (one.Start > two.Start) { if (one.End > two.End) { return Smaller(one, two); } else if (one.End < two.End) { return true; } else { return true; } } else if (one.Start < two.Start) { if (one.End > two.End) { return false; } else if (one.End < two.End) { return Smaller(one, two); } else { return false; } } else { if (one.End > two.End) { return false; } else if (one.End < two.End) { return true; } else { return true; } } }
protected void GetSourceLocation(Node node) { _lineno = node.Start.Line; // IronPython counts from 1; CPython counts from 0 _col_offset = node.Start.Column - 1; }
private bool Process(Node node) { if (contextType != null && node.GetType() == contextType) { PushContext(node); } if (node.Start <= location && location < node.End) { SaveCandidate(node); } return true; }
private static bool Smaller(Node one, Node two) { int oneL = one.End.Line - one.Start.Line; int twoL = two.End.Line - two.Start.Line; if (oneL < twoL) { return true; } else if (oneL > twoL) { return false; } else { int oneC = one.End.Column - one.Start.Column; int twoC = one.End.Column - one.Start.Column; if (oneC < twoC) { return true; } else if (oneC > twoC) { return false; } else { return true; } } }
private void PostProcess(Node node) { if (contextType != null && node.GetType() == contextType) { PopContext(node); } }
private void CompleteParameterName(Node node, string name, HashSet<string> names) { if (_sink != null) { _sink.StartName(GetSourceSpan(), name); } CheckUniqueParameter(names, name); node.SetLoc(_globalParent, GetStart(), GetEnd()); }
private void PopContext(Node n) { Debug.Assert(contextType != null && n.GetType() == contextType); Debug.Assert(context != null && context.Count > 0); Node n2 = context.Pop(); Debug.Assert((object)n2 == (object)n); }