string GetNodeTitle(AstNode node) { StringBuilder b = new StringBuilder(); b.Append(node.Role.ToString()); b.Append(": "); b.Append(node.GetType().Name); bool hasProperties = false; foreach (PropertyInfo p in node.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance)) { if (p.Name == "NodeType" || p.Name == "IsNull") continue; if (p.PropertyType == typeof(string) || p.PropertyType.IsEnum || p.PropertyType == typeof(bool)) { if (!hasProperties) { hasProperties = true; b.Append(" ("); } else { b.Append(", "); } b.Append(p.Name); b.Append(" = "); try { object val = p.GetValue(node, null); b.Append(val != null ? val.ToString() : "**null**"); } catch (TargetInvocationException ex) { b.Append("**" + ex.InnerException.GetType().Name + "**"); } } } if (hasProperties) b.Append(")"); return b.ToString(); }
private void GetUsingsAndRemoveUsingsAndNamespaces(AstNode node, List <UsingDeclaration> usings, List <TypeDeclaration> types) { if (node.Role.ToString() == "Member" && node.GetType().Name == "UsingDeclaration") { usings.Add((UsingDeclaration)node); } else if (node.Role.ToString() == "Member" && node.GetType().Name == "NamespaceDeclaration") { AstNode parent = node.Parent; foreach (AstNode child in node.Children) { if (child.NodeType == NodeType.TypeDeclaration) { types.Add((TypeDeclaration)child); } else if (child.ToString() == "Member" && child.GetType().Name == "NamespaceDeclaration") { GetUsingsAndRemoveUsingsAndNamespaces(child, usings, types); } } } else { if (node.Children.Count() >= 1) { foreach (var child in node.Children) { GetUsingsAndRemoveUsingsAndNamespaces(child, usings, types); } } } }
public object Visit(AstNode node) { if (!_methods.ContainsKey(node.GetType())) { return(null); } return(_methods[node.GetType()].Invoke(this, new[] { node })); }
public void Print(AstNode node) { if (_withoutPositions) { _main = new string(' ', _indent * 2) + node.GetType().Name.Substring(3); } else { _main = new string(' ', _indent * 2) + node.GetType().Name.Substring(3) + " " + (node.Start.Line + 1) + ":" + (node.Start.Column + 1) + " - " + (node.End.Line + 1) + ":" + (node.End.Column + 1); } }
/// <summary> /// 对节点进行求值 /// </summary> /// <param name="ast"></param> /// <returns></returns> public static EvalValue Eval(IEnvironment env, AstNode ast) { var result = new EvalValue(); if (ast is AstProg) { result = CalcProg(env, ast as AstProg); } else if (ast is AstExpression) { result = CalcExpression(env, ast as AstExpression); } else if (ast is AstIf) { result = CalcIf(env, ast as AstIf); } else if (ast is AstWhile) { result = CalcWhile(env, ast as AstWhile); } else if (ast is AstReturn) { result = CalcReturn(env, ast as AstReturn); } else if (ast is AstStringLiteral) { result = CalcString(env, ast as AstStringLiteral); } else if (ast is AstNumberLiteral) { result = CalcNumber(env, ast as AstNumberLiteral); } else if (ast is AstIdentifier) { result = CalcIdentifier(env, ast as AstIdentifier); } else if (ast is AstFuncDef) { env.GetMainEnv().SetFuncDef(ast as AstFuncDef); result = EvalValue.ZERO; // 函数定义,不返回任何有意义的值 } else if (ast is AstFunction) { result = CalcFunction(new FuncEnvironment(env, Utils.GetScopeString()), ast as AstFunction); // 函数调用 } else { throw new RunTimeException("非法节点类型: " + ast.GetType().ToString(), ast); } Utils.LogDebug("[R] 正在计算[{0}]{1}的值,结果为:{2}", ast.GetType(), ast, result); return(result); }
string ToDebug(AstNode node) { if (node == null) { return(null); } var region = node.GetRegion(); if (region != null && !region.IsEmpty) { return(String.Format("{1}: [{2}, {3}] - {0}", node.GetType().Name, region.FileName, region.BeginLine, region.BeginColumn)); } return(node.GetType().Name); }
public static bool? AreEqual(AstNode node1, AstNode node2) { if (node1 == node2) return true; if (node1 == null || node1.IsNull || node2 == null || node2.IsNull) return false; Type nodeType = node1.GetType(); if (nodeType != node2.GetType()) return false; if (node1.Role != node2.Role) return false; AstNode child1 = node1.FirstChild; AstNode child2 = node2.FirstChild; bool? result = true; while (result != false && (child1 != null || child2 != null)) { result &= AreEqual(child1, child2); child1 = child1.NextSibling; child2 = child2.NextSibling; } if (nodeTypesWithoutExtraInfo.Contains(nodeType)) return result; if (nodeType == typeof(Identifier)) return ((Identifier)node1).Name == ((Identifier)node2).Name; return null; }
public void FindUsingAPIDecl(AstNode invocation, CSharpFile file) { if (invocation.GetType().Name == "UsingDeclaration") { file.IndexOfUsingDecl.Add((UsingDeclaration)invocation); } }
string ParseExpression(AstNode expr, ExpressOption option) { if (expr is MemberReferenceExpression) return ParseMemberReferenceExpression(expr as MemberReferenceExpression, option); else if (expr is PrimitiveExpression) return ParsePrimitiveExpression(expr); else if (expr is BinaryOperatorExpression) return ParseBinaryOperatorExpression(expr); else if (expr is IdentifierExpression) return ParseIdentifierExpression(expr as IdentifierExpression, option); else if (expr is AssignmentExpression) return ParseBinaryOperatorExpression(expr); else if (expr is ExpressionStatement) return ParseExpressionStatement(expr); else if (expr is ThisReferenceExpression) return ParseThisReferenceExpression(expr); else if (expr is ObjectCreateExpression) return ParseObjectCreateExpression(expr); else if (expr is InvocationExpression) return ParseInvocationExpression(expr); else if (expr is LambdaExpression) return ParseLambdaExpression(expr); else { logger.LogError("not support Expression element:" + expr.GetType().Name + "|" + expr.NodeType + "|" + expr.StartLocation.Line); return null; } }
void CheckPositionConsistency(AstNode node) { string comment = "(" + node.GetType().Name + " at " + node.StartLocation + " in " + currentFileName + ")"; var pred = node.StartLocation <= node.EndLocation; if (!pred) { PrintNode(node); } Assert.IsTrue(pred, "StartLocation must be before EndLocation " + comment); var prevNodeEnd = node.StartLocation; var prevNode = node; for (AstNode child = node.FirstChild; child != null; child = child.NextSibling) { bool assertion = child.StartLocation >= prevNodeEnd; if (!assertion) { PrintNode(prevNode); PrintNode(node); } Assert.IsTrue(assertion, currentFileName + ": Child " + child.GetType() + " (" + child.StartLocation + ")" + " must start after previous sibling " + prevNode.GetType() + "(" + prevNode.StartLocation + ")"); CheckPositionConsistency(child); prevNodeEnd = child.EndLocation; prevNode = child; } Assert.IsTrue(prevNodeEnd <= node.EndLocation, "Last child must end before parent node ends " + comment); }
public override Node Visit(AstNode node) { if (node == null) { throw new NullReferenceException("Passed node was null."); } try { if (!(node is SyntaxTree syntaxTree)) { throw new WrongVisitorException(nameof(SyntaxTreeVisitor), node.GetType().Name); } return(Visit(syntaxTree)); } catch (VisitorException e) { throw e; } catch (Exception e) { throw new VisitorException(e); } }
static void CheckWhitespace(AstNode startNode, TextLocation whitespaceStart, AstNode endNode, TextLocation whitespaceEnd, string currentFileName, IDocument currentDocument) { if (whitespaceStart == whitespaceEnd || startNode == endNode) { return; } Assert.Greater(whitespaceStart.Line, 0); Assert.Greater(whitespaceStart.Column, 0); Assert.Greater(whitespaceEnd.Line, 0); Assert.Greater(whitespaceEnd.Column, 0); Assert.IsTrue(whitespaceEnd >= whitespaceStart, endNode.GetType().Name + ".StartLocation < " + startNode.GetType().Name + ".EndLocation: " + whitespaceEnd + " < " + whitespaceStart); int start = currentDocument.GetOffset(whitespaceStart.Line, whitespaceStart.Column); int end = currentDocument.GetOffset(whitespaceEnd.Line, whitespaceEnd.Column); string text = currentDocument.GetText(start, end - start); bool assertion = string.IsNullOrWhiteSpace(text); if (!assertion) { if (startNode.Parent != endNode.Parent) { PrintNode(startNode.Parent); } PrintNode(endNode.Parent); } Assert.IsTrue(assertion, "Expected whitespace between " + startNode.GetType() + ":" + whitespaceStart + " and " + endNode.GetType() + ":" + whitespaceEnd + ", but got '" + text + "' (in " + currentFileName + " parent:" + startNode.Parent.GetType() + ")"); }
public override Node Visit(AstNode node) { if (node == null) { throw new NullReferenceException("Passed node was null."); } try { if (!(node is Attribute converted)) { throw new WrongVisitorException(nameof(AttributeVisitor), node.GetType().Name); } return(Visit(converted)); } catch (VisitorException e) { throw e; } catch (Exception e) { throw new VisitorException(e); } }
internal override bool CanMatch(AstNode node) { if (specialNodeType != null && node.GetType() == specialNodeType) { return(true); } InvocationExpression ie = node as InvocationExpression; if (ie != null) { Expression target = ResolveVisitor.UnpackParenthesizedExpression(ie.Target); IdentifierExpression ident = target as IdentifierExpression; if (ident != null) { return(ident.Identifier == method.Name); } MemberReferenceExpression mre = target as MemberReferenceExpression; if (mre != null) { return(mre.MemberName == method.Name); } PointerReferenceExpression pre = target as PointerReferenceExpression; if (pre != null) { return(pre.MemberName == method.Name); } } return(node is MethodDeclaration); }
string GetNodeTitle(AstNode node) { var b = new StringBuilder(); b.Append(node.Role.ToString()); b.Append(": "); b.Append(node.GetType().Name); bool hasProperties = false; foreach (PropertyInfo p in node.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance)) { if (p.Name == "NodeType" || p.Name == "IsNull") { continue; } if (p.PropertyType == typeof(string) || p.PropertyType.IsEnum || p.PropertyType == typeof(bool)) { if (!hasProperties) { hasProperties = true; b.Append(" ("); } else { b.Append(", "); } b.Append(p.Name); b.Append(" = "); try { object val = p.GetValue(node, null); b.Append(val != null ? val.ToString() : "**null**"); } catch (TargetInvocationException ex) { b.Append("**" + ex.InnerException.GetType().Name + "**"); } } } if (hasProperties) { b.Append(")"); } // b.Append(" Start " + node.StartLocation); // b.Append(" End " + node.EndLocation); return(b.ToString()); }
//----------------------------------------------------------------------- /// <param name="node"></param> /// <returns></returns> public string GetNodeTitle(AstNode node) { // negrb node.child StringBuilder b = new StringBuilder(); b.Append(node.Role.ToString()); b.Append(": "); b.Append(node.GetType().Name); string TempNodeData = node.ToString(); bool hasProperties = false; foreach (PropertyInfo p in node.GetType().GetProperties(System.Reflection.BindingFlags.Public | BindingFlags.Instance)) { if (p.Name == "NodeType" || p.Name == "IsNull" || p.Name == "IsFrozen" || p.Name == "HasChildren") { continue; } if (p.PropertyType == typeof(string) || p.PropertyType.IsEnum || p.PropertyType == typeof(bool)) { if (!hasProperties) { hasProperties = true; b.Append(" ("); } else { b.Append(", "); } b.Append(p.Name); b.Append(" = "); try { object val = p.GetValue(node, null); b.Append(val != null ? val.ToString() : "**null**"); } catch (TargetInvocationException ex) { b.Append("**" + ex.InnerException.GetType().Name + "**"); } } } if (hasProperties) { b.Append(")"); } return(b.ToString()); }
public void FindWebMethod(AstNode invocation, CSharpFile file) { if (invocation.GetType().Name == "MethodDeclaration" && FindWebMethod(invocation)) { file.IndexOfWebMthdDecl.Add((MethodDeclaration)invocation); } }
static void PrintNode (AstNode node) { Console.WriteLine ("Parent:" + node.GetType ()); Console.WriteLine ("Children:"); foreach (var c in node.Children) Console.WriteLine (c.GetType () +" at:"+ c.StartLocation +"-"+ c.EndLocation + " Role: "+ c.Role); Console.WriteLine ("----"); }
protected Type InnerBuild(AstNode node) { Func <AstNode, Type> func; _dispatchTable.TryGetValue(node.GetType(), out func); return(func?.Invoke(node)); }
private void CheckExpression(AstNode astNode, int indent, object[] objects, ref int index) { var exprName = (string)objects[index++]; Type type = null; switch (exprName) { case "const": type = typeof(ConstantAstNode); break; case "binop": type = typeof(BinaryAstNode); break; case "unop": type = typeof(UnaryAstNode); break; case "call": type = typeof(MethodCallAstNode); break; case "error": type = typeof(ErrorAstNode); break; default: throw new InvalidOperationException(string.Format("Test error: unrecognized expression type abbreviation '{0}'", exprName)); } Trace.WriteLine(string.Format("{0}: {1}{2} (Current: {3})", indent, new string(' ', indent * 2), type.Name, astNode)); Assert.That(astNode.GetType(), Is.EqualTo(type)); if (exprName == "const") { Assert.That((astNode as ConstantAstNode).Value, Is.EqualTo(objects[index++])); } else if (exprName == "binop") { Assert.That((astNode as BinaryAstNode).Operator.Kind, Is.EqualTo(objects[index++])); } else if (exprName == "unop") { Assert.That((astNode as UnaryAstNode).Operator.Kind, Is.EqualTo(objects[index++])); } else if (exprName == "call") { Assert.That((astNode as MethodCallAstNode).Token.Kind, Is.EqualTo(objects[index++])); Assert.That((astNode as MethodCallAstNode).Token.Value, Is.EqualTo(objects[index++])); } foreach (var child in astNode.Children) { CheckExpression(child, indent + 1, objects, ref index); } }
public static void GenerateNode(JSContext context, TextWriter textWriter, AstNode node) { if (node == null) { throw new ArgumentNullException("node"); } if (node is IJSBasicNode) { IJSBasicNode jsBasicNode = (IJSBasicNode)node; jsBasicNode.GenerateJavaScript(context, textWriter); } else if (node is Token) { Token t = (Token)node; switch (t.Terminal.Name) { case "VARIABLE": if (t.Text.EndsWith("$")) { textWriter.Write("s_" + t.Text.Substring(0, t.Text.Length - 1)); } else { throw new BasicSyntaxErrorException("Invalid variable name: " + t.Text); } break; case "BOOLEAN_OP": { switch (t.Text.ToLowerInvariant()) { case "and": textWriter.Write("&&"); break; case "or": textWriter.Write("||"); break; default: textWriter.Write(t.Text); break; } } break; default: textWriter.Write(ConvertToJavascript(t.Text)); break; } textWriter.Write(" "); } else { throw new ApplicationException("Unhandled statement type: " + node.GetType().FullName); } }
public virtual IVisitorException CreateException(AstNode node, string message) { if (String.IsNullOrEmpty(message)) { message = String.Format("Language construction {0} is not supported", node.GetType().Name); } return new EmitterException(node, message); }
public void Scan(AstNode node) { switch (node) { case BlockNode block: for (var index = 0; index < block.Nodes.Count; index++) { AstNode child = block.Nodes[index]; Scan(child); } break; case AssignmentNode assign: CheckIdentifierExists(assign.Identifier); break; case IdentifierNode identifier: CheckIdentifierExists(identifier); break; case ConstantNode: // can't be an identifier and no recursion break; case ReadNode: // can't be an identifier and no recursion break; case WhileNode whileNode: Scan(whileNode.Comparison); Scan(whileNode.Statements); break; case PairNode pair: Scan(pair.Left); Scan(pair.Right); break; case ExpressionNode expression: Scan(expression.Left); Scan(expression.Right); break; case IfNode ifNode: Scan(ifNode.Comparison); Scan(ifNode.Statements); break; case WriteNode writeNode: Scan(writeNode.Expression); break; default: logger.LogError("Semantic analysis: unknown node type: {NodeType}", node.GetType().Name); break; } }
public static string Serialize(AstNode Node) { var Parameters = new List<string>(); foreach (var Child in Node.Childs) { Parameters.Add(Serialize(Child)); } return Node.GetType().Name + "(" + String.Join(", ", Parameters) + ")"; }
static public string Serialize(AstNode Node) { var Parameters = new List <string>(); foreach (var Child in Node.Childs) { Parameters.Add(Serialize(Child)); } return(Node.GetType().Name + "(" + String.Join(", ", Parameters) + ")"); }
public static string Serialize(AstNode node) { var parameters = new List <string>(); foreach (var child in node.Childs) { parameters.Add(Serialize(child)); } return(node.GetType().Name + "(" + string.Join(", ", parameters) + ")"); }
void PrintNode(AstNode node) { Console.WriteLine("Parent:" + node.GetType()); Console.WriteLine("Children:"); foreach (var c in node.Children) { Console.WriteLine(c.GetType() + " at:" + c.StartLocation + "-" + c.EndLocation + " Role: " + c.Role); } Console.WriteLine("----"); }
private Node CreateDummy(AstNode astNode) { Node result = new DummyNode(astNode.GetType().Name); foreach (var child in astNode.Children) { var adapted = child.AcceptVisitor(this); result.Children.Add(adapted); } return(result); }
/// <summary> /// Gets (or creates new) <see cref="INodeCompiler"/> associated with given <paramref name="node"/>. /// </summary> /// <param name="node"><see cref="AstNode"/> instance.</param> /// <returns>Associuated <see cref="INodeCompiler"/> instance.</returns> public static T /*!*/ NodeCompiler <T>(this AstNode /*!*/ node) where T : class { var obj = node.Properties[AstNodeCompilerKey] as T; if (obj == null) { node.Properties[AstNodeCompilerKey] = obj = CreateNodeCompiler(node) as T; Debug.Assert(obj != null, "AstNode " + node.GetType().ToString() + " does not implement INodeCompiler of type " + typeof(T).ToString()); } return(obj); }
static private void SerializeAsXml(AstNode Node, IndentedStringBuilder Out, bool Spaces) { var NodeName = Node.GetType().Name; Out.Write("<" + NodeName); var Parameters = Node.Info; if (Parameters != null) { foreach (var Pair in Parameters) { Out.Write(String.Format(" {0}=\"{1}\"", Pair.Key, Pair.Value)); } } if (Node.Childs.Count() > 0) { Out.Write(">"); if (Spaces) { Out.WriteNewLine(); } if (Spaces) { Out.Indent(() => { foreach (var Child in Node.Childs) { SerializeAsXml(Child, Out, Spaces); } }); } else { foreach (var Child in Node.Childs) { SerializeAsXml(Child, Out, Spaces); } } Out.Write("</" + NodeName + ">"); if (Spaces) { Out.WriteNewLine(); } } else { Out.Write(" />"); if (Spaces) { Out.WriteNewLine(); } } }
private static void SerializeAsXml(AstNode node, IndentedStringBuilder Out, bool spaces) { var nodeName = node.GetType().Name; Out.Write("<" + nodeName); var parameters = node.Info; if (parameters != null) { foreach (var pair in parameters) { Out.Write($" {pair.Key}=\"{pair.Value}\""); } } if (node.Childs.Any()) { Out.Write(">"); if (spaces) { Out.WriteNewLine(); } if (spaces) { Out.Indent(() => { foreach (var child in node.Childs) { SerializeAsXml(child, Out, spaces: true); } }); } else { foreach (var child in node.Childs) { SerializeAsXml(child, Out, spaces: false); } } Out.Write("</" + nodeName + ">"); if (spaces) { Out.WriteNewLine(); } } else { Out.Write(" />"); if (spaces) { Out.WriteNewLine(); } } }
public static void Lower(AstNode node, LoweringContext context) { Type nodeType = node.GetType(); if (LoweringDictionary.ContainsKey(nodeType)) { foreach (AstLoweringHandler handler in LoweringDictionary[nodeType]) { handler(node, context); } } }
public void Visit(AstNode n) { switch (n) { case DeclarationAstNode node: VisitDeclaration(node); break; case DeclaratorAstNode node: VisitDeclarator(node); break; case EnumeratorAstNode node: VisitEnumerator(node); break; case ExpressionAstNode node: VisitExpression(node); break; case LabelAstNode node: VisitLabel(node); break; case ParameterDeclarationAstNode node: VisitParameterDeclaration(node); break; case ParameterTypeListAstNode node: VisitParameterTypeList(node); break; case PointerAstNode node: VisitPointer(node); break; case SpecifierAstNode node: VisitSpecifier(node); break; case StatementAstNode node: VisitStatement(node); break; case TypeNameAstNode node: VisitTypeName(node); break; default: throw new NotImplementedException(n.GetType().Name); } }
public static T ParseStatement <T>(string stmt, bool expectErrors = false) where T : AstNode { CSharpParser parser = new CSharpParser(); var statements = parser.ParseStatements(new StringReader(stmt)); Assert.AreEqual(expectErrors, parser.HasErrors, "HasErrors"); AstNode statement = statements.Single(); Type type = typeof(T); Assert.IsTrue(type.IsAssignableFrom(statement.GetType()), String.Format("Parsed statement was {0} instead of {1} ({2})", statement.GetType(), type, statement)); return((T)statement); }
public static T ParseGlobal <T>(string code, bool expectErrors = false) where T : AstNode { CSharpParser parser = new CSharpParser(); CompilationUnit cu = parser.Parse(new StringReader(code)); Assert.AreEqual(expectErrors, parser.HasErrors, "HasErrors"); AstNode node = cu.Children.Single(); Type type = typeof(T); Assert.IsTrue(type.IsAssignableFrom(node.GetType()), String.Format("Parsed node was {0} instead of {1} ({2})", node.GetType(), type, node)); return((T)node); }
public AstNode Optimize(AstNode AstNode) { //if (AstNode != null) { //Console.WriteLine("Optimize.AstNode: {0}", AstNode); AstNode.TransformNodes(Optimize); var AstNodeType = AstNode.GetType(); if (GenerateMappings.ContainsKey(AstNodeType)) { AstNode = (AstNode)GenerateMappings[AstNodeType].Invoke(this, new[] { AstNode }); } else { //throw(new NotImplementedException(String.Format("Don't know how to optimize {0}", AstNodeType))); } } return AstNode; }
public static void CheckPositionConsistency (AstNode node, string currentFileName, IDocument currentDocument = null) { if (currentDocument == null) currentDocument = new ReadOnlyDocument(File.ReadAllText(currentFileName)); string comment = "(" + node.GetType ().Name + " at " + node.StartLocation + " in " + currentFileName + ")"; var pred = node.StartLocation <= node.EndLocation; if (!pred) PrintNode (node); Assert.IsTrue(pred, "StartLocation must be before EndLocation " + comment); var prevNodeEnd = node.StartLocation; var prevNode = node; for (AstNode child = node.FirstChild; child != null; child = child.NextSibling) { bool assertion = child.StartLocation >= prevNodeEnd; if (!assertion) { PrintNode (prevNode); PrintNode (node); } Assert.IsTrue(assertion, currentFileName + ": Child " + child.GetType () +" (" + child.StartLocation + ")" +" must start after previous sibling " + prevNode.GetType () + "(" + prevNode.StartLocation + ")"); CheckPositionConsistency(child, currentFileName, currentDocument); prevNodeEnd = child.EndLocation; prevNode = child; } Assert.IsTrue(prevNodeEnd <= node.EndLocation, "Last child must end before parent node ends " + comment); }
private static void SerializeAsXml(AstNode Node, IndentedStringBuilder Out, bool Spaces) { var NodeName = Node.GetType().Name; Out.Write("<" + NodeName); var Parameters = Node.Info; if (Parameters != null) { foreach (var Pair in Parameters) { Out.Write(String.Format(" {0}=\"{1}\"", Pair.Key, Pair.Value)); } } if (Node.Childs.Count() > 0) { Out.Write(">"); if (Spaces) Out.WriteNewLine(); if (Spaces) { Out.Indent(() => { foreach (var Child in Node.Childs) SerializeAsXml(Child, Out, Spaces); }); } else { foreach (var Child in Node.Childs) SerializeAsXml(Child, Out, Spaces); } Out.Write("</" + NodeName + ">"); if (Spaces) Out.WriteNewLine(); } else { Out.Write(" />"); if (Spaces) Out.WriteNewLine(); } }
private void NotSupported(AstNode node) { context.ReportError(node.Region, node.GetType().Name + " is unsupported"); wasSuccessful = false; }
private void CompileRecursive(AstNode node, Syt syt, StringBuilder sb) { if (node is AstBinOp) CompileBinOp((AstBinOp)node, syt, sb); else if (node is AstBoolLit) CompileBoolLit((AstBoolLit)node, syt, sb); else if (node is AstCall) CompileCall((AstCall)node, syt, sb); else if (node is AstClass) CompileClass((AstClass)node, syt, sb); else if (node is AstClassDecl) CompileClassDecl((AstClassDecl)node, syt, sb); else if (node is AstDo) CompileDo((AstDo)node, syt, sb); else if (node is AstDot) CompileDot((AstDot)node, syt, sb); else if (node is AstIf) CompileIf((AstIf)node, syt, sb); else if (node is AstIndex) CompileIndex((AstIndex)node, syt, sb); else if (node is AstIntLit) CompileIntLit((AstIntLit)node, syt, sb); else if (node is AstLet) CompileLet((AstLet)node, syt, sb); else if (node is AstNull) CompileNull((AstNull)node, syt, sb); else if (node is AstReturn) CompileReturn((AstReturn)node, syt, sb); else if (node is AstStringLit) CompileStringLit((AstStringLit)node, syt, sb); else if (node is AstSubroutine) CompileSubroutine((AstSubroutine)node, syt, sb); else if (node is AstThis) CompileThis((AstThis)node, syt, sb); else if (node is AstUnOp) CompileUnOp((AstUnOp)node, syt, sb); else if (node is AstVarDecl) CompileVarDecl((AstVarDecl)node, syt, sb); else if (node is AstVarRef) CompileVarRef((AstVarRef)node, syt, sb); else if (node is AstWhile) CompileWhile((AstWhile)node, syt, sb); else throw new ArgumentException("Unkonwn node " + node.GetType()); }
public static string GetLeafNodeString(AstNode node) { string nodeRole = node.Role.ToString(); var properties = node.GetProperties(); if (nodeRole == "Comment") { CommentType commentType = properties.GetPropertyValueEnum<CommentType>(node, "CommentType"); string content = properties.GetPropertyValue(node, "Content"); switch (commentType) { default: case CommentType.SingleLine: return "//" + content; case CommentType.Documentation: return "///" + content; case CommentType.MultiLine: return "/*" + content + "*/"; case CommentType.InactiveCode: return content; case CommentType.MultiLineDocumentation: return "/**" + content + "*/"; } } else if (nodeRole == "Modifier") { return properties.GetPropertyValue(node, "Modifier").ToLower(); } else if (nodeRole == "Target" || nodeRole == "Right") { string typeName = node.GetType().Name; if (typeName == nameof(ThisReferenceExpression)) return "this"; else if (typeName == nameof(BaseReferenceExpression)) return "base"; else if (typeName == nameof(NullReferenceExpression)) return "null"; } else if (nodeRole == "PreProcessorDirective") { var type = properties.GetPropertyValue(node, "Type").ToLower(); var argument = properties.GetPropertyValue(node, "Argument"); var result = "#" + type; if (argument != string.Empty) result += " " + argument; return result; } if (node is ThisReferenceExpression) return "this"; else if (node is BaseReferenceExpression) return "base"; else if (node is NullReferenceExpression) return "null"; else if (node is CSharpTokenNode || node is CSharpModifierToken) return nodeRole; else if (node is NewLineNode) return ""; return properties .FirstOrDefault(p => NameKeys.Contains(p.Name)) .GetValue(node, null) .ToString(); }
private void GetUsingsAndRemoveUsingsAndNamespaces(AstNode node, List<UsingDeclaration> usings, List<TypeDeclaration> types) { if (node.Role.ToString() == "Member" && node.GetType().Name == "UsingDeclaration") { usings.Add((UsingDeclaration)node); } else if (node.Role.ToString() == "Member" && node.GetType().Name == "NamespaceDeclaration") { var parent = node.Parent; foreach (var child in node.Children) { if (child.NodeType == NodeType.TypeDeclaration) { types.Add((TypeDeclaration)child); } else if (child.ToString() == "Member" && child.GetType().Name == "NamespaceDeclaration") { GetUsingsAndRemoveUsingsAndNamespaces(child, usings, types); } } } else { if (node.Children.Count() >= 1) { foreach (var child in node.Children) GetUsingsAndRemoveUsingsAndNamespaces(child, usings, types); } } }
public static JavaScriptSymbol StartNew(AstNode node, int startLine, int startColumn, int sourceFileId) { if (startLine == int.MaxValue) { throw new ArgumentOutOfRangeException("startLine"); } if (startColumn == int.MaxValue) { throw new ArgumentOutOfRangeException("startColumn"); } return new JavaScriptSymbol { // destination line/col number are fed to us as zero-based, so add one to get to // the one-based values we desire. Context objects store the source line/col as // one-based already. m_startLine = startLine + 1, m_startColumn = startColumn + 1, m_sourceContext = node != null ? node.Context : null, m_symbolType = node != null ? node.GetType().Name : "[UNKNOWN]", m_sourceFileId = sourceFileId, }; }
internal override bool CanMatch(AstNode node) { if (specialNodeType != null && node.GetType() == specialNodeType) return true; Expression expr = node as Expression; if (expr == null) return node is MethodDeclaration; InvocationExpression ie = node as InvocationExpression; if (ie != null) { Expression target = ParenthesizedExpression.UnpackParenthesizedExpression(ie.Target); IdentifierExpression ident = target as IdentifierExpression; if (ident != null) return ident.Identifier == method.Name; MemberReferenceExpression mre = target as MemberReferenceExpression; if (mre != null) return mre.MemberName == method.Name; PointerReferenceExpression pre = target as PointerReferenceExpression; if (pre != null) return pre.MemberName == method.Name; } else if (expr.Role != Roles.TargetExpression) { // MemberReferences & Identifiers that aren't used in an invocation can still match the method // as delegate name. if (expr.GetChildByRole(Roles.Identifier).Name == method.Name) potentialMethodGroupConversions.Add(expr); } return node is MethodDeclaration; }
private Node TransformLiteral(AstNode node) { decompiler.AddToken(node.GetType()); return node; }
bool NodeSupportsBlockMovement(AstNode node) { var blockNodeTypes = new[] { typeof(IfElseStatement), typeof(ForStatement), typeof(ForeachStatement), typeof(WhileStatement), typeof(DoWhileStatement), typeof(UsingStatement) }; return blockNodeTypes.Contains(node.GetType()); }
/// <summary> /// Resolves the specified node. /// </summary> public ResolveResult Resolve(AstNode node, CancellationToken cancellationToken = default(CancellationToken)) { if(node == null) return ErrorResolveResult.UnknownError; lock(resolve_visitor){ InitResolver(); resolve_visitor.cancellation_token = cancellationToken; try{ ResolveResult rr = resolve_visitor.GetResolveResult(node); if(rr == null){ Debug.Fail(string.Format("{0} resolved to null.{1}:'{2}'", node.GetType(), node.StartLocation, node.GetText())); } return rr; }finally{ resolve_visitor.cancellation_token = CancellationToken.None; } } }
private string GetDefaultXMLNamespace(AstNode astNode) { return GetDefaultXMLNamespace(astNode.GetType()); }
private PropertyBindingAttributePair GetPropertyBinding(AstNode astNode, XName xObjectName, bool patchDefaultNamespace) { Type astNodeType = astNode.GetType(); if (this._PropertyMappingDictionary.ContainsKey(astNodeType)) { Dictionary<XName, PropertyBindingAttributePair> propertyBinding = this._PropertyMappingDictionary[astNodeType]; if (propertyBinding.ContainsKey(xObjectName)) { return propertyBinding[xObjectName]; } else if (patchDefaultNamespace && xObjectName.NamespaceName.Equals(String.Empty)) { // targetNamespace is not applied to attributes, so this code will use the DefaultXMLNamespace to perform XName matching when specified (i.e. for attributes) return GetPropertyBinding(astNode, XName.Get(xObjectName.LocalName, GetDefaultXMLNamespace(astNode)), false); } } return null; }
// Might want to convert this to polymorphism - move transform* // functions into the AstNode subclasses. OTOH that would make // IR transformation part of the public AST API - desirable? // Another possibility: create AstTransformer interface and adapter. public Node Transform(AstNode node) { switch (node.GetType()) { case Token.ARRAYCOMP: { return TransformArrayComp((ArrayComprehension)node); } case Token.ARRAYLIT: { return TransformArrayLiteral((ArrayLiteral)node); } case Token.BLOCK: { return TransformBlock(node); } case Token.BREAK: { return TransformBreak((BreakStatement)node); } case Token.CALL: { return TransformFunctionCall((FunctionCall)node); } case Token.CONTINUE: { return TransformContinue((ContinueStatement)node); } case Token.DO: { return TransformDoLoop((DoLoop)node); } case Token.EMPTY: { return node; } case Token.FOR: { if (node is ForInLoop) { return TransformForInLoop((ForInLoop)node); } else { return TransformForLoop((ForLoop)node); } goto case Token.FUNCTION; } case Token.FUNCTION: { return TransformFunction((FunctionNode)node); } case Token.GENEXPR: { return TransformGenExpr((GeneratorExpression)node); } case Token.GETELEM: { return TransformElementGet((ElementGet)node); } case Token.GETPROP: { return TransformPropertyGet((PropertyGet)node); } case Token.HOOK: { return TransformCondExpr((ConditionalExpression)node); } case Token.IF: { return TransformIf((IfStatement)node); } case Token.TRUE: case Token.FALSE: case Token.THIS: case Token.NULL: case Token.DEBUGGER: { return TransformLiteral(node); } case Token.NAME: { return TransformName((Name)node); } case Token.NUMBER: { return TransformNumber((NumberLiteral)node); } case Token.NEW: { return TransformNewExpr((NewExpression)node); } case Token.OBJECTLIT: { return TransformObjectLiteral((ObjectLiteral)node); } case Token.REGEXP: { return TransformRegExp((RegExpLiteral)node); } case Token.RETURN: { return TransformReturn((ReturnStatement)node); } case Token.SCRIPT: { return TransformScript((ScriptNode)node); } case Token.STRING: { return TransformString((StringLiteral)node); } case Token.SWITCH: { return TransformSwitch((SwitchStatement)node); } case Token.THROW: { return TransformThrow((ThrowStatement)node); } case Token.TRY: { return TransformTry((TryStatement)node); } case Token.WHILE: { return TransformWhileLoop((WhileLoop)node); } case Token.WITH: { return TransformWith((WithStatement)node); } case Token.YIELD: { return TransformYield((Yield)node); } default: { if (node is ExpressionStatement) { return TransformExprStmt((ExpressionStatement)node); } if (node is Assignment) { return TransformAssignment((Assignment)node); } if (node is UnaryExpression) { return TransformUnary((UnaryExpression)node); } if (node is XmlMemberGet) { return TransformXmlMemberGet((XmlMemberGet)node); } if (node is InfixExpression) { return TransformInfix((InfixExpression)node); } if (node is VariableDeclaration) { return TransformVariables((VariableDeclaration)node); } if (node is ParenthesizedExpression) { return TransformParenExpr((ParenthesizedExpression)node); } if (node is LabeledStatement) { return TransformLabeledStatement((LabeledStatement)node); } if (node is LetNode) { return TransformLetNode((LetNode)node); } if (node is XmlRef) { return TransformXmlRef((XmlRef)node); } if (node is XmlLiteral) { return TransformXmlLiteral((XmlLiteral)node); } throw new ArgumentException("Can't transform: " + node); } } }
static bool CreatesLocalVariableDeclarationSpace(AstNode node) { return localVariableDeclarationSpaceCreators.Contains(node.GetType()); }
internal void Decompile(AstNode node) { switch (node.GetType()) { case Token.ARRAYLIT: { DecompileArrayLiteral((ArrayLiteral)node); break; } case Token.OBJECTLIT: { DecompileObjectLiteral((ObjectLiteral)node); break; } case Token.STRING: { decompiler.AddString(((StringLiteral)node).GetValue()); break; } case Token.NAME: { decompiler.AddName(((Name)node).GetIdentifier()); break; } case Token.NUMBER: { decompiler.AddNumber(((NumberLiteral)node).GetNumber()); break; } case Token.GETPROP: { DecompilePropertyGet((PropertyGet)node); break; } case Token.EMPTY: { break; } case Token.GETELEM: { DecompileElementGet((ElementGet)node); break; } case Token.THIS: { decompiler.AddToken(node.GetType()); break; } default: { Kit.CodeBug("unexpected token: " + Token.TypeToName(node.GetType())); break; } } }
static void CheckWhitespace(AstNode startNode, TextLocation whitespaceStart, AstNode endNode, TextLocation whitespaceEnd, string currentFileName, IDocument currentDocument) { if (whitespaceStart == whitespaceEnd || startNode == endNode) return; Assert.Greater(whitespaceStart.Line, 0); Assert.Greater(whitespaceStart.Column, 0); Assert.Greater(whitespaceEnd.Line, 0); Assert.Greater(whitespaceEnd.Column, 0); Assert.IsTrue(whitespaceEnd >= whitespaceStart, endNode.GetType().Name + ".StartLocation < " + startNode.GetType().Name + ".EndLocation: " + whitespaceEnd + " < " + whitespaceStart); int start = currentDocument.GetOffset(whitespaceStart.Line, whitespaceStart.Column); int end = currentDocument.GetOffset(whitespaceEnd.Line, whitespaceEnd.Column); string text = currentDocument.GetText(start, end - start); bool assertion = string.IsNullOrWhiteSpace(text); if (!assertion) { if (startNode.Parent != endNode.Parent) PrintNode (startNode.Parent); PrintNode (endNode.Parent); } Assert.IsTrue(assertion, "Expected whitespace between " + startNode.GetType () +":" + whitespaceStart + " and " + endNode.GetType () + ":" + whitespaceEnd + ", but got '" + text + "' (in " + currentFileName + " parent:" + startNode.Parent.GetType () +")"); }
internal override bool CanMatch(AstNode node) { if (specialNodeType != null && node.GetType() == specialNodeType) return true; InvocationExpression ie = node as InvocationExpression; if (ie != null) { Expression target = ResolveVisitor.UnpackParenthesizedExpression(ie.Target); IdentifierExpression ident = target as IdentifierExpression; if (ident != null) return ident.Identifier == method.Name; MemberReferenceExpression mre = target as MemberReferenceExpression; if (mre != null) return mre.MemberName == method.Name; PointerReferenceExpression pre = target as PointerReferenceExpression; if (pre != null) return pre.MemberName == method.Name; } return node is MethodDeclaration; }
private string GetNodeName(AstNode node, string nodeType) { foreach (PropertyInfo p in node.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance)) { if (p.Name == "NodeType" || p.Name == "IsNull" || p.Name == "IsFrozen" || p.Name == "HasChildren") { continue; } if ((p.PropertyType == typeof (string) || p.PropertyType.IsEnum || p.PropertyType == typeof (bool))) { if (nodeType == "CLASS") { if (p.Name == "ClassType") { foreach (PropertyInfo p2 in node.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance)) { if (p2.Name == "NodeType" || p2.Name == "IsNull" || p2.Name == "IsFrozen" || p2.Name == "HasChildren") { continue; } if ((p2.PropertyType == typeof (string) || p2.PropertyType.IsEnum || p2.PropertyType == typeof (bool))) { if (p2.Name.ToUpper() == "NAME") //如果当前是Class节点 { return p2.GetValue(node, null).ToString(); } } } } } else if (nodeType == "METHOD") { if (p.Name == "EntityType" && p.GetValue(node, null).ToString() == "Method") { foreach (PropertyInfo p2 in node.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance)) { if (p2.Name == "NodeType" || p2.Name == "IsNull" || p2.Name == "IsFrozen" || p2.Name == "HasChildren") { continue; } if ((p2.PropertyType == typeof (string) || p2.PropertyType.IsEnum || p2.PropertyType == typeof (bool))) { if (p2.Name.ToUpper() == "NAME") //如果当前是Method节点 { return p2.GetValue(node, null).ToString(); } } } } } else if (nodeType == "NAMESPACE") { if (node.GetType().Name == "NamespaceDeclaration") { foreach (PropertyInfo p2 in node.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance)) { if (p2.Name == "NodeType" || p2.Name == "IsNull" || p2.Name == "IsFrozen" || p2.Name == "HasChildren") { continue; } if ((p2.PropertyType == typeof(string) || p2.PropertyType.IsEnum || p2.PropertyType == typeof(bool))) { if (p2.Name.ToUpper() == "NAME") //如果当前是Method节点 { return p2.GetValue(node, null).ToString(); } } } } } else { } } } return string.Empty; }
/// <summary> /// Get the first expression to be excecuted if the instruction pointer is at the start of the given node. /// Try blocks may not be entered in any way. If possible, the try block is returned as the node to be executed. /// </summary> AstNode Enter(AstNode node, HashSet<AstNode> visitedNodes) { if (node == null) throw new ArgumentNullException(); if (!visitedNodes.Add(node)) return null; // Infinite loop AstLabel label = node as AstLabel; if (label != null) { return Exit(label, visitedNodes); } AstExpression expr = node as AstExpression; if (expr != null) { if (expr.Code == AstCode.Br || expr.Code == AstCode.Leave) { AstLabel target = (AstLabel)expr.Operand; // Early exit - same try-block if (GetParents(expr).OfType<AstTryCatchBlock>().FirstOrDefault() == GetParents(target).OfType<AstTryCatchBlock>().FirstOrDefault()) return Enter(target, visitedNodes); // Make sure we are not entering any try-block var srcTryBlocks = GetParents(expr).OfType<AstTryCatchBlock>().Reverse().ToList(); var dstTryBlocks = GetParents(target).OfType<AstTryCatchBlock>().Reverse().ToList(); // Skip blocks that we are already in int i = 0; while(i < srcTryBlocks.Count && i < dstTryBlocks.Count && srcTryBlocks[i] == dstTryBlocks[i]) i++; if (i == dstTryBlocks.Count) { return Enter(target, visitedNodes); } else { AstTryCatchBlock dstTryBlock = dstTryBlocks[i]; // Check that the goto points to the start AstTryCatchBlock current = dstTryBlock; while(current != null) { foreach(AstNode n in current.TryBlock.Body) { if (n is AstLabel) { if (n == target) return dstTryBlock; } else if (!n.Match(AstCode.Nop)) { current = n as AstTryCatchBlock; break; } } } return null; } } else if (expr.Code == AstCode.Nop) { return Exit(expr, visitedNodes); } else if (expr.Code == AstCode.LoopOrSwitchBreak) { AstNode breakBlock = GetParents(expr).First(n => n is AstSwitch); return Exit(breakBlock, new HashSet<AstNode>() { expr }); } else { return expr; } } AstBlock block = node as AstBlock; if (block != null) { if (block.EntryGoto != null) { return Enter(block.EntryGoto, visitedNodes); } else if (block.Body.Count > 0) { return Enter(block.Body[0], visitedNodes); } else { return Exit(block, visitedNodes); } } AstTryCatchBlock tryCatch = node as AstTryCatchBlock; if (tryCatch != null) { return tryCatch; } AstSwitch astSwitch = node as AstSwitch; if (astSwitch != null) { return astSwitch.Condition; } throw new NotSupportedException(node.GetType().ToString()); }
/// <summary> /// Resolves the specified node. /// </summary> public ResolveResult Resolve(AstNode node, CancellationToken cancellationToken = default(CancellationToken)) { if (node == null || node.IsNull || IsUnresolvableNode(node)) return ErrorResolveResult.UnknownError; lock (resolveVisitor) { InitResolver(); resolveVisitor.cancellationToken = cancellationToken; try { ResolveResult rr = resolveVisitor.GetResolveResult(node); if (rr == null) Debug.Fail (node.GetType () + " resolved to null.", node.StartLocation + ":'" + node.GetText () + "'"); return rr; } finally { resolveVisitor.cancellationToken = CancellationToken.None; } } }
public bool Visit(AstNode node) { int type = node.GetType(); if (type == Token.SCRIPT) { return true; } if (node.GetParent() == null) { throw new InvalidOperationException("No parent for node: " + node + "\n" + node.ToSource(0)); } return true; }