[svn] r7502: vs-plugin/trunk/Nemerle.Compiler.Utils/Nemerle.Completion2/CodeModel: ExprFinder.n ExprWalker...

kliss svnadmin at nemerle.org
Tue Feb 27 21:11:43 CET 2007


Log:
Add support for walking and finding Decls.

Author: kliss
Date: Tue Feb 27 21:11:36 2007
New Revision: 7502

Modified:
   vs-plugin/trunk/Nemerle.Compiler.Utils/Nemerle.Completion2/CodeModel/ExprFinder.n
   vs-plugin/trunk/Nemerle.Compiler.Utils/Nemerle.Completion2/CodeModel/ExprWalker.n

Modified: vs-plugin/trunk/Nemerle.Compiler.Utils/Nemerle.Completion2/CodeModel/ExprFinder.n
==============================================================================
--- vs-plugin/trunk/Nemerle.Compiler.Utils/Nemerle.Completion2/CodeModel/ExprFinder.n	(original)
+++ vs-plugin/trunk/Nemerle.Compiler.Utils/Nemerle.Completion2/CodeModel/ExprFinder.n	Tue Feb 27 21:11:36 2007
@@ -19,6 +19,7 @@
     mutable _parentObject : object;
     mutable _pexprObject  : object;
     mutable _texprObject  : object;
+    mutable _decl         : Decl;
     mutable _location     : Location;
 
     GetTypedObject(obj : object) : object
@@ -73,6 +74,37 @@
           info.Skip();
       }
 
+      def findDecl(obj : Decl)
+      {
+        def loc = obj.Location;
+
+        Print(obj, loc, info.Nodes.Length);
+
+        if (IsIn(loc))
+        {
+          when(_decl == null || _location.Contains(loc))
+          {
+            _parentObject = null;
+            _pexprObject = null;
+            _location = loc;
+            _texprObject = null;
+            _decl = info.Node :> Decl;
+          }
+          
+          match (info.Node)
+          {
+          | Decl.GlobalAttribute
+          | Decl.None => Debug.WriteLine($"$(info.Node.GetType().Name)"); info.Stop();
+          | _ => ()
+          }
+        }
+        else if (_line < loc.Line || _line == loc.Line && _col < loc.Column)
+          info.Stop()
+        else when (_line > loc.EndLine || _line == loc.EndLine && _col > loc.EndColumn)
+          info.Skip();
+      }
+
+
       match (info.Node)
       {
       //| PExpr.MacroCall as mc =>
@@ -81,10 +113,29 @@
       //  else              Print(mc, mc.loc, info.Nodes.Length);
 
       | l is Located          => find(l);
+      | d is Decl             => findDecl(d);
       | _                     => ()
       }
     }
 
+    // This method accepts Decl as a starting point.
+    public Find(pRoot : Decl, line : int, col : int) : Location * object
+    {
+      if(pRoot == null)
+       (Location.Default, null);
+      else
+      {
+         Init(line, col);
+         ExprWalker().Walk(pRoot, PFinder);
+         if(_pexprObject != null)
+           (_location, _pexprObject)
+         else if(_decl != null)
+          (_location, _decl);
+         else
+          (Location.Default, null);
+      }  
+    }
+    
     public Find(pRoot : PExpr, _tRoot : TExpr, line : int, col : int) : Location * object * object
     {
       if (pRoot != null)
@@ -221,6 +272,16 @@
       !location.IsGenerated && location.Contains(_line, _col);
     }
 
+    IsInList(locations : list[Location]) : bool
+    {
+      match(locations.Find(IsIn))
+      {
+      | Some => true;
+      | _ => false;
+      }
+    }
+    
+
     IsInEx(location : Location) : bool
     {
       location.Contains(_line, _col);
@@ -231,24 +292,20 @@
       _ = obj.ToString();
 
 #if PRINT_AST && DEBUG
-      mutable s = "";
-
-      for (mutable i = 0; i < level; i++)
-        s += "  ";
-
+      def indentation = string(' ', level);
       mutable os = obj.ToString();
 
       when (os.Length > 200)
         os = os.Substring(0, 200) + "...";
 
       Trace.WriteLine("");
-      Trace.WriteLine(s + 
+      Trace.WriteLine(indentation + 
         $"$(obj.GetType().FullName) "
          "$(loc.Line):$(loc.Column):$(loc.EndLine):$(loc.EndColumn)"
          "$(if (loc.IsGenerated) '-' else '+')"
          "$(if (GetTypedObject(obj) == null) '-' else '+') "
          "$_line:$_col.");
-      Trace.WriteLine(s + os.Replace("\n", "\n" + s));
+      Trace.WriteLine(indentation + os.Replace("\n", "\n" + indentation));
 #endif
 
       ignore(obj); ignore(loc); ignore(level);
@@ -257,10 +314,7 @@
     PrintAdd(level : int) : void
     {
 #if PRINT_AST && DEBUG
-      mutable s = "";
-
-      for (mutable i = 0; i < level; i++)
-        s += "  ";
+      def s = string(' ', level);
 
       Trace.WriteLine(s + "+");
 #endif

Modified: vs-plugin/trunk/Nemerle.Compiler.Utils/Nemerle.Completion2/CodeModel/ExprWalker.n
==============================================================================
--- vs-plugin/trunk/Nemerle.Compiler.Utils/Nemerle.Completion2/CodeModel/ExprWalker.n	(original)
+++ vs-plugin/trunk/Nemerle.Compiler.Utils/Nemerle.Completion2/CodeModel/ExprWalker.n	Tue Feb 27 21:11:36 2007
@@ -34,6 +34,43 @@
     private Go(lst : list[T.Pattern])       : void { when (lst != null) foreach (item in lst) Go(item); }
     private Go(lst : list[T.Match_case])    : void { when (lst != null) foreach (item in lst) Go(item); }
 
+    private Go(lst : list[Decl])    : void { when (lst != null) foreach (item in lst) Go(item); }
+    private Go(lst : list[IMember])    : void { when (lst != null) foreach (item in lst) Go(item); }
+    
+    private Go(decl : Decl) : void
+    {
+      when(_info.Push(decl))
+      {
+        match(decl)
+        {
+        | n is Decl.Namespace => Go(n.Decls);
+        | t is Decl.Type => Go(t.Builder);
+        | GlobalAttribute
+        | Using
+        | None => ()
+        }
+        _info.Pop();
+      }
+    
+    }
+    
+    private Go(member : IMember) : void 
+    {
+      when(_info.Push(member))
+      {
+        match(member)
+        {
+        | mb is MethodBuilder => Go(mb.BodyParsed);
+        | fb is FieldBuilder => Go(fb.InitializerParsed);
+        | tb is TypeBuilder => Go(tb.GetDirectMembers().Reverse());
+        | pb is PropertyBuilder => Go(pb.GetGetter()); Go(pb.GetSetter());
+        | _ => ();
+        }
+        _info.Pop();
+      }  
+      
+    }
+    
     private Go(body : FunBody) : void
     {
       when (_info.Push(body))
@@ -494,6 +531,12 @@
       Go(expression);
     }
 
+    public Walk([NotNull] expression : Decl, [NotNull] walkHandler : ExprWalkHandler) : void
+    {
+      _info.Init(walkHandler);
+      Go(expression);
+    }
+    
     public GetLocation(expression : PExpr) : Location
     {
       mutable loc = expression.Location;



More information about the svn mailing list